2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef __MATH_VECTOR_H__
23 #define __MATH_VECTOR_H__
25 #include "globaldefs.h"
27 #if GDEF_COMPILER_MSVC
28 #pragma warning(disable : 4244)
34 //#define DotProduct(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
35 //#define VectorSubtract(a,b,c) ((c)[0]=(a)[0]-(b)[0],(c)[1]=(a)[1]-(b)[1],(c)[2]=(a)[2]-(b)[2])
36 //#define VectorAdd(a,b,c) ((c)[0]=(a)[0]+(b)[0],(c)[1]=(a)[1]+(b)[1],(c)[2]=(a)[2]+(b)[2])
37 //#define VectorCopy(a,b) ((b)[0]=(a)[0],(b)[1]=(a)[1],(b)[2]=(a)[2])
38 //#define VectorCopy(a,b) ((b).x=(a).x,(b).y=(a).y,(b).z=(a).z])
40 //#define VectorScale(v, s, o) ((o)[0]=(v)[0]*(s),(o)[1]=(v)[1]*(s),(o)[2]=(v)[2]*(s))
41 #define __VectorMA( v, s, b, o ) ( ( o )[0] = ( v )[0] + ( b )[0] * ( s ),( o )[1] = ( v )[1] + ( b )[1] * ( s ),( o )[2] = ( v )[2] + ( b )[2] * ( s ) )
42 //#define CrossProduct(a,b,c) ((c)[0]=(a)[1]*(b)[2]-(a)[2]*(b)[1],(c)[1]=(a)[2]*(b)[0]-(a)[0]*(b)[2],(c)[2]=(a)[0]*(b)[1]-(a)[1]*(b)[0])
44 #define DotProduct4( x,y ) ( ( x )[0] * ( y )[0] + ( x )[1] * ( y )[1] + ( x )[2] * ( y )[2] + ( x )[3] * ( y )[3] )
45 #define VectorSubtract4( a,b,c ) ( ( c )[0] = ( a )[0] - ( b )[0],( c )[1] = ( a )[1] - ( b )[1],( c )[2] = ( a )[2] - ( b )[2],( c )[3] = ( a )[3] - ( b )[3] )
46 #define VectorAdd4( a,b,c ) ( ( c )[0] = ( a )[0] + ( b )[0],( c )[1] = ( a )[1] + ( b )[1],( c )[2] = ( a )[2] + ( b )[2],( c )[3] = ( a )[3] + ( b )[3] )
47 #define VectorCopy4( a,b ) ( ( b )[0] = ( a )[0],( b )[1] = ( a )[1],( b )[2] = ( a )[2],( b )[3] = ( a )[3] )
48 #define VectorScale4( v, s, o ) ( ( o )[0] = ( v )[0] * ( s ),( o )[1] = ( v )[1] * ( s ),( o )[2] = ( v )[2] * ( s ),( o )[3] = ( v )[3] * ( s ) )
49 #define VectorMA4( v, s, b, o ) ( ( o )[0] = ( v )[0] + ( b )[0] * ( s ),( o )[1] = ( v )[1] + ( b )[1] * ( s ),( o )[2] = ( v )[2] + ( b )[2] * ( s ),( o )[3] = ( v )[3] + ( b )[3] * ( s ) )
52 //#define VectorClear(a) ((a)[0]=(a)[1]=(a)[2]=0)
53 #define VectorNegate( a,b ) ( ( b )[0] = -( a )[0],( b )[1] = -( a )[1],( b )[2] = -( a )[2] )
54 //#define VectorSet(v, x, y, z) ((v)[0]=(x), (v)[1]=(y), (v)[2]=(z))
55 #define Vector4Copy( a,b ) ( ( b )[0] = ( a )[0],( b )[1] = ( a )[1],( b )[2] = ( a )[2],( b )[3] = ( a )[3] )
57 #define SnapVector( v ) {v[0] = (int)v[0]; v[1] = (int)v[1]; v[2] = (int)v[2]; }
60 //#include "util_heap.h"
63 #define EQUAL_EPSILON 0.001
66 float Q_fabs( float f );
69 #define ID_INLINE GDEF_ATTRIBUTE_INLINE
72 // if this is defined, vec3 will take four elements, which may allow
73 // easier SIMD optimizations
81 // Vanilla PPC code, but since PPC has a reciprocal square root estimate instruction,
82 // runs *much* faster than calling sqrt(). We'll use two Newton-Raphson
83 // refinement steps to get bunch more precision in the 1/sqrt() value for very little cost.
84 // We'll then multiply 1/sqrt times the original value to get the sqrt.
85 // This is about 12.4 times faster than sqrt() and according to my testing (not exhaustive)
86 // it returns fairly accurate results (error below 1.0e-5 up to 100000.0 in 0.1 increments).
88 static inline float idSqrt( float x ) {
89 const float half = 0.5;
90 const float one = 1.0;
93 // This'll NaN if it hits frsqrte. Handle both +0.0 and -0.0
94 if ( fabs( x ) == 0.0 ) {
100 asm ( "frsqrte %0,%1" : "=f" ( y0 ) : "f" ( B ) );
104 /* First refinement step */
106 y1 = y0 + half * y0 * ( one - B * y0 * y0 );
108 /* Second refinement step -- copy the output of the last step to the input of this step */
111 y1 = y0 + half * y0 * ( one - B * y0 * y0 );
113 /* Get sqrt(x) from x * 1/sqrt(x) */
117 static inline double idSqrt( double x ) {
123 //class idVec3 : public idHeap<idVec3> {
135 idVec3() {dist = 0.0f; };
137 idVec3( const float x, const float y, const float z );
141 float operator[]( const int index ) const;
142 float &operator[]( const int index );
144 void set( const float x, const float y, const float z );
146 idVec3 operator-() const;
148 idVec3 &operator=( const idVec3 &a );
150 float operator*( const idVec3 &a ) const;
151 idVec3 operator*( const float a ) const;
152 friend idVec3 operator*( float a, idVec3 b );
154 idVec3 operator+( const idVec3 &a ) const;
155 idVec3 operator-( const idVec3 &a ) const;
157 idVec3 &operator+=( const idVec3 &a );
158 idVec3 &operator-=( const idVec3 &a );
159 idVec3 &operator*=( const float a );
161 int operator==( const idVec3 &a ) const;
162 int operator!=( const idVec3 &a ) const;
164 idVec3 Cross( const idVec3 &a ) const;
165 idVec3 &Cross( const idVec3 &a, const idVec3 &b );
167 float Length( void ) const;
168 float Normalize( void );
172 void SnapTowards( const idVec3 &to );
175 float toPitch( void );
176 angles_t toAngles( void );
177 friend idVec3 LerpVector( const idVec3 &w1, const idVec3 &w2, const float t );
179 char *string( void );
182 extern idVec3 vec_zero;
184 ID_INLINE idVec3::idVec3( const float x, const float y, const float z ) {
193 ID_INLINE float idVec3::operator[]( const int index ) const {
194 return ( &x )[ index ];
197 ID_INLINE float &idVec3::operator[]( const int index ) {
198 return ( &x )[ index ];
201 ID_INLINE idVec3::operator float *( void ) {
205 ID_INLINE idVec3 idVec3::operator-() const {
206 return idVec3( -x, -y, -z );
209 ID_INLINE idVec3 &idVec3::operator=( const idVec3 &a ) {
217 ID_INLINE void idVec3::set( const float x, const float y, const float z ) {
223 ID_INLINE idVec3 idVec3::operator-( const idVec3 &a ) const {
224 return idVec3( x - a.x, y - a.y, z - a.z );
227 ID_INLINE float idVec3::operator*( const idVec3 &a ) const {
228 return x * a.x + y * a.y + z * a.z;
231 ID_INLINE idVec3 idVec3::operator*( const float a ) const {
232 return idVec3( x * a, y * a, z * a );
235 ID_INLINE idVec3 operator*( const float a, const idVec3 b ) {
236 return idVec3( b.x * a, b.y * a, b.z * a );
239 ID_INLINE idVec3 idVec3::operator+( const idVec3 &a ) const {
240 return idVec3( x + a.x, y + a.y, z + a.z );
243 ID_INLINE idVec3 &idVec3::operator+=( const idVec3 &a ) {
251 ID_INLINE idVec3 &idVec3::operator-=( const idVec3 &a ) {
259 ID_INLINE idVec3 &idVec3::operator*=( const float a ) {
267 ID_INLINE int idVec3::operator==( const idVec3 &a ) const {
268 if ( Q_fabs( x - a.x ) > EQUAL_EPSILON ) {
272 if ( Q_fabs( y - a.y ) > EQUAL_EPSILON ) {
276 if ( Q_fabs( z - a.z ) > EQUAL_EPSILON ) {
283 ID_INLINE int idVec3::operator!=( const idVec3 &a ) const {
284 if ( Q_fabs( x - a.x ) > EQUAL_EPSILON ) {
288 if ( Q_fabs( y - a.y ) > EQUAL_EPSILON ) {
292 if ( Q_fabs( z - a.z ) > EQUAL_EPSILON ) {
299 ID_INLINE idVec3 idVec3::Cross( const idVec3 &a ) const {
300 return idVec3( y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x );
303 ID_INLINE idVec3 &idVec3::Cross( const idVec3 &a, const idVec3 &b ) {
304 x = a.y * b.z - a.z * b.y;
305 y = a.z * b.x - a.x * b.z;
306 z = a.x * b.y - a.y * b.x;
311 ID_INLINE float idVec3::Length( void ) const {
314 length = x * x + y * y + z * z;
315 return ( float )idSqrt( length );
318 ID_INLINE float idVec3::Normalize( void ) {
322 length = this->Length();
324 ilength = 1.0f / length;
333 ID_INLINE void idVec3::Zero( void ) {
339 ID_INLINE void idVec3::Snap( void ) {
340 x = float( int( x ) );
341 y = float( int( y ) );
342 z = float( int( z ) );
346 ======================
349 Round a vector to integers for more efficient network
350 transmission, but make sure that it rounds towards a given point
351 rather than blindly truncating. This prevents it from truncating
353 ======================
355 ID_INLINE void idVec3::SnapTowards( const idVec3 &to ) {
357 x = float( int( x ) );
360 x = float( int( x ) + 1 );
364 y = float( int( y ) );
367 y = float( int( y ) + 1 );
371 z = float( int( z ) );
374 z = float( int( z ) + 1 );
378 //===============================================================
385 Bounds( const idVec3 &mins, const idVec3 &maxs );
389 float Radius(); // radius from origin, not from center
391 void AddPoint( const idVec3 &v );
392 void AddBounds( const Bounds &bb );
394 bool ContainsPoint( const idVec3 &p );
395 bool IntersectsBounds( const Bounds &b2 ); // touching is NOT intersecting
398 extern Bounds boundsZero;
400 ID_INLINE Bounds::Bounds(){
403 ID_INLINE bool Bounds::IsCleared() {
404 return b[0][0] > b[1][0];
407 ID_INLINE bool Bounds::ContainsPoint( const idVec3 &p ) {
408 if ( p[0] < b[0][0] || p[1] < b[0][1] || p[2] < b[0][2]
409 || p[0] > b[1][0] || p[1] > b[1][1] || p[2] > b[1][2] ) {
415 ID_INLINE bool Bounds::IntersectsBounds( const Bounds &b2 ) {
416 if ( b2.b[1][0] < b[0][0] || b2.b[1][1] < b[0][1] || b2.b[1][2] < b[0][2]
417 || b2.b[0][0] > b[1][0] || b2.b[0][1] > b[1][1] || b2.b[0][2] > b[1][2] ) {
423 ID_INLINE Bounds::Bounds( const idVec3 &mins, const idVec3 &maxs ) {
428 ID_INLINE idVec3 Bounds::Center() {
429 return idVec3( ( b[1][0] + b[0][0] ) * 0.5f, ( b[1][1] + b[0][1] ) * 0.5f, ( b[1][2] + b[0][2] ) * 0.5f );
432 ID_INLINE void Bounds::Clear() {
433 b[0][0] = b[0][1] = b[0][2] = 99999;
434 b[1][0] = b[1][1] = b[1][2] = -99999;
437 ID_INLINE void Bounds::Zero() {
438 b[0][0] = b[0][1] = b[0][2] =
439 b[1][0] = b[1][1] = b[1][2] = 0;
442 ID_INLINE void Bounds::AddPoint( const idVec3 &v ) {
443 if ( v[0] < b[0][0] ) {
446 if ( v[0] > b[1][0] ) {
449 if ( v[1] < b[0][1] ) {
452 if ( v[1] > b[1][1] ) {
455 if ( v[2] < b[0][2] ) {
458 if ( v[2] > b[1][2] ) {
464 ID_INLINE void Bounds::AddBounds( const Bounds &bb ) {
465 if ( bb.b[0][0] < b[0][0] ) {
466 b[0][0] = bb.b[0][0];
468 if ( bb.b[0][1] < b[0][1] ) {
469 b[0][1] = bb.b[0][1];
471 if ( bb.b[0][2] < b[0][2] ) {
472 b[0][2] = bb.b[0][2];
475 if ( bb.b[1][0] > b[1][0] ) {
476 b[1][0] = bb.b[1][0];
478 if ( bb.b[1][1] > b[1][1] ) {
479 b[1][1] = bb.b[1][1];
481 if ( bb.b[1][2] > b[1][2] ) {
482 b[1][2] = bb.b[1][2];
486 ID_INLINE float Bounds::Radius() {
492 for ( i = 0 ; i < 3 ; i++ ) {
493 a = (float)fabs( b[0][i] );
494 aa = (float)fabs( b[1][i] );
501 return (float)idSqrt( total );
504 //===============================================================
513 float operator[]( int index ) const;
514 float &operator[]( int index );
517 ID_INLINE float idVec2::operator[]( int index ) const {
518 return ( &x )[ index ];
521 ID_INLINE float& idVec2::operator[]( int index ) {
522 return ( &x )[ index ];
525 ID_INLINE idVec2::operator float *( void ) {
529 class idVec4 : public idVec3 {
537 idVec4( float x, float y, float z, float dist );
538 float operator[]( int index ) const;
539 float &operator[]( int index );
542 ID_INLINE idVec4::idVec4() {}
543 ID_INLINE idVec4::idVec4( float x, float y, float z, float dist ) {
550 ID_INLINE float idVec4::operator[]( int index ) const {
551 return ( &x )[ index ];
554 ID_INLINE float& idVec4::operator[]( int index ) {
555 return ( &x )[ index ];
559 class idVec5_t : public idVec3 {
563 float operator[]( int index ) const;
564 float &operator[]( int index );
568 ID_INLINE float idVec5_t::operator[]( int index ) const {
569 return ( &x )[ index ];
572 ID_INLINE float& idVec5_t::operator[]( int index ) {
573 return ( &x )[ index ];
576 #endif /* !__MATH_VECTOR_H__ */