| 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 "FloatPoint.h" |
| 27 | |
| 28 | namespace WebCore { |
| 29 | |
| 30 | class FloatPoint3D { |
| 31 | public: |
| 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 | float x() const { return m_x; } |
| 54 | void setX(float x) { m_x = x; } |
| 55 | |
| 56 | float y() const { return m_y; } |
| 57 | void setY(float y) { m_y = y; } |
| 58 | |
| 59 | FloatPoint xy() const { return { m_x, m_y }; } |
| 60 | void setXY(FloatPoint p) |
| 61 | { |
| 62 | m_x = p.x(); |
| 63 | m_y = p.y(); |
| 64 | } |
| 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 | |
| 126 | private: |
| 127 | float m_x; |
| 128 | float m_y; |
| 129 | float m_z; |
| 130 | }; |
| 131 | |
| 132 | inline FloatPoint3D& operator +=(FloatPoint3D& a, const FloatPoint3D& b) |
| 133 | { |
| 134 | a.move(b.x(), b.y(), b.z()); |
| 135 | return a; |
| 136 | } |
| 137 | |
| 138 | inline FloatPoint3D& operator -=(FloatPoint3D& a, const FloatPoint3D& b) |
| 139 | { |
| 140 | a.move(-b.x(), -b.y(), -b.z()); |
| 141 | return a; |
| 142 | } |
| 143 | |
| 144 | inline 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 | |
| 149 | inline 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 | |
| 154 | inline 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 | |
| 159 | inline 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 | |
| 164 | inline float operator*(const FloatPoint3D& a, const FloatPoint3D& b) |
| 165 | { |
| 166 | // dot product |
| 167 | return a.dot(b); |
| 168 | } |
| 169 | |
| 170 | inline FloatPoint3D operator*(float k, const FloatPoint3D& v) |
| 171 | { |
| 172 | return FloatPoint3D(k * v.x(), k * v.y(), k * v.z()); |
| 173 | } |
| 174 | |
| 175 | inline FloatPoint3D operator*(const FloatPoint3D& v, float k) |
| 176 | { |
| 177 | return FloatPoint3D(k * v.x(), k * v.y(), k * v.z()); |
| 178 | } |
| 179 | |
| 180 | inline float FloatPoint3D::distanceTo(const FloatPoint3D& a) const |
| 181 | { |
| 182 | return (*this - a).length(); |
| 183 | } |
| 184 | |
| 185 | WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const FloatPoint3D&); |
| 186 | |
| 187 | } // namespace WebCore |
| 188 | |
| 189 | #endif // FloatPoint3D_h |
| 190 | |