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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
45 # include <simgear/compiler.h>
48 # include STL_IOSTREAM
50 SG_USING_STD(ostream);
51 SG_USING_STD(istream);
57 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
59 # include <iostream.h>
64 # if defined(sgi) && !defined(__GNUC__)
74 # if !(defined(_MSC_VER) && _MSC_VER <= 1200)
81 #include "FGJSBBase.h"
83 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
87 #define ID_COLUMNVECTOR3 "$Id$"
89 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
95 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
99 /** This class implements a 3 dimensional vector.
100 @author Jon S. Berndt, Tony Peden, et. al.
104 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
108 class FGColumnVector3 : public FGJSBBase
111 /** Default initializer.
112 Create a zero vector. */
113 FGColumnVector3(void);
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) {
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) {
138 ~FGColumnVector3(void) { Debug(1); }
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); }
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); }
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]; }
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]; }
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;
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) {
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];
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); }
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));
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;
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) );
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) );
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) );
236 /// Subtract an other vector.
237 FGColumnVector3& operator-=(const FGColumnVector3 &B) {
244 /// Add an other vector.
245 FGColumnVector3& operator+=(const FGColumnVector3 &B) {
252 /// Scale by a scalar.
253 FGColumnVector3& operator*=(const double scalar) {
260 /// Scale by a 1/scalar.
261 FGColumnVector3& operator/=(const double scalar);
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;
269 /** Length of the vector.
270 Compute and return the euclidean norm of this vector. */
271 double Magnitude(void) const;
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) );
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);
285 // little trick here.
287 AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
288 AssignRef operator<<(const double ff) {
290 return AssignRef(Ref, idx+1);
292 FGColumnVector3& Ref;
295 AssignRef operator<<(const double ff) {
297 return AssignRef(*this, 2);
303 void Debug(int from);
306 /** Scalar multiplication.
307 @param scalar scalar value to multiply with.
308 @param A Vector to multiply.
309 Multiply the Vector with a scalar value.*/
310 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
311 // use already defined operation.
315 /** Write vector to a stream.
316 @param os Stream to write to.
317 @param M Matrix to write.
318 Write the matrix to a stream.*/
319 ostream& operator<<(ostream& os, const FGColumnVector3& col);
321 } // namespace JSBSim
323 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%