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