# source:golgotha/src/i4/math/vector.hhTweet

Last change on this file was 80, checked in by Sam Hocevar, 11 years ago
• Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 6.7 KB
Line
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9#ifndef __VECTOR_HPP_
10#define __VECTOR_HPP_
11
12#include "math/num_type.hh"
13#include <math.h>
14
15#ifdef _WINDOWS
16//#define G1_WIN32_VECTOR_ASM
17#endif
18
19
20template <class Coord>
21class i4_vector3_template
22{
23public:
24  Coord x,y,z;
25
26  Coord& operator()(int pos) const { return ((Coord*)&x)[pos]; }
27  Coord& operator[](int pos) const { return ((Coord*)&x)[pos]; }
28
29  i4_vector3_template() {}
30  i4_vector3_template(const i4_vector3_template& p) : x(p.x), y(p.y), z(p.z) {}
31  i4_vector3_template(Coord _x,Coord _y,Coord _z) : x(_x),y(_y),z(_z) {}
32  i4_vector3_template& set(Coord px,Coord py,Coord pz)
33  {
34    x = px;
35    y = py;
36    z = pz;
37
38    return *this;
39  }
40  i4_vector3_template& operator=(const i4_vector3_template& p)
41  {
42    x = p.x;
43    y = p.y;
44    z = p.z;
45
46    return *this;
47  }
48
49  i4_vector3_template& operator+=(const i4_vector3_template& b)
50  {
51    x += b.x;
52    y += b.y;
53    z += b.z;
54
55    return *this;
56  }
57  i4_vector3_template& operator-=(const i4_vector3_template& b)
58  {
59    x -= b.x;
60    y -= b.y;
61    z -= b.z;
62
63    return *this;
64  }
65
66  i4_vector3_template& operator*=(Coord b)
67  {
68    x *= b;
69    y *= b;
70    z *= b;
71
72    return *this;
73  }
74
75  i4_vector3_template operator*(Coord b)
76  {
77    i4_vector3_template tmp;
78    tmp.x = x * b;
79    tmp.y = y * b;
80    tmp.z = z * b;
81
82    return tmp;
83  }
84
85
86  i4_vector3_template operator+(Coord b)
87  {
88    i4_vector3_template tmp;
89    tmp.x = x + b;
90    tmp.y = y + b;
91    tmp.z = z + b;
92
93    return tmp;
94  }
95
96  i4_vector3_template operator+(i4_vector3_template v)
97  {
98    i4_vector3_template tmp;
99    tmp.x = x + v.x;
100    tmp.y = y + v.y;
101    tmp.z = z + v.z;
102
103    return tmp;
104  }
105
106  i4_vector3_template operator*(i4_vector3_template v)
107  {
108    i4_vector3_template tmp;
109    tmp.x = x * v.x;
110    tmp.y = y * v.y;
111    tmp.z = z * v.z;
112
113    return tmp;
114  }
115
116  i4_vector3_template& operator/=(Coord b)
117  {
118    x /= b;
119    y /= b;
120    z /= b;
121
122    return *this;
123  }
124
125  i4_vector3_template& interpolate(const i4_vector3_template& from, const i4_vector3_template& to,
126                                   i4_float ratio)
127  {
128    x = i4_interpolate(from.x,to.x,ratio);
129    y = i4_interpolate(from.y,to.y,ratio);
130    z = i4_interpolate(from.z,to.z,ratio);
131    return *this;
132  }
133
134  Coord length() const
135  {
136    return (Coord)sqrt(dot(*this));
137  }
138  void normalize()
139  {
140    *this /= length();
141  }
142
143  i4_vector3_template& reverse()
144  {
145    x=-x; y=-y; z=-z;
146
147    return *this;
148  }
149
150  Coord dot(const i4_vector3_template& b) const
151  {
152#ifdef G1_WIN32_VECTOR_ASM
153    i4_float dotret;
154    _asm
155    {
156      mov     ecx, b
157      mov     eax, this
158
159      ;optimized dot product; 15 cycles
160      fld dword ptr   [eax+0]     ;starts & ends on cycle 0
161      fmul dword ptr  [ecx+0]     ;starts on cycle 1
162      fld dword ptr   [eax+4]     ;starts & ends on cycle 2
163      fmul dword ptr  [ecx+4]     ;starts on cycle 3
164      fld dword ptr   [eax+8]     ;starts & ends on cycle 4
165      fmul dword ptr  [ecx+8]     ;starts on cycle 5
166      fxch            st(1)       ;no cost
167      faddp           st(2),st(0) ;starts on cycle 6, stalls for cycles 7-8
168      faddp           st(1),st(0) ;starts on cycle 9, stalls for cycles 10-12
169      fstp dword ptr  [dotret]    ;starts on cycle 13, ends on cycle 14
170    }
171    return dotret;
172#else
173    return x*b.x + y*b.y + z*b.z;
174#endif
175  }
176
177    i4_vector3_template& cross(const i4_vector3_template& a,
178                                    const i4_vector3_template& b)
179  {
180#ifdef UNFINISHED_WINDOWS
181    _asm
182    {
183      mov     eax,this
184      mov     ecx,a
185      mov     edx,b
186
187      ;optimized cross product; 22 cycles
188      fld dword ptr   [ecx+4]     ;starts & ends on cycle 0
189      fmul dword ptr  [edx+8]     ;starts on cycle 1
190      fld dword ptr   [ecx+8]     ;starts & ends on cycle 2
191      fmul dword ptr  [edx+0]     ;starts on cycle 3
192      fld dword ptr   [ecx+0]     ;starts & ends on cycle 4
193      fmul dword ptr  [edx+4]     ;starts on cycle 5
194      fld dword ptr   [ecx+8]     ;starts & ends on cycle 6
195      fmul dword ptr  [edx+4]     ;starts on cycle 7
196      fld dword ptr   [ecx+0]     ;starts & ends on cycle 8
197      fmul dword ptr  [edx+8]     ;starts on cycle 9
198      fld dword ptr   [ecx+4]     ;starts & ends on cycle 10
199      fmul dword ptr  [edx+0]     ;starts on cycle 11
200      fxch            st(2)       ;no cost
201      fsubrp          st(5),st(0) ;starts on cycle 12
202      fsubrp          st(3),st(0) ;starts on cycle 13
203      fsubrp          st(1),st(0) ;starts on cycle 14
204      fxch            st(2)       ;no cost, stalls for cycle 15
205      fstp dword ptr  [eax+0]     ;starts on cycle 16, ends on cycle 17
206      fstp dword ptr  [eax+4]     ;starts on cycle 18, ends on cycle 19
207      fstp dword ptr  [eax+8]     ;starts on cycle 20, ends on cycle 21
208    }
209#else
210    x = a.y*b.z - a.z*b.y;
211    y = a.z*b.x - a.x*b.z;
212    z = a.x*b.y - a.y*b.x;
213#endif
214    return *this;
215  }
216};
217
218
219class i4_2d_vector
220{
221public:
222  i4_float x,y;
223
224  i4_2d_vector() { ; }
225
226  i4_2d_vector(i4_float x, i4_float y) : x(x), y(y) {}
227
228  i4_float length() const { return sqrt(x*x+y*y); }
229  i4_float dot(const i4_2d_vector &b)  { return x*b.x + y*b.y; }
230  i4_float perp_dot(const i4_2d_vector &b)  { return x*b.y-y*b.x; }
231
232  i4_2d_vector& operator-=(const i4_2d_vector& b) { x-=b.x; y-=b.y; return *this; }
233  i4_2d_vector& operator+=(const i4_2d_vector& b) { x+=b.x; y+=b.y; return *this; }
234  i4_2d_vector& operator*=(const i4_2d_vector& b) { x*=b.x; y*=b.y; return *this; }
235  i4_2d_vector& operator/=(const i4_2d_vector& b) { x/=b.x; y/=b.y; return *this; }
236
237  i4_2d_vector operator+(const i4_2d_vector& b) { return i4_2d_vector(x+b.x, y+b.y); }
238  i4_2d_vector operator-(const i4_2d_vector& b) { return i4_2d_vector(x-b.x, y-b.y); }
239  i4_2d_vector operator*(const i4_2d_vector& b) { return i4_2d_vector(x*b.x, y*b.y); }
240  i4_2d_vector operator/(const i4_2d_vector& b) { return i4_2d_vector(x*b.x, y*b.y); }
241
242  void normalize()
243  {
244    i4_float ool=1.0/length();
245    x*=ool;
246    y*=ool;
247  }
248};
249
250
251typedef i4_vector3_template<i4_float>  i4_3d_vector;
252
253#endif
254
255
Note: See TracBrowser for help on using the repository browser.