Line data Source code
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 : union {
22 : struct {
23 : T x, y, z;
24 : };
25 : T components[3];
26 : };
27 :
28 : // Constructors
29 36 : BasePoint3D() : x(0), y(0), z(0) {}
30 424 : BasePoint3D(T aX, T aY, T aZ) : x(aX), y(aY), z(aZ) {}
31 :
32 : void MoveTo(T aX, T aY, T aZ) { x = aX; y = aY; z = aZ; }
33 : void MoveBy(T aDx, T aDy, T aDz) { x += aDx; y += aDy; z += aDz; }
34 :
35 : // Note that '=' isn't defined so we'll get the
36 : // compiler generated default assignment operator
37 :
38 0 : T& operator[](int aIndex) {
39 0 : MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
40 0 : return *((&x)+aIndex);
41 : }
42 :
43 : const T& operator[](int aIndex) const {
44 : MOZ_ASSERT(aIndex >= 0 && aIndex <= 2);
45 : return *((&x)+aIndex);
46 : }
47 :
48 0 : bool operator==(const Sub& aPoint) const {
49 0 : return x == aPoint.x && y == aPoint.y && z == aPoint.z;
50 : }
51 0 : bool operator!=(const Sub& aPoint) const {
52 0 : return x != aPoint.x || y != aPoint.y || z != aPoint.z;
53 : }
54 :
55 0 : Sub operator+(const Sub& aPoint) const {
56 0 : return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z);
57 : }
58 0 : Sub operator-(const Sub& aPoint) const {
59 0 : return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z);
60 : }
61 : Sub& operator+=(const Sub& aPoint) {
62 : x += aPoint.x;
63 : y += aPoint.y;
64 : z += aPoint.z;
65 : return *static_cast<Sub*>(this);
66 : }
67 : Sub& operator-=(const Sub& aPoint) {
68 : x -= aPoint.x;
69 : y -= aPoint.y;
70 : z -= aPoint.z;
71 : return *static_cast<Sub*>(this);
72 : }
73 :
74 0 : Sub operator*(T aScale) const {
75 0 : return Sub(x * aScale, y * aScale, z * aScale);
76 : }
77 : Sub operator/(T aScale) const {
78 : return Sub(x / aScale, y / aScale, z / aScale);
79 : }
80 :
81 0 : Sub& operator*=(T aScale) {
82 0 : x *= aScale;
83 0 : y *= aScale;
84 0 : z *= aScale;
85 0 : return *static_cast<Sub*>(this);
86 : }
87 :
88 0 : Sub& operator/=(T aScale) {
89 0 : x /= aScale;
90 0 : y /= aScale;
91 0 : z /= aScale;
92 0 : return *static_cast<Sub*>(this);
93 : }
94 :
95 0 : Sub operator-() const {
96 0 : return Sub(-x, -y, -z);
97 : }
98 :
99 0 : Sub CrossProduct(const Sub& aPoint) const {
100 0 : return Sub(y * aPoint.z - aPoint.y * z,
101 0 : z * aPoint.x - aPoint.z * x,
102 0 : x * aPoint.y - aPoint.x * y);
103 : }
104 :
105 0 : T DotProduct(const Sub& aPoint) const {
106 0 : return x * aPoint.x + y * aPoint.y + z * aPoint.z;
107 : }
108 :
109 0 : T Length() const {
110 0 : return sqrt(x*x + y*y + z*z);
111 : }
112 :
113 : // Invalid for points with distance from origin of 0.
114 0 : void Normalize() {
115 0 : *this /= Length();
116 0 : }
117 : };
118 :
119 : } // namespace gfx
120 : } // namespace mozilla
121 :
122 : #endif /* MOZILLA_BASEPOINT3D_H_ */
|