Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xvector.h
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn & Chris Fry
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #ifdef __cplusplus
23 class XVector;
24 
25 inline XVector operator*( const XVector &lval, const QFLOAT obj );
26 
27 inline XVector operator*( const QFLOAT obj, const XVector &rval );
28 
29 inline XVector operator+=( XVector &lval, const XVector &obj );
30 inline XVector operator-=( XVector &lval, const XVector &obj );
31 
32 inline QFLOAT DotProduct( const XVector &a, const XVector &b );
33 inline void Normalize( XVector &r );
34 class YVector;
35 class XVector
36 {
37 public:
38  union
39  {
40  QFLOAT i;
41  QFLOAT x;
42  };
43  union
44  {
45  QFLOAT j;
46  QFLOAT y;
47  };
48  union
49  {
50  QFLOAT k;
51  QFLOAT z;
52  };
53 
54  XVector() {}
55 private:
56  friend class Quadsquare;
57  friend class QuadTree;
58  friend class CoordinateSelect;
59  friend class AIScript;
60  //friend class PlanetaryTransform;
61  friend class SphericalTransform;
62  inline YVector operator=( const YVector& );
63 public:
64  inline XVector( const YVector& );
65  inline YVector Cast() const;
66  inline XVector( QFLOAT i, QFLOAT j, QFLOAT k )
67  {
68  this->i = i;
69  this->j = j;
70  this->k = k;
71  }
72  inline void Set( QFLOAT x, QFLOAT y, QFLOAT z )
73  {
74  i = x;
75  j = y;
76  k = z;
77  }
78  inline void netswap()
79  {
80  this->i = NetSwap( this->i );
81  this->j = NetSwap( this->j );
82  this->k = NetSwap( this->k );
83  }
84  void Yaw( QFLOAT rad );
85  void Roll( QFLOAT rad );
86  void Pitch( QFLOAT rad );
87  inline XVector Scale( QFLOAT s ) const
88  {
89  return XVector( s*i, s*j, s*k );
90  }
91  inline XVector Transform( const XVector &p, const XVector &q, const XVector &r )
92  {
93  XVector tvect = XVector( DotProduct( *this, p ), DotProduct( *this, q ), DotProduct( *this, r ) );
94  *this = tvect;
95  return *this;
96  }
97  inline XVector operator+( const XVector &obj ) const
98  {
99  return XVector( i+obj.i, j+obj.j, k+obj.k );
100  }
101  inline XVector operator-( const XVector &obj ) const
102  {
103  return XVector( i-obj.i, j-obj.j, k-obj.k );
104  }
105  inline XVector Normalize()
106  {
107  ::Normalize( *this );
108  return *this;
109  }
110  inline XVector operator-() const
111  {
112  return XVector( -i, -j, -k );
113  }
114  inline bool operator==( const XVector &b ) const
115  {
116  return i == b.i && j == b.j && k == b.k;
117  }
118  inline XVector Cross( const XVector &v ) const
119  {
120  return XVector( this->j*v.k-this->k*v.j,
121  this->k*v.i-this->i*v.k,
122  this->i*v.j-this->j*v.i );
123  }
124  inline QFLOAT operator*( const XVector &b ) const
125  {
126  return i*b.i+j*b.j+k*b.k;
127  }
128  inline QFLOAT Dot( const XVector &b ) const
129  {
130  return DotProduct( *this, b );
131  }
132  inline QFLOAT Magnitude() const
133  {
134  return XSQRT( i*i+j*j+k*k );
135  }
136  inline QFLOAT MagnitudeSquared() const
137  {
138  return i*i+j*j+k*k;
139  }
140  inline XVector Vabs() const
141  {
142  return XVector( i >= 0 ? i : -i,
143  j >= 0 ? j : -j,
144  k >= 0 ? k : -k );
145  }
146  inline const XVector Transform( const class Matrix &m1 ) const;
147 
148  inline XVector Min( const XVector &other ) const
149  {
150  return XVector( (i < other.i) ? i : other.i,
151  (j < other.j) ? j : other.j,
152  (k < other.k) ? k : other.k );
153  }
154  inline XVector Max( const XVector &other ) const
155  {
156  return XVector( (i > other.i) ? i : other.i,
157  (j > other.j) ? j : other.j,
158  (k > other.k) ? k : other.k );
159  }
160  XVector( struct _object* );
161 };
162 
163 inline XVector operator/( const XVector &lval, const QFLOAT obj )
164 {
165  XVector retval( lval.i/obj, lval.j/obj, lval.k/obj );
166  return retval;
167 }
168 
169 inline XVector operator+=( XVector &lval, const XVector &obj )
170 {
171  lval.i += obj.i;
172  lval.j += obj.j;
173  lval.k += obj.k;
174  return lval;
175 }
176 inline XVector operator-=( XVector &lval, const XVector &obj )
177 {
178  lval.i -= obj.i;
179  lval.j -= obj.j;
180  lval.k -= obj.k;
181  return lval;
182 }
183 
184 inline XVector operator*=( XVector &lval, const QFLOAT &obj )
185 {
186  lval.i *= obj;
187  lval.j *= obj, lval.k *= obj;
188  return lval;
189 }
190 
191 inline void Normalize( XVector &r )
192 {
193  QFLOAT size = r.i*r.i+r.j*r.j+r.k*r.k;
194  if (size > 0.00000000001) {
195  QFLOAT isize = QFLOAT(1.0) / XSQRT(size);
196  r.i *= isize;
197  r.j *= isize;
198  r.k *= isize;
199  }
200 }
201 
202 inline QFLOAT DotProduct( const XVector &a, const XVector &b )
203 {
204  return a.i*b.i+a.j*b.j+a.k*b.k;
205 }
206 
207 inline XVector operator*( const XVector &lval, const double obj )
208 {
209  XVector retval( lval.i*obj, lval.j*obj, lval.k*obj );
210  return retval;
211 }
212 inline XVector operator*( const XVector &lval, const float obj )
213 {
214  XVector retval( lval.i*obj, lval.j*obj, lval.k*obj );
215  return retval;
216 }
217 
218 inline XVector operator*( const double obj, const XVector &rval )
219 {
220  return XVector( rval.i*obj, rval.j*obj, rval.k*obj );
221 }
222 
223 inline XVector operator*( const float obj, const XVector &rval )
224 {
225  return XVector( rval.i*obj, rval.j*obj, rval.k*obj );
226 }
227 inline XVector operator*( const XVector &lval, const int obj )
228 {
229  XVector retval( lval.i*obj, lval.j*obj, lval.k*obj );
230  return retval;
231 }
232 
233 inline XVector operator*( const int obj, const XVector &rval )
234 {
235  return XVector( rval.i*obj, rval.j*obj, rval.k*obj );
236 }
237 
238 inline bool IsShorterThan(const XVector& a, QFLOAT delta)
239 {
240  return (a.MagnitudeSquared() < (delta * delta));
241 }
242 
243 inline void ScaledCrossProduct( const XVector &a, const XVector &b, XVector &r )
244 {
245  r.i = a.j*b.k-a.k*b.j;
246  r.j = a.k*b.i-a.i*b.k;
247  r.k = a.i*b.j-a.j*b.i;
248  QFLOAT size = XSQRT( r.i*r.i+r.j*r.j+r.k*r.k );
249  if (size < 0.00001) {
250  r.i = r.j = r.k = 0;
251  } else {
252  r.i /= size;
253  r.j /= size;
254  r.k /= size;
255  }
256 }
257 
258 inline XVector PolygonNormal( XVector v1, XVector v2, XVector v3 )
259 {
260  XVector temp;
261  ScaledCrossProduct( v2-v1, v3-v1, temp );
262  return temp;
263 }
264 
265 inline XVector Transform( const XVector &p, const XVector &q, const XVector &r, const XVector &v )
266 {
267  return XVector( p.i*v.i+q.i*v.j+r.i*v.k,
268  p.j*v.i+q.j*v.j+r.j*v.k,
269  p.k*v.i+q.k*v.j+r.k*v.k );
270 }
271 inline XVector CrossProduct( const XVector &v1, const XVector &v2 )
272 {
273  XVector result;
274  result.i = v1.j*v2.k-v1.k*v2.j;
275  result.j = v1.k*v2.i-v1.i*v2.k;
276  result.k = v1.i*v2.j-v1.j*v2.i;
277  return result;
278 }
279 
280 inline void CrossProduct( const XVector &a, const XVector &b, XVector &RES )
281 {
282  RES = a.Cross( b );
283 }
284 
285 void Yaw( QFLOAT rad, XVector &p, XVector &q, XVector &r );
286 void Pitch( QFLOAT rad, XVector &p, XVector &q, XVector &r );
287 void Roll( QFLOAT rad, XVector &p, XVector &q, XVector &r );
288 void ResetVectors( XVector &p, XVector &q, XVector &r );
289 void MakeRVector( XVector &p, XVector &q, XVector &r );
290 void Orthogonize( XVector &p, XVector &q, XVector &r );
292 #endif
293