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_BASEPOINT4D_H_
7 : #define MOZILLA_BASEPOINT4D_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 BasePoint4D {
21 : union {
22 : struct {
23 : T x, y, z, w;
24 : };
25 : T components[4];
26 : };
27 :
28 : // Constructors
29 54345 : BasePoint4D() : x(0), y(0), z(0), w(0) {}
30 8461 : BasePoint4D(T aX, T aY, T aZ, T aW) : x(aX), y(aY), z(aZ), w(aW) {}
31 :
32 : void MoveTo(T aX, T aY, T aZ, T aW) { x = aX; y = aY; z = aZ; w = aW; }
33 : void MoveBy(T aDx, T aDy, T aDz, T aDw) { x += aDx; y += aDy; z += aDz; w += aDw; }
34 :
35 : // Note that '=' isn't defined so we'll get the
36 : // compiler generated default assignment operator
37 :
38 : bool operator==(const Sub& aPoint) const {
39 : return x == aPoint.x && y == aPoint.y &&
40 : z == aPoint.z && w == aPoint.w;
41 : }
42 : bool operator!=(const Sub& aPoint) const {
43 : return x != aPoint.x || y != aPoint.y ||
44 : z != aPoint.z || w != aPoint.w;
45 : }
46 :
47 0 : Sub operator+(const Sub& aPoint) const {
48 0 : return Sub(x + aPoint.x, y + aPoint.y, z + aPoint.z, w + aPoint.w);
49 : }
50 0 : Sub operator-(const Sub& aPoint) const {
51 0 : return Sub(x - aPoint.x, y - aPoint.y, z - aPoint.z, w - aPoint.w);
52 : }
53 0 : Sub& operator+=(const Sub& aPoint) {
54 0 : x += aPoint.x;
55 0 : y += aPoint.y;
56 0 : z += aPoint.z;
57 0 : w += aPoint.w;
58 0 : return *static_cast<Sub*>(this);
59 : }
60 0 : Sub& operator-=(const Sub& aPoint) {
61 0 : x -= aPoint.x;
62 0 : y -= aPoint.y;
63 0 : z -= aPoint.z;
64 0 : w -= aPoint.w;
65 0 : return *static_cast<Sub*>(this);
66 : }
67 :
68 0 : Sub operator*(T aScale) const {
69 0 : return Sub(x * aScale, y * aScale, z * aScale, w * aScale);
70 : }
71 0 : Sub operator/(T aScale) const {
72 0 : return Sub(x / aScale, y / aScale, z / aScale, w / aScale);
73 : }
74 :
75 0 : Sub& operator*=(T aScale) {
76 0 : x *= aScale;
77 0 : y *= aScale;
78 0 : z *= aScale;
79 0 : w *= aScale;
80 0 : return *static_cast<Sub*>(this);
81 : }
82 :
83 2832 : Sub& operator/=(T aScale) {
84 2832 : x /= aScale;
85 2832 : y /= aScale;
86 2832 : z /= aScale;
87 2832 : w /= aScale;
88 2832 : return *static_cast<Sub*>(this);
89 : }
90 :
91 : Sub operator-() const {
92 : return Sub(-x, -y, -z, -w);
93 : }
94 :
95 0 : T& operator[](int aIndex) {
96 0 : MOZ_ASSERT(aIndex >= 0 && aIndex <= 3, "Invalid array index");
97 0 : return *((&x)+aIndex);
98 : }
99 :
100 0 : const T& operator[](int aIndex) const {
101 0 : MOZ_ASSERT(aIndex >= 0 && aIndex <= 3, "Invalid array index");
102 0 : return *((&x)+aIndex);
103 : }
104 :
105 14160 : T DotProduct(const Sub& aPoint) const {
106 14160 : return x * aPoint.x + y * aPoint.y + z * aPoint.z + w * aPoint.w;
107 : }
108 :
109 : // Ignores the 4th component!
110 0 : Sub CrossProduct(const Sub& aPoint) const {
111 0 : return Sub(y * aPoint.z - aPoint.y * z,
112 0 : z * aPoint.x - aPoint.z * x,
113 0 : x * aPoint.y - aPoint.x * y,
114 0 : 0);
115 : }
116 :
117 2832 : T Length() const {
118 2832 : return sqrt(x*x + y*y + z*z + w*w);
119 : }
120 :
121 2832 : void Normalize() {
122 2832 : *this /= Length();
123 2832 : }
124 :
125 1741 : bool HasPositiveWCoord() { return w > 0; }
126 : };
127 :
128 : } // namespace gfx
129 : } // namespace mozilla
130 :
131 : #endif /* MOZILLA_BASEPOINT4D_H_ */
|