gfx/2d/BasePoint4D.h

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

mercurial