1/* 2 Copyright (C) 2004, 2005, 2006 Nikolas Zimmermann <wildfox@kde.org> 3 2004, 2005 Rob Buis <buis@kde.org> 4 2005 Eric Seidel <eric@webkit.org> 5 2010 Zoltan Herczeg <zherczeg@webkit.org> 6 7 This library is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Library General Public 9 License as published by the Free Software Foundation; either 10 version 2 of the License, or (at your option) any later version. 11 12 This library is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Library General Public License for more details. 16 17 You should have received a copy of the GNU Library General Public License 18 aint with this library; see the file COPYING.LIB. If not, write to 19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 Boston, MA 02110-1301, USA. 21*/ 22 23#ifndef FloatPoint3D_h 24#define FloatPoint3D_h 25 26#include "platform/geometry/FloatPoint.h" 27 28namespace blink { 29 30class PLATFORM_EXPORT FloatPoint3D { 31public: 32 FloatPoint3D() 33 : m_x(0) 34 , m_y(0) 35 , m_z(0) 36 { 37 } 38 39 FloatPoint3D(float x, float y, float z) 40 : m_x(x) 41 , m_y(y) 42 , m_z(z) 43 { 44 } 45 46 FloatPoint3D(const FloatPoint& p) 47 : m_x(p.x()) 48 , m_y(p.y()) 49 , m_z(0) 50 { 51 } 52 53 FloatPoint3D(const FloatPoint3D& p) 54 : m_x(p.x()) 55 , m_y(p.y()) 56 , m_z(p.z()) 57 { 58 } 59 60 float x() const { return m_x; } 61 void setX(float x) { m_x = x; } 62 63 float y() const { return m_y; } 64 void setY(float y) { m_y = y; } 65 66 float z() const { return m_z; } 67 void setZ(float z) { m_z = z; } 68 void set(float x, float y, float z) 69 { 70 m_x = x; 71 m_y = y; 72 m_z = z; 73 } 74 void move(float dx, float dy, float dz) 75 { 76 m_x += dx; 77 m_y += dy; 78 m_z += dz; 79 } 80 void scale(float sx, float sy, float sz) 81 { 82 m_x *= sx; 83 m_y *= sy; 84 m_z *= sz; 85 } 86 87 bool isZero() const 88 { 89 return !m_x && !m_y && !m_z; 90 } 91 92 void normalize(); 93 94 float dot(const FloatPoint3D& a) const 95 { 96 return m_x * a.x() + m_y * a.y() + m_z * a.z(); 97 } 98 99 // Sets this FloatPoint3D to the cross product of the passed two. 100 // It is safe for "this" to be the same as either or both of the 101 // arguments. 102 void cross(const FloatPoint3D& a, const FloatPoint3D& b) 103 { 104 float x = a.y() * b.z() - a.z() * b.y(); 105 float y = a.z() * b.x() - a.x() * b.z(); 106 float z = a.x() * b.y() - a.y() * b.x(); 107 m_x = x; 108 m_y = y; 109 m_z = z; 110 } 111 112 // Convenience function returning "this cross point" as a 113 // stack-allocated result. 114 FloatPoint3D cross(const FloatPoint3D& point) const 115 { 116 FloatPoint3D result; 117 result.cross(*this, point); 118 return result; 119 } 120 121 float lengthSquared() const { return this->dot(*this); } 122 float length() const { return sqrtf(lengthSquared()); } 123 124 float distanceTo(const FloatPoint3D& a) const; 125 126private: 127 float m_x; 128 float m_y; 129 float m_z; 130}; 131 132inline FloatPoint3D& operator +=(FloatPoint3D& a, const FloatPoint3D& b) 133{ 134 a.move(b.x(), b.y(), b.z()); 135 return a; 136} 137 138inline FloatPoint3D& operator -=(FloatPoint3D& a, const FloatPoint3D& b) 139{ 140 a.move(-b.x(), -b.y(), -b.z()); 141 return a; 142} 143 144inline FloatPoint3D operator+(const FloatPoint3D& a, const FloatPoint3D& b) 145{ 146 return FloatPoint3D(a.x() + b.x(), a.y() + b.y(), a.z() + b.z()); 147} 148 149inline FloatPoint3D operator-(const FloatPoint3D& a, const FloatPoint3D& b) 150{ 151 return FloatPoint3D(a.x() - b.x(), a.y() - b.y(), a.z() - b.z()); 152} 153 154inline bool operator==(const FloatPoint3D& a, const FloatPoint3D& b) 155{ 156 return a.x() == b.x() && a.y() == b.y() && a.z() == b.z(); 157} 158 159inline bool operator!=(const FloatPoint3D& a, const FloatPoint3D& b) 160{ 161 return a.x() != b.x() || a.y() != b.y() || a.z() != b.z(); 162} 163 164inline float operator*(const FloatPoint3D& a, const FloatPoint3D& b) 165{ 166 // dot product 167 return a.dot(b); 168} 169 170inline FloatPoint3D operator*(float k, const FloatPoint3D& v) 171{ 172 return FloatPoint3D(k * v.x(), k * v.y(), k * v.z()); 173} 174 175inline FloatPoint3D operator*(const FloatPoint3D& v, float k) 176{ 177 return FloatPoint3D(k * v.x(), k * v.y(), k * v.z()); 178} 179 180inline float FloatPoint3D::distanceTo(const FloatPoint3D& a) const 181{ 182 return (*this - a).length(); 183} 184 185} // namespace blink 186 187#endif // FloatPoint3D_h 188