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 (jsb@hal-pc.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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
56 #include "FGJSBBase.h"
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62 #define ID_COLUMNVECTOR3 "$Id$"
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
74 /** This class implements a 3 element column vector.
75 @author Jon S. Berndt, Tony Peden, et. al.
79 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
83 class FGColumnVector3 : public FGJSBBase
86 /** Default initializer.
87 Create a zero vector. */
88 FGColumnVector3(void);
90 /** Initialization by given values.
91 @param X value of the x-conponent.
92 @param Y value of the y-conponent.
93 @param Z value of the z-conponent.
94 Create a vector from the doubles given in the arguments. */
95 FGColumnVector3(double X, double Y, double Z) {
102 /** Copy constructor.
103 @param v Vector which is used for initialization.
104 Create copy of the vector given in the argument. */
105 FGColumnVector3(const FGColumnVector3& v) {
113 ~FGColumnVector3(void) { Debug(1); }
115 /** Read access the entries of the vector.
116 @param idx the component index.
117 Return the value of the matrix entry at the given index.
118 Indices are counted starting with 1.
119 Note that the index given in the argument is unchecked. */
120 double operator()(unsigned int idx) const { return Entry(idx); }
122 /** Write access the entries of the vector.
123 @param idx the component index.
124 Return a reference to the vector entry at the given index.
125 Indices are counted starting with 1.
126 Note that the index given in the argument is unchecked. */
127 double& operator()(unsigned int idx) { return Entry(idx); }
129 /** Read access the entries of the vector.
130 @param idx the component index.
131 Return the value of the matrix entry at the given index.
132 Indices are counted starting with 1.
133 This function is just a shortcut for the <tt>double
134 operator()(unsigned int idx) const</tt> function. It is
135 used internally to access the elements in a more convenient way.
136 Note that the index given in the argument is unchecked. */
137 double Entry(unsigned int idx) const { return data[idx-1]; }
139 /** Write access the entries of the vector.
140 @param idx the component index.
141 Return a reference to the vector entry at the given index.
142 Indices are counted starting with 1.
143 This function is just a shortcut for the <tt>double&
144 operator()(unsigned int idx)</tt> function. It is
145 used internally to access the elements in a more convenient way.
146 Note that the index given in the argument is unchecked. */
147 double& Entry(unsigned int idx) { return data[idx-1]; }
149 /** Prints the contents of the vector
150 @param delimeter the item separator (tab or comma)
151 @return a string with the delimeter-separated contents of the vector */
152 string Dump(string delimeter) const;
154 /** Assignment operator.
155 @param b source vector.
156 Copy the content of the vector given in the argument into *this. */
157 FGColumnVector3& operator=(const FGColumnVector3& b) {
164 /** Comparison operator.
165 @param b other vector.
166 Returns true if both vectors are exactly the same. */
167 bool operator==(const FGColumnVector3& b) const {
168 return data[0] == b.data[0] && data[1] == b.data[1] && data[2] == b.data[2];
171 /** Comparison operator.
172 @param b other vector.
173 Returns false if both vectors are exactly the same. */
174 bool operator!=(const FGColumnVector3& b) const { return ! operator==(b); }
176 /** Multiplication by a scalar.
177 @param scalar scalar value to multiply the vector with.
178 @return The resulting vector from the multiplication with that scalar.
179 Multiply the vector with the scalar given in the argument. */
180 FGColumnVector3 operator*(const double scalar) const {
181 return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
184 /** Multiply by 1/scalar.
185 @param scalar scalar value to devide the vector through.
186 @return The resulting vector from the division through that scalar.
187 Multiply the vector with the 1/scalar given in the argument. */
188 FGColumnVector3 operator/(const double scalar) const;
190 /** Cross product multiplication.
191 @param V vector to multiply with.
192 @return The resulting vector from the cross product multiplication.
193 Compute and return the cross product of the current vector with
194 the given argument. */
195 FGColumnVector3 operator*(const FGColumnVector3& V) const {
196 return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
197 Entry(3) * V(1) - Entry(1) * V(3),
198 Entry(1) * V(2) - Entry(2) * V(1) );
201 /// Addition operator.
202 FGColumnVector3 operator+(const FGColumnVector3& B) const {
203 return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
206 /// Subtraction operator.
207 FGColumnVector3 operator-(const FGColumnVector3& B) const {
208 return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
211 /// Subtract an other vector.
212 FGColumnVector3& operator-=(const FGColumnVector3 &B) {
219 /// Add an other vector.
220 FGColumnVector3& operator+=(const FGColumnVector3 &B) {
227 /// Scale by a scalar.
228 FGColumnVector3& operator*=(const double scalar) {
235 /// Scale by a 1/scalar.
236 FGColumnVector3& operator/=(const double scalar);
238 void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
239 void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
240 void InitMatrix(double a, double b, double c) {
241 data[0]=a; data[1]=b; data[2]=c;
244 /** Length of the vector.
245 Compute and return the euclidean norm of this vector. */
246 double Magnitude(void) const;
248 /** Length of the vector in a coordinate axis plane.
249 Compute and return the euclidean norm of this vector projected into
250 the coordinate axis plane idx1-idx2. */
251 double Magnitude(int idx1, int idx2) const {
252 return sqrt( Entry(idx1)*Entry(idx1) + Entry(idx2)*Entry(idx2) );
256 Normalize the vector to have the Magnitude() == 1.0. If the vector
257 is equal to zero it is left untouched. */
258 FGColumnVector3& Normalize(void);
260 // little trick here.
262 AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
263 AssignRef operator<<(const double ff) {
265 return AssignRef(Ref, idx+1);
267 FGColumnVector3& Ref;
270 AssignRef operator<<(const double ff) {
272 return AssignRef(*this, 2);
278 void Debug(int from);
281 /** Scalar multiplication.
282 @param scalar scalar value to multiply with.
283 @param A Vector to multiply.
284 Multiply the Vector with a scalar value.*/
285 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
286 // use already defined operation.
290 /** Write vector to a stream.
291 @param os Stream to write to.
292 @param M Matrix to write.
293 Write the matrix to a stream.*/
294 ostream& operator<<(ostream& os, const FGColumnVector3& col);
296 } // namespace JSBSim
298 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%