Branch data Line data Source code
1 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 : :
3 : : Header: FGColumnVector3.h
4 : : Author: Originally by Tony Peden [formatted and adapted here by Jon Berndt]
5 : : Date started: Unknown
6 : :
7 : : ------------- Copyright (C) 2001 by Tony Peden and Jon S. Berndt (jon@jsbsim.org)
8 : :
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
12 : : version.
13 : :
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
17 : : details.
18 : :
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.
22 : :
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.
25 : :
26 : : HISTORY
27 : : --------------------------------------------------------------------------------
28 : : ??/??/???? ?? Initial version and more.
29 : : 03/06/2004 MF Rework, document and do much inlineing.
30 : :
31 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32 : : SENTRY
33 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
34 : :
35 : : #ifndef FGCOLUMNVECTOR3_H
36 : : #define FGCOLUMNVECTOR3_H
37 : :
38 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 : : INCLUDES
40 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
41 : :
42 : : #include <iosfwd>
43 : : #include <string>
44 : : #include "FGJSBBase.h"
45 : :
46 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 : : DEFINITIONS
48 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49 : :
50 : : #define ID_COLUMNVECTOR3 "$Id: FGColumnVector3.h,v 1.13 2010/07/25 15:25:05 jberndt Exp $"
51 : :
52 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53 : : FORWARD DECLARATIONS
54 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
55 : :
56 : : namespace JSBSim {
57 : :
58 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 : : CLASS DOCUMENTATION
60 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
61 : :
62 : : /** This class implements a 3 element column vector.
63 : : @author Jon S. Berndt, Tony Peden, et. al.
64 : : @version $Id: FGColumnVector3.h,v 1.13 2010/07/25 15:25:05 jberndt Exp $
65 : : */
66 : :
67 : : /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 : : CLASS DECLARATION
69 : : %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70 : :
71 : : class FGColumnVector3 : public FGJSBBase
72 : : {
73 : : public:
74 : : /** Default initializer.
75 : : Create a zero vector. */
76 : : FGColumnVector3(void);
77 : :
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 : 11037032 : FGColumnVector3(double X, double Y, double Z) {
84 : 11037032 : data[0] = X;
85 : 11037032 : data[1] = Y;
86 : 7742664 : data[2] = Z;
87 : : // Debug(0);
88 : : }
89 : :
90 : : /** Copy constructor.
91 : : @param v Vector which is used for initialization.
92 : : Create copy of the vector given in the argument. */
93 : 664008 : FGColumnVector3(const FGColumnVector3& v) {
94 : 664008 : data[0] = v.data[0];
95 : 664008 : data[1] = v.data[1];
96 : 664008 : data[2] = v.data[2];
97 : : // Debug(0);
98 : : }
99 : :
100 : : /// Destructor.
101 [ # # ][ # # ]: 2057779 : ~FGColumnVector3(void) { /* Debug(1); */ }
[ # # ][ # # ]
[ # # ][ + + ]
[ + + ][ # # ]
[ - + ][ # # ]
102 : :
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 : 43131863 : double operator()(unsigned int idx) const { return data[idx-1]; }
109 : :
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 : 32251664 : double& operator()(unsigned int idx) { return data[idx-1]; }
116 : :
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]; }
126 : :
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]; }
136 : :
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;
141 : :
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) {
146 : 3456079 : data[0] = b.data[0];
147 : 3456079 : data[1] = b.data[1];
148 : 3456079 : data[2] = b.data[2];
149 : 2645969 : return *this;
150 : : }
151 : :
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];
157 : : }
158 : :
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); }
163 : :
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 : 3584034 : return FGColumnVector3(scalar*data[0], scalar*data[1], scalar*data[2]);
170 : : }
171 : :
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;
177 : :
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 : 270030 : FGColumnVector3 operator*(const FGColumnVector3& V) const {
184 : : return FGColumnVector3( data[1] * V(3) - data[2] * V(2),
185 : : data[2] * V(1) - data[0] * V(3),
186 : 7919828 : data[0] * V(2) - data[1] * V(1) );
187 : : }
188 : :
189 : : /// Addition operator.
190 : : FGColumnVector3 operator+(const FGColumnVector3& B) const {
191 : 6261076 : return FGColumnVector3( data[0] + B(1), data[1] + B(2), data[2] + B(3) );
192 : : }
193 : :
194 : : /// Subtraction operator.
195 : : FGColumnVector3 operator-(const FGColumnVector3& B) const {
196 : 3186358 : return FGColumnVector3( data[0] - B(1), data[1] - B(2), data[2] - B(3) );
197 : : }
198 : :
199 : : /// Subtract an other vector.
200 : : FGColumnVector3& operator-=(const FGColumnVector3 &B) {
201 : 216024 : data[0] -= B(1);
202 : 216024 : data[1] -= B(2);
203 : 216024 : data[2] -= B(3);
204 : 108012 : return *this;
205 : : }
206 : :
207 : : /// Add an other vector.
208 : : FGColumnVector3& operator+=(const FGColumnVector3 &B) {
209 : 6658560 : data[0] += B(1);
210 : 6658560 : data[1] += B(2);
211 : 6658560 : data[2] += B(3);
212 : 2908872 : return *this;
213 : : }
214 : :
215 : : /// Scale by a scalar.
216 : : FGColumnVector3& operator*=(const double scalar) {
217 : 107111 : data[0] *= scalar;
218 : 107111 : data[1] *= scalar;
219 : 107111 : data[2] *= scalar;
220 : 107111 : return *this;
221 : : }
222 : :
223 : : /// Scale by a 1/scalar.
224 : : FGColumnVector3& operator/=(const double scalar);
225 : :
226 : 648100 : void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; }
227 : 6 : void InitMatrix(double a) { data[0] = data[1] = data[2] = a; }
228 : : void InitMatrix(double a, double b, double c) {
229 : 0 : data[0]=a; data[1]=b; data[2]=c;
230 : : }
231 : :
232 : : /** Length of the vector.
233 : : Compute and return the euclidean norm of this vector. */
234 : : double Magnitude(void) const;
235 : :
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 : 0 : return sqrt( data[idx1-1]*data[idx1-1] + data[idx2-1]*data[idx2-1] );
241 : : }
242 : :
243 : : /** Normalize.
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);
247 : :
248 : : /** Dot product of two vectors
249 : : Compute and return the euclidean dot (or scalar) product of two vectors
250 : : v1 and v2 */
251 : : friend inline double DotProduct(const FGColumnVector3& v1, const FGColumnVector3& v2) {
252 : 0 : return v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3);
253 : : }
254 : :
255 : : private:
256 : : double data[3];
257 : :
258 : : void Debug(int from);
259 : : };
260 : :
261 : : /** Scalar multiplication.
262 : : @param scalar scalar value to multiply with.
263 : : @param A Vector to multiply.
264 : : Multiply the Vector with a scalar value.*/
265 : : inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
266 : : // use already defined operation.
267 : 3314007 : return A*scalar;
268 : : }
269 : :
270 : : /** Write vector to a stream.
271 : : @param os Stream to write to.
272 : : @param M Matrix to write.
273 : : Write the matrix to a stream.*/
274 : : std::ostream& operator<<(std::ostream& os, const FGColumnVector3& col);
275 : :
276 : : } // namespace JSBSim
277 : :
278 : : //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 : : #endif
|