| 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 ([email protected]) |
| 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 |
| 11 | Software Foundation; either version 2 of the License, or (at your option) any |
| 12 | later 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 |
| 20 | with this program; if not, write to the Free Software Foundation, Inc., 59 |
| 21 | Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | Further information about the GNU Lesser General Public License can also be |
| 24 | found on 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 | |
| 45 | #include "JSBSim_API.h" |
| 46 | |
| 47 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 48 | FORWARD DECLARATIONS |
| 49 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 50 | |
| 51 | namespace JSBSim { |
| 52 | |
| 53 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 54 | CLASS DOCUMENTATION |
| 55 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 56 | |
| 57 | /** This class implements a 3 element column vector. |
| 58 | @author Jon S. Berndt, Tony Peden, et. al. |
| 59 | */ |
| 60 | |
| 61 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 62 | CLASS DECLARATION |
| 63 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 64 | |
| 65 | class JSBSIM_API FGColumnVector3 |
| 66 | { |
| 67 | public: |
| 68 | /** Default initializer. |
| 69 | Create a zero vector. */ |
| 70 | FGColumnVector3(void); |
| 71 | |
| 72 | /** Initialization by given values. |
| 73 | @param X value of the x-conponent. |
| 74 | @param Y value of the y-conponent. |
| 75 | @param Z value of the z-conponent. |
| 76 | Create a vector from the doubles given in the arguments. */ |
| 77 | FGColumnVector3(const double X, const double Y, const double Z) { |
| 78 | data[0] = X; |
| 79 | data[1] = Y; |
| 80 | data[2] = Z; |
| 81 | } |
| 82 | |
| 83 | /** Copy constructor. |
| 84 | @param v Vector which is used for initialization. |
| 85 | Create copy of the vector given in the argument. */ |
| 86 | FGColumnVector3(const FGColumnVector3& v) { |
| 87 | data[0] = v.data[0]; |
| 88 | data[1] = v.data[1]; |
| 89 | data[2] = v.data[2]; |
| 90 | } |
| 91 | |
| 92 | /// Destructor. |
| 93 | ~FGColumnVector3(void) { } |
| 94 | |
| 95 | /** Read access the entries of the vector. |
| 96 | @param idx the component index. |
| 97 | Return the value of the matrix entry at the given index. |
| 98 | Indices are counted starting with 1. |
| 99 | Note that the index given in the argument is unchecked. */ |
| 100 | double operator()(const unsigned int idx) const { return data[idx-1]; } |
| 101 | |
| 102 | /** Write access the entries of the vector. |
| 103 | @param idx the component index. |
| 104 | Return a reference to the vector entry at the given index. |
| 105 | Indices are counted starting with 1. |
| 106 | Note that the index given in the argument is unchecked. */ |
| 107 | double& operator()(const unsigned int idx) { return data[idx-1]; } |
| 108 | |
| 109 | /** Read access the entries of the vector. |
| 110 | @param idx the component index. |
| 111 | Return the value of the matrix entry at the given index. |
| 112 | Indices are counted starting with 1. |
| 113 | This function is just a shortcut for the <tt>double |
| 114 | operator()(unsigned int idx) const</tt> function. It is |
| 115 | used internally to access the elements in a more convenient way. |
| 116 | Note that the index given in the argument is unchecked. */ |
| 117 | double Entry(const unsigned int idx) const { return data[idx-1]; } |
| 118 | |
| 119 | /** Write access the entries of the vector. |
| 120 | @param idx the component index. |
| 121 | Return a reference to the vector entry at the given index. |
| 122 | Indices are counted starting with 1. |
| 123 | This function is just a shortcut for the <tt>double& |
| 124 | operator()(unsigned int idx)</tt> function. It is |
| 125 | used internally to access the elements in a more convenient way. |
| 126 | Note that the index given in the argument is unchecked. */ |
| 127 | double& Entry(const unsigned int idx) { return data[idx-1]; } |
| 128 | |
| 129 | /** Prints the contents of the vector |
| 130 | @param delimeter the item separator (tab or comma) |
| 131 | @return a string with the delimeter-separated contents of the vector */ |
| 132 | std::string Dump(const std::string& delimeter) const; |
| 133 | |
| 134 | /** Assignment operator. |
| 135 | @param b source vector. |
| 136 | Copy the content of the vector given in the argument into *this. */ |
| 137 | FGColumnVector3& operator=(const FGColumnVector3& b) { |
| 138 | data[0] = b.data[0]; |
| 139 | data[1] = b.data[1]; |
| 140 | data[2] = b.data[2]; |
| 141 | return *this; |
| 142 | } |
| 143 | |
| 144 | /** Assignment operator. |
| 145 | @param lv initializer list of at most 3 values (i.e. {x, y, Z}) |
| 146 | Copy the content of the list into *this. */ |
| 147 | FGColumnVector3& operator=(std::initializer_list<double> lv) { |
| 148 | double *v = data; |
| 149 | for(auto &x : lv) |
| 150 | *(v++) = x; |
| 151 | |
| 152 | return *this; |
| 153 | } |
| 154 | |
| 155 | /** Comparison operator. |
| 156 | @param b other vector. |
| 157 | Returns true if both vectors are exactly the same. */ |
| 158 | bool operator==(const FGColumnVector3& b) const { |
| 159 | return data[0] == b.data[0] && data[1] == b.data[1] && data[2] == b.data[2]; |
| 160 | } |
| 161 | |
| 162 | /** Comparison operator. |
| 163 | @param b other vector. |
| 164 | Returns false if both vectors are exactly the same. */ |
| 165 | bool operator!=(const FGColumnVector3& b) const { return ! operator==(b); } |
| 166 | |
| 167 | /** Multiplication by a scalar. |
| 168 | @param scalar scalar value to multiply the vector with. |
| 169 | @return The resulting vector from the multiplication with that scalar. |
| 170 | Multiply the vector with the scalar given in the argument. */ |
| 171 | FGColumnVector3 operator*(const double scalar) const { |
| 172 | return FGColumnVector3(scalar*data[0], scalar*data[1], scalar*data[2]); |
| 173 | } |
| 174 | |
| 175 | /** Multiply by 1/scalar. |
| 176 | @param scalar scalar value to devide the vector through. |
| 177 | @return The resulting vector from the division through that scalar. |
| 178 | Multiply the vector with the 1/scalar given in the argument. */ |
| 179 | FGColumnVector3 operator/(const double scalar) const; |
| 180 | |
| 181 | /** Cross product multiplication. |
| 182 | @param V vector to multiply with. |
| 183 | @return The resulting vector from the cross product multiplication. |
| 184 | Compute and return the cross product of the current vector with |
| 185 | the given argument. */ |
| 186 | FGColumnVector3 operator*(const FGColumnVector3& V) const { |
| 187 | return FGColumnVector3( data[1] * V.data[2] - data[2] * V.data[1], |
| 188 | data[2] * V.data[0] - data[0] * V.data[2], |
| 189 | data[0] * V.data[1] - data[1] * V.data[0] ); |
| 190 | } |
| 191 | |
| 192 | /// Addition operator. |
| 193 | FGColumnVector3 operator+(const FGColumnVector3& B) const { |
| 194 | return FGColumnVector3( data[0] + B.data[0], data[1] + B.data[1], |
| 195 | data[2] + B.data[2] ); |
| 196 | } |
| 197 | |
| 198 | /// Subtraction operator. |
| 199 | FGColumnVector3 operator-(const FGColumnVector3& B) const { |
| 200 | return FGColumnVector3( data[0] - B.data[0], data[1] - B.data[1], |
| 201 | data[2] - B.data[2] ); |
| 202 | } |
| 203 | |
| 204 | /// Subtract an other vector. |
| 205 | FGColumnVector3& operator-=(const FGColumnVector3 &B) { |
| 206 | data[0] -= B.data[0]; |
| 207 | data[1] -= B.data[1]; |
| 208 | data[2] -= B.data[2]; |
| 209 | return *this; |
| 210 | } |
| 211 | |
| 212 | /// Add an other vector. |
| 213 | FGColumnVector3& operator+=(const FGColumnVector3 &B) { |
| 214 | data[0] += B.data[0]; |
| 215 | data[1] += B.data[1]; |
| 216 | data[2] += B.data[2]; |
| 217 | return *this; |
| 218 | } |
| 219 | |
| 220 | /// Scale by a scalar. |
| 221 | FGColumnVector3& operator*=(const double scalar) { |
| 222 | data[0] *= scalar; |
| 223 | data[1] *= scalar; |
| 224 | data[2] *= scalar; |
| 225 | return *this; |
| 226 | } |
| 227 | |
| 228 | /// Scale by a 1/scalar. |
| 229 | FGColumnVector3& operator/=(const double scalar); |
| 230 | |
| 231 | void InitMatrix(void) { data[0] = data[1] = data[2] = 0.0; } |
| 232 | void InitMatrix(const double a) { data[0] = data[1] = data[2] = a; } |
| 233 | void InitMatrix(const double a, const double b, const double c) { |
| 234 | data[0]=a; data[1]=b; data[2]=c; |
| 235 | } |
| 236 | |
| 237 | /** Length of the vector. |
| 238 | Compute and return the euclidean norm of this vector. */ |
| 239 | double Magnitude(void) const; |
| 240 | |
| 241 | /** Length of the vector in a coordinate axis plane. |
| 242 | Compute and return the euclidean norm of this vector projected into |
| 243 | the coordinate axis plane idx1-idx2. */ |
| 244 | double Magnitude(const int idx1, const int idx2) const; |
| 245 | |
| 246 | /** Normalize. |
| 247 | Normalize the vector to have the Magnitude() == 1.0. If the vector |
| 248 | is equal to zero it is left untouched. */ |
| 249 | FGColumnVector3& Normalize(void); |
| 250 | |
| 251 | private: |
| 252 | double data[3]; |
| 253 | }; |
| 254 | |
| 255 | /** Dot product of two vectors |
| 256 | Compute and return the euclidean dot (or scalar) product of two vectors |
| 257 | v1 and v2 */ |
| 258 | inline double DotProduct(const FGColumnVector3& v1, const FGColumnVector3& v2) { |
| 259 | return v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); |
| 260 | } |
| 261 | |
| 262 | /** Scalar multiplication. |
| 263 | @param scalar scalar value to multiply with. |
| 264 | @param A Vector to multiply. |
| 265 | Multiply the Vector with a scalar value. Note: At this time, this |
| 266 | operator MUST be inlined, or a multiple definition link error will occur.*/ |
| 267 | inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) { |
| 268 | // use already defined operation. |
| 269 | return A*scalar; |
| 270 | } |
| 271 | |
| 272 | /** Write vector to a stream. |
| 273 | @param os Stream to write to. |
| 274 | @param col vector to write. |
| 275 | Write the vector to a stream.*/ |
| 276 | JSBSIM_API std::ostream& operator<<(std::ostream& os, const FGColumnVector3& col); |
| 277 | |
| 278 | } // namespace JSBSim |
| 279 | |
| 280 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 281 | #endif |