#ifndef FloatPoint3D_h
#define FloatPoint3D_h
#include "platform/geometry/FloatPoint.h"
namespace WebCore {
class PLATFORM_EXPORT FloatPoint3D {
public:
FloatPoint3D()
: m_x(0)
, m_y(0)
, m_z(0)
{
}
FloatPoint3D(float x, float y, float z)
: m_x(x)
, m_y(y)
, m_z(z)
{
}
FloatPoint3D(const FloatPoint& p)
: m_x(p.x())
, m_y(p.y())
, m_z(0)
{
}
FloatPoint3D(const FloatPoint3D& p)
: m_x(p.x())
, m_y(p.y())
, m_z(p.z())
{
}
float x() const { return m_x; }
void setX(float x) { m_x = x; }
float y() const { return m_y; }
void setY(float y) { m_y = y; }
float z() const { return m_z; }
void setZ(float z) { m_z = z; }
void set(float x, float y, float z)
{
m_x = x;
m_y = y;
m_z = z;
}
void move(float dx, float dy, float dz)
{
m_x += dx;
m_y += dy;
m_z += dz;
}
void scale(float sx, float sy, float sz)
{
m_x *= sx;
m_y *= sy;
m_z *= sz;
}
bool isZero() const
{
return !m_x && !m_y && !m_z;
}
void normalize();
float dot(const FloatPoint3D& a) const
{
return m_x * a.x() + m_y * a.y() + m_z * a.z();
}
void cross(const FloatPoint3D& a, const FloatPoint3D& b)
{
float x = a.y() * b.z() - a.z() * b.y();
float y = a.z() * b.x() - a.x() * b.z();
float z = a.x() * b.y() - a.y() * b.x();
m_x = x;
m_y = y;
m_z = z;
}
FloatPoint3D cross(const FloatPoint3D& point) const
{
FloatPoint3D result;
result.cross(*this, point);
return result;
}
float lengthSquared() const { return this->dot(*this); }
float length() const { return sqrtf(lengthSquared()); }
float distanceTo(const FloatPoint3D& a) const;
private:
float m_x;
float m_y;
float m_z;
};
inline FloatPoint3D& operator +=(FloatPoint3D& a, const FloatPoint3D& b)
{
a.move(b.x(), b.y(), b.z());
return a;
}
inline FloatPoint3D& operator -=(FloatPoint3D& a, const FloatPoint3D& b)
{
a.move(-b.x(), -b.y(), -b.z());
return a;
}
inline FloatPoint3D operator+(const FloatPoint3D& a, const FloatPoint3D& b)
{
return FloatPoint3D(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());
}
inline FloatPoint3D operator-(const FloatPoint3D& a, const FloatPoint3D& b)
{
return FloatPoint3D(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());
}
inline bool operator==(const FloatPoint3D& a, const FloatPoint3D& b)
{
return a.x() == b.x() && a.y() == b.y() && a.z() == b.z();
}
inline bool operator!=(const FloatPoint3D& a, const FloatPoint3D& b)
{
return a.x() != b.x() || a.y() != b.y() || a.z() != b.z();
}
inline float operator*(const FloatPoint3D& a, const FloatPoint3D& b)
{
return a.dot(b);
}
inline FloatPoint3D operator*(float k, const FloatPoint3D& v)
{
return FloatPoint3D(k * v.x(), k * v.y(), k * v.z());
}
inline FloatPoint3D operator*(const FloatPoint3D& v, float k)
{
return FloatPoint3D(k * v.x(), k * v.y(), k * v.z());
}
inline float FloatPoint3D::distanceTo(const FloatPoint3D& a) const
{
return (*this - a).length();
}
}
#endif