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