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.
92 Create a zero vector. */
93 FGColumnVector3(void);
95 /** Initialization by given values.
96 @param X value of the x-conponent.
97 @param Y value of the y-conponent.
98 @param Z value of the z-conponent.
99 Create a vector from the doubles given in the arguments. */
100 FGColumnVector3(double X, double Y, double Z) {
107 /** Copy constructor.
108 @param v Vector which is used for initialization.
109 Create copy of the vector given in the argument. */
110 FGColumnVector3(const FGColumnVector3& v) {
118 ~FGColumnVector3(void) { Debug(1); }
120 /** Read access the entries of the vector.
121 @param idx the component index.
122 Return the value of the matrix entry at the given index.
123 Indices are counted starting with 1.
124 Note that the index given in the argument is unchecked. */
125 double operator()(unsigned int idx) const { return Entry(idx); }
127 /** Write access the entries of the vector.
128 @param idx the component index.
129 Return a reference to the vector entry at the given index.
130 Indices are counted starting with 1.
131 Note that the index given in the argument is unchecked. */
132 double& operator()(unsigned int idx) { return Entry(idx); }
134 /** Read access the entries of the vector.
135 @param idx the component index.
136 Return the value of the matrix entry at the given index.
137 Indices are counted starting with 1.
138 This function is just a shortcut for the @ref double
139 operator()(unsigned int idx) const function. It is
140 used internally to access the elements in a more convenient way.
141 Note that the index given in the argument is unchecked. */
142 double Entry(unsigned int idx) const { return data[idx-1]; }
144 /** Write access the entries of the vector.
145 @param idx the component index.
146 Return a reference to the vector entry at the given index.
147 Indices are counted starting with 1.
148 This function is just a shortcut for the @ref double&
149 operator()(unsigned int idx) function. It is
150 used internally to access the elements in a more convenient way.
151 Note that the index given in the argument is unchecked. */
152 double& Entry(unsigned int idx) { return data[idx-1]; }
154 /** Prints the contents of the vector
155 @param delimeter the item separator (tab or comma)
156 @return a string with the delimeter-separated contents of the vector */
157 string Dump(string delimeter) const;
159 /** Assignment operator.
160 @param b source vector.
161 Copy the content of the vector given in the argument into *this. */
162 FGColumnVector3& operator=(const FGColumnVector3& b) {
169 /** Comparison operator.
170 @param b other vector.
171 Returns true if both vectors are exactly the same. */
172 bool operator==(const FGColumnVector3& b) const {
173 return data[0] == b.data[0] && data[1] == b.data[1] && data[2] == b.data[2];
176 /** Comparison operator.
177 @param b other vector.
178 Returns false if both vectors are exactly the same. */
179 bool operator!=(const FGColumnVector3& b) const { return ! operator==(b); }
181 /** Multiplication by a scalar.
182 @param scalar scalar value to multiply the vector with.
183 @return The resulting vector from the multiplication with that scalar.
184 Multiply the vector with the scalar given in the argument. */
185 FGColumnVector3 operator*(const double scalar) const {
186 return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
189 /** Multiply by 1/scalar.
190 @param scalar scalar value to devide the vector through.
191 @return The resulting vector from the division through that scalar.
192 Multiply the vector with the 1/scalar given in the argument. */
193 FGColumnVector3 operator/(const double scalar) const;
195 /** Cross product multiplication.
196 @param v vector to multiply with.
197 @return The resulting vector from the cross product multiplication.
198 Compute and return the cross product of the current vector with
199 the given argument. */
200 FGColumnVector3 operator*(const FGColumnVector3& V) const {
201 return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
202 Entry(3) * V(1) - Entry(1) * V(3),
203 Entry(1) * V(2) - Entry(2) * V(1) );
206 /// Addition operator.
207 FGColumnVector3 operator+(const FGColumnVector3& B) const {
208 return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
211 /// Subtraction operator.
212 FGColumnVector3 operator-(const FGColumnVector3& B) const {
213 return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
216 /// Subtract an other vector.
217 FGColumnVector3& operator-=(const FGColumnVector3 &B) {
224 /// Add an other vector.
225 FGColumnVector3& operator+=(const FGColumnVector3 &B) {
232 /// Scale by a scalar.
233 FGColumnVector3& operator*=(const double scalar) {
240 /// Scale by a 1/scalar.
241 FGColumnVector3& operator/=(const double scalar);
243 void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
244 void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
245 void InitMatrix(double a, double b, double c) {
246 data[0]=a; data[1]=b; data[2]=c;
249 /** Length of the vector.
250 Compute and return the euclidean norm of this vector. */
251 double Magnitude(void) const;
253 /** Length of the vector in a coordinate axis plane.
254 Compute and return the euclidean norm of this vector projected into
255 the coordinate axis plane idx1-idx2. */
256 double Magnitude(int idx1, int idx2) const {
257 return sqrt( Entry(idx1)*Entry(idx1) + Entry(idx2)*Entry(idx2) );
261 Normalize the vector to have the Magnitude() == 1.0. If the vector
262 is equal to zero it is left untouched. */
263 FGColumnVector3& Normalize(void);
265 // ??? Is this something sensible ??
266 FGColumnVector3 multElementWise(const FGColumnVector3& V) const;
268 // little trick here.
270 AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
271 AssignRef operator<<(const double ff) {
273 return AssignRef(Ref, idx+1);
275 FGColumnVector3& Ref;
278 AssignRef operator<<(const double ff) {
280 return AssignRef(*this, 2);
286 void Debug(int from);
289 /** Scalar multiplication.
290 @param scalar scalar value to multiply with.
291 @param A Vector to multiply.
292 Multiply the Vector with a scalar value.*/
293 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
294 // use already defined operation.
298 /** Write vector to a stream.
299 @param os Stream to write to.
300 @param M Matrix to write.
301 Write the matrix to a stream.*/
302 ostream& operator<<(ostream& os, const FGColumnVector3& col);
304 } // namespace JSBSim
306 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%