]> git.mxchange.org Git - flightgear.git/blob - src/Time/mymath.h
Added support for parsing socket options.
[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
40 #include <Include/compiler.h>
41
42 #include <math.h>
43 #include STL_FSTREAM
44 #include STL_IOMANIP
45
46 FG_USING_NAMESPACE(std);
47
48 #include <fg_constants.h>
49 extern const  double PiOver180;
50 extern const double Pix4dif3;
51
52
53
54 class Matrix;
55
56 class Vector
57 {
58     private:
59         int nrData;
60         double* data;
61
62         public:
63         Vector();
64         Vector(int size);
65         Vector(int size, double* dta);
66         Vector(double x, double y, double z);
67         Vector(Vector& other);
68         Vector(istream& is);
69         ~Vector();
70
71         void SetVal(double x, double y, double z);
72
73         void build(int);
74         Vector* GetVector();
75         double GetX();
76         double GetY();
77         double GetZ();
78         void AddX(double x);
79         void AddY(double y);
80         void AddZ(double z);
81         void SubtractX(double x);
82         void SubtractY(double y);
83         void SubtractZ(double z);
84         double VecLen();
85         Vector& operator = (Vector&);
86         double& operator[] (int);
87         int getDim();
88         
89         ostream& binSave(ostream& os);
90         
91         Vector operator +=(Vector other);
92         Vector operator -=(Vector other);
93         
94         friend double VecDot(Vector first, Vector second);
95         friend Vector VecCross(Vector first, Vector second);
96         friend Vector operator-(Vector first, Vector second);
97         friend Vector operator+(Vector first, Vector second);
98         friend Vector operator*(Matrix mx, Vector vc);
99         friend Vector operator*(Vector v1, Vector v2);
100         friend Vector operator*(Vector vec, double d);
101         friend Vector operator/(Vector vec, double d);
102         
103         friend ostream& operator << (ostream& os, Vector& vec);
104         friend istream& operator >> (istream& is, Vector& vec);
105 };
106
107 /*-----------------------------------------------------------------------------
108                                 nonmember friend functions
109 ------------------------------------------------------------------------------*/
110
111 double VecDot(Vector first, Vector second);
112 Vector VecCross(Vector first, Vector second);
113 Vector operator-(Vector first, Vector second);
114 Vector operator+(Vector first, Vector second);
115 Vector operator*(Matrix mx, Vector vc);
116 Vector operator*(Vector v1, Vector v2);
117 Vector operator*(Vector vec, double d);
118 Vector operator/(Vector vec, double d);
119
120 ostream& operator << (ostream& os, Vector& vec);
121 istream& operator >> (istream& is, Vector& vec);
122
123
124 /*------------------------------------------------------------------------------
125                         inline member functions
126 ------------------------------------------------------------------------------*/
127
128 inline Vector::Vector()
129 {
130         nrData = 0;
131     data = 0;
132 }
133
134 inline void Vector::build(int size)
135 {
136         nrData = size;
137     data = new double[nrData];
138     #ifdef DEBUG
139     if (!data)
140     {
141                 ::MessageBox(0, "Error Allocating Memory for a new Vector", "Error",
142                                 MB_OK);
143         exit(1);
144     }
145     #endif
146     //for (int i = 0; i < nrData; i++)
147     //  data[i] = 0.00;
148 }
149
150
151 inline Vector::Vector(int size, double* dta)
152 {
153         build(size);
154     memcpy(data, dta, nrData*sizeof(double));
155 }
156
157
158 inline Vector::Vector(Vector& other)
159 {
160         build(other.nrData);
161     memcpy(data,other.data,nrData*sizeof(double));
162 }
163
164 inline Vector::Vector(double x, double y, double z)
165 {
166     build(4);      // one extra for matrix multiplication...
167         data[0] = x;
168         data[1] = y;
169         data[2] = z;
170     data[3] = 0.00;
171
172 }
173
174 inline Vector::Vector(istream& is)
175 {
176         is.read((char*) &nrData, sizeof(int));
177     data = new double[nrData];
178     is.read((char*) data, nrData * sizeof(double));
179 }
180
181 inline Vector::~Vector()
182 {
183         delete [] data;
184 }
185
186
187 inline void Vector::SetVal(double x, double y, double z)
188 {
189         #ifdef DEBUG
190     if (nrData != 4)
191     {
192         ::MessageBox(0, "Attempt to assign data to a vector\n"
193                         "With size unequal to 4 in function\n"
194                         " Vector::Setval(double, double, double", "Error" , MB_OK);
195         exit(1);
196     }
197     #endif
198     data[0] = x,
199     data[1] = y;
200     data[2] = z;
201     data[3] = 0.00;
202 }
203
204 inline Vector* Vector::GetVector()
205 {
206         return this;
207 }
208
209 inline double Vector::GetX()
210 {
211         #ifdef DEBUG
212     if (nrData < 1)
213     {
214         ::MessageBox(0, "Attempt to retrieve x-value of a vector\n"
215                         "With size smaller than 1 in function\n"
216                         " Vector::GetX();", "Error", MB_OK);
217         exit(1);
218     }
219     #endif
220     return data[0];
221 }
222
223 inline double Vector::GetY()
224 {
225         #ifdef DEBUG
226     if (nrData < 2)
227     {
228         ::MessageBox(0, "Attempt to retrieve the y value of a vector\n"
229                         "With size smaller than 2 in function\n"
230                         " Vector::GetY();", "Error", MB_OK);
231         exit(1);
232     }
233     #endif
234     return data[1];
235 }
236
237 inline double Vector::GetZ()
238 {
239         #ifdef DEBUG
240     if (nrData < 3)
241     {
242                 ::MessageBox(0, "Attempt to retrieve the z value of a vector\n"
243                         "With size smaller than 2 in function\n"
244                         " Vector::GetZ();", "Error", MB_OK);
245         exit(1);
246     }
247     #endif
248     return data[2];
249 }
250
251 inline void Vector::AddX(double x)
252 {
253         #ifdef DEBUG
254     if (nrData < 1)
255     {
256         ::MessageBox(0, "Attempt to chance x-value to a vector\n"
257                         "With size smaller than 1 in function\n"
258                         " Vector::AddX(double);", "Error", MB_OK);
259         exit(1);
260     }
261     #endif
262     data[0] += x;
263 }
264
265 inline void Vector::AddY(double y)
266 {
267         #ifdef DEBUG
268     if (nrData < 2)
269     {
270         ::MessageBox(0, "Attempt to chance y-value to a vector\n"
271                         "With size smaller than 2 in function\n"
272                         " Vector::AddY(double);", "Error", MB_OK);
273         exit(1);
274     }
275     #endif
276     data[1] += y;
277 }
278
279 inline void Vector::AddZ(double z)
280 {
281         #ifdef DEBUG
282     if (nrData < 3)
283     {
284         ::MessageBox(0, "Attempt to chance z-value to a vector\n"
285                         "With size smaller than 3 in function\n"
286                         " Vector::AddZ(double);", "Error", MB_OK);
287         exit(1);
288     }
289     #endif
290     data[2] += z;
291 }
292
293 inline void Vector::SubtractX(double x)
294 {
295         #ifdef DEBUG
296     if (nrData < 1)
297     {
298         ::MessageBox(0, "Attempt to chance x-value to a vector\n"
299                         "With size smaller than 1 in function\n"
300                         " Vector::SubtractX(double);", "Error", MB_OK);
301         exit(1);
302     }
303     #endif
304     data[0] -= x;
305 }
306
307 inline void Vector::SubtractY(double y)
308 {
309         #ifdef DEBUG
310     if (nrData < 2)
311     {
312         ::MessageBox(0, "Attempt to chance y-value to a vector\n"
313                         "With size smaller than 2 in function\n"
314                         " Vector::SubractY(double);", "Error", MB_OK);
315         exit(1);
316     }
317     #endif
318     data[1] -= y;
319 }
320
321 inline void Vector::SubtractZ(double z)
322 {
323         #ifdef DEBUG
324     if (nrData < 3)
325     {
326         ::MessageBox(0, "Attempt to chance z-value to a vector\n"
327                         "With size smaller than 3 in function\n"
328                         " Vector::SubtractZ(double);", "Error", MB_OK);
329         exit(1);
330     }
331     #endif
332     data[2] -= z;
333 }
334
335
336 inline Vector& Vector::operator= (Vector& other)
337 {
338     if (data)
339         delete[] data;
340     build(other.nrData);
341     memcpy(data, other.data, nrData*sizeof(double));
342     return *this;
343 }
344
345 inline double& Vector::operator [](int index)
346 {
347         return *(data+index);
348 }
349
350
351 inline int Vector::getDim()
352 {
353         return nrData;
354 }
355
356 /*-----------------------------------------------------------------------------
357                                                         Some generic conversion routines
358 ------------------------------------------------------------------------------*/
359
360 float CosD(float angle);
361 float SinD(float angle);
362 float Radians(float angle);
363 int Round(float value);
364
365 /* ----------------------------------------------------------------------------
366                                 And their inlined implementation
367 ------------------------------------------------------------------------------*/
368
369 inline float CosD(float angle)
370 {
371         return cos(Radians(angle));
372 }
373
374 inline float SinD(float angle)
375 {
376         return(Radians(angle));
377 }
378
379
380 inline float Radians(float angle)
381 {
382         return (angle*PiOver180);
383 }
384
385 inline int Round(float value)
386 {
387         return ( (int) (value+0.5));
388 }
389
390
391
392 /******************************************************************************
393
394                                                         Matrix class
395
396 ******************************************************************************/
397
398 class Matrix
399 {
400     protected:
401         int rows;
402         int columns;
403         Vector* data;
404
405     public:
406
407         Matrix();
408         Matrix(int r, int c);
409                 Matrix(int r, int c, double* dta);
410         Matrix(int r, int c, double** dta);
411         Matrix(int r, int c, Vector*dta);
412         Matrix(Matrix&);
413         ~Matrix();
414
415         void build(int r, int c);
416         Matrix& operator=(Matrix&);
417         Vector& operator[](int);
418         Vector operator ()(int);
419
420                 int getrows();
421         int getcols();
422         void norm(int scal);
423
424         friend Vector operator*(Matrix mc, Vector vc);
425 };
426
427 /*------------------------------------------------------------------------------
428                                         inline Matrix routines
429 ------------------------------------------------------------------------------*/
430
431 inline Matrix::Matrix()
432 {
433         rows = 0;
434     columns = 0;
435     data = 0;
436 }
437
438 inline Matrix::Matrix(int r, int c)
439 {
440         build(r, c);
441 }
442
443 inline Matrix::~Matrix()
444 {
445         delete [] data;
446 }
447
448
449 inline Vector& Matrix::operator[] (int row)
450 {
451         return data[row];
452 }
453
454
455 #endif // _MYMATH_H_
456
457