]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGColumnVector3.h
Fix an issue where math.h doesn't define sqrt in namespace std (at least on MSVC...
[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   /** Multiplication by a scalar.
196     
197       @param scalar scalar value to multiply the vector with.
198       @return The resulting vector from the multiplication with that scalar.
199     
200       Multiply the vector with the scalar given in the argument.
201    */
202   FGColumnVector3 operator*(const double scalar) const {
203     return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
204   }
205
206   /** Multiply by 1/scalar.
207     
208       @param scalar scalar value to devide the vector through.
209       @return The resulting vector from the division through that scalar.
210     
211       Multiply the vector with the 1/scalar given in the argument.
212    */
213   FGColumnVector3 operator/(const double scalar) const;
214
215   /** Cross product multiplication.
216     
217       @param v vector to multiply with.
218       @return The resulting vector from the cross product multiplication.
219     
220       Compute and return the cross product of the current vector with
221       the given argument.
222    */
223   FGColumnVector3 operator*(const FGColumnVector3& V) const {
224     return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
225                             Entry(3) * V(1) - Entry(1) * V(3),
226                             Entry(1) * V(2) - Entry(2) * V(1) );
227   }
228
229   /** Addition operator.
230    */
231   FGColumnVector3 operator+(const FGColumnVector3& B) const {
232     return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
233   }
234
235   /** Subtraction operator.
236    */
237   FGColumnVector3 operator-(const FGColumnVector3& B) const {
238     return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
239   }
240
241   /** Subtract an other vector.
242    */
243   FGColumnVector3& operator-=(const FGColumnVector3 &B) {
244     Entry(1) -= B(1);
245     Entry(2) -= B(2);
246     Entry(3) -= B(3);
247     return *this;
248   }
249
250   /** Add an other vector.
251    */
252   FGColumnVector3& operator+=(const FGColumnVector3 &B) {
253     Entry(1) += B(1);
254     Entry(2) += B(2);
255     Entry(3) += B(3);
256     return *this;
257   }
258
259   /** Scale by a scalar.
260    */
261   FGColumnVector3& operator*=(const double scalar) {
262     Entry(1) *= scalar;
263     Entry(2) *= scalar;
264     Entry(3) *= scalar;
265     return *this;
266   }
267
268   /** Scale by a 1/scalar.
269    */
270   FGColumnVector3& operator/=(const double scalar);
271
272   void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
273   void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
274   void InitMatrix(double a, double b, double c) {
275     data[0]=a; data[1]=b; data[2]=c;
276   }
277
278   /** Length of the vector.
279     
280       Compute and return the euclidean norm of this vector.
281    */
282   double Magnitude(void) const;
283
284   /** Normialze.
285     
286       Normalize the vector to have the Magnitude() == 1.0. If the vector
287       is equal to zero it is left untouched.
288    */
289   FGColumnVector3& Normalize(void);
290
291   // ??? Is this something sensible ??
292   FGColumnVector3 multElementWise(const FGColumnVector3& V) const;
293
294   // little trick here.
295   struct AssignRef {
296     AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
297     AssignRef operator<<(const double ff) {
298       Ref.Entry(idx) = ff;
299       return AssignRef(Ref, idx+1);
300     }
301     FGColumnVector3& Ref;
302     int idx;
303   };
304   AssignRef operator<<(const double ff) {
305     Entry(1) = ff;
306     return AssignRef(*this, 2);
307   }
308
309 private:
310   double data[3];
311
312   void Debug(int from);
313 };
314
315 /** Scalar multiplication.
316
317     @param scalar scalar value to multiply with.
318     @param A Vector to multiply.
319
320     Multiply the Vector with a scalar value.
321 */
322 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
323   // use already defined operation.
324   return A*scalar;
325 }
326
327 /** Write vector to a stream.
328
329     @param os Stream to write to.
330     @param M Matrix to write.
331
332     Write the matrix to a stream.
333 */
334 ostream& operator<<(ostream& os, const FGColumnVector3& col);
335
336 } // namespace JSBSim
337
338 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 #endif