1 /* -*- Mode: C++ -*- *****************************************************
3 * Written by Durk Talsma, around 1995/1996.
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.
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.
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.
19 **************************************************************************/
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.
31 * The original versions were written under windows, hence the occasional
32 *::MessageBox() statements between conditional compile statements
34 ********************************************************************/
40 #include <simgear/compiler.h>
46 FG_USING_NAMESPACE(std);
48 #include <simgear/constants.h>
50 extern const double PiOver180;
51 extern const double Pix4dif3;
66 Vector(int size, double* dta);
67 Vector(double x, double y, double z);
68 Vector(Vector& other);
72 void SetVal(double x, double y, double z);
82 void SubtractX(double x);
83 void SubtractY(double y);
84 void SubtractZ(double z);
86 Vector& operator = (Vector&);
87 double& operator[] (int);
90 ostream& binSave(ostream& os);
92 Vector operator +=(Vector other);
93 Vector operator -=(Vector other);
95 friend double VecDot(Vector first, Vector second);
96 friend Vector VecCross(Vector first, Vector second);
97 friend Vector operator-(Vector first, Vector second);
98 friend Vector operator+(Vector first, Vector second);
99 friend Vector operator*(Matrix mx, Vector vc);
100 friend Vector operator*(Vector v1, Vector v2);
101 friend Vector operator*(Vector vec, double d);
102 friend Vector operator/(Vector vec, double d);
104 friend ostream& operator << (ostream& os, Vector& vec);
105 friend istream& operator >> (istream& is, Vector& vec);
108 /*-----------------------------------------------------------------------------
109 nonmember friend functions
110 ------------------------------------------------------------------------------*/
112 double VecDot(Vector first, Vector second);
113 Vector VecCross(Vector first, Vector second);
114 Vector operator-(Vector first, Vector second);
115 Vector operator+(Vector first, Vector second);
116 Vector operator*(Matrix mx, Vector vc);
117 Vector operator*(Vector v1, Vector v2);
118 Vector operator*(Vector vec, double d);
119 Vector operator/(Vector vec, double d);
121 ostream& operator << (ostream& os, Vector& vec);
122 istream& operator >> (istream& is, Vector& vec);
125 /*------------------------------------------------------------------------------
126 inline member functions
127 ------------------------------------------------------------------------------*/
129 inline Vector::Vector()
135 inline void Vector::build(int size)
138 data = new double[nrData];
142 ::MessageBox(0, "Error Allocating Memory for a new Vector", "Error",
147 //for (int i = 0; i < nrData; i++)
152 inline Vector::Vector(int size, double* dta)
155 memcpy(data, dta, nrData*sizeof(double));
159 inline Vector::Vector(Vector& other)
162 memcpy(data,other.data,nrData*sizeof(double));
165 inline Vector::Vector(double x, double y, double z)
167 build(4); // one extra for matrix multiplication...
175 inline Vector::Vector(istream& is)
177 is.read((char*) &nrData, sizeof(int));
178 data = new double[nrData];
179 is.read((char*) data, nrData * sizeof(double));
182 inline Vector::~Vector()
188 inline void Vector::SetVal(double x, double y, double z)
193 ::MessageBox(0, "Attempt to assign data to a vector\n"
194 "With size unequal to 4 in function\n"
195 " Vector::Setval(double, double, double", "Error" , MB_OK);
205 inline Vector* Vector::GetVector()
210 inline double Vector::GetX()
215 ::MessageBox(0, "Attempt to retrieve x-value of a vector\n"
216 "With size smaller than 1 in function\n"
217 " Vector::GetX();", "Error", MB_OK);
224 inline double Vector::GetY()
229 ::MessageBox(0, "Attempt to retrieve the y value of a vector\n"
230 "With size smaller than 2 in function\n"
231 " Vector::GetY();", "Error", MB_OK);
238 inline double Vector::GetZ()
243 ::MessageBox(0, "Attempt to retrieve the z value of a vector\n"
244 "With size smaller than 2 in function\n"
245 " Vector::GetZ();", "Error", MB_OK);
252 inline void Vector::AddX(double x)
257 ::MessageBox(0, "Attempt to chance x-value to a vector\n"
258 "With size smaller than 1 in function\n"
259 " Vector::AddX(double);", "Error", MB_OK);
266 inline void Vector::AddY(double y)
271 ::MessageBox(0, "Attempt to chance y-value to a vector\n"
272 "With size smaller than 2 in function\n"
273 " Vector::AddY(double);", "Error", MB_OK);
280 inline void Vector::AddZ(double z)
285 ::MessageBox(0, "Attempt to chance z-value to a vector\n"
286 "With size smaller than 3 in function\n"
287 " Vector::AddZ(double);", "Error", MB_OK);
294 inline void Vector::SubtractX(double x)
299 ::MessageBox(0, "Attempt to chance x-value to a vector\n"
300 "With size smaller than 1 in function\n"
301 " Vector::SubtractX(double);", "Error", MB_OK);
308 inline void Vector::SubtractY(double y)
313 ::MessageBox(0, "Attempt to chance y-value to a vector\n"
314 "With size smaller than 2 in function\n"
315 " Vector::SubractY(double);", "Error", MB_OK);
322 inline void Vector::SubtractZ(double z)
327 ::MessageBox(0, "Attempt to chance z-value to a vector\n"
328 "With size smaller than 3 in function\n"
329 " Vector::SubtractZ(double);", "Error", MB_OK);
337 inline Vector& Vector::operator= (Vector& other)
342 memcpy(data, other.data, nrData*sizeof(double));
346 inline double& Vector::operator [](int index)
348 return *(data+index);
352 inline int Vector::getDim()
357 /*-----------------------------------------------------------------------------
358 Some generic conversion routines
359 ------------------------------------------------------------------------------*/
361 float CosD(float angle);
362 float SinD(float angle);
363 float Radians(float angle);
364 int Round(float value);
366 /* ----------------------------------------------------------------------------
367 And their inlined implementation
368 ------------------------------------------------------------------------------*/
370 inline float CosD(float angle)
372 return cos(Radians(angle));
375 inline float SinD(float angle)
377 return(Radians(angle));
381 inline float Radians(float angle)
383 return (angle*PiOver180);
386 inline int Round(float value)
388 return ( (int) (value+0.5));
393 /******************************************************************************
397 ******************************************************************************/
409 Matrix(int r, int c);
410 Matrix(int r, int c, double* dta);
411 Matrix(int r, int c, double** dta);
412 Matrix(int r, int c, Vector*dta);
416 void build(int r, int c);
417 Matrix& operator=(Matrix&);
418 Vector& operator[](int);
419 Vector operator ()(int);
425 friend Vector operator*(Matrix mc, Vector vc);
428 /*------------------------------------------------------------------------------
429 inline Matrix routines
430 ------------------------------------------------------------------------------*/
432 inline Matrix::Matrix()
439 inline Matrix::Matrix(int r, int c)
444 inline Matrix::~Matrix()
450 inline Vector& Matrix::operator[] (int row)