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 /** Multiplication by a scalar.
197 @param scalar scalar value to multiply the vector with.
198 @return The resulting vector from the multiplication with that scalar.
200 Multiply the vector with the scalar given in the argument.
202 FGColumnVector3 operator*(const double scalar) const {
203 return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
206 /** Multiply by 1/scalar.
208 @param scalar scalar value to devide the vector through.
209 @return The resulting vector from the division through that scalar.
211 Multiply the vector with the 1/scalar given in the argument.
213 FGColumnVector3 operator/(const double scalar) const;
215 /** Cross product multiplication.
217 @param v vector to multiply with.
218 @return The resulting vector from the cross product multiplication.
220 Compute and return the cross product of the current vector with
223 FGColumnVector3 operator*(const FGColumnVector3& V) const {
224 return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
225 Entry(3) * V(1) - Entry(1) * V(3),
226 Entry(1) * V(2) - Entry(2) * V(1) );
229 /** Addition operator.
231 FGColumnVector3 operator+(const FGColumnVector3& B) const {
232 return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
235 /** Subtraction operator.
237 FGColumnVector3 operator-(const FGColumnVector3& B) const {
238 return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
241 /** Subtract an other vector.
243 FGColumnVector3& operator-=(const FGColumnVector3 &B) {
250 /** Add an other vector.
252 FGColumnVector3& operator+=(const FGColumnVector3 &B) {
259 /** Scale by a scalar.
261 FGColumnVector3& operator*=(const double scalar) {
268 /** Scale by a 1/scalar.
270 FGColumnVector3& operator/=(const double scalar);
272 void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
273 void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
274 void InitMatrix(double a, double b, double c) {
275 data[0]=a; data[1]=b; data[2]=c;
278 /** Length of the vector.
280 Compute and return the euclidean norm of this vector.
282 double Magnitude(void) const;
286 Normalize the vector to have the Magnitude() == 1.0. If the vector
287 is equal to zero it is left untouched.
289 FGColumnVector3& Normalize(void);
291 // ??? Is this something sensible ??
292 FGColumnVector3 multElementWise(const FGColumnVector3& V) const;
294 // little trick here.
296 AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
297 AssignRef operator<<(const double ff) {
299 return AssignRef(Ref, idx+1);
301 FGColumnVector3& Ref;
304 AssignRef operator<<(const double ff) {
306 return AssignRef(*this, 2);
312 void Debug(int from);
315 /** Scalar multiplication.
317 @param scalar scalar value to multiply with.
318 @param A Vector to multiply.
320 Multiply the Vector with a scalar value.
322 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
323 // use already defined operation.
327 /** Write vector to a stream.
329 @param os Stream to write to.
330 @param M Matrix to write.
332 Write the matrix to a stream.
334 ostream& operator<<(ostream& os, const FGColumnVector3& col);
336 } // namespace JSBSim
338 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%