1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Insoo Woo <iwoo@purdue.edu> |
---|
6 | */ |
---|
7 | #ifndef VRVECTOR3F_H |
---|
8 | #define VRVECTOR3F_H |
---|
9 | |
---|
10 | #include <cstdlib> |
---|
11 | #include <cmath> |
---|
12 | |
---|
13 | namespace vrmath { |
---|
14 | |
---|
15 | class Vector3f |
---|
16 | { |
---|
17 | public: |
---|
18 | Vector3f() : |
---|
19 | x(0.0f), y(0.0f), z(0.0f) |
---|
20 | {} |
---|
21 | |
---|
22 | Vector3f(const Vector3f& v3) : |
---|
23 | x(v3.x), y(v3.y), z(v3.z) |
---|
24 | {} |
---|
25 | |
---|
26 | Vector3f(float x1, float y1, float z1) : |
---|
27 | x(x1), y(y1), z(z1) |
---|
28 | {} |
---|
29 | |
---|
30 | Vector3f& operator=(const Vector3f& other) |
---|
31 | { |
---|
32 | if (&other != this) { |
---|
33 | set(other.x, other.y, other.z); |
---|
34 | } |
---|
35 | return *this; |
---|
36 | } |
---|
37 | |
---|
38 | void set(float x, float y, float z); |
---|
39 | |
---|
40 | void set(const Vector3f& v); |
---|
41 | |
---|
42 | double length() const; |
---|
43 | |
---|
44 | double distance(const Vector3f& v) const; |
---|
45 | |
---|
46 | double distance(float x, float y, float z) const; |
---|
47 | |
---|
48 | double distanceSquare(const Vector3f& v) const; |
---|
49 | |
---|
50 | double distanceSquare(float x, float y, float z) const; |
---|
51 | |
---|
52 | double dot(const Vector3f& v) const; |
---|
53 | |
---|
54 | Vector3f cross(const Vector3f& v) const; |
---|
55 | |
---|
56 | Vector3f normalize() const; |
---|
57 | |
---|
58 | Vector3f scale(const Vector3f& scale) const; |
---|
59 | |
---|
60 | Vector3f scale(double scale) const; |
---|
61 | |
---|
62 | bool operator==(const Vector3f& v) const; |
---|
63 | |
---|
64 | bool operator!=(const Vector3f& v) const; |
---|
65 | |
---|
66 | Vector3f operator-() const; |
---|
67 | |
---|
68 | // scalar ops |
---|
69 | Vector3f operator+(double scalar) const; |
---|
70 | Vector3f operator-(double scalar) const; |
---|
71 | Vector3f operator*(double scalar) const; |
---|
72 | Vector3f operator/(double scalar) const; |
---|
73 | Vector3f& operator*=(double scalar); |
---|
74 | |
---|
75 | // vector ops |
---|
76 | Vector3f operator+(const Vector3f& op2) const; |
---|
77 | Vector3f operator-(const Vector3f& op2) const; |
---|
78 | Vector3f& operator+=(const Vector3f& op2); |
---|
79 | Vector3f& operator-=(const Vector3f& op2); |
---|
80 | |
---|
81 | bool isEqual(const Vector3f& v) const; |
---|
82 | bool isAlmostEqual(const Vector3f& v, float tol) const; |
---|
83 | |
---|
84 | float getX() const; |
---|
85 | float getY() const; |
---|
86 | float getZ() const; |
---|
87 | |
---|
88 | //friend Vector3f operator*(float scale, const Vector3f& value); |
---|
89 | |
---|
90 | union { |
---|
91 | struct { |
---|
92 | float x, y, z; |
---|
93 | }; |
---|
94 | struct { |
---|
95 | float r, g, b; |
---|
96 | }; |
---|
97 | }; |
---|
98 | }; |
---|
99 | |
---|
100 | inline bool Vector3f::operator==(const Vector3f &v) const |
---|
101 | { |
---|
102 | return (v.x == x && v.y == y && v.z == z); |
---|
103 | } |
---|
104 | |
---|
105 | inline bool Vector3f::operator!=(const Vector3f &v) const |
---|
106 | { |
---|
107 | return !(v.x == x && v.y == y && v.z == z); |
---|
108 | } |
---|
109 | |
---|
110 | inline Vector3f Vector3f::operator-() const |
---|
111 | { |
---|
112 | return Vector3f(-x, -y, -z); |
---|
113 | } |
---|
114 | |
---|
115 | inline Vector3f Vector3f::operator+(double scalar) const |
---|
116 | { |
---|
117 | return Vector3f(x + scalar, y + scalar, z + scalar); |
---|
118 | } |
---|
119 | |
---|
120 | inline Vector3f Vector3f::operator-(double scalar) const |
---|
121 | { |
---|
122 | return Vector3f(x - scalar, y - scalar, z - scalar); |
---|
123 | } |
---|
124 | |
---|
125 | inline Vector3f Vector3f::operator*(double scalar) const |
---|
126 | { |
---|
127 | return Vector3f(x * scalar, y * scalar, z * scalar); |
---|
128 | } |
---|
129 | |
---|
130 | inline Vector3f Vector3f::operator/(double scalar) const |
---|
131 | { |
---|
132 | return Vector3f(x / scalar, y / scalar, z / scalar); |
---|
133 | } |
---|
134 | |
---|
135 | inline Vector3f& Vector3f::operator*=(double scalar) |
---|
136 | { |
---|
137 | x *= scalar; |
---|
138 | y *= scalar; |
---|
139 | z *= scalar; |
---|
140 | return *this; |
---|
141 | } |
---|
142 | |
---|
143 | inline Vector3f Vector3f::operator+(const Vector3f& op2) const |
---|
144 | { |
---|
145 | return Vector3f(x + op2.x, y + op2.y, z + op2.z); |
---|
146 | } |
---|
147 | |
---|
148 | inline Vector3f Vector3f::operator-(const Vector3f& op2) const |
---|
149 | { |
---|
150 | return Vector3f(x - op2.x, y - op2.y, z - op2.z); |
---|
151 | } |
---|
152 | |
---|
153 | inline Vector3f& Vector3f::operator+=(const Vector3f& op2) |
---|
154 | { |
---|
155 | x += op2.x; |
---|
156 | y += op2.y; |
---|
157 | z += op2.z; |
---|
158 | return *this; |
---|
159 | } |
---|
160 | |
---|
161 | inline Vector3f& Vector3f::operator-=(const Vector3f& op2) |
---|
162 | { |
---|
163 | x -= op2.x; |
---|
164 | y -= op2.y; |
---|
165 | z -= op2.z; |
---|
166 | return *this; |
---|
167 | } |
---|
168 | |
---|
169 | #if 0 |
---|
170 | inline Vector3f operator*(double scale, const Vector3f& value) |
---|
171 | { |
---|
172 | return Vector3f(value.x * scale, value.y * scale, value.z * scale); |
---|
173 | } |
---|
174 | #endif |
---|
175 | |
---|
176 | inline void Vector3f::set(float x, float y, float z) |
---|
177 | { |
---|
178 | this->x = x; |
---|
179 | this->y = y; |
---|
180 | this->z = z; |
---|
181 | } |
---|
182 | |
---|
183 | inline void Vector3f::set(const Vector3f& v) |
---|
184 | { |
---|
185 | x = v.x; |
---|
186 | y = v.y; |
---|
187 | z = v.z; |
---|
188 | } |
---|
189 | |
---|
190 | inline double Vector3f::dot(const Vector3f& v) const |
---|
191 | { |
---|
192 | return ((double)x * v.x + (double)y * v.y + (double)z * v.z); |
---|
193 | } |
---|
194 | |
---|
195 | inline double Vector3f::length() const |
---|
196 | { |
---|
197 | return sqrt((double)x * x + (double)y * y + (double)z * z); |
---|
198 | } |
---|
199 | |
---|
200 | inline double Vector3f::distance(const Vector3f& v) const |
---|
201 | { |
---|
202 | double x1 = ((double)v.x - (double)x) , y1 = ((double)v.y - (double)y), z1 = ((double)v.z - (double)z); |
---|
203 | return sqrt(x1 * x1 + y1 * y1 + z1 * z1); |
---|
204 | } |
---|
205 | |
---|
206 | inline double Vector3f::distance(float x, float y, float z) const |
---|
207 | { |
---|
208 | double x1 = ((double)x - (double)this->x) , y1 = ((double)y - (double)this->y), z1 = ((double)z - (double)this->z); |
---|
209 | return sqrt(x1 * x1 + y1 * y1 + z1 * z1); |
---|
210 | } |
---|
211 | |
---|
212 | inline double Vector3f::distanceSquare(const Vector3f& v) const |
---|
213 | { |
---|
214 | double x1 = ((double)v.x - (double)x) , y1 = ((double)v.y - (double)y), z1 = ((double)v.z - (double)z); |
---|
215 | return (x1 * x1 + y1 * y1 + z1 * z1); |
---|
216 | } |
---|
217 | |
---|
218 | inline double Vector3f::distanceSquare(float x, float y, float z) const |
---|
219 | { |
---|
220 | double x1 = ((double)x - (double)this->x) , y1 = ((double)y - (double)this->y), z1 = ((double)z - (double)this->z); |
---|
221 | return (x1 * x1 + y1 * y1 + z1 * z1); |
---|
222 | } |
---|
223 | |
---|
224 | inline Vector3f Vector3f::cross(const Vector3f& op2) const |
---|
225 | { |
---|
226 | return Vector3f(y * op2.z - z * op2.y, |
---|
227 | z * op2.x - x * op2.z, |
---|
228 | x * op2.y - y * op2.x); |
---|
229 | } |
---|
230 | |
---|
231 | inline Vector3f Vector3f::normalize() const |
---|
232 | { |
---|
233 | double len = length(); |
---|
234 | if (len > 0.0) { |
---|
235 | return Vector3f(x / len, y / len, z / len); |
---|
236 | } else { |
---|
237 | return *this; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | inline Vector3f Vector3f::scale(double scale) const |
---|
242 | { |
---|
243 | return Vector3f(x * scale, y * scale, z * scale); |
---|
244 | } |
---|
245 | |
---|
246 | inline Vector3f Vector3f::scale(const Vector3f& scale) const |
---|
247 | { |
---|
248 | return Vector3f(x * scale.x, y * scale.y, z * scale.z); |
---|
249 | } |
---|
250 | |
---|
251 | inline bool Vector3f::isAlmostEqual(const Vector3f& v, float tol) const |
---|
252 | { |
---|
253 | return (v.x - tol) <= x && x <= (v.x + tol) && |
---|
254 | (v.y - tol) <= y && y <= (v.y + tol) && |
---|
255 | (v.z - tol) <= z && z <= (v.z + tol); |
---|
256 | } |
---|
257 | |
---|
258 | inline bool Vector3f::isEqual(const Vector3f& v) const |
---|
259 | { |
---|
260 | return ( v.x == x && v.y == y && v.z == z ); |
---|
261 | } |
---|
262 | |
---|
263 | inline float Vector3f::getX() const |
---|
264 | { |
---|
265 | return x; |
---|
266 | } |
---|
267 | |
---|
268 | inline float Vector3f::getY() const |
---|
269 | { |
---|
270 | return y; |
---|
271 | } |
---|
272 | |
---|
273 | inline float Vector3f::getZ() const |
---|
274 | { |
---|
275 | return z; |
---|
276 | } |
---|
277 | |
---|
278 | inline Vector3f cross(const Vector3f& op1, const Vector3f& op2) |
---|
279 | { |
---|
280 | return Vector3f(op1.cross(op2)); |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | * \brief Linear interpolation of 2 vectors |
---|
285 | */ |
---|
286 | inline Vector3f vlerp(const Vector3f& v1, const Vector3f& v2, double t) |
---|
287 | { |
---|
288 | return Vector3f(v1.x * (1.0-t) + v2.x * t, |
---|
289 | v1.y * (1.0-t) + v2.y * t, |
---|
290 | v1.z * (1.0-t) + v2.z * t); |
---|
291 | } |
---|
292 | |
---|
293 | } |
---|
294 | |
---|
295 | #endif |
---|