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);
38 # if defined(sgi) && !defined(__GNUC__) && (_COMPILER_VERSION < 740)
40 # include <iostream.h>
45 # if defined(sgi) && !defined(__GNUC__)
55 # if !(defined(_MSC_VER) && _MSC_VER <= 1200)
62 #include "FGJSBBase.h"
64 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
68 #define ID_COLUMNVECTOR3 "$Id$"
70 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
76 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
80 /** This class implements a 3 dimensional vector.
81 @author Jon S. Berndt, Tony Peden, et. al.
85 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
89 class FGColumnVector3 : public FGJSBBase
92 /** Default initializer.
93 Create a zero vector. */
94 FGColumnVector3(void);
96 /** Initialization by given values.
97 @param X value of the x-conponent.
98 @param Y value of the y-conponent.
99 @param Z value of the z-conponent.
100 Create a vector from the doubles given in the arguments. */
101 FGColumnVector3(double X, double Y, double Z) {
108 /** Copy constructor.
109 @param v Vector which is used for initialization.
110 Create copy of the vector given in the argument. */
111 FGColumnVector3(const FGColumnVector3& v) {
119 ~FGColumnVector3(void) { Debug(1); }
121 /** Read access the entries of the vector.
122 @param idx the component index.
123 Return the value of the matrix entry at the given index.
124 Indices are counted starting with 1.
125 Note that the index given in the argument is unchecked. */
126 double operator()(unsigned int idx) const { return Entry(idx); }
128 /** Write access the entries of the vector.
129 @param idx the component index.
130 Return a reference to the vector entry at the given index.
131 Indices are counted starting with 1.
132 Note that the index given in the argument is unchecked. */
133 double& operator()(unsigned int idx) { return Entry(idx); }
135 /** Read access the entries of the vector.
136 @param idx the component index.
137 Return the value of the matrix entry at the given index.
138 Indices are counted starting with 1.
139 This function is just a shortcut for the @ref double
140 operator()(unsigned int idx) const function. It is
141 used internally to access the elements in a more convenient way.
142 Note that the index given in the argument is unchecked. */
143 double Entry(unsigned int idx) const { return data[idx-1]; }
145 /** Write access the entries of the vector.
146 @param idx the component index.
147 Return a reference to the vector entry at the given index.
148 Indices are counted starting with 1.
149 This function is just a shortcut for the @ref double&
150 operator()(unsigned int idx) function. It is
151 used internally to access the elements in a more convenient way.
152 Note that the index given in the argument is unchecked. */
153 double& Entry(unsigned int idx) { return data[idx-1]; }
155 /** Prints the contents of the vector
156 @param delimeter the item separator (tab or comma)
157 @return a string with the delimeter-separated contents of the vector */
158 string Dump(string delimeter) const;
160 /** Assignment operator.
161 @param b source vector.
162 Copy the content of the vector given in the argument into *this. */
163 FGColumnVector3& operator=(const FGColumnVector3& b) {
170 /** Comparison operator.
171 @param b other vector.
172 Returns true if both vectors are exactly the same. */
173 bool operator==(const FGColumnVector3& b) const {
174 return data[0] == b.data[0] && data[1] == b.data[1] && data[2] == b.data[2];
177 /** Comparison operator.
178 @param b other vector.
179 Returns false if both vectors are exactly the same. */
180 bool operator!=(const FGColumnVector3& b) const { return ! operator==(b); }
182 /** Multiplication by a scalar.
183 @param scalar scalar value to multiply the vector with.
184 @return The resulting vector from the multiplication with that scalar.
185 Multiply the vector with the scalar given in the argument. */
186 FGColumnVector3 operator*(const double scalar) const {
187 return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
190 /** Multiply by 1/scalar.
191 @param scalar scalar value to devide the vector through.
192 @return The resulting vector from the division through that scalar.
193 Multiply the vector with the 1/scalar given in the argument. */
194 FGColumnVector3 operator/(const double scalar) const;
196 /** Cross product multiplication.
197 @param v vector to multiply with.
198 @return The resulting vector from the cross product multiplication.
199 Compute and return the cross product of the current vector with
200 the given argument. */
201 FGColumnVector3 operator*(const FGColumnVector3& V) const {
202 return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
203 Entry(3) * V(1) - Entry(1) * V(3),
204 Entry(1) * V(2) - Entry(2) * V(1) );
207 /// Addition operator.
208 FGColumnVector3 operator+(const FGColumnVector3& B) const {
209 return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
212 /// Subtraction operator.
213 FGColumnVector3 operator-(const FGColumnVector3& B) const {
214 return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
217 /// Subtract an other vector.
218 FGColumnVector3& operator-=(const FGColumnVector3 &B) {
225 /// Add an other vector.
226 FGColumnVector3& operator+=(const FGColumnVector3 &B) {
233 /// Scale by a scalar.
234 FGColumnVector3& operator*=(const double scalar) {
241 /// Scale by a 1/scalar.
242 FGColumnVector3& operator/=(const double scalar);
244 void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
245 void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
246 void InitMatrix(double a, double b, double c) {
247 data[0]=a; data[1]=b; data[2]=c;
250 /** Length of the vector.
251 Compute and return the euclidean norm of this vector. */
252 double Magnitude(void) const;
254 /** Length of the vector in a coordinate axis plane.
255 Compute and return the euclidean norm of this vector projected into
256 the coordinate axis plane idx1-idx2. */
257 double Magnitude(int idx1, int idx2) const {
258 return sqrt( Entry(idx1)*Entry(idx1) + Entry(idx2)*Entry(idx2) );
262 Normalize the vector to have the Magnitude() == 1.0. If the vector
263 is equal to zero it is left untouched. */
264 FGColumnVector3& Normalize(void);
266 // ??? Is this something sensible ??
267 FGColumnVector3 multElementWise(const FGColumnVector3& V) const;
269 // little trick here.
271 AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
272 AssignRef operator<<(const double ff) {
274 return AssignRef(Ref, idx+1);
276 FGColumnVector3& Ref;
279 AssignRef operator<<(const double ff) {
281 return AssignRef(*this, 2);
287 void Debug(int from);
290 /** Scalar multiplication.
291 @param scalar scalar value to multiply with.
292 @param A Vector to multiply.
293 Multiply the Vector with a scalar value.*/
294 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
295 // use already defined operation.
299 /** Write vector to a stream.
300 @param os Stream to write to.
301 @param M Matrix to write.
302 Write the matrix to a stream.*/
303 ostream& operator<<(ostream& os, const FGColumnVector3& col);
305 } // namespace JSBSim
307 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%