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 (jon@jsbsim.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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
44 #include "FGJSBBase.h"
46 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50 #define ID_COLUMNVECTOR3 "$Id$"
52 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
58 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
62 /** This class implements a 3 element column vector.
63 @author Jon S. Berndt, Tony Peden, et. al.
67 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
71 class FGColumnVector3 : public FGJSBBase
74 /** Default initializer.
75 Create a zero vector. */
76 FGColumnVector3(void);
78 /** Initialization by given values.
79 @param X value of the x-conponent.
80 @param Y value of the y-conponent.
81 @param Z value of the z-conponent.
82 Create a vector from the doubles given in the arguments. */
83 FGColumnVector3(double X, double Y, double Z) {
91 @param v Vector which is used for initialization.
92 Create copy of the vector given in the argument. */
93 FGColumnVector3(const FGColumnVector3& v) {
101 ~FGColumnVector3(void) { Debug(1); }
103 /** Read access the entries of the vector.
104 @param idx the component index.
105 Return the value of the matrix entry at the given index.
106 Indices are counted starting with 1.
107 Note that the index given in the argument is unchecked. */
108 double operator()(unsigned int idx) const { return Entry(idx); }
110 /** Write access the entries of the vector.
111 @param idx the component index.
112 Return a reference to the vector entry at the given index.
113 Indices are counted starting with 1.
114 Note that the index given in the argument is unchecked. */
115 double& operator()(unsigned int idx) { return Entry(idx); }
117 /** Read access the entries of the vector.
118 @param idx the component index.
119 Return the value of the matrix entry at the given index.
120 Indices are counted starting with 1.
121 This function is just a shortcut for the <tt>double
122 operator()(unsigned int idx) const</tt> function. It is
123 used internally to access the elements in a more convenient way.
124 Note that the index given in the argument is unchecked. */
125 double Entry(unsigned int idx) const { return data[idx-1]; }
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 This function is just a shortcut for the <tt>double&
132 operator()(unsigned int idx)</tt> function. It is
133 used internally to access the elements in a more convenient way.
134 Note that the index given in the argument is unchecked. */
135 double& Entry(unsigned int idx) { return data[idx-1]; }
137 /** Prints the contents of the vector
138 @param delimeter the item separator (tab or comma)
139 @return a string with the delimeter-separated contents of the vector */
140 std::string Dump(const std::string& delimeter) const;
142 /** Assignment operator.
143 @param b source vector.
144 Copy the content of the vector given in the argument into *this. */
145 FGColumnVector3& operator=(const FGColumnVector3& b) {
152 /** Comparison operator.
153 @param b other vector.
154 Returns true if both vectors are exactly the same. */
155 bool operator==(const FGColumnVector3& b) const {
156 return data[0] == b.data[0] && data[1] == b.data[1] && data[2] == b.data[2];
159 /** Comparison operator.
160 @param b other vector.
161 Returns false if both vectors are exactly the same. */
162 bool operator!=(const FGColumnVector3& b) const { return ! operator==(b); }
164 /** Multiplication by a scalar.
165 @param scalar scalar value to multiply the vector with.
166 @return The resulting vector from the multiplication with that scalar.
167 Multiply the vector with the scalar given in the argument. */
168 FGColumnVector3 operator*(const double scalar) const {
169 return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
172 /** Multiply by 1/scalar.
173 @param scalar scalar value to devide the vector through.
174 @return The resulting vector from the division through that scalar.
175 Multiply the vector with the 1/scalar given in the argument. */
176 FGColumnVector3 operator/(const double scalar) const;
178 /** Cross product multiplication.
179 @param V vector to multiply with.
180 @return The resulting vector from the cross product multiplication.
181 Compute and return the cross product of the current vector with
182 the given argument. */
183 FGColumnVector3 operator*(const FGColumnVector3& V) const {
184 return FGColumnVector3( Entry(2) * V(3) - Entry(3) * V(2),
185 Entry(3) * V(1) - Entry(1) * V(3),
186 Entry(1) * V(2) - Entry(2) * V(1) );
189 /// Addition operator.
190 FGColumnVector3 operator+(const FGColumnVector3& B) const {
191 return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
194 /// Subtraction operator.
195 FGColumnVector3 operator-(const FGColumnVector3& B) const {
196 return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
199 /// Subtract an other vector.
200 FGColumnVector3& operator-=(const FGColumnVector3 &B) {
207 /// Add an other vector.
208 FGColumnVector3& operator+=(const FGColumnVector3 &B) {
215 /// Scale by a scalar.
216 FGColumnVector3& operator*=(const double scalar) {
223 /// Scale by a 1/scalar.
224 FGColumnVector3& operator/=(const double scalar);
226 void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
227 void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
228 void InitMatrix(double a, double b, double c) {
229 data[0]=a; data[1]=b; data[2]=c;
232 /** Length of the vector.
233 Compute and return the euclidean norm of this vector. */
234 double Magnitude(void) const;
236 /** Length of the vector in a coordinate axis plane.
237 Compute and return the euclidean norm of this vector projected into
238 the coordinate axis plane idx1-idx2. */
239 double Magnitude(int idx1, int idx2) const {
240 return sqrt( Entry(idx1)*Entry(idx1) + Entry(idx2)*Entry(idx2) );
244 Normalize the vector to have the Magnitude() == 1.0. If the vector
245 is equal to zero it is left untouched. */
246 FGColumnVector3& Normalize(void);
248 // little trick here.
250 AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
251 AssignRef operator<<(const double ff) {
253 return AssignRef(Ref, idx+1);
255 FGColumnVector3& Ref;
258 AssignRef operator<<(const double ff) {
260 return AssignRef(*this, 2);
266 void Debug(int from);
269 /** Scalar multiplication.
270 @param scalar scalar value to multiply with.
271 @param A Vector to multiply.
272 Multiply the Vector with a scalar value.*/
273 inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
274 // use already defined operation.
278 /** Write vector to a stream.
279 @param os Stream to write to.
280 @param M Matrix to write.
281 Write the matrix to a stream.*/
282 std::ostream& operator<<(std::ostream& os, const FGColumnVector3& col);
284 } // namespace JSBSim
286 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%