xref: /haiku/src/apps/haiku3d/Vector3.h (revision b6b0567fbd186f8ce8a0c90bdc7a7b5b4c649678)
1 /*
2  * Copyright 2008 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Alexandre Deckner
7  *
8  */
9 
10 /*
11  *
12  * This is a refactored and stripped down version of bullet-2.66 src\LinearMath\btVector3.h
13  * The dependancies on base class btQuadWord have been removed for simplification.
14  *
15  */
16 
17 /*
18 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans  http://continuousphysics.com/Bullet/
19 
20 This software is provided 'as-is', without any express or implied warranty.
21 In no event will the authors be held liable for any damages arising from the use of this software.
22 Permission is granted to anyone to use this software for any purpose,
23 including commercial applications, and to alter it and redistribute it freely,
24 subject to the following restrictions:
25 
26 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
27 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
28 3. This notice may not be removed or altered from any source distribution.
29 */
30 
31 
32 #ifndef __VECTOR3_H__
33 #define __VECTOR3_H__
34 
35 #include "assert.h"
36 #include "math.h"
37 
38 ///Vector3 can be used to represent 3D points and vectors.
39 
40 class	Vector3 {
41 protected:
42 	float	m_x;
43 	float	m_y;
44 	float	m_z;
45 
46 public:
47 	inline Vector3() {}
48 
49 
50 	inline Vector3(const Vector3& v)
51 	{
52 		*((Vector3*)this) = v;
53 	}
54 
55 
56 	inline Vector3(const float& x, const float& y, const float& z)
57 	{
58 		m_x = x, m_y = y, m_z = z;
59 	}
60 
61 
62 	inline const float& x() const { return m_x; }
63 
64 
65 	inline const float& y() const { return m_y; }
66 
67 
68 	inline const float& z() const { return m_z; }
69 
70 
71 	inline  void 	setValue(const float& x, const float& y, const float& z)
72 	{
73 		m_x=x;
74 		m_y=y;
75 		m_z=z;
76 	}
77 
78 	inline Vector3& operator+=(const Vector3& v)
79 	{
80 		m_x += v.x(); m_y += v.y(); m_z += v.z();
81 		return *this;
82 	}
83 
84 
85 	inline Vector3& operator-=(const Vector3& v)
86 	{
87 		m_x -= v.x(); m_y -= v.y(); m_z -= v.z();
88 		return *this;
89 	}
90 
91 
92 	inline Vector3& operator*=(const float& s)
93 	{
94 		m_x *= s; m_y *= s; m_z *= s;
95 		return *this;
96 	}
97 
98 
99 	inline Vector3& operator/=(const float& s)
100 	{
101 		assert(s != 0.0f);
102 		return *this *= 1.0f / s;
103 	}
104 
105 
106 	inline float dot(const Vector3& v) const
107 	{
108 		return m_x * v.x() + m_y * v.y() + m_z * v.z();
109 	}
110 
111 
112 	inline float length2() const
113 	{
114 		return dot(*this);
115 	}
116 
117 
118 	inline float length() const
119 	{
120 		return sqrt(length2());
121 	}
122 
123 
124 	inline float distance2(const Vector3& v) const;
125 
126 
127 	inline float distance(const Vector3& v) const;
128 
129 
130 	inline Vector3& normalize()
131 	{
132 		return *this /= length();
133 	}
134 
135 
136 	inline Vector3 normalized() const;
137 
138 
139 	inline Vector3 rotate( const Vector3& wAxis, const float angle );
140 
141 
142 	inline float angle(const Vector3& v) const
143 	{
144 		float s = sqrt(length2() * v.length2());
145 		assert(s != 0.0f);
146 		return acos(dot(v) / s);
147 	}
148 
149 
150 	inline Vector3 absolute() const
151 	{
152 		return Vector3(
153 			fabs(m_x),
154 			fabs(m_y),
155 			fabs(m_z));
156 	}
157 
158 
159 	inline Vector3 cross(const Vector3& v) const
160 	{
161 		return Vector3(
162 			m_y * v.z() - m_z * v.y(),
163 			m_z * v.x() - m_x * v.z(),
164 			m_x * v.y() - m_y * v.x());
165 	}
166 
167 
168 	inline float triple(const Vector3& v1, const Vector3& v2) const
169 	{
170 		return m_x * (v1.y() * v2.z() - v1.z() * v2.y()) +
171 			m_y * (v1.z() * v2.x() - v1.x() * v2.z()) +
172 			m_z * (v1.x() * v2.y() - v1.y() * v2.x());
173 	}
174 
175 
176 	inline int minAxis() const
177 	{
178 		return m_x < m_y ? (m_x < m_z ? 0 : 2) : (m_y < m_z ? 1 : 2);
179 	}
180 
181 
182 	inline int maxAxis() const
183 	{
184 		return m_x < m_y ? (m_y < m_z ? 2 : 1) : (m_x < m_z ? 2 : 0);
185 	}
186 
187 
188 	inline int furthestAxis() const
189 	{
190 		return absolute().minAxis();
191 	}
192 
193 
194 	inline int closestAxis() const
195 	{
196 		return absolute().maxAxis();
197 	}
198 
199 
200 	inline void setInterpolate3(const Vector3& v0, const Vector3& v1, float rt)
201 	{
202 		float s = 1.0f - rt;
203 		m_x = s * v0.x() + rt * v1.x();
204 		m_y = s * v0.y() + rt * v1.y();
205 		m_z = s * v0.z() + rt * v1.z();
206 		//don't do the unused w component
207 		//		m_co[3] = s * v0[3] + rt * v1[3];
208 	}
209 
210 
211 	inline Vector3 lerp(const Vector3& v, const float& t) const
212 	{
213 		return Vector3(m_x + (v.x() - m_x) * t,
214 			m_y + (v.y() - m_y) * t,
215 			m_z + (v.z() - m_z) * t);
216 	}
217 
218 
219 	inline Vector3& operator*=(const Vector3& v)
220 	{
221 		m_x *= v.x(); m_y *= v.y(); m_z *= v.z();
222 		return *this;
223 	}
224 
225 };
226 
227 inline Vector3
228 operator+(const Vector3& v1, const Vector3& v2)
229 {
230 	return Vector3(v1.x() + v2.x(), v1.y() + v2.y(), v1.z() + v2.z());
231 }
232 
233 inline Vector3
234 operator*(const Vector3& v1, const Vector3& v2)
235 {
236 	return Vector3(v1.x() * v2.x(), v1.y() * v2.y(), v1.z() * v2.z());
237 }
238 
239 inline Vector3
240 operator-(const Vector3& v1, const Vector3& v2)
241 {
242 	return Vector3(v1.x() - v2.x(), v1.y() - v2.y(), v1.z() - v2.z());
243 }
244 
245 inline Vector3
246 operator-(const Vector3& v)
247 {
248 	return Vector3(-v.x(), -v.y(), -v.z());
249 }
250 
251 inline Vector3
252 operator*(const Vector3& v, const float& s)
253 {
254 	return Vector3(v.x() * s, v.y() * s, v.z() * s);
255 }
256 
257 inline Vector3
258 operator*(const float& s, const Vector3& v)
259 {
260 	return v * s;
261 }
262 
263 inline Vector3
264 operator/(const Vector3& v, const float& s)
265 {
266 	assert(s != 0.0f);
267 	return v * (1.0f / s);
268 }
269 
270 inline Vector3
271 operator/(const Vector3& v1, const Vector3& v2)
272 {
273 	return Vector3(v1.x() / v2.x(),v1.y() / v2.y(),v1.z() / v2.z());
274 }
275 
276 inline float
277 dot(const Vector3& v1, const Vector3& v2)
278 {
279 	return v1.dot(v2);
280 }
281 
282 inline float
283 distance2(const Vector3& v1, const Vector3& v2)
284 {
285 	return v1.distance2(v2);
286 }
287 
288 
289 inline float
290 distance(const Vector3& v1, const Vector3& v2)
291 {
292 	return v1.distance(v2);
293 }
294 
295 inline float
296 angle(const Vector3& v1, const Vector3& v2)
297 {
298 	return v1.angle(v2);
299 }
300 
301 inline Vector3
302 cross(const Vector3& v1, const Vector3& v2)
303 {
304 	return v1.cross(v2);
305 }
306 
307 inline float
308 triple(const Vector3& v1, const Vector3& v2, const Vector3& v3)
309 {
310 	return v1.triple(v2, v3);
311 }
312 
313 inline Vector3
314 lerp(const Vector3& v1, const Vector3& v2, const float& t)
315 {
316 	return v1.lerp(v2, t);
317 }
318 
319 
320 inline bool operator==(const Vector3& p1, const Vector3& p2)
321 {
322 	return p1.x() == p2.x() && p1.y() == p2.y() && p1.z() == p2.z();
323 }
324 
325 inline float Vector3::distance2(const Vector3& v) const
326 {
327 	return (v - *this).length2();
328 }
329 
330 inline float Vector3::distance(const Vector3& v) const
331 {
332 	return (v - *this).length();
333 }
334 
335 inline Vector3 Vector3::normalized() const
336 {
337 	return *this / length();
338 }
339 
340 inline Vector3 Vector3::rotate( const Vector3& wAxis, const float angle )
341 {
342 	// wAxis must be a unit lenght vector
343 
344 	Vector3 o = wAxis * wAxis.dot( *this );
345 	Vector3 x = *this - o;
346 	Vector3 y;
347 
348 	y = wAxis.cross( *this );
349 
350 	return ( o + x * cos( angle ) + y * sin( angle ) );
351 }
352 
353 #endif //__VECTOR3_H__
354