2 Copyright (C) 1999-2007 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__
26 #pragma warning(disable : 4244)
32 //#define DotProduct(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
33 //#define VectorSubtract(a,b,c) ((c)[0]=(a)[0]-(b)[0],(c)[1]=(a)[1]-(b)[1],(c)[2]=(a)[2]-(b)[2])
34 //#define VectorAdd(a,b,c) ((c)[0]=(a)[0]+(b)[0],(c)[1]=(a)[1]+(b)[1],(c)[2]=(a)[2]+(b)[2])
35 //#define VectorCopy(a,b) ((b)[0]=(a)[0],(b)[1]=(a)[1],(b)[2]=(a)[2])
36 //#define VectorCopy(a,b) ((b).x=(a).x,(b).y=(a).y,(b).z=(a).z])
38 //#define VectorScale(v, s, o) ((o)[0]=(v)[0]*(s),(o)[1]=(v)[1]*(s),(o)[2]=(v)[2]*(s))
39 #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 ) )
40 //#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])
42 #define DotProduct4( x,y ) ( ( x )[0] * ( y )[0] + ( x )[1] * ( y )[1] + ( x )[2] * ( y )[2] + ( x )[3] * ( y )[3] )
43 #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] )
44 #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] )
45 #define VectorCopy4( a,b ) ( ( b )[0] = ( a )[0],( b )[1] = ( a )[1],( b )[2] = ( a )[2],( b )[3] = ( a )[3] )
46 #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 ) )
47 #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 ) )
50 //#define VectorClear(a) ((a)[0]=(a)[1]=(a)[2]=0)
51 #define VectorNegate( a,b ) ( ( b )[0] = -( a )[0],( b )[1] = -( a )[1],( b )[2] = -( a )[2] )
52 //#define VectorSet(v, x, y, z) ((v)[0]=(x), (v)[1]=(y), (v)[2]=(z))
53 #define Vector4Copy( a,b ) ( ( b )[0] = ( a )[0],( b )[1] = ( a )[1],( b )[2] = ( a )[2],( b )[3] = ( a )[3] )
55 #define SnapVector( v ) {v[0] = (int)v[0]; v[1] = (int)v[1]; v[2] = (int)v[2]; }
58 //#include "util_heap.h"
61 #define EQUAL_EPSILON 0.001
64 float Q_fabs( float f );
68 #define ID_INLINE __inline
70 #define ID_INLINE inline
74 // if this is defined, vec3 will take four elements, which may allow
75 // easier SIMD optimizations
83 // Vanilla PPC code, but since PPC has a reciprocal square root estimate instruction,
84 // runs *much* faster than calling sqrt(). We'll use two Newton-Raphson
85 // refinement steps to get bunch more precision in the 1/sqrt() value for very little cost.
86 // We'll then multiply 1/sqrt times the original value to get the sqrt.
87 // This is about 12.4 times faster than sqrt() and according to my testing (not exhaustive)
88 // it returns fairly accurate results (error below 1.0e-5 up to 100000.0 in 0.1 increments).
90 static inline float idSqrt( float x ) {
91 const float half = 0.5;
92 const float one = 1.0;
95 // This'll NaN if it hits frsqrte. Handle both +0.0 and -0.0
96 if ( fabs( x ) == 0.0 ) {
102 asm ( "frsqrte %0,%1" : "=f" ( y0 ) : "f" ( B ) );
106 /* First refinement step */
108 y1 = y0 + half * y0 * ( one - B * y0 * y0 );
110 /* Second refinement step -- copy the output of the last step to the input of this step */
113 y1 = y0 + half * y0 * ( one - B * y0 * y0 );
115 /* Get sqrt(x) from x * 1/sqrt(x) */
119 static inline double idSqrt( double x ) {
125 //class idVec3 : public idHeap<idVec3> {
137 idVec3() {dist = 0.0f; };
139 idVec3( const float x, const float y, const float z );
143 float operator[]( const int index ) const;
144 float &operator[]( const int index );
146 void set( const float x, const float y, const float z );
148 idVec3 operator-() const;
150 idVec3 &operator=( const idVec3 &a );
152 float operator*( const idVec3 &a ) const;
153 idVec3 operator*( const float a ) const;
154 friend idVec3 operator*( float a, idVec3 b );
156 idVec3 operator+( const idVec3 &a ) const;
157 idVec3 operator-( const idVec3 &a ) const;
159 idVec3 &operator+=( const idVec3 &a );
160 idVec3 &operator-=( const idVec3 &a );
161 idVec3 &operator*=( const float a );
163 int operator==( const idVec3 &a ) const;
164 int operator!=( const idVec3 &a ) const;
166 idVec3 Cross( const idVec3 &a ) const;
167 idVec3 &Cross( const idVec3 &a, const idVec3 &b );
169 float Length( void ) const;
170 float Normalize( void );
174 void SnapTowards( const idVec3 &to );
177 float toPitch( void );
178 angles_t toAngles( void );
179 friend idVec3 LerpVector( const idVec3 &w1, const idVec3 &w2, const float t );
181 char *string( void );
184 extern idVec3 vec_zero;
186 ID_INLINE idVec3::idVec3( const float x, const float y, const float z ) {
195 ID_INLINE float idVec3::operator[]( const int index ) const {
196 return ( &x )[ index ];
199 ID_INLINE float &idVec3::operator[]( const int index ) {
200 return ( &x )[ index ];
203 ID_INLINE idVec3::operator float *( void ) {
207 ID_INLINE idVec3 idVec3::operator-() const {
208 return idVec3( -x, -y, -z );
211 ID_INLINE idVec3 &idVec3::operator=( const idVec3 &a ) {
219 ID_INLINE void idVec3::set( const float x, const float y, const float z ) {
225 ID_INLINE idVec3 idVec3::operator-( const idVec3 &a ) const {
226 return idVec3( x - a.x, y - a.y, z - a.z );
229 ID_INLINE float idVec3::operator*( const idVec3 &a ) const {
230 return x * a.x + y * a.y + z * a.z;
233 ID_INLINE idVec3 idVec3::operator*( const float a ) const {
234 return idVec3( x * a, y * a, z * a );
237 ID_INLINE idVec3 operator*( const float a, const idVec3 b ) {
238 return idVec3( b.x * a, b.y * a, b.z * a );
241 ID_INLINE idVec3 idVec3::operator+( const idVec3 &a ) const {
242 return idVec3( x + a.x, y + a.y, z + a.z );
245 ID_INLINE idVec3 &idVec3::operator+=( const idVec3 &a ) {
253 ID_INLINE idVec3 &idVec3::operator-=( const idVec3 &a ) {
261 ID_INLINE idVec3 &idVec3::operator*=( const float a ) {
269 ID_INLINE int idVec3::operator==( const idVec3 &a ) const {
270 if ( Q_fabs( x - a.x ) > EQUAL_EPSILON ) {
274 if ( Q_fabs( y - a.y ) > EQUAL_EPSILON ) {
278 if ( Q_fabs( z - a.z ) > EQUAL_EPSILON ) {
285 ID_INLINE int idVec3::operator!=( const idVec3 &a ) const {
286 if ( Q_fabs( x - a.x ) > EQUAL_EPSILON ) {
290 if ( Q_fabs( y - a.y ) > EQUAL_EPSILON ) {
294 if ( Q_fabs( z - a.z ) > EQUAL_EPSILON ) {
301 ID_INLINE idVec3 idVec3::Cross( const idVec3 &a ) const {
302 return idVec3( y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x );
305 ID_INLINE idVec3 &idVec3::Cross( const idVec3 &a, const idVec3 &b ) {
306 x = a.y * b.z - a.z * b.y;
307 y = a.z * b.x - a.x * b.z;
308 z = a.x * b.y - a.y * b.x;
313 ID_INLINE float idVec3::Length( void ) const {
316 length = x * x + y * y + z * z;
317 return ( float )idSqrt( length );
320 ID_INLINE float idVec3::Normalize( void ) {
324 length = this->Length();
326 ilength = 1.0f / length;
335 ID_INLINE void idVec3::Zero( void ) {
341 ID_INLINE void idVec3::Snap( void ) {
342 x = float( int( x ) );
343 y = float( int( y ) );
344 z = float( int( z ) );
348 ======================
351 Round a vector to integers for more efficient network
352 transmission, but make sure that it rounds towards a given point
353 rather than blindly truncating. This prevents it from truncating
355 ======================
357 ID_INLINE void idVec3::SnapTowards( const idVec3 &to ) {
359 x = float( int( x ) );
362 x = float( int( x ) + 1 );
366 y = float( int( y ) );
369 y = float( int( y ) + 1 );
373 z = float( int( z ) );
376 z = float( int( z ) + 1 );
380 //===============================================================
387 Bounds( const idVec3 &mins, const idVec3 &maxs );
391 float Radius(); // radius from origin, not from center
393 void AddPoint( const idVec3 &v );
394 void AddBounds( const Bounds &bb );
396 bool ContainsPoint( const idVec3 &p );
397 bool IntersectsBounds( const Bounds &b2 ); // touching is NOT intersecting
400 extern Bounds boundsZero;
402 ID_INLINE Bounds::Bounds(){
405 ID_INLINE bool Bounds::IsCleared() {
406 return b[0][0] > b[1][0];
409 ID_INLINE bool Bounds::ContainsPoint( const idVec3 &p ) {
410 if ( p[0] < b[0][0] || p[1] < b[0][1] || p[2] < b[0][2]
411 || p[0] > b[1][0] || p[1] > b[1][1] || p[2] > b[1][2] ) {
417 ID_INLINE bool Bounds::IntersectsBounds( const Bounds &b2 ) {
418 if ( b2.b[1][0] < b[0][0] || b2.b[1][1] < b[0][1] || b2.b[1][2] < b[0][2]
419 || b2.b[0][0] > b[1][0] || b2.b[0][1] > b[1][1] || b2.b[0][2] > b[1][2] ) {
425 ID_INLINE Bounds::Bounds( const idVec3 &mins, const idVec3 &maxs ) {
430 ID_INLINE idVec3 Bounds::Center() {
431 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 );
434 ID_INLINE void Bounds::Clear() {
435 b[0][0] = b[0][1] = b[0][2] = 99999;
436 b[1][0] = b[1][1] = b[1][2] = -99999;
439 ID_INLINE void Bounds::Zero() {
440 b[0][0] = b[0][1] = b[0][2] =
441 b[1][0] = b[1][1] = b[1][2] = 0;
444 ID_INLINE void Bounds::AddPoint( const idVec3 &v ) {
445 if ( v[0] < b[0][0] ) {
448 if ( v[0] > b[1][0] ) {
451 if ( v[1] < b[0][1] ) {
454 if ( v[1] > b[1][1] ) {
457 if ( v[2] < b[0][2] ) {
460 if ( v[2] > b[1][2] ) {
466 ID_INLINE void Bounds::AddBounds( const Bounds &bb ) {
467 if ( bb.b[0][0] < b[0][0] ) {
468 b[0][0] = bb.b[0][0];
470 if ( bb.b[0][1] < b[0][1] ) {
471 b[0][1] = bb.b[0][1];
473 if ( bb.b[0][2] < b[0][2] ) {
474 b[0][2] = bb.b[0][2];
477 if ( bb.b[1][0] > b[1][0] ) {
478 b[1][0] = bb.b[1][0];
480 if ( bb.b[1][1] > b[1][1] ) {
481 b[1][1] = bb.b[1][1];
483 if ( bb.b[1][2] > b[1][2] ) {
484 b[1][2] = bb.b[1][2];
488 ID_INLINE float Bounds::Radius() {
494 for ( i = 0 ; i < 3 ; i++ ) {
495 a = (float)fabs( b[0][i] );
496 aa = (float)fabs( b[1][i] );
503 return (float)idSqrt( total );
506 //===============================================================
515 float operator[]( int index ) const;
516 float &operator[]( int index );
519 ID_INLINE float idVec2::operator[]( int index ) const {
520 return ( &x )[ index ];
523 ID_INLINE float& idVec2::operator[]( int index ) {
524 return ( &x )[ index ];
527 ID_INLINE idVec2::operator float *( void ) {
531 class idVec4 : public idVec3 {
539 idVec4( float x, float y, float z, float dist );
540 float operator[]( int index ) const;
541 float &operator[]( int index );
544 ID_INLINE idVec4::idVec4() {}
545 ID_INLINE idVec4::idVec4( float x, float y, float z, float dist ) {
552 ID_INLINE float idVec4::operator[]( int index ) const {
553 return ( &x )[ index ];
556 ID_INLINE float& idVec4::operator[]( int index ) {
557 return ( &x )[ index ];
561 class idVec5_t : public idVec3 {
565 float operator[]( int index ) const;
566 float &operator[]( int index );
570 ID_INLINE float idVec5_t::operator[]( int index ) const {
571 return ( &x )[ index ];
574 ID_INLINE float& idVec5_t::operator[]( int index ) {
575 return ( &x )[ index ];
578 #endif /* !__MATH_VECTOR_H__ */