KDIS  2-8-x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DeadReckoningCalculator.h
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright 2013 Karl Jones
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 For Further Information Please Contact me at
26 Karljj1@yahoo.com
27 http://p.sf.net/kdis/UserGuide
28 *********************************************************************/
29 
30 /********************************************************************
31  class: DeadReckoningCalculator
32  created: 06/08/2009
33  author: Karl Jones, Robert Ioiart
34 
35  purpose: Class for calculating dead reckoning of an entity.
36 
37  Dead Reckoning Algorithms consist of 3 elements.
38 
39  XYZ
40 
41  X - Specifies rotation as either fixed(F) or rotating(R).
42 
43  Y - Specifies dead reckoning rates to be held constant as either rate of position (P)
44  or rate of velocity (V).
45 
46  Z - Specifies the coordinate system as either world coordinates (W) or body axis coordinates(B).
47 
48  Note: These algorithms are still in a BETA state, they need testing and validating.
49  If you find a problem please let me know and if you find a solution I would be even happier to hear from you :)
50 
51  Status:
52 
53  1 Static - N/A
54  2 FPW - Position Works
55  3 RPW - Position Works, orientation is a simplified version.(If you know how to do it the correct way please let me know)
56  4 RVW - Position Works, orientation is a simplified version.(If you know how to do it the correct way please let me know)
57  5 FVW - Position Works
58  6 FPB - Position Works
59  7 RPB - Position Works, orientation is a simplified version.(If you know how to do it the correct way please let me know)
60  8 RVB - Position Works, orientation is a simplified version.(If you know how to do it the correct way please let me know)
61  9 FVB - Position Works
62 *********************************************************************/
63 
64 #pragma once
65 
66 #include "./../KDefines.h"
67 #include "./../DataTypes/EulerAngles.h"
68 #include "./../DataTypes/WorldCoordinates.h"
69 #include "./../DataTypes/Vector.h"
70 #include <vector>
71 
72 namespace KDIS {
73 namespace UTILS {
74 
75 ////////////////////////////////////////////////////////
76 // A simple matrix class for simplifying the dead reckoning equations.
77 ////////////////////////////////////////////////////////
78 
79 template<class Type, KUINT8 cols, KUINT8 rows>
80 class Matrix
81 {
82 public:
83 
84  Type Data[cols][rows];
85 
87  {
88  for( KUINT16 i = 0; i < cols; ++i )
89  {
90  for( KUINT16 j = 0; j < rows; ++j )
91  {
92  if( i == j )Data[i][j] = 1;
93  else Data[i][j] = 0;
94  }
95  }
96  };
97 
98  Matrix operator + ( const Matrix & Value )
99  {
100  Matrix m = *this;
101  for( KUINT16 i = 0; i < cols; ++i )
102  {
103  for( KUINT16 j = 0; j < rows; ++j )
104  {
105  m.Data[i][j] += Value.Data[i][j];
106  }
107  }
108 
109  return m;
110  };
111 
112  Matrix & operator += ( const Matrix & Value )
113  {
114  for( KUINT16 i = 0; i < cols; ++i )
115  {
116  for( KUINT16 j = 0; j < rows; ++j )
117  {
118  Data[i][j] += Value.Data[i][j];
119  }
120  }
121 
122  return *this;
123  };
124 
125  Matrix operator - ( const Matrix & Value )
126  {
127  Matrix m = *this;
128  for( KUINT16 i = 0; i < cols; ++i )
129  {
130  for( KUINT16 j = 0; j < rows; ++j )
131  {
132  m.Data[i][j] -= Value.Data[i][j];
133  }
134  }
135 
136  return m;
137  };
138 
139  Matrix & operator -= ( const Matrix & Value )
140  {
141  for( KUINT16 i = 0; i < cols; ++i )
142  {
143  for( KUINT16 j = 0; j < rows; ++j )
144  {
145  Data[i][j] -= Value.Data[i][j];
146  }
147  }
148 
149  return *this;
150  };
151 
152  Matrix operator * ( const Matrix & Value )
153  {
154  Matrix m = *this;
155  Type tmp = 0,
156  tmp2 = 0,
157  sum = 0;
158 
159  for( KUINT16 i = 0; i < rows; ++i )
160  {
161  for( KUINT16 j = 0; j < cols; ++j )
162  {
163  for( KUINT16 k = 0; k < cols; ++k )
164  {
165  tmp += m.Data[i][k] * Value.Data[k][j];
166  }
167 
168  m.Data[i][j] = tmp;
169  tmp = 0;
170  }
171  }
172 
173  return m;
174  };
175 
176  template<class T>
177  Matrix operator * ( const T Value )
178  {
179  Matrix m = *this;
180  for( KUINT16 i = 0; i < cols; ++i )
181  {
182  for( KUINT16 j = 0; j < rows; ++j )
183  {
184  m.Data[i][j] *= Value;
185  }
186  }
187 
188  return m;
189  };
190 
191  template<class T>
192  Matrix & operator *= ( const T Value )
193  {
194  for( KUINT16 i = 0; i < cols; ++i )
195  {
196  for( KUINT16 j = 0; j < rows; ++j )
197  {
198  Data[i][j] *= Value;
199  }
200  }
201 
202  return *this;
203  };
204 
206  {
208  for( KUINT16 i = 0; i < rows; ++i )
209  {
210  for( KUINT16 j = 0; j < cols; ++j )
211  {
212  res[i] += Data[i][j] * Value[j];
213  }
214  }
215 
216  return res;
217  };
218 
220  {
221  if( cols != rows )return;
222 
223  for( KUINT16 i = 0; i < rows; ++i )
224  {
225  for( KUINT16 j = 0; j < i; ++j )
226  {
227  Type aux = Data[i][j];
228  Data[i][j] = Data[j][i];
229  Data[j][i] = aux;
230  }
231  }
232  }
233 };
234 
235 //////////////////////////////////////////////////////////////////////////
237 
239 {
240 protected:
241 
244 
249 
250  // Some algorithms are implemented as an integral form for rotation, thus using the time since t0
252 
253  // Orientation cache
254  KDIS::DATA_TYPE::EulerAngles m_initOrientation, m_initEulerAngularVelocity;
255  TMATRIX m_initOrientationMatrix, m_initOrientationMatrixTranspose;
256 
257  // Angular velocity cache
261 
262  KDIS::DATA_TYPE::ENUMS::DeadReckoningAlgorithm m_DRA; // The selected dead reckoning algorithm
263 
264  void positionReset( const KDIS::DATA_TYPE::WorldCoordinates & Position );
265  void orientationReset( const KDIS::DATA_TYPE::EulerAngles & Orientation );
266  void angularVelocityReset( const KDIS::DATA_TYPE::Vector & AngularVelocity );
267  void velocityReset( const KDIS::DATA_TYPE::Vector & LinearVelocity );
268  void accelerationReset( const KDIS::DATA_TYPE::Vector & LinearAcceleration );
269  void quatAxisReset(const KDIS::DATA_TYPE::Vector & QuatAxis);
270 
271  KDIS::DATA_TYPE::EulerAngles toEulerAngularVelocity( const KDIS::DATA_TYPE::Vector & angVel, const KDIS::DATA_TYPE::EulerAngles & Orientation );
272 
273  void calcOrientation_simplified( KDIS::DATA_TYPE::EulerAngles & OrientationOut, const KFLOAT32 DeltaTime );
274  void calcOrientation( KDIS::DATA_TYPE::EulerAngles & OrientationOut, const KFLOAT32 totalTimeSinceReset );
275 
276  void computeRotationAxis( const TMATRIX& curOrientationMatrix );
277  void computeDRMatrix( TMATRIX & res, const KFLOAT32 totalTimeSinceReset );
278  void computeDRMatrix_asQuat( TMATRIX & res, const KFLOAT32 totalTimeSinceReset );
279 
280  /************************************************************************/
281  /* Dead Reckoning Algorithm Implementations */
282  /************************************************************************/
283 
284  // FPW //
285  void calcDeadReckoningFPW( KDIS::DATA_TYPE::WorldCoordinates & PositionOut, const KFLOAT32 totalTimeSinceReset);
286 
287  // RPW //
288  void calcDeadReckoningRPW( KDIS::DATA_TYPE::WorldCoordinates & PositionOut, KDIS::DATA_TYPE::EulerAngles & OrientationOut, const KFLOAT32 totalTimeSinceReset );
289 
290  // RVW //
291  void calcDeadReckoningRVW( KDIS::DATA_TYPE::WorldCoordinates & PositionOut,KDIS::DATA_TYPE::EulerAngles & OrientationOut, const KFLOAT32 totalTimeSinceReset );
292 
293  // FVW //
294  void calcDeadReckoningFVW( KDIS::DATA_TYPE::WorldCoordinates & PositionOut, const KFLOAT32 totalTimeSinceReset );
295 
296  // FPB //
297  void calcDeadReckoningFPB( KDIS::DATA_TYPE::WorldCoordinates & PositionOut, const KFLOAT32 totalTimeSinceReset);
298 
299  // RPB //
300  void calcDeadReckoningRPB( KDIS::DATA_TYPE::WorldCoordinates & PositionOut, KDIS::DATA_TYPE::EulerAngles & OrientationOut, const KFLOAT32 totalTimeSinceReset );
301 
302  // RVB //
303  void calcDeadReckoningRVB( KDIS::DATA_TYPE::WorldCoordinates & PositionOut, KDIS::DATA_TYPE::EulerAngles & OrientationOut, const KFLOAT32 totalTimeSinceReset );
304 
305  // FVB //
306  void calcDeadReckoningFVB( KDIS::DATA_TYPE::WorldCoordinates & PositionOut, const KFLOAT32 totalTimeSinceReset );
307 
308 public:
309 
311 
313 
314  //************************************
315  // FullName: KDIS::UTILS::DeadReckoningCalculator::Reset
316  // Author: Robert Ioiart - 04/05/2010
317  //!Description: Resets this object. For a local entity should be called before sending out
318  //! an entity state PDU so the local entity can dead-reckon itself.
319  //! For a remote entity should be called when an ES PDU is received.
320  // Parameter: const Vector & LinearVelocity
321  // Parameter: const Vector & LinearAcceleration
322  // Parameter: const Vector & AngularVelocity - Angular velocity around the DIS body axes.
323  // Parameter: const WorldCoordinates & Position
324  // Parameter: const EulerAngles & Orientation
325  // Parameter: DeadReckoningAlgorithm DRA
326  //************************************
327  void Reset( const KDIS::DATA_TYPE::Vector & LinearVelocity, const KDIS::DATA_TYPE::Vector & LinearAcceleration, const KDIS::DATA_TYPE::Vector & AngularVelocity,
329 
330  //************************************
331  // FullName: KDIS::UTILS::DeadReckoningCalculator::RunAlgorithm
332  // Author: Robert Ioiart - 04/05/2010
333  //!Description: Steps algorithm for a time step, computing the dead-reckoned position and orientation
334  //! based on the values provided at the last Reset.
335  // Parameter: const KFLOAT64 TotalTimeSinceReset
336  // Parameter: WorldCoordinates & PositionOut
337  // Parameter: EulerAngles & OrientationOut
338  //************************************
339  void RunAlgorithm( const KFLOAT32 TotalTimeSinceReset, KDIS::DATA_TYPE::WorldCoordinates & PositionOut, KDIS::DATA_TYPE::EulerAngles & OrientationOut );
340 
341  //************************************
342  // FullName: KDIS::DeadReckoningCalculator::GenerateSmoothingPoints
343  //!Description: When a new update of position is received from another entity, a correction
344  //! in position is usually required so that the entity may be depicted in simulation
345  //! as accurately as possible.
346  //! The preferred method is to gradually correct the position of the entity over a period of time.
347  //! This is called smoothing, this function will generate the smoothing points for you.
348  // Parameter: const WorldCoordinates & StartPosition,
349  // Parameter: const WorldCoordinates & EndPosition,
350  // Parameter: KUINT32 NumberOfPoints
351  //************************************
352  std::vector<KDIS::DATA_TYPE::WorldCoordinates> GenerateSmoothingPoints( const KDIS::DATA_TYPE::WorldCoordinates & StartPosition, const KDIS::DATA_TYPE::WorldCoordinates & EndPosition, KUINT32 NumberOfPoints );
353 
354  //************************************
355  // FullName: KDIS::DeadReckoningCalculator::GenerateSmoothingPoints
356  //!Description: When a new update of position is received from another entity, a correction
357  //! in position is usually required so that the entity may be depicted in simulation
358  //! as accurately as possible.
359  //! The preferred method is to gradually correct the position of the entity over a period of time.
360  //! This is called smoothing, this function will generate the smoothing points for you.
361  //
362  //!Note: This is the preferred version for generating smoothing points, because it does not
363  //! incur the cost of a copy of the vector.
364  //
365  // Parameter: const WorldCoordinates & StartPosition,
366  // Parameter: const WorldCoordinates & EndPosition,
367  // Parameter: KUINT32 NumberOfPoints
368  // Parameter: vector<WorldCoordinates> & v
369  //************************************
370  void GenerateSmoothingPoints( const KDIS::DATA_TYPE::WorldCoordinates & StartPosition, const KDIS::DATA_TYPE::WorldCoordinates & EndPosition, KUINT32 NumberOfPoints, std::vector<KDIS::DATA_TYPE::WorldCoordinates> & v );
371 };
372 
373 } // END namespace UTILS
374 } // END namespace KDIS
375 
unsigned int KUINT32
Definition: KDefines.h:103
TMATRIX m_initOrientationMatrixTranspose
Definition: DeadReckoningCalculator.h:255
KDIS::DATA_TYPE::WorldCoordinates m_initPosition
Definition: DeadReckoningCalculator.h:242
Definition: DeadReckoningCalculator.h:80
Matrix< KFLOAT32, 3, 3 > TMATRIX
Definition: DeadReckoningCalculator.h:236
KDIS::DATA_TYPE::Vector m_initAngularVelocity
Definition: DeadReckoningCalculator.h:258
unsigned short int KUINT16
Definition: KDefines.h:101
Matrix()
Definition: DeadReckoningCalculator.h:86
KFLOAT32 m_f64Magnitude
Definition: DeadReckoningCalculator.h:251
float KFLOAT32
Definition: KDefines.h:113
Matrix & operator-=(const Matrix &Value)
Definition: DeadReckoningCalculator.h:139
Matrix operator*(const Matrix &Value)
Definition: DeadReckoningCalculator.h:152
Definition: Vector.h:71
TMATRIX m_wwMatrix
Definition: DeadReckoningCalculator.h:259
TMATRIX m_SkewOmegaMatrix
Definition: DeadReckoningCalculator.h:260
Definition: WorldCoordinates.h:52
bool KBOOL
Definition: KDefines.h:119
Definition: EulerAngles.h:46
void inPlanceTranspose()
Definition: DeadReckoningCalculator.h:219
KDIS::DATA_TYPE::Vector m_initLinearVelocity
Definition: DeadReckoningCalculator.h:243
Matrix operator-(const Matrix &Value)
Definition: DeadReckoningCalculator.h:125
Matrix operator+(const Matrix &Value)
Definition: DeadReckoningCalculator.h:98
KDIS::DATA_TYPE::Vector m_quatAxis
Definition: DeadReckoningCalculator.h:248
Matrix & operator*=(const T Value)
Definition: DeadReckoningCalculator.h:192
Definition: EnumEntityInfoInteraction.h:398
DeadReckoningAlgorithm
Definition: EnumEntityInfoInteraction.h:841
#define KDIS_EXPORT
Definition: KDefines.h:82
Type Data[cols][rows]
Definition: DeadReckoningCalculator.h:84
Matrix & operator+=(const Matrix &Value)
Definition: DeadReckoningCalculator.h:112
KDIS::DATA_TYPE::Vector m_initLinearAcceleration
Definition: DeadReckoningCalculator.h:245
KDIS::DATA_TYPE::ENUMS::DeadReckoningAlgorithm m_DRA
Definition: DeadReckoningCalculator.h:262
KBOOL m_bQuaxAxisSet
Definition: DeadReckoningCalculator.h:247
KDIS::DATA_TYPE::EulerAngles m_initOrientation
Definition: DeadReckoningCalculator.h:254
Definition: DeadReckoningCalculator.h:238
KDIS::DATA_TYPE::Vector m_Ab
Definition: DeadReckoningCalculator.h:246