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__
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
101 asm("frsqrte %0,%1" : "=f" (y0) : "f" (B));
105 /* First refinement step */
107 y1 = y0 + half*y0*(one - B*y0*y0);
109 /* Second refinement step -- copy the output of the last step to the input of this step */
112 y1 = y0 + half*y0*(one - B*y0*y0);
114 /* Get sqrt(x) from x * 1/sqrt(x) */
118 static inline double idSqrt(double x) {
124 //class idVec3 : public idHeap<idVec3> {
136 idVec3() {dist = 0.0f;};
138 idVec3( const float x, const float y, const float z );
142 float operator[]( const int index ) const;
143 float &operator[]( const int index );
145 void set( const float x, const float y, const float z );
147 idVec3 operator-() const;
149 idVec3 &operator=( const idVec3 &a );
151 float operator*( const idVec3 &a ) const;
152 idVec3 operator*( const float a ) const;
153 friend idVec3 operator*( float a, idVec3 b );
155 idVec3 operator+( const idVec3 &a ) const;
156 idVec3 operator-( const idVec3 &a ) const;
158 idVec3 &operator+=( const idVec3 &a );
159 idVec3 &operator-=( const idVec3 &a );
160 idVec3 &operator*=( const float a );
162 int operator==( const idVec3 &a ) const;
163 int operator!=( const idVec3 &a ) const;
165 idVec3 Cross( const idVec3 &a ) const;
166 idVec3 &Cross( const idVec3 &a, const idVec3 &b );
168 float Length( void ) const;
169 float Normalize( void );
173 void SnapTowards( const idVec3 &to );
176 float toPitch( void );
177 angles_t toAngles( void );
178 friend idVec3 LerpVector( const idVec3 &w1, const idVec3 &w2, const float t );
180 char *string( void );
183 extern idVec3 vec_zero;
185 ID_INLINE idVec3::idVec3( const float x, const float y, const float z ) {
194 ID_INLINE float idVec3::operator[]( const int index ) const {
195 return ( &x )[ index ];
198 ID_INLINE float &idVec3::operator[]( const int index ) {
199 return ( &x )[ index ];
202 ID_INLINE idVec3::operator float *( void ) {
206 ID_INLINE idVec3 idVec3::operator-() const {
207 return idVec3( -x, -y, -z );
210 ID_INLINE idVec3 &idVec3::operator=( const idVec3 &a ) {
218 ID_INLINE void idVec3::set( const float x, const float y, const float z ) {
224 ID_INLINE idVec3 idVec3::operator-( const idVec3 &a ) const {
225 return idVec3( x - a.x, y - a.y, z - a.z );
228 ID_INLINE float idVec3::operator*( const idVec3 &a ) const {
229 return x * a.x + y * a.y + z * a.z;
232 ID_INLINE idVec3 idVec3::operator*( const float a ) const {
233 return idVec3( x * a, y * a, z * a );
236 ID_INLINE idVec3 operator*( const float a, const idVec3 b ) {
237 return idVec3( b.x * a, b.y * a, b.z * a );
240 ID_INLINE idVec3 idVec3::operator+( const idVec3 &a ) const {
241 return idVec3( x + a.x, y + a.y, z + a.z );
244 ID_INLINE idVec3 &idVec3::operator+=( const idVec3 &a ) {
252 ID_INLINE idVec3 &idVec3::operator-=( const idVec3 &a ) {
260 ID_INLINE idVec3 &idVec3::operator*=( const float a ) {
268 ID_INLINE int idVec3::operator==( const idVec3 &a ) const {
269 if ( Q_fabs( x - a.x ) > EQUAL_EPSILON ) {
273 if ( Q_fabs( y - a.y ) > EQUAL_EPSILON ) {
277 if ( Q_fabs( z - a.z ) > EQUAL_EPSILON ) {
284 ID_INLINE int idVec3::operator!=( const idVec3 &a ) const {
285 if ( Q_fabs( x - a.x ) > EQUAL_EPSILON ) {
289 if ( Q_fabs( y - a.y ) > EQUAL_EPSILON ) {
293 if ( Q_fabs( z - a.z ) > EQUAL_EPSILON ) {
300 ID_INLINE idVec3 idVec3::Cross( const idVec3 &a ) const {
301 return idVec3( y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x );
304 ID_INLINE idVec3 &idVec3::Cross( const idVec3 &a, const idVec3 &b ) {
305 x = a.y * b.z - a.z * b.y;
306 y = a.z * b.x - a.x * b.z;
307 z = a.x * b.y - a.y * b.x;
312 ID_INLINE float idVec3::Length( void ) const {
315 length = x * x + y * y + z * z;
316 return ( float )idSqrt( length );
319 ID_INLINE float idVec3::Normalize( void ) {
323 length = this->Length();
325 ilength = 1.0f / length;
334 ID_INLINE void idVec3::Zero( void ) {
340 ID_INLINE void idVec3::Snap( void ) {
341 x = float( int( x ) );
342 y = float( int( y ) );
343 z = float( int( z ) );
347 ======================
350 Round a vector to integers for more efficient network
351 transmission, but make sure that it rounds towards a given point
352 rather than blindly truncating. This prevents it from truncating
354 ======================
356 ID_INLINE void idVec3::SnapTowards( const idVec3 &to ) {
358 x = float( int( x ) );
360 x = float( int( x ) + 1 );
364 y = float( int( y ) );
366 y = float( int( y ) + 1 );
370 z = float( int( z ) );
372 z = float( int( z ) + 1 );
376 //===============================================================
383 Bounds( const idVec3 &mins, const idVec3 &maxs );
387 float Radius(); // radius from origin, not from center
389 void AddPoint( const idVec3 &v );
390 void AddBounds( const Bounds &bb );
392 bool ContainsPoint( const idVec3 &p );
393 bool IntersectsBounds( const Bounds &b2 ); // touching is NOT intersecting
396 extern Bounds boundsZero;
398 ID_INLINE Bounds::Bounds(){
401 ID_INLINE bool Bounds::IsCleared() {
402 return b[0][0] > b[1][0];
405 ID_INLINE bool Bounds::ContainsPoint( const idVec3 &p ) {
406 if ( p[0] < b[0][0] || p[1] < b[0][1] || p[2] < b[0][2]
407 || p[0] > b[1][0] || p[1] > b[1][1] || p[2] > b[1][2] ) {
413 ID_INLINE bool Bounds::IntersectsBounds( const Bounds &b2 ) {
414 if ( b2.b[1][0] < b[0][0] || b2.b[1][1] < b[0][1] || b2.b[1][2] < b[0][2]
415 || b2.b[0][0] > b[1][0] || b2.b[0][1] > b[1][1] || b2.b[0][2] > b[1][2] ) {
421 ID_INLINE Bounds::Bounds( const idVec3 &mins, const idVec3 &maxs ) {
426 ID_INLINE idVec3 Bounds::Center() {
427 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 );
430 ID_INLINE void Bounds::Clear() {
431 b[0][0] = b[0][1] = b[0][2] = 99999;
432 b[1][0] = b[1][1] = b[1][2] = -99999;
435 ID_INLINE void Bounds::Zero() {
436 b[0][0] = b[0][1] = b[0][2] =
437 b[1][0] = b[1][1] = b[1][2] = 0;
440 ID_INLINE void Bounds::AddPoint( const idVec3 &v ) {
441 if ( v[0] < b[0][0]) {
444 if ( v[0] > b[1][0]) {
447 if ( v[1] < b[0][1] ) {
450 if ( v[1] > b[1][1]) {
453 if ( v[2] < b[0][2] ) {
456 if ( v[2] > b[1][2]) {
462 ID_INLINE void Bounds::AddBounds( const Bounds &bb ) {
463 if ( bb.b[0][0] < b[0][0]) {
464 b[0][0] = bb.b[0][0];
466 if ( bb.b[0][1] < b[0][1]) {
467 b[0][1] = bb.b[0][1];
469 if ( bb.b[0][2] < b[0][2]) {
470 b[0][2] = bb.b[0][2];
473 if ( bb.b[1][0] > b[1][0]) {
474 b[1][0] = bb.b[1][0];
476 if ( bb.b[1][1] > b[1][1]) {
477 b[1][1] = bb.b[1][1];
479 if ( bb.b[1][2] > b[1][2]) {
480 b[1][2] = bb.b[1][2];
484 ID_INLINE float Bounds::Radius( ) {
490 for (i=0 ; i<3 ; i++) {
491 a = (float)fabs( b[0][i] );
492 aa = (float)fabs( b[1][i] );
499 return (float)idSqrt( total );
502 //===============================================================
511 float operator[]( int index ) const;
512 float &operator[]( int index );
515 ID_INLINE float idVec2::operator[]( int index ) const {
516 return ( &x )[ index ];
519 ID_INLINE float& idVec2::operator[]( int index ) {
520 return ( &x )[ index ];
523 ID_INLINE idVec2::operator float *( void ) {
527 class idVec4 : public idVec3 {
535 idVec4( float x, float y, float z, float dist );
536 float operator[]( int index ) const;
537 float &operator[]( int index );
540 ID_INLINE idVec4::idVec4() {}
541 ID_INLINE idVec4::idVec4( float x, float y, float z, float dist ) {
548 ID_INLINE float idVec4::operator[]( int index ) const {
549 return ( &x )[ index ];
552 ID_INLINE float& idVec4::operator[]( int index ) {
553 return ( &x )[ index ];
557 class idVec5_t : public idVec3 {
561 float operator[]( int index ) const;
562 float &operator[]( int index );
566 ID_INLINE float idVec5_t::operator[]( int index ) const {
567 return ( &x )[ index ];
570 ID_INLINE float& idVec5_t::operator[]( int index ) {
571 return ( &x )[ index ];
574 #endif /* !__MATH_VECTOR_H__ */