1 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
2 | /** |
---|
3 | * Contains code for homogeneous points. |
---|
4 | * \file IceHPoint.h |
---|
5 | * \author Pierre Terdiman |
---|
6 | * \date April, 4, 2000 |
---|
7 | */ |
---|
8 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
9 | |
---|
10 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
11 | // Include Guard |
---|
12 | #ifndef __ICEHPOINT_H__ |
---|
13 | #define __ICEHPOINT_H__ |
---|
14 | |
---|
15 | class ICEMATHS_API HPoint : public Point |
---|
16 | { |
---|
17 | public: |
---|
18 | |
---|
19 | //! Empty constructor |
---|
20 | inline_ HPoint() {} |
---|
21 | //! Constructor from floats |
---|
22 | inline_ HPoint(float xx, float yy, float zz, float ww=0.0f) : Point(xx, yy, zz), w(ww) {} |
---|
23 | //! Constructor from array |
---|
24 | inline_ HPoint(const float f[4]) : Point(f), w(f[3]) {} |
---|
25 | //! Constructor from a Point |
---|
26 | inline_ HPoint(const Point& p, float ww=0.0f) : Point(p), w(ww) {} |
---|
27 | //! Destructor |
---|
28 | inline_ ~HPoint() {} |
---|
29 | |
---|
30 | //! Clear the point |
---|
31 | inline_ HPoint& Zero() { x = y = z = w = 0.0f; return *this; } |
---|
32 | |
---|
33 | //! Assignment from values |
---|
34 | inline_ HPoint& Set(float xx, float yy, float zz, float ww ) { x = xx; y = yy; z = zz; w = ww; return *this; } |
---|
35 | //! Assignment from array |
---|
36 | inline_ HPoint& Set(const float f[4]) { x = f[X]; y = f[Y]; z = f[Z]; w = f[W]; return *this; } |
---|
37 | //! Assignment from another h-point |
---|
38 | inline_ HPoint& Set(const HPoint& src) { x = src.x; y = src.y; z = src.z; w = src.w; return *this; } |
---|
39 | |
---|
40 | //! Add a vector |
---|
41 | inline_ HPoint& Add(float xx, float yy, float zz, float ww ) { x += xx; y += yy; z += zz; w += ww; return *this; } |
---|
42 | //! Add a vector |
---|
43 | inline_ HPoint& Add(const float f[4]) { x += f[X]; y += f[Y]; z += f[Z]; w += f[W]; return *this; } |
---|
44 | |
---|
45 | //! Subtract a vector |
---|
46 | inline_ HPoint& Sub(float xx, float yy, float zz, float ww ) { x -= xx; y -= yy; z -= zz; w -= ww; return *this; } |
---|
47 | //! Subtract a vector |
---|
48 | inline_ HPoint& Sub(const float f[4]) { x -= f[X]; y -= f[Y]; z -= f[Z]; w -= f[W]; return *this; } |
---|
49 | |
---|
50 | //! Multiplies by a scalar |
---|
51 | inline_ HPoint& Mul(float s) { x *= s; y *= s; z *= s; w *= s; return *this; } |
---|
52 | |
---|
53 | //! Returns MIN(x, y, z, w); |
---|
54 | float Min() const { return MIN(x, MIN(y, MIN(z, w))); } |
---|
55 | //! Returns MAX(x, y, z, w); |
---|
56 | float Max() const { return MAX(x, MAX(y, MAX(z, w))); } |
---|
57 | //! Sets each element to be componentwise minimum |
---|
58 | HPoint& Min(const HPoint& p) { x = MIN(x, p.x); y = MIN(y, p.y); z = MIN(z, p.z); w = MIN(w, p.w); return *this; } |
---|
59 | //! Sets each element to be componentwise maximum |
---|
60 | HPoint& Max(const HPoint& p) { x = MAX(x, p.x); y = MAX(y, p.y); z = MAX(z, p.z); w = MAX(w, p.w); return *this; } |
---|
61 | |
---|
62 | //! Computes square magnitude |
---|
63 | inline_ float SquareMagnitude() const { return x*x + y*y + z*z + w*w; } |
---|
64 | //! Computes magnitude |
---|
65 | inline_ float Magnitude() const { return sqrtf(x*x + y*y + z*z + w*w); } |
---|
66 | |
---|
67 | //! Normalize the vector |
---|
68 | inline_ HPoint& Normalize() |
---|
69 | { |
---|
70 | float M = Magnitude(); |
---|
71 | if(M) |
---|
72 | { |
---|
73 | M = 1.0f / M; |
---|
74 | x *= M; |
---|
75 | y *= M; |
---|
76 | z *= M; |
---|
77 | w *= M; |
---|
78 | } |
---|
79 | return *this; |
---|
80 | } |
---|
81 | |
---|
82 | // Arithmetic operators |
---|
83 | //! Operator for HPoint Negate = - HPoint; |
---|
84 | inline_ HPoint operator-() const { return HPoint(-x, -y, -z, -w); } |
---|
85 | |
---|
86 | //! Operator for HPoint Plus = HPoint + HPoint; |
---|
87 | inline_ HPoint operator+(const HPoint& p) const { return HPoint(x + p.x, y + p.y, z + p.z, w + p.w); } |
---|
88 | //! Operator for HPoint Minus = HPoint - HPoint; |
---|
89 | inline_ HPoint operator-(const HPoint& p) const { return HPoint(x - p.x, y - p.y, z - p.z, w - p.w); } |
---|
90 | |
---|
91 | //! Operator for HPoint Mul = HPoint * HPoint; |
---|
92 | inline_ HPoint operator*(const HPoint& p) const { return HPoint(x * p.x, y * p.y, z * p.z, w * p.w); } |
---|
93 | //! Operator for HPoint Scale = HPoint * float; |
---|
94 | inline_ HPoint operator*(float s) const { return HPoint(x * s, y * s, z * s, w * s); } |
---|
95 | //! Operator for HPoint Scale = float * HPoint; |
---|
96 | inline_ friend HPoint operator*(float s, const HPoint& p) { return HPoint(s * p.x, s * p.y, s * p.z, s * p.w); } |
---|
97 | |
---|
98 | //! Operator for HPoint Div = HPoint / HPoint; |
---|
99 | inline_ HPoint operator/(const HPoint& p) const { return HPoint(x / p.x, y / p.y, z / p.z, w / p.w); } |
---|
100 | //! Operator for HPoint Scale = HPoint / float; |
---|
101 | inline_ HPoint operator/(float s) const { s = 1.0f / s; return HPoint(x * s, y * s, z * s, w * s); } |
---|
102 | //! Operator for HPoint Scale = float / HPoint; |
---|
103 | inline_ friend HPoint operator/(float s, const HPoint& p) { return HPoint(s / p.x, s / p.y, s / p.z, s / p.w); } |
---|
104 | |
---|
105 | //! Operator for float DotProd = HPoint | HPoint; |
---|
106 | inline_ float operator|(const HPoint& p) const { return x*p.x + y*p.y + z*p.z + w*p.w; } |
---|
107 | // No cross-product in 4D |
---|
108 | |
---|
109 | //! Operator for HPoint += HPoint; |
---|
110 | inline_ HPoint& operator+=(const HPoint& p) { x += p.x; y += p.y; z += p.z; w += p.w; return *this; } |
---|
111 | //! Operator for HPoint += float; |
---|
112 | inline_ HPoint& operator+=(float s) { x += s; y += s; z += s; w += s; return *this; } |
---|
113 | |
---|
114 | //! Operator for HPoint -= HPoint; |
---|
115 | inline_ HPoint& operator-=(const HPoint& p) { x -= p.x; y -= p.y; z -= p.z; w -= p.w; return *this; } |
---|
116 | //! Operator for HPoint -= float; |
---|
117 | inline_ HPoint& operator-=(float s) { x -= s; y -= s; z -= s; w -= s; return *this; } |
---|
118 | |
---|
119 | //! Operator for HPoint *= HPoint; |
---|
120 | inline_ HPoint& operator*=(const HPoint& p) { x *= p.x; y *= p.y; z *= p.z; w *= p.w; return *this; } |
---|
121 | //! Operator for HPoint *= float; |
---|
122 | inline_ HPoint& operator*=(float s) { x*=s; y*=s; z*=s; w*=s; return *this; } |
---|
123 | |
---|
124 | //! Operator for HPoint /= HPoint; |
---|
125 | inline_ HPoint& operator/=(const HPoint& p) { x /= p.x; y /= p.y; z /= p.z; w /= p.w; return *this; } |
---|
126 | //! Operator for HPoint /= float; |
---|
127 | inline_ HPoint& operator/=(float s) { s = 1.0f / s; x*=s; y*=s; z*=s; w*=s; return *this; } |
---|
128 | |
---|
129 | // Arithmetic operators |
---|
130 | |
---|
131 | //! Operator for Point Mul = HPoint * Matrix3x3; |
---|
132 | Point operator*(const Matrix3x3& mat) const; |
---|
133 | //! Operator for HPoint Mul = HPoint * Matrix4x4; |
---|
134 | HPoint operator*(const Matrix4x4& mat) const; |
---|
135 | |
---|
136 | // HPoint *= Matrix3x3 doesn't exist, the matrix is first casted to a 4x4 |
---|
137 | //! Operator for HPoint *= Matrix4x4 |
---|
138 | HPoint& operator*=(const Matrix4x4& mat); |
---|
139 | |
---|
140 | // Logical operators |
---|
141 | |
---|
142 | //! Operator for "if(HPoint==HPoint)" |
---|
143 | inline_ bool operator==(const HPoint& p) const { return ( (x==p.x)&&(y==p.y)&&(z==p.z)&&(w==p.w)); } |
---|
144 | //! Operator for "if(HPoint!=HPoint)" |
---|
145 | inline_ bool operator!=(const HPoint& p) const { return ( (x!=p.x)||(y!=p.y)||(z!=p.z)||(w!=p.w)); } |
---|
146 | |
---|
147 | // Cast operators |
---|
148 | |
---|
149 | //! Cast a HPoint to a Point. w is discarded. |
---|
150 | #ifdef _MSC_VER |
---|
151 | inline_ operator Point() const { return Point(x, y, z); } |
---|
152 | // gcc complains that conversion to a base class will never use a type conversion operator |
---|
153 | #endif |
---|
154 | |
---|
155 | public: |
---|
156 | float w; |
---|
157 | }; |
---|
158 | |
---|
159 | #endif // __ICEHPOINT_H__ |
---|
160 | |
---|