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