]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGColumnVector3.h
Attached patches remove BORLANDC, and hence SG_MATH_EXCEPTION_CLASH and SG_INCOM
[flightgear.git] / src / FDM / JSBSim / math / FGColumnVector3.h
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Header: FGColumnVector3.h
4 Author: Originally by Tony Peden [formatted and adapted here by Jon Berndt]
5 Date started: Unknown
6
7  ------------- Copyright (C) 2001 by Tony Peden and Jon S. Berndt (jsb@hal-pc.org)
8
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17  details.
18
19  You should have received a copy of the GNU Lesser General Public License along with
20  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21  Place - Suite 330, Boston, MA  02111-1307, USA.
22
23  Further information about the GNU Lesser General Public License can also be found on
24  the world wide web at http://www.gnu.org.
25
26 HISTORY
27 --------------------------------------------------------------------------------
28 ??/??/???? ??   Initial version and more.
29 03/06/2004 MF   Rework, document and do much inlineing.
30
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 SENTRY
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
34
35 #ifndef FGCOLUMNVECTOR3_H
36 #define FGCOLUMNVECTOR3_H
37
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 INCLUDES
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41
42 #include <cstdlib>
43 #include <string>
44 #include <fstream>
45 #include <iostream>
46 #include <cmath>
47
48 using std::ostream;
49 using std::istream;
50 using std::cerr;
51 using std::cout;
52 using std::endl;
53 using std::sqrt;
54 using std::string;
55
56 #include "FGJSBBase.h"
57
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 DEFINITIONS
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61
62 #define ID_COLUMNVECTOR3 "$Id$"
63
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 FORWARD DECLARATIONS
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67
68 namespace JSBSim {
69
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 CLASS DOCUMENTATION
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73
74 /** This class implements a 3 element column vector.
75     @author Jon S. Berndt, Tony Peden, et. al.
76     @version $Id$
77 */
78
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 CLASS DECLARATION
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
82
83 class FGColumnVector3 : public FGJSBBase
84 {
85 public:
86   /** Default initializer.
87       Create a zero vector.   */
88   FGColumnVector3(void);
89
90   /** Initialization by given values.
91       @param X value of the x-conponent.
92       @param Y value of the y-conponent.
93       @param Z value of the z-conponent.
94       Create a vector from the doubles given in the arguments.   */
95   FGColumnVector3(double X, double Y, double Z) {
96     data[0] = X;
97     data[1] = Y;
98     data[2] = Z;
99     Debug(0);
100   }
101
102   /** Copy constructor.
103       @param v Vector which is used for initialization.
104       Create copy of the vector given in the argument.   */
105   FGColumnVector3(const FGColumnVector3& v) {
106     data[0] = v.data[0];
107     data[1] = v.data[1];
108     data[2] = v.data[2];
109     Debug(0);
110   }
111
112   /// Destructor.
113   ~FGColumnVector3(void) { Debug(1); }
114
115   /** Read access the entries of the vector.
116       @param idx the component index.
117       Return the value of the matrix entry at the given index.
118       Indices are counted starting with 1.
119       Note that the index given in the argument is unchecked.   */
120   double operator()(unsigned int idx) const { return Entry(idx); }
121
122   /** Write access the entries of the vector.
123       @param idx the component index.
124       Return a reference to the vector entry at the given index.
125       Indices are counted starting with 1.
126       Note that the index given in the argument is unchecked.   */
127   double& operator()(unsigned int idx) { return Entry(idx); }
128
129   /** Read access the entries of the vector.
130       @param idx the component index.
131       Return the value of the matrix entry at the given index.
132       Indices are counted starting with 1.
133       This function is just a shortcut for the <tt>double
134       operator()(unsigned int idx) const</tt> function. It is
135       used internally to access the elements in a more convenient way.
136       Note that the index given in the argument is unchecked.   */
137   double Entry(unsigned int idx) const { return data[idx-1]; }
138
139   /** Write access the entries of the vector.
140       @param idx the component index.
141       Return a reference to the vector entry at the given index.
142       Indices are counted starting with 1.
143       This function is just a shortcut for the <tt>double&
144       operator()(unsigned int idx)</tt> function. It is
145       used internally to access the elements in a more convenient way.
146       Note that the index given in the argument is unchecked.   */
147   double& Entry(unsigned int idx) { return data[idx-1]; }
148
149   /** Prints the contents of the vector
150       @param delimeter the item separator (tab or comma)
151       @return a string with the delimeter-separated contents of the vector  */
152   string Dump(string delimeter) const;
153
154   /** Assignment operator.
155       @param b source vector.
156       Copy the content of the vector given in the argument into *this.   */
157   FGColumnVector3& operator=(const FGColumnVector3& b) {
158     data[0] = b.data[0];
159     data[1] = b.data[1];
160     data[2] = b.data[2];
161     return *this;
162   }
163
164   /**  Comparison operator.
165       @param b other vector.
166       Returns true if both vectors are exactly the same.   */
167   bool operator==(const FGColumnVector3& b) const {
168     return data[0] == b.data[0] && data[1] == b.data[1] && data[2] == b.data[2];
169   }
170
171   /** Comparison operator.
172       @param b other vector.
173       Returns false if both vectors are exactly the same.   */
174   bool operator!=(const FGColumnVector3& b) const { return ! operator==(b); }
175
176   /** Multiplication by a scalar.
177       @param scalar scalar value to multiply the vector with.
178       @return The resulting vector from the multiplication with that scalar.
179       Multiply the vector with the scalar given in the argument.   */
180   FGColumnVector3 operator*(const double scalar) const {
181     return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
182   }
183
184   /** Multiply by 1/scalar.
185       @param scalar scalar value to devide the vector through.
186       @return The resulting vector from the division through that scalar.
187       Multiply the vector with the 1/scalar given in the argument.   */
188   FGColumnVector3 operator/(const double scalar) const;
189
190   /** Cross product multiplication.
191       @param V vector to multiply with.
192       @return The resulting vector from the cross product multiplication.
193       Compute and return the cross product of the current vector with
194       the given argument.   */
195   FGColumnVector3 operator*(const FGColumnVector3& V) const {
196     return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
197                             Entry(3) * V(1) - Entry(1) * V(3),
198                             Entry(1) * V(2) - Entry(2) * V(1) );
199   }
200
201   /// Addition operator.
202   FGColumnVector3 operator+(const FGColumnVector3& B) const {
203     return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
204   }
205
206   /// Subtraction operator.
207   FGColumnVector3 operator-(const FGColumnVector3& B) const {
208     return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
209   }
210
211   /// Subtract an other vector.
212   FGColumnVector3& operator-=(const FGColumnVector3 &B) {
213     Entry(1) -= B(1);
214     Entry(2) -= B(2);
215     Entry(3) -= B(3);
216     return *this;
217   }
218
219   /// Add an other vector.
220   FGColumnVector3& operator+=(const FGColumnVector3 &B) {
221     Entry(1) += B(1);
222     Entry(2) += B(2);
223     Entry(3) += B(3);
224     return *this;
225   }
226
227   /// Scale by a scalar.
228   FGColumnVector3& operator*=(const double scalar) {
229     Entry(1) *= scalar;
230     Entry(2) *= scalar;
231     Entry(3) *= scalar;
232     return *this;
233   }
234
235   /// Scale by a 1/scalar.
236   FGColumnVector3& operator/=(const double scalar);
237
238   void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
239   void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
240   void InitMatrix(double a, double b, double c) {
241     data[0]=a; data[1]=b; data[2]=c;
242   }
243
244   /** Length of the vector.
245       Compute and return the euclidean norm of this vector.   */
246   double Magnitude(void) const;
247
248   /** Length of the vector in a coordinate axis plane.
249       Compute and return the euclidean norm of this vector projected into
250       the coordinate axis plane idx1-idx2.   */
251   double Magnitude(int idx1, int idx2) const {
252     return sqrt( Entry(idx1)*Entry(idx1) +  Entry(idx2)*Entry(idx2) );
253   }
254
255   /** Normalize.
256       Normalize the vector to have the Magnitude() == 1.0. If the vector
257       is equal to zero it is left untouched.   */
258   FGColumnVector3& Normalize(void);
259
260   // little trick here.
261   struct AssignRef {
262     AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
263     AssignRef operator<<(const double ff) {
264       Ref.Entry(idx) = ff;
265       return AssignRef(Ref, idx+1);
266     }
267     FGColumnVector3& Ref;
268     int idx;
269   };
270   AssignRef operator<<(const double ff) {
271     Entry(1) = ff;
272     return AssignRef(*this, 2);
273   }
274
275 private:
276   double data[3];
277
278   void Debug(int from);
279 };
280
281 /** Scalar multiplication.
282     @param scalar scalar value to multiply with.
283     @param A Vector to multiply.
284     Multiply the Vector with a scalar value.*/
285 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
286   // use already defined operation.
287   return A*scalar;
288 }
289
290 /** Write vector to a stream.
291     @param os Stream to write to.
292     @param M Matrix to write.
293     Write the matrix to a stream.*/
294 ostream& operator<<(ostream& os, const FGColumnVector3& col);
295
296 } // namespace JSBSim
297
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299 #endif