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