1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Header: FGColumnVector3.h
4 Author: Originally by Tony Peden [formatted and adapted here by Jon Berndt]
8 --------------------------------------------------------------------------------
9 ??/??/???? ?? Initial version and more.
10 03/06/2004 MF Rework, document and do much inlineing.
12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
16 #ifndef FGCOLUMNVECTOR3_H
17 #define FGCOLUMNVECTOR3_H
19 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
26 # include <simgear/compiler.h>
29 # include STL_IOSTREAM
31 SG_USING_STD(ostream);
32 SG_USING_STD(istream);
36 // SG_USING_STD(sqrt);
39 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
41 # include <iostream.h>
46 # if defined(sgi) && !defined(__GNUC__)
61 #include "FGJSBBase.h"
63 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
67 #define ID_COLUMNVECTOR3 "$Id$"
69 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
75 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
79 /** This class implements a 3 dimensional vector.
80 @author Jon S. Berndt, Tony Peden, et. al.
84 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
88 class FGColumnVector3 : public FGJSBBase
91 /** Default initializer.
95 FGColumnVector3(void);
97 /** Initialization by given values.
99 @param X value of the x-conponent.
100 @param Y value of the y-conponent.
101 @param Z value of the z-conponent.
103 Create a vector from the doubles given in the arguments.
105 FGColumnVector3(double X, double Y, double Z) {
112 /** Copy constructor.
114 @param v Vector which is used for initialization.
116 Create copy of the vector given in the argument.
118 FGColumnVector3(const FGColumnVector3& v) {
127 ~FGColumnVector3(void) { Debug(1); }
130 /** Read access the entries of the vector.
132 @param idx the component index.
134 Return the value of the matrix entry at the given index.
135 Indices are counted starting with 1.
137 Note that the index given in the argument is unchecked.
139 double operator()(unsigned int idx) const { return Entry(idx); }
141 /** Write access the entries of the vector.
143 @param idx the component index.
145 Return a reference to the vector entry at the given index.
146 Indices are counted starting with 1.
148 Note that the index given in the argument is unchecked.
150 double& operator()(unsigned int idx) { return Entry(idx); }
152 /** Read access the entries of the vector.
154 @param idx the component index.
156 Return the value of the matrix entry at the given index.
157 Indices are counted starting with 1.
159 This function is just a shortcut for the @ref double
160 operator()(unsigned int idx) const function. It is
161 used internally to access the elements in a more convenient way.
163 Note that the index given in the argument is unchecked.
165 double Entry(unsigned int idx) const { return data[idx-1]; }
167 /** Write access the entries of the vector.
169 @param idx the component index.
171 Return a reference to the vector entry at the given index.
172 Indices are counted starting with 1.
174 This function is just a shortcut for the @ref double&
175 operator()(unsigned int idx) function. It is
176 used internally to access the elements in a more convenient way.
178 Note that the index given in the argument is unchecked.
180 double& Entry(unsigned int idx) { return data[idx-1]; }
182 /** Assignment operator.
184 @param b source vector.
186 Copy the content of the vector given in the argument into *this.
188 FGColumnVector3& operator=(const FGColumnVector3& b) {
195 /** Comparison operator.
197 @param b other vector.
199 Returns true if both vectors are exactly the same.
201 bool operator==(const FGColumnVector3& b) const {
202 return data[0] == b.data[0] && data[1] == b.data[1] && data[2] == b.data[2];
205 /** Comparison operator.
207 @param b other vector.
209 Returns false if both vectors are exactly the same.
211 bool operator!=(const FGColumnVector3& b) const { return ! operator==(b); }
213 /** Multiplication by a scalar.
215 @param scalar scalar value to multiply the vector with.
216 @return The resulting vector from the multiplication with that scalar.
218 Multiply the vector with the scalar given in the argument.
220 FGColumnVector3 operator*(const double scalar) const {
221 return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
224 /** Multiply by 1/scalar.
226 @param scalar scalar value to devide the vector through.
227 @return The resulting vector from the division through that scalar.
229 Multiply the vector with the 1/scalar given in the argument.
231 FGColumnVector3 operator/(const double scalar) const;
233 /** Cross product multiplication.
235 @param v vector to multiply with.
236 @return The resulting vector from the cross product multiplication.
238 Compute and return the cross product of the current vector with
241 FGColumnVector3 operator*(const FGColumnVector3& V) const {
242 return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
243 Entry(3) * V(1) - Entry(1) * V(3),
244 Entry(1) * V(2) - Entry(2) * V(1) );
247 /** Addition operator.
249 FGColumnVector3 operator+(const FGColumnVector3& B) const {
250 return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
253 /** Subtraction operator.
255 FGColumnVector3 operator-(const FGColumnVector3& B) const {
256 return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
259 /** Subtract an other vector.
261 FGColumnVector3& operator-=(const FGColumnVector3 &B) {
268 /** Add an other vector.
270 FGColumnVector3& operator+=(const FGColumnVector3 &B) {
277 /** Scale by a scalar.
279 FGColumnVector3& operator*=(const double scalar) {
286 /** Scale by a 1/scalar.
288 FGColumnVector3& operator/=(const double scalar);
290 void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
291 void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
292 void InitMatrix(double a, double b, double c) {
293 data[0]=a; data[1]=b; data[2]=c;
296 /** Length of the vector.
298 Compute and return the euclidean norm of this vector.
300 double Magnitude(void) const;
304 Normalize the vector to have the Magnitude() == 1.0. If the vector
305 is equal to zero it is left untouched.
307 FGColumnVector3& Normalize(void);
309 // ??? Is this something sensible ??
310 FGColumnVector3 multElementWise(const FGColumnVector3& V) const;
312 // little trick here.
314 AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
315 AssignRef operator<<(const double ff) {
317 return AssignRef(Ref, idx+1);
319 FGColumnVector3& Ref;
322 AssignRef operator<<(const double ff) {
324 return AssignRef(*this, 2);
330 void Debug(int from);
333 /** Scalar multiplication.
335 @param scalar scalar value to multiply with.
336 @param A Vector to multiply.
338 Multiply the Vector with a scalar value.
340 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
341 // use already defined operation.
345 /** Write vector to a stream.
347 @param os Stream to write to.
348 @param M Matrix to write.
350 Write the matrix to a stream.
352 ostream& operator<<(ostream& os, const FGColumnVector3& col);
354 } // namespace JSBSim
356 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%