~ubuntu-branches/ubuntu/precise/flightgear/precise

« back to all changes in this revision

Viewing changes to src/FDM/JSBSim/math/FGColumnVector3.h

  • Committer: Package Import Robot
  • Author(s): Ove Kaaven
  • Date: 2011-09-03 22:16:12 UTC
  • mfrom: (3.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20110903221612-2cjy0z7ztj5nkln5
Tags: 2.4.0-1
* New upstream release. Closes: #638588.
* Build-Depend on OpenSceneGraph 3.0, and the Subversion library.
* Recommend fgfs-scenery-base.
* Enable parallel builds (shorter compile times on multicore CPUs).
* Removed hack that tried to build without optimizations if
  building with optimizations fails.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
 
42
42
#include <iosfwd>
43
43
#include <string>
44
 
#include "FGJSBBase.h"
45
44
 
46
45
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47
46
DEFINITIONS
48
47
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
49
48
 
50
 
#define ID_COLUMNVECTOR3 "$Id$"
 
49
#define ID_COLUMNVECTOR3 "$Id: FGColumnVector3.h,v 1.16 2010/12/07 12:57:14 jberndt Exp $"
51
50
 
52
51
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53
52
FORWARD DECLARATIONS
61
60
 
62
61
/** This class implements a 3 element column vector.
63
62
    @author Jon S. Berndt, Tony Peden, et. al.
64
 
    @version $Id$
 
63
    @version $Id: FGColumnVector3.h,v 1.16 2010/12/07 12:57:14 jberndt Exp $
65
64
*/
66
65
 
67
66
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68
67
CLASS DECLARATION
69
68
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
70
69
 
71
 
class FGColumnVector3 : public FGJSBBase
 
70
class FGColumnVector3
72
71
{
73
72
public:
74
73
  /** Default initializer.
80
79
      @param Y value of the y-conponent.
81
80
      @param Z value of the z-conponent.
82
81
      Create a vector from the doubles given in the arguments.   */
83
 
  FGColumnVector3(double X, double Y, double Z) {
 
82
  FGColumnVector3(const double X, const double Y, const double Z) {
84
83
    data[0] = X;
85
84
    data[1] = Y;
86
85
    data[2] = Z;
87
 
    Debug(0);
88
86
  }
89
87
 
90
88
  /** Copy constructor.
94
92
    data[0] = v.data[0];
95
93
    data[1] = v.data[1];
96
94
    data[2] = v.data[2];
97
 
    Debug(0);
98
95
  }
99
96
 
100
97
  /// Destructor.
101
 
  ~FGColumnVector3(void) { Debug(1); }
 
98
  ~FGColumnVector3(void) { }
102
99
 
103
100
  /** Read access the entries of the vector.
104
101
      @param idx the component index.
105
102
      Return the value of the matrix entry at the given index.
106
103
      Indices are counted starting with 1.
107
104
      Note that the index given in the argument is unchecked.   */
108
 
  double operator()(unsigned int idx) const { return Entry(idx); }
 
105
  double operator()(const unsigned int idx) const { return data[idx-1]; }
109
106
 
110
107
  /** Write access the entries of the vector.
111
108
      @param idx the component index.
112
109
      Return a reference to the vector entry at the given index.
113
110
      Indices are counted starting with 1.
114
111
      Note that the index given in the argument is unchecked.   */
115
 
  double& operator()(unsigned int idx) { return Entry(idx); }
 
112
  double& operator()(const unsigned int idx) { return data[idx-1]; }
116
113
 
117
114
  /** Read access the entries of the vector.
118
115
      @param idx the component index.
122
119
      operator()(unsigned int idx) const</tt> function. It is
123
120
      used internally to access the elements in a more convenient way.
124
121
      Note that the index given in the argument is unchecked.   */
125
 
  double Entry(unsigned int idx) const { return data[idx-1]; }
 
122
  double Entry(const unsigned int idx) const { return data[idx-1]; }
126
123
 
127
124
  /** Write access the entries of the vector.
128
125
      @param idx the component index.
132
129
      operator()(unsigned int idx)</tt> function. It is
133
130
      used internally to access the elements in a more convenient way.
134
131
      Note that the index given in the argument is unchecked.   */
135
 
  double& Entry(unsigned int idx) { return data[idx-1]; }
 
132
  double& Entry(const unsigned int idx) { return data[idx-1]; }
136
133
 
137
134
  /** Prints the contents of the vector
138
135
      @param delimeter the item separator (tab or comma)
166
163
      @return The resulting vector from the multiplication with that scalar.
167
164
      Multiply the vector with the scalar given in the argument.   */
168
165
  FGColumnVector3 operator*(const double scalar) const {
169
 
    return FGColumnVector3(scalar*Entry(1), scalar*Entry(2), scalar*Entry(3));
 
166
    return FGColumnVector3(scalar*data[0], scalar*data[1], scalar*data[2]);
170
167
  }
171
168
 
172
169
  /** Multiply by 1/scalar.
181
178
      Compute and return the cross product of the current vector with
182
179
      the given argument.   */
183
180
  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) );
 
181
    return FGColumnVector3( data[1] * V.data[2] - data[2] * V.data[1],
 
182
                            data[2] * V.data[0] - data[0] * V.data[2],
 
183
                            data[0] * V.data[1] - data[1] * V.data[0] );
187
184
  }
188
185
 
189
186
  /// Addition operator.
190
187
  FGColumnVector3 operator+(const FGColumnVector3& B) const {
191
 
    return FGColumnVector3( Entry(1) + B(1), Entry(2) + B(2), Entry(3) + B(3) );
 
188
    return FGColumnVector3( data[0] + B.data[0], data[1] + B.data[1], data[2] + B.data[2] );
192
189
  }
193
190
 
194
191
  /// Subtraction operator.
195
192
  FGColumnVector3 operator-(const FGColumnVector3& B) const {
196
 
    return FGColumnVector3( Entry(1) - B(1), Entry(2) - B(2), Entry(3) - B(3) );
 
193
    return FGColumnVector3( data[0] - B.data[0], data[1] - B.data[1], data[2] - B.data[2] );
197
194
  }
198
195
 
199
196
  /// Subtract an other vector.
200
197
  FGColumnVector3& operator-=(const FGColumnVector3 &B) {
201
 
    Entry(1) -= B(1);
202
 
    Entry(2) -= B(2);
203
 
    Entry(3) -= B(3);
 
198
    data[0] -= B.data[0];
 
199
    data[1] -= B.data[1];
 
200
    data[2] -= B.data[2];
204
201
    return *this;
205
202
  }
206
203
 
207
204
  /// Add an other vector.
208
205
  FGColumnVector3& operator+=(const FGColumnVector3 &B) {
209
 
    Entry(1) += B(1);
210
 
    Entry(2) += B(2);
211
 
    Entry(3) += B(3);
 
206
    data[0] += B.data[0];
 
207
    data[1] += B.data[1];
 
208
    data[2] += B.data[2];
212
209
    return *this;
213
210
  }
214
211
 
215
212
  /// Scale by a scalar.
216
213
  FGColumnVector3& operator*=(const double scalar) {
217
 
    Entry(1) *= scalar;
218
 
    Entry(2) *= scalar;
219
 
    Entry(3) *= scalar;
 
214
    data[0] *= scalar;
 
215
    data[1] *= scalar;
 
216
    data[2] *= scalar;
220
217
    return *this;
221
218
  }
222
219
 
224
221
  FGColumnVector3& operator/=(const double scalar);
225
222
 
226
223
  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) {
 
224
  void InitMatrix(const double a) { data[0] = data[1] = data[2] = a; }
 
225
  void InitMatrix(const double a, const double b, const double c) {
229
226
    data[0]=a; data[1]=b; data[2]=c;
230
227
  }
231
228
 
236
233
  /** Length of the vector in a coordinate axis plane.
237
234
      Compute and return the euclidean norm of this vector projected into
238
235
      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) );
241
 
  }
 
236
  double Magnitude(const int idx1, const int idx2) const;
242
237
 
243
238
  /** Normalize.
244
239
      Normalize the vector to have the Magnitude() == 1.0. If the vector
245
240
      is equal to zero it is left untouched.   */
246
241
  FGColumnVector3& Normalize(void);
247
242
 
248
 
  // little trick here.
249
 
  struct AssignRef {
250
 
    AssignRef(FGColumnVector3& r, int i) : Ref(r), idx(i) {}
251
 
    AssignRef operator<<(const double ff) {
252
 
      Ref.Entry(idx) = ff;
253
 
      return AssignRef(Ref, idx+1);
254
 
    }
255
 
    FGColumnVector3& Ref;
256
 
    int idx;
257
 
  };
258
 
  AssignRef operator<<(const double ff) {
259
 
    Entry(1) = ff;
260
 
    return AssignRef(*this, 2);
 
243
  /** Dot product of two vectors
 
244
      Compute and return the euclidean dot (or scalar) product of two vectors
 
245
      v1 and v2 */
 
246
  friend inline double DotProduct(const FGColumnVector3& v1, const FGColumnVector3& v2) {
 
247
    return v1.data[0]*v2.data[0] + v1.data[1]*v2.data[1] + v1.data[2]*v2.data[2];
261
248
  }
262
249
 
263
250
private:
264
251
  double data[3];
265
 
 
266
 
  void Debug(int from);
267
252
};
268
253
 
269
254
/** Scalar multiplication.
270
255
    @param scalar scalar value to multiply with.
271
256
    @param A Vector to multiply.
272
 
    Multiply the Vector with a scalar value.*/
 
257
    Multiply the Vector with a scalar value. Note: At this time, this
 
258
    operator MUST be inlined, or a multiple definition link error will occur.*/
273
259
inline FGColumnVector3 operator*(double scalar, const FGColumnVector3& A) {
274
260
  // use already defined operation.
275
261
  return A*scalar;
277
263
 
278
264
/** Write vector to a stream.
279
265
    @param os Stream to write to.
280
 
    @param M Matrix to write.
281
 
    Write the matrix to a stream.*/
 
266
    @param col vector to write.
 
267
    Write the vector to a stream.*/
282
268
std::ostream& operator<<(std::ostream& os, const FGColumnVector3& col);
283
269
 
284
270
} // namespace JSBSim