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