]> git.mxchange.org Git - flightgear.git/blob - src/Time/mymath.h
Initial revision of:
[flightgear.git] / src / Time / mymath.h
1 /* -*- Mode: C++ -*- *****************************************************
2  * mymath.h
3  * Written by Durk Talsma, around 1995/1996.
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  **************************************************************************/
20
21 /********************************************************************
22  * This file defines a simple Vector and Matrix library. These were 
23  * originally written for my (yet) unpublished planetarium / solar 
24  * system simulator program. The are included here, because I have 
25  * experience the code to calculate angles between vectors. I'm sure 
26  * similar functions exist already somewhere else so I don't mind 
27  * whether this gets eventually replaced by something more suitable
28  * The functions are based on a description in Tyler. A. (1994). C++
29  * Real-time 3D graphics. Sigma press, Wilmslow, England.
30  * 
31  * The original versions were written under windows, hence the occasional 
32  *::MessageBox() statements between conditional compile statements
33  *
34  ********************************************************************/
35
36
37 #ifndef _MY_MATH_H_
38 #define _MY_MATH_H__
39 #include <math.h>
40 #include <fstream>
41 #include <iomanip>
42
43 using namespace std;
44
45 #include <fg_constants.h>
46 extern const  double PiOver180;
47 extern const double Pix4dif3;
48
49
50
51 class Matrix;
52
53 class Vector
54 {
55     private:
56         int nrData;
57         double* data;
58
59         public:
60         Vector();
61         Vector(int size);
62         Vector(int size, double* dta);
63         Vector(double x, double y, double z);
64         Vector(Vector& other);
65         Vector(istream& is);
66         ~Vector();
67
68         void SetVal(double x, double y, double z);
69
70         void build(int);
71         Vector* GetVector();
72         double GetX();
73         double GetY();
74         double GetZ();
75         void AddX(double x);
76         void AddY(double y);
77         void AddZ(double z);
78         void SubtractX(double x);
79         void SubtractY(double y);
80         void SubtractZ(double z);
81         double VecLen();
82         Vector& operator = (Vector&);
83         double& operator[] (int);
84         int getDim();
85         
86         ostream& binSave(ostream& os);
87         
88         Vector operator +=(Vector other);
89         Vector operator -=(Vector other);
90         
91         friend double VecDot(Vector first, Vector second);
92         friend Vector VecCross(Vector first, Vector second);
93         friend Vector operator-(Vector first, Vector second);
94         friend Vector operator+(Vector first, Vector second);
95         friend Vector operator*(Matrix mx, Vector vc);
96         friend Vector operator*(Vector v1, Vector v2);
97         friend Vector operator*(Vector vec, double d);
98         friend Vector operator/(Vector vec, double d);
99         
100         friend ostream& operator << (ostream& os, Vector& vec);
101         friend istream& operator >> (istream& is, Vector& vec);
102 };
103
104 /*-----------------------------------------------------------------------------
105                                 nonmember friend functions
106 ------------------------------------------------------------------------------*/
107
108 double VecDot(Vector first, Vector second);
109 Vector VecCross(Vector first, Vector second);
110 Vector operator-(Vector first, Vector second);
111 Vector operator+(Vector first, Vector second);
112 Vector operator*(Matrix mx, Vector vc);
113 Vector operator*(Vector v1, Vector v2);
114 Vector operator*(Vector vec, double d);
115 Vector operator/(Vector vec, double d);
116
117 ostream& operator << (ostream& os, Vector& vec);
118 istream& operator >> (istream& is, Vector& vec);
119
120
121 /*------------------------------------------------------------------------------
122                         inline member functions
123 ------------------------------------------------------------------------------*/
124
125 inline Vector::Vector()
126 {
127         nrData = 0;
128     data = 0;
129 }
130
131 inline void Vector::build(int size)
132 {
133         nrData = size;
134     data = new double[nrData];
135     #ifdef DEBUG
136     if (!data)
137     {
138                 ::MessageBox(0, "Error Allocating Memory for a new Vector", "Error",
139                                 MB_OK);
140         exit(1);
141     }
142     #endif
143     //for (int i = 0; i < nrData; i++)
144     //  data[i] = 0.00;
145 }
146
147
148 inline Vector::Vector(int size, double* dta)
149 {
150         build(size);
151     memcpy(data, dta, nrData*sizeof(double));
152 }
153
154
155 inline Vector::Vector(Vector& other)
156 {
157         build(other.nrData);
158     memcpy(data,other.data,nrData*sizeof(double));
159 }
160
161 inline Vector::Vector(double x, double y, double z)
162 {
163     build(4);      // one extra for matrix multiplication...
164         data[0] = x;
165         data[1] = y;
166         data[2] = z;
167     data[3] = 0.00;
168
169 }
170
171 inline Vector::Vector(istream& is)
172 {
173         is.read((char*) &nrData, sizeof(int));
174     data = new double[nrData];
175     is.read((char*) data, nrData * sizeof(double));
176 }
177
178 inline Vector::~Vector()
179 {
180         delete [] data;
181 }
182
183
184 inline void Vector::SetVal(double x, double y, double z)
185 {
186         #ifdef DEBUG
187     if (nrData != 4)
188     {
189         ::MessageBox(0, "Attempt to assign data to a vector\n"
190                         "With size unequal to 4 in function\n"
191                         " Vector::Setval(double, double, double", "Error" , MB_OK);
192         exit(1);
193     }
194     #endif
195     data[0] = x,
196     data[1] = y;
197     data[2] = z;
198     data[3] = 0.00;
199 }
200
201 inline Vector* Vector::GetVector()
202 {
203         return this;
204 }
205
206 inline double Vector::GetX()
207 {
208         #ifdef DEBUG
209     if (nrData < 1)
210     {
211         ::MessageBox(0, "Attempt to retrieve x-value of a vector\n"
212                         "With size smaller than 1 in function\n"
213                         " Vector::GetX();", "Error", MB_OK);
214         exit(1);
215     }
216     #endif
217     return data[0];
218 }
219
220 inline double Vector::GetY()
221 {
222         #ifdef DEBUG
223     if (nrData < 2)
224     {
225         ::MessageBox(0, "Attempt to retrieve the y value of a vector\n"
226                         "With size smaller than 2 in function\n"
227                         " Vector::GetY();", "Error", MB_OK);
228         exit(1);
229     }
230     #endif
231     return data[1];
232 }
233
234 inline double Vector::GetZ()
235 {
236         #ifdef DEBUG
237     if (nrData < 3)
238     {
239                 ::MessageBox(0, "Attempt to retrieve the z value of a vector\n"
240                         "With size smaller than 2 in function\n"
241                         " Vector::GetZ();", "Error", MB_OK);
242         exit(1);
243     }
244     #endif
245     return data[2];
246 }
247
248 inline void Vector::AddX(double x)
249 {
250         #ifdef DEBUG
251     if (nrData < 1)
252     {
253         ::MessageBox(0, "Attempt to chance x-value to a vector\n"
254                         "With size smaller than 1 in function\n"
255                         " Vector::AddX(double);", "Error", MB_OK);
256         exit(1);
257     }
258     #endif
259     data[0] += x;
260 }
261
262 inline void Vector::AddY(double y)
263 {
264         #ifdef DEBUG
265     if (nrData < 2)
266     {
267         ::MessageBox(0, "Attempt to chance y-value to a vector\n"
268                         "With size smaller than 2 in function\n"
269                         " Vector::AddY(double);", "Error", MB_OK);
270         exit(1);
271     }
272     #endif
273     data[1] += y;
274 }
275
276 inline void Vector::AddZ(double z)
277 {
278         #ifdef DEBUG
279     if (nrData < 3)
280     {
281         ::MessageBox(0, "Attempt to chance z-value to a vector\n"
282                         "With size smaller than 3 in function\n"
283                         " Vector::AddZ(double);", "Error", MB_OK);
284         exit(1);
285     }
286     #endif
287     data[2] += z;
288 }
289
290 inline void Vector::SubtractX(double x)
291 {
292         #ifdef DEBUG
293     if (nrData < 1)
294     {
295         ::MessageBox(0, "Attempt to chance x-value to a vector\n"
296                         "With size smaller than 1 in function\n"
297                         " Vector::SubtractX(double);", "Error", MB_OK);
298         exit(1);
299     }
300     #endif
301     data[0] -= x;
302 }
303
304 inline void Vector::SubtractY(double y)
305 {
306         #ifdef DEBUG
307     if (nrData < 2)
308     {
309         ::MessageBox(0, "Attempt to chance y-value to a vector\n"
310                         "With size smaller than 2 in function\n"
311                         " Vector::SubractY(double);", "Error", MB_OK);
312         exit(1);
313     }
314     #endif
315     data[1] -= y;
316 }
317
318 inline void Vector::SubtractZ(double z)
319 {
320         #ifdef DEBUG
321     if (nrData < 3)
322     {
323         ::MessageBox(0, "Attempt to chance z-value to a vector\n"
324                         "With size smaller than 3 in function\n"
325                         " Vector::SubtractZ(double);", "Error", MB_OK);
326         exit(1);
327     }
328     #endif
329     data[2] -= z;
330 }
331
332
333 inline Vector& Vector::operator= (Vector& other)
334 {
335     if (data)
336         delete[] data;
337     build(other.nrData);
338     memcpy(data, other.data, nrData*sizeof(double));
339     return *this;
340 }
341
342 inline double& Vector::operator [](int index)
343 {
344         return *(data+index);
345 }
346
347
348 inline int Vector::getDim()
349 {
350         return nrData;
351 }
352
353 /*-----------------------------------------------------------------------------
354                                                         Some generic conversion routines
355 ------------------------------------------------------------------------------*/
356
357 float CosD(float angle);
358 float SinD(float angle);
359 float Radians(float angle);
360 int Round(float value);
361
362 /* ----------------------------------------------------------------------------
363                                 And their inlined implementation
364 ------------------------------------------------------------------------------*/
365
366 inline float CosD(float angle)
367 {
368         return cos(Radians(angle));
369 }
370
371 inline float SinD(float angle)
372 {
373         return(Radians(angle));
374 }
375
376
377 inline float Radians(float angle)
378 {
379         return (angle*PiOver180);
380 }
381
382 inline int Round(float value)
383 {
384         return ( (int) (value+0.5));
385 }
386
387
388
389 /******************************************************************************
390
391                                                         Matrix class
392
393 ******************************************************************************/
394
395 class Matrix
396 {
397     protected:
398         int rows;
399         int columns;
400         Vector* data;
401
402     public:
403
404         Matrix();
405         Matrix(int r, int c);
406                 Matrix(int r, int c, double* dta);
407         Matrix(int r, int c, double** dta);
408         Matrix(int r, int c, Vector*dta);
409         Matrix(Matrix&);
410         ~Matrix();
411
412         void build(int r, int c);
413         Matrix& operator=(Matrix&);
414         Vector& operator[](int);
415         Vector operator ()(int);
416
417                 int getrows();
418         int getcols();
419         void norm(int scal);
420
421         friend Vector operator*(Matrix mc, Vector vc);
422 };
423
424 /*------------------------------------------------------------------------------
425                                         inline Matrix routines
426 ------------------------------------------------------------------------------*/
427
428 inline Matrix::Matrix()
429 {
430         rows = 0;
431     columns = 0;
432     data = 0;
433 }
434
435 inline Matrix::Matrix(int r, int c)
436 {
437         build(r, c);
438 }
439
440 inline Matrix::~Matrix()
441 {
442         delete [] data;
443 }
444
445
446 inline Vector& Matrix::operator[] (int row)
447 {
448         return data[row];
449 }
450
451
452 #endif // _MYMATH_H_
453
454