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 ********************************************************************/
45 #include <fg_constants.h>
46 extern const double PiOver180;
47 extern const double Pix4dif3;
62 Vector(int size, double* dta);
63 Vector(double x, double y, double z);
64 Vector(Vector& other);
68 void SetVal(double x, double y, double z);
78 void SubtractX(double x);
79 void SubtractY(double y);
80 void SubtractZ(double z);
82 Vector& operator = (Vector&);
83 double& operator[] (int);
86 ostream& binSave(ostream& os);
88 Vector operator +=(Vector other);
89 Vector operator -=(Vector other);
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);
100 friend ostream& operator << (ostream& os, Vector& vec);
101 friend istream& operator >> (istream& is, Vector& vec);
104 /*-----------------------------------------------------------------------------
105 nonmember friend functions
106 ------------------------------------------------------------------------------*/
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);
117 ostream& operator << (ostream& os, Vector& vec);
118 istream& operator >> (istream& is, Vector& vec);
121 /*------------------------------------------------------------------------------
122 inline member functions
123 ------------------------------------------------------------------------------*/
125 inline Vector::Vector()
131 inline void Vector::build(int size)
134 data = new double[nrData];
138 ::MessageBox(0, "Error Allocating Memory for a new Vector", "Error",
143 //for (int i = 0; i < nrData; i++)
148 inline Vector::Vector(int size, double* dta)
151 memcpy(data, dta, nrData*sizeof(double));
155 inline Vector::Vector(Vector& other)
158 memcpy(data,other.data,nrData*sizeof(double));
161 inline Vector::Vector(double x, double y, double z)
163 build(4); // one extra for matrix multiplication...
171 inline Vector::Vector(istream& is)
173 is.read((char*) &nrData, sizeof(int));
174 data = new double[nrData];
175 is.read((char*) data, nrData * sizeof(double));
178 inline Vector::~Vector()
184 inline void Vector::SetVal(double x, double y, double z)
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);
201 inline Vector* Vector::GetVector()
206 inline double Vector::GetX()
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);
220 inline double Vector::GetY()
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);
234 inline double Vector::GetZ()
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);
248 inline void Vector::AddX(double x)
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);
262 inline void Vector::AddY(double y)
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);
276 inline void Vector::AddZ(double z)
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);
290 inline void Vector::SubtractX(double x)
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);
304 inline void Vector::SubtractY(double y)
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);
318 inline void Vector::SubtractZ(double z)
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);
333 inline Vector& Vector::operator= (Vector& other)
338 memcpy(data, other.data, nrData*sizeof(double));
342 inline double& Vector::operator [](int index)
344 return *(data+index);
348 inline int Vector::getDim()
353 /*-----------------------------------------------------------------------------
354 Some generic conversion routines
355 ------------------------------------------------------------------------------*/
357 float CosD(float angle);
358 float SinD(float angle);
359 float Radians(float angle);
360 int Round(float value);
362 /* ----------------------------------------------------------------------------
363 And their inlined implementation
364 ------------------------------------------------------------------------------*/
366 inline float CosD(float angle)
368 return cos(Radians(angle));
371 inline float SinD(float angle)
373 return(Radians(angle));
377 inline float Radians(float angle)
379 return (angle*PiOver180);
382 inline int Round(float value)
384 return ( (int) (value+0.5));
389 /******************************************************************************
393 ******************************************************************************/
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);
412 void build(int r, int c);
413 Matrix& operator=(Matrix&);
414 Vector& operator[](int);
415 Vector operator ()(int);
421 friend Vector operator*(Matrix mc, Vector vc);
424 /*------------------------------------------------------------------------------
425 inline Matrix routines
426 ------------------------------------------------------------------------------*/
428 inline Matrix::Matrix()
435 inline Matrix::Matrix(int r, int c)
440 inline Matrix::~Matrix()
446 inline Vector& Matrix::operator[] (int row)