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
vec.cpp
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 /* © 1998 Chris Fry & Daniel Horn*/
23 #include "vegastrike.h"
24 #include <math.h>
25 #include "vec.h"
26 #include <boost/version.hpp>
27 #if BOOST_VERSION != 102800
28 #if defined (_MSC_VER) && _MSC_VER <= 1200
29 #define Vector Vactor
30 #endif
31 #include "cs_boostpythonclass.h"
32 #include <boost/python/to_python_converter.hpp>
33 #include <boost/python/to_python_indirect.hpp>
34 #include <boost/python/to_python_value.hpp>
35 #include <boost/python/converter/builtin_converters.hpp>
36 #include "cs_boostpython.h"
37 #if defined (_MSC_VER) && _MSC_VER <= 1200
38 #undef Vector
39 #endif
40 #else
41 #include <boost/python/detail/extension_class.hpp>
42 #endif
43 //#include "glob_externs.h"
44 #define _CZ (761.465325527)
45 //extern Vector _LightVector;
46 //extern float _LVRed;
47 //extern float _LVGreen;
48 //extern float _LVBlue;
49 //extern Vector _CamCoord;
50 //extern Vector _CamP;
51 //extern Vector _CamQ;
52 //extern Vector _CamR;
53 
54 //extern float _CamTransConst;
55 
56 Vector::Vector( PyObject *p )
57 {
58  Vector vec( 0, 0, 0 );
59  static char fff[4] = "fff";
60  PyArg_ParseTuple( p, fff, &vec.i, &vec.j, &vec.k );
61  *this = vec;
62 }
63 
64 QVector::QVector( PyObject *p )
65 {
66  QVector vec( 0, 0, 0 );
67  static char ddd[4] = "ddd";
68  PyArg_ParseTuple( p, ddd, &vec.i, &vec.j, &vec.k );
69  *this = vec;
70 }
71 
73 //Yaws a unit vector
75 
76 void Vector::Yaw( float rad ) //only works with unit vector
77 {
78  float theta;
79  theta = 0.0f; //FIXME This line added temporarily by chuck_starchaser
80  if (i > 0)
81  theta = (float) atan( k/i );
82  else if (i < 0)
83  theta = PI+(float) atan( k/i );
84  else if (k <= 0 && i == 0)
85  theta = -PI/2;
86  else if (k > 0 && i == 0)
87  theta = PI/2;
88  theta += rad; //FIXME If none of the if's is true, theta is uninitialized!
89  i = cosf( theta );
90  k = sinf( theta );
91 }
92 
93 void Vector::Roll( float rad )
94 {
95  float theta;
96  theta = 0.0f; //FIXME This line added temporarily by chuck_starchaser
97  if (i > 0)
98  theta = (float) atan( j/i );
99  else if (i < 0)
100  theta = PI+(float) atan( j/i );
101  else if (j <= 0 && i == 0)
102  theta = -PI/2;
103  else if (j > 0 && i == 0)
104  theta = PI/2;
105  theta += rad; //FIXME If none of the if's is true, theta is uninitialized!
106  i = cosf( theta );
107  j = sinf( theta );
108 }
109 
110 void Vector::Pitch( float rad )
111 {
112  float theta;
113  theta = 0.0f; //FIXME This line added temporarily by chuck_starchaser
114  if (k > 0)
115  theta = (float) atan( j/k );
116  else if (k < 0)
117  theta = PI+(float) atan( j/k );
118  else if (j <= 0 && k == 0)
119  theta = -PI/2;
120  else if (j > 0 && k == 0)
121  theta = PI/2;
122  theta += rad; //FIXME If none of the if's is true, theta is uninitialized!
123  k = cosf( theta );
124  j = sinf( theta );
125 }
126 
127 void Yaw( float rad, Vector &p, Vector &q, Vector &r )
128 {
129  Vector temp1, temp2, temp3;
130  temp1.i = 1;
131  temp1.j = 0;
132  temp1.k = 0;
133  temp1.Yaw( rad );
134  temp2.i = temp1.i*p.i+temp1.j*q.i+temp1.k*r.i;
135  temp2.j = temp1.i*p.j+temp1.j*q.j+temp1.k*r.j;
136  temp2.k = temp1.i*p.k+temp1.j*q.k+temp1.k*r.k;
137  temp1.i = 0;
138  temp1.j = 0;
139  temp1.k = 1;
140  temp1.Yaw( rad );
141  temp3.i = temp1.i*p.i+temp1.j*q.i+temp1.k*r.i;
142  temp3.j = temp1.i*p.j+temp1.j*q.j+temp1.k*r.j;
143  temp3.k = temp1.i*p.k+temp1.j*q.k+temp1.k*r.k;
144  p = temp2;
145  r = temp3;
146 }
147 
148 void Pitch( float rad, Vector &p, Vector &q, Vector &r )
149 {
150  Vector temp1, temp2, temp3;
151  temp1.i = 0;
152  temp1.j = 1;
153  temp1.k = 0;
154  temp1.Pitch( rad );
155  temp2.i = temp1.i*p.i+temp1.j*q.i+temp1.k*r.i;
156  temp2.j = temp1.i*p.j+temp1.j*q.j+temp1.k*r.j;
157  temp2.k = temp1.i*p.k+temp1.j*q.k+temp1.k*r.k;
158  temp1.i = 0;
159  temp1.j = 0;
160  temp1.k = 1;
161  temp1.Pitch( rad );
162  temp3.i = temp1.i*p.i+temp1.j*q.i+temp1.k*r.i;
163  temp3.j = temp1.i*p.j+temp1.j*q.j+temp1.k*r.j;
164  temp3.k = temp1.i*p.k+temp1.j*q.k+temp1.k*r.k;
165  q = temp2;
166  r = temp3;
167 }
168 
169 void Roll( float rad, Vector &p, Vector &q, Vector &r )
170 {
171  Vector temp1, temp2, temp3;
172  temp1.i = 1;
173  temp1.j = 0;
174  temp1.k = 0;
175  temp1.Roll( rad );
176  temp2.i = temp1.i*p.i+temp1.j*q.i+temp1.k*r.i;
177  temp2.j = temp1.i*p.j+temp1.j*q.j+temp1.k*r.j;
178  temp2.k = temp1.i*p.k+temp1.j*q.k+temp1.k*r.k;
179  temp1.i = 0;
180  temp1.j = 1;
181  temp1.k = 0;
182  temp1.Roll( rad );
183  temp3.i = temp1.i*p.i+temp1.j*q.i+temp1.k*r.i;
184  temp3.j = temp1.i*p.j+temp1.j*q.j+temp1.k*r.j;
185  temp3.k = temp1.i*p.k+temp1.j*q.k+temp1.k*r.k;
186  p = temp2;
187  q = temp3;
188 }
189 
191 {
192  p.i = q.j = r.k = 1;
193  p.j = p.k = q.i = q.k = r.i = r.j = 0;
194 }
195 
196 void MakeRVector( Vector &p, Vector &q, Vector &r )
197 {
198  ScaledCrossProduct( p, q, r );
199  ScaledCrossProduct( r, p, q );
200  Normalize( p );
201 }
202 
203 //Makes a non-colinear vector q to given r. assumes magnitude of r is nonzero, does not check
205 {
206  Vector q( p );
207  if ( (p.i == p.j) == 0 ) {
208  q.i = p.k;
209  q.j = p.i;
210  q.k = p.j;
211  } else {
212  q.i = -p.j;
213  q.j = p.i;
214  q.k = p.k;
215  }
216  return q;
217 }
218 
219 void Orthogonize( Vector &p, Vector &q, Vector &r )
220 {
221  Normalize( r );
222  ScaledCrossProduct( r, p, q ); //result of scaled cross put into q
223  ScaledCrossProduct( q, r, p ); //result of scaled cross put back into p
224 }
225