1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Header: FGColumnVector3.h
4 Author: Originally by Tony Peden [formatted and adapted here by Jon Berndt]
7 ------------- Copyright (C) 2001 by Tony Peden and Jon S. Berndt (jon@jsbsim.org)
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
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
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.
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.
27 --------------------------------------------------------------------------------
28 ??/??/???? ?? Initial version and more.
29 03/06/2004 MF Rework, document and do much inlineing.
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
35 #ifndef FGCOLUMNVECTOR3_H
36 #define FGCOLUMNVECTOR3_H
38 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 #define ID_COLUMNVECTOR3 "$Id: FGColumnVector3.h,v 1.16 2010/12/07 12:57:14 jberndt Exp $"
51 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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 $
66 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73 /** Default initializer.
74 Create a zero vector. */
75 FGColumnVector3(void);
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) {
89 @param v Vector which is used for initialization.
90 Create copy of the vector given in the argument. */
91 FGColumnVector3(const FGColumnVector3& v) {
98 ~FGColumnVector3(void) { }
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]; }
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]; }
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]; }
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]; }
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;
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) {
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];
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); }
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]);
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;
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] );
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] );
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] );
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];
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];
212 /// Scale by a scalar.
213 FGColumnVector3& operator*=(const double scalar) {
220 /// Scale by a 1/scalar.
221 FGColumnVector3& operator/=(const double scalar);
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;
229 /** Length of the vector.
230 Compute and return the euclidean norm of this vector. */
231 double Magnitude(void) const;
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;
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);
243 /** Dot product of two vectors
244 Compute and return the euclidean dot (or scalar) product of two vectors
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];
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.
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);
270 } // namespace JSBSim
272 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%