|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef MOZILLA_BASEPOINT3D_H_ |
|
7 #define MOZILLA_BASEPOINT3D_H_ |
|
8 |
|
9 #include "mozilla/Assertions.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace gfx { |
|
13 |
|
14 /** |
|
15 * Do not use this class directly. Subclass it, pass that subclass as the |
|
16 * Sub parameter, and only use that subclass. This allows methods to safely |
|
17 * cast 'this' to 'Sub*'. |
|
18 */ |
|
19 template <class T, class Sub> |
|
20 struct BasePoint3D { |
|
21 T x, y, z; |
|
22 |
|
23 // Constructors |
|
24 BasePoint3D() : x(0), y(0), z(0) {} |
|
25 BasePoint3D(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {} |
|
26 |
|
27 void MoveTo(T aX, T aY, T aZ) { x = aX; y = aY; z = aZ; } |
|
28 void MoveBy(T aDx, T aDy, T aDz) { x += aDx; y += aDy; z += aDz; } |
|
29 |
|
30 // Note that '=' isn't defined so we'll get the |
|
31 // compiler generated default assignment operator |
|
32 |
|
33 T& operator[](int aIndex) { |
|
34 MOZ_ASSERT(aIndex >= 0 && aIndex <= 2); |
|
35 return *((&x)+aIndex); |
|
36 } |
|
37 |
|
38 const T& operator[](int aIndex) const { |
|
39 MOZ_ASSERT(aIndex >= 0 && aIndex <= 2); |
|
40 return *((&x)+aIndex); |
|
41 } |
|
42 |
|
43 bool operator==(const Sub& aPoint) const { |
|
44 return x == aPoint.x && y == aPoint.y && z == aPoint.z; |
|
45 } |
|
46 bool operator!=(const Sub& aPoint) const { |
|
47 return x != aPoint.x || y != aPoint.y || z != aPoint.z; |
|
48 } |
|
49 |
|
50 Sub operator+(const Sub& aPoint) const { |
|
51 return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z); |
|
52 } |
|
53 Sub operator-(const Sub& aPoint) const { |
|
54 return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z); |
|
55 } |
|
56 Sub& operator+=(const Sub& aPoint) { |
|
57 x += aPoint.x; |
|
58 y += aPoint.y; |
|
59 z += aPoint.z; |
|
60 return *static_cast<Sub*>(this); |
|
61 } |
|
62 Sub& operator-=(const Sub& aPoint) { |
|
63 x -= aPoint.x; |
|
64 y -= aPoint.y; |
|
65 z -= aPoint.z; |
|
66 return *static_cast<Sub*>(this); |
|
67 } |
|
68 |
|
69 Sub operator*(T aScale) const { |
|
70 return Sub(x * aScale, y * aScale, z * aScale); |
|
71 } |
|
72 Sub operator/(T aScale) const { |
|
73 return Sub(x / aScale, y / aScale, z / aScale); |
|
74 } |
|
75 |
|
76 Sub& operator*=(T aScale) { |
|
77 x *= aScale; |
|
78 y *= aScale; |
|
79 z *= aScale; |
|
80 return *static_cast<Sub*>(this); |
|
81 } |
|
82 |
|
83 Sub& operator/=(T aScale) { |
|
84 x /= aScale; |
|
85 y /= aScale; |
|
86 z /= aScale; |
|
87 return *static_cast<Sub*>(this); |
|
88 } |
|
89 |
|
90 Sub operator-() const { |
|
91 return Sub(-x, -y, -z); |
|
92 } |
|
93 |
|
94 Sub CrossProduct(const Sub& aPoint) const { |
|
95 return Sub(y * aPoint.z - aPoint.y * z, |
|
96 z * aPoint.x - aPoint.z * x, |
|
97 x * aPoint.y - aPoint.x * y); |
|
98 } |
|
99 |
|
100 T DotProduct(const Sub& aPoint) const { |
|
101 return x * aPoint.x + y * aPoint.y + z * aPoint.z; |
|
102 } |
|
103 |
|
104 T Length() const { |
|
105 return sqrt(x*x + y*y + z*z); |
|
106 } |
|
107 |
|
108 // Invalid for points with distance from origin of 0. |
|
109 void Normalize() { |
|
110 *this /= Length(); |
|
111 } |
|
112 }; |
|
113 |
|
114 } |
|
115 } |
|
116 |
|
117 #endif /* MOZILLA_BASEPOINT3D_H_ */ |