]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGColumnVector3.h
Frederic Bouvier:
[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
93       Create a zero vector.
94    */
95   FGColumnVector3(void);
96
97   /** Initialization by given values.
98
99       @param X value of the x-conponent.
100       @param Y value of the y-conponent.
101       @param Z value of the z-conponent.
102
103       Create a vector from the doubles given in the arguments.
104    */
105   FGColumnVector3(double X, double Y, double Z) {
106     data[0] = X;
107     data[1] = Y;
108     data[2] = Z;
109     Debug(0);
110   }
111
112   /** Copy constructor.
113
114       @param v Vector which is used for initialization.
115
116       Create copy of the vector given in the argument.
117    */
118   FGColumnVector3(const FGColumnVector3& v) {
119     data[0] = v.data[0];
120     data[1] = v.data[1];
121     data[2] = v.data[2];
122     Debug(0);
123   }
124
125   /** Destructor.
126    */
127   ~FGColumnVector3(void) { Debug(1); }
128
129
130   /** Read access the entries of the vector.
131
132       @param idx the component index.
133
134       Return the value of the matrix entry at the given index.
135       Indices are counted starting with 1.
136
137       Note that the index given in the argument is unchecked.
138    */
139   double operator()(unsigned int idx) const { return Entry(idx); }
140
141   /** Write access the entries of the vector.
142
143       @param idx the component index.
144
145       Return a reference to the vector entry at the given index.
146       Indices are counted starting with 1.
147
148       Note that the index given in the argument is unchecked.
149    */
150   double& operator()(unsigned int idx) { return Entry(idx); }
151
152   /** Read access the entries of the vector.
153
154       @param idx the component index.
155
156       Return the value of the matrix entry at the given index.
157       Indices are counted starting with 1.
158
159       This function is just a shortcut for the @ref double
160       operator()(unsigned int idx) const function. It is
161       used internally to access the elements in a more convenient way.
162
163       Note that the index given in the argument is unchecked.
164    */
165   double Entry(unsigned int idx) const { return data[idx-1]; }
166
167   /** Write access the entries of the vector.
168
169       @param idx the component index.
170
171       Return a reference to the vector entry at the given index.
172       Indices are counted starting with 1.
173
174       This function is just a shortcut for the @ref double&
175       operator()(unsigned int idx) function. It is
176       used internally to access the elements in a more convenient way.
177
178       Note that the index given in the argument is unchecked.
179    */
180   double& Entry(unsigned int idx) { return data[idx-1]; }
181
182   /** Assignment operator.
183
184       @param b source vector.
185
186       Copy the content of the vector given in the argument into *this.
187    */
188   FGColumnVector3& operator=(const FGColumnVector3& b) {
189     data[0] = b.data[0];
190     data[1] = b.data[1];
191     data[2] = b.data[2];
192     return *this;
193   }
194
195   /**  Comparison operator.
196
197       @param b other vector.
198
199       Returns true if both vectors are exactly the same.
200    */
201   bool operator==(const FGColumnVector3& b) const {
202     return data[0] == b.data[0] && data[1] == b.data[1] && data[2] == b.data[2];
203   }
204
205   /** Comparison operator.
206
207       @param b other vector.
208
209       Returns false if both vectors are exactly the same.
210    */
211   bool operator!=(const FGColumnVector3& b) const { return ! operator==(b); }
212
213   /** Multiplication by a scalar.
214
215       @param scalar scalar value to multiply the vector with.
216       @return The resulting vector from the multiplication with that scalar.
217
218       Multiply the vector with the scalar given in the argument.
219    */
220   FGColumnVector3 operator*(const double scalar) const {
221     return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
222   }
223
224   /** Multiply by 1/scalar.
225
226       @param scalar scalar value to devide the vector through.
227       @return The resulting vector from the division through that scalar.
228
229       Multiply the vector with the 1/scalar given in the argument.
230    */
231   FGColumnVector3 operator/(const double scalar) const;
232
233   /** Cross product multiplication.
234
235       @param v vector to multiply with.
236       @return The resulting vector from the cross product multiplication.
237
238       Compute and return the cross product of the current vector with
239       the given argument.
240    */
241   FGColumnVector3 operator*(const FGColumnVector3& V) const {
242     return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
243                             Entry(3) * V(1) - Entry(1) * V(3),
244                             Entry(1) * V(2) - Entry(2) * V(1) );
245   }
246
247   /** Addition operator.
248    */
249   FGColumnVector3 operator+(const FGColumnVector3& B) const {
250     return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
251   }
252
253   /** Subtraction operator.
254    */
255   FGColumnVector3 operator-(const FGColumnVector3& B) const {
256     return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
257   }
258
259   /** Subtract an other vector.
260    */
261   FGColumnVector3& operator-=(const FGColumnVector3 &B) {
262     Entry(1) -= B(1);
263     Entry(2) -= B(2);
264     Entry(3) -= B(3);
265     return *this;
266   }
267
268   /** Add an other vector.
269    */
270   FGColumnVector3& operator+=(const FGColumnVector3 &B) {
271     Entry(1) += B(1);
272     Entry(2) += B(2);
273     Entry(3) += B(3);
274     return *this;
275   }
276
277   /** Scale by a scalar.
278    */
279   FGColumnVector3& operator*=(const double scalar) {
280     Entry(1) *= scalar;
281     Entry(2) *= scalar;
282     Entry(3) *= scalar;
283     return *this;
284   }
285
286   /** Scale by a 1/scalar.
287    */
288   FGColumnVector3& operator/=(const double scalar);
289
290   void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
291   void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
292   void InitMatrix(double a, double b, double c) {
293     data[0]=a; data[1]=b; data[2]=c;
294   }
295
296   /** Length of the vector.
297
298       Compute and return the euclidean norm of this vector.
299    */
300   double Magnitude(void) const;
301
302   /** Normalize.
303
304       Normalize the vector to have the Magnitude() == 1.0. If the vector
305       is equal to zero it is left untouched.
306    */
307   FGColumnVector3& Normalize(void);
308
309   // ??? Is this something sensible ??
310   FGColumnVector3 multElementWise(const FGColumnVector3& V) const;
311
312   // little trick here.
313   struct AssignRef {
314     AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
315     AssignRef operator<<(const double ff) {
316       Ref.Entry(idx) = ff;
317       return AssignRef(Ref, idx+1);
318     }
319     FGColumnVector3& Ref;
320     int idx;
321   };
322   AssignRef operator<<(const double ff) {
323     Entry(1) = ff;
324     return AssignRef(*this, 2);
325   }
326
327 private:
328   double data[3];
329
330   void Debug(int from);
331 };
332
333 /** Scalar multiplication.
334
335     @param scalar scalar value to multiply with.
336     @param A Vector to multiply.
337
338     Multiply the Vector with a scalar value.
339 */
340 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
341   // use already defined operation.
342   return A*scalar;
343 }
344
345 /** Write vector to a stream.
346
347     @param os Stream to write to.
348     @param M Matrix to write.
349
350     Write the matrix to a stream.
351 */
352 ostream& operator<<(ostream& os, const FGColumnVector3& col);
353
354 } // namespace JSBSim
355
356 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 #endif