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 <Include/compiler.h>
46 FG_USING_NAMESPACE(std);
48 #include <fg_constants.h>
49 extern const double PiOver180;
50 extern const double Pix4dif3;
65 Vector(int size, double* dta);
66 Vector(double x, double y, double z);
67 Vector(Vector& other);
71 void SetVal(double x, double y, double z);
81 void SubtractX(double x);
82 void SubtractY(double y);
83 void SubtractZ(double z);
85 Vector& operator = (Vector&);
86 double& operator[] (int);
89 ostream& binSave(ostream& os);
91 Vector operator +=(Vector other);
92 Vector operator -=(Vector other);
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);
103 friend ostream& operator << (ostream& os, Vector& vec);
104 friend istream& operator >> (istream& is, Vector& vec);
107 /*-----------------------------------------------------------------------------
108 nonmember friend functions
109 ------------------------------------------------------------------------------*/
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);
120 ostream& operator << (ostream& os, Vector& vec);
121 istream& operator >> (istream& is, Vector& vec);
124 /*------------------------------------------------------------------------------
125 inline member functions
126 ------------------------------------------------------------------------------*/
128 inline Vector::Vector()
134 inline void Vector::build(int size)
137 data = new double[nrData];
141 ::MessageBox(0, "Error Allocating Memory for a new Vector", "Error",
146 //for (int i = 0; i < nrData; i++)
151 inline Vector::Vector(int size, double* dta)
154 memcpy(data, dta, nrData*sizeof(double));
158 inline Vector::Vector(Vector& other)
161 memcpy(data,other.data,nrData*sizeof(double));
164 inline Vector::Vector(double x, double y, double z)
166 build(4); // one extra for matrix multiplication...
174 inline Vector::Vector(istream& is)
176 is.read((char*) &nrData, sizeof(int));
177 data = new double[nrData];
178 is.read((char*) data, nrData * sizeof(double));
181 inline Vector::~Vector()
187 inline void Vector::SetVal(double x, double y, double z)
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);
204 inline Vector* Vector::GetVector()
209 inline double Vector::GetX()
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);
223 inline double Vector::GetY()
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);
237 inline double Vector::GetZ()
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);
251 inline void Vector::AddX(double x)
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);
265 inline void Vector::AddY(double y)
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);
279 inline void Vector::AddZ(double z)
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);
293 inline void Vector::SubtractX(double x)
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);
307 inline void Vector::SubtractY(double y)
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);
321 inline void Vector::SubtractZ(double z)
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);
336 inline Vector& Vector::operator= (Vector& other)
341 memcpy(data, other.data, nrData*sizeof(double));
345 inline double& Vector::operator [](int index)
347 return *(data+index);
351 inline int Vector::getDim()
356 /*-----------------------------------------------------------------------------
357 Some generic conversion routines
358 ------------------------------------------------------------------------------*/
360 float CosD(float angle);
361 float SinD(float angle);
362 float Radians(float angle);
363 int Round(float value);
365 /* ----------------------------------------------------------------------------
366 And their inlined implementation
367 ------------------------------------------------------------------------------*/
369 inline float CosD(float angle)
371 return cos(Radians(angle));
374 inline float SinD(float angle)
376 return(Radians(angle));
380 inline float Radians(float angle)
382 return (angle*PiOver180);
385 inline int Round(float value)
387 return ( (int) (value+0.5));
392 /******************************************************************************
396 ******************************************************************************/
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);
415 void build(int r, int c);
416 Matrix& operator=(Matrix&);
417 Vector& operator[](int);
418 Vector operator ()(int);
424 friend Vector operator*(Matrix mc, Vector vc);
427 /*------------------------------------------------------------------------------
428 inline Matrix routines
429 ------------------------------------------------------------------------------*/
431 inline Matrix::Matrix()
438 inline Matrix::Matrix(int r, int c)
443 inline Matrix::~Matrix()
449 inline Vector& Matrix::operator[] (int row)