LRAUV  revA
AuvMath.h
Go to the documentation of this file.
1 
15 #ifndef AUVMATH_H_
16 #define AUVMATH_H_
17 
18 #include <math.h>
19 #include "data/Point3D.h"
20 #include "data/Matrix3x3.h"
21 
22 #ifndef M_2PI
23 # define M_2PI (6.28318530717958647692)
24 #endif
25 
26 #ifndef M_1_2PI
27 # define M_1_2PI (0.15915494309189533577)
28 #endif
29 
30 #ifndef UINT16_MAX
31 # define UINT16_MAX (65535)
32 #endif
33 
34 #ifndef UINT32_MAX
35 # define UINT32_MAX (4294967295U)
36 #endif
37 
39 #ifndef D2R
40 #define D2R(x) ( ( x ) * M_PI / 180.0 )
41 #endif
42 
44 #define R2D(x) ( ( x ) * 180.0 / M_PI)
45 
48 #ifndef MIN
49 # define MIN(a,b) ( ( ( a ) != ( a ) || ( a ) <= ( b ) ) ? ( a ) : ( b ) != ( b ) || ( a ) > ( b ) ? ( b ) : ( a ) )
50 #endif
51 
54 #ifndef MAX
55 # define MAX(a,b) ( ( ( a ) != ( a ) || ( a ) >= ( b ) ) ? ( a ) : ( b ) != ( b ) || ( a ) < ( b ) ? ( b ) : ( a ) )
56 #endif
57 
64 class AuvMath
65 {
66 public:
67 
68  // 2 times pi
69  static const double TWOPI = M_PI * 2.0;
70  static const double DEG_TO_RAD;
71  static const double RAD_TO_DEG;
72 
77  template <typename T>
78  static T Limit( const T& value, const T& limit1, const T& limit2 );
79 
84  template <typename T>
85  static bool InLimit( const T& value, const T& limit1, const T& limit2 );
86 
90  template <typename T>
91  inline static T Min( const T& a, const T& b )
92  {
93  return MIN( a, b );
94  }
95 
99  template <typename T>
100  inline static T Max( const T& a, const T& b )
101  {
102  return MAX( a, b );
103  }
104 
106  template <typename T>
107  static T Square( T number )
108  {
109  return number * number;
110  }
111 
113  template <typename T>
114  inline static void Swap( T& a, T& b )
115  {
116  T t( a );
117  a = b;
118  b = t;
119  }
120 
123  template<typename T>
124  static T FlipEndian( T value );
125 
129  template <typename T>
130  static T Round( const T& value, const T& divisor );
131 
135  template <typename T>
136  static T Sign( const T& x );
137 
141  template <typename T>
142  static T ModPi( const T& angle );
143 
146  template <typename T>
147  static T NanZero( const T& value );
148 
153  template <typename T>
154  static int FindLtEqIndex( const T* values, const T& value,
155  const unsigned int indexMin, const unsigned int indexMax );
156 
159  template <typename T>
160  static T Interpolate1D( const T& x, const T& data0, const T& data1,
161  const T& x0, const T& x1 );
162 
166  template <typename T>
167  static T Interpolate2D( const T& x, const T& y, T data[][ 2 ],
168  const T& x0, const T& x1, const T& y0, const T& y1 );
169 
173  template <typename T>
174  static T Interpolate3D( const T& x, const T& y, const T& z,
175  T data[][ 2 ][ 2 ],
176  const T& x0, const T& x1,
177  const T& y0, const T& y1,
178  const T& z0, const T& z1 );
179 
183  template <typename T>
184  static T Interpolate4D( const T& w, const T& x, const T& y,
185  const T& z, T data[][ 2 ][ 2 ][ 2 ],
186  const T& w0, const T& w1,
187  const T& x0, const T& x1,
188  const T& y0, const T& y1,
189  const T& z0, const T& z1 );
190 
210  static double BulkModulus( double salinity, double temperature, double pressure );
212 
234  static double Density( double salinity, double temperature, double pressure );
236 
257  static double OceanDepth( const double& pressure, const double& latitude );
259 
274  static double OceanPressure( const double& depth, const double& latitude );
276 
277  //
278  // Simple sort routine, invented by Donald Shell 1959. It is 0(n^2).
279  // The code is short, and appropriate for small arrays here.
280  //
281  template <typename T>
282  static void ShellSort( T array[], int arraySize )
283  {
284  int i, j, increment;
285  T temp;
286 
287  increment = 3;
288  while( increment > 0 )
289  {
290  for( i = 0; i < arraySize; i++ )
291  {
292  j = i;
293  temp = array[i];
294  while( ( j >= increment ) && ( array[j - increment] > temp ) )
295  {
296  array[j] = array[j - increment];
297  j = j - increment;
298  }
299  array[j] = temp;
300  }
301  if( increment / 2 != 0 )
302  increment = increment / 2;
303  else if( increment == 1 )
304  increment = 0;
305  else
306  increment = 1;
307  }
308  }
309 
310  static bool AreaContains( float x, float y, int segments,
311  float* x0, float* y0, float* x1, float* y1 );
312 
313  static bool TriangleContains( float x, float y,
314  float x0, float y0, float x1, float y1,
315  float x2, float y2 );
316 
317  static void UnitVectorToAzimuthAndElevation( Point3D &uhat, float &azimuth, float &elevation );
318 
319  static void AzimuthAndElevationToUnitVector( float &azimuth, float &elevation, Point3D &uhat );
320 
321 };
322 
323 #endif /*AUVMATH_H_*/
static T Max(const T &a, const T &b)
Returns the greater of the two arguments If either input is nan, nan is returned Unit tests in AuvMat...
Definition: AuvMath.h:100
static const double DEG_TO_RAD
Definition: AuvMath.h:70
#define MAX(a, b)
Maximum of two values – also available as AuvMath::Max() template function If either argument is a N...
Definition: AuvMath.h:55
static const double TWOPI
Definition: AuvMath.h:69
static double OceanPressure(const double &depth, const double &latitude)
OceanPressure (depth, latitude)
Definition: AuvMath.cpp:530
static T Interpolate3D(const T &x, const T &y, const T &z, T data[][2][2], const T &x0, const T &x1, const T &y0, const T &y1, const T &z0, const T &z1)
Examines a 2x2x2 data field for the value, where x0 <= x <= x1, y0 <= y <= y1, z0 <= z <= z1 Unit tes...
Definition: AuvMath.cpp:326
static T ModPi(const T &angle)
modulo Pi function: If input is nan, nan is returned Unit tests in AuvMath_Test.testModPi ...
Definition: AuvMath.cpp:107
static T FlipEndian(T value)
Does not test the endianness of the input value, Just flips it.
static T NanZero(const T &value)
Casts nan values to zero – otherwise returns the value Unit tests in AuvMath_Test.testNanZero.
Definition: AuvMath.cpp:118
static bool AreaContains(float x, float y, int segments, float *x0, float *y0, float *x1, float *y1)
Definition: AuvMath.cpp:555
static T Interpolate2D(const T &x, const T &y, T data[][2], const T &x0, const T &x1, const T &y0, const T &y1)
Examines a 2x2 data field for the value, where x0 <= x <= x1, y0 <= y <= y1, Unit tests in AuvMath_Te...
Definition: AuvMath.cpp:299
#define MIN(a, b)
Minimum of two values – also available as AuvMath::Min() template function If either argument is a N...
Definition: AuvMath.h:49
static T Interpolate4D(const T &w, const T &x, const T &y, const T &z, T data[][2][2][2], const T &w0, const T &w1, const T &x0, const T &x1, const T &y0, const T &y1, const T &z0, const T &z1)
Examines a 2x2x2x2 data field for the value, where w0 <= w <= w1, x0 <= x <= x1, y0 <= y <= y1...
Definition: AuvMath.cpp:366
static void Swap(T &a, T &b)
Swaps the two arguments.
Definition: AuvMath.h:114
Contains the Point3D class definition.
static double BulkModulus(double salinity, double temperature, double pressure)
Seawater bulk modulus (S,T,P)
Definition: AuvMath.cpp:420
static void UnitVectorToAzimuthAndElevation(Point3D &uhat, float &azimuth, float &elevation)
Definition: AuvMath.cpp:577
static T Limit(const T &value, const T &limit1, const T &limit2)
Makes sure a variable falls within defined limits...
Definition: AuvMath.cpp:20
static double Density(double salinity, double temperature, double pressure)
Seawater density (S,T,P)
Definition: AuvMath.cpp:476
static const double RAD_TO_DEG
Definition: AuvMath.h:71
Wraps three double precision (8-byte) floating point numbers.
Definition: Point3D.h:28
Contains useful math-related utilities.
Definition: AuvMath.h:64
static int FindLtEqIndex(const T *values, const T &value, const unsigned int indexMin, const unsigned int indexMax)
Finds the index within an array where the value is <= the supplied value Searches from indexMin to in...
Definition: AuvMath.cpp:239
static T Sign(const T &x)
Returns signum function: If input is nan, nan is returned Unit tests in AuvMath_Test.testSign.
Definition: AuvMath.cpp:226
static void AzimuthAndElevationToUnitVector(float &azimuth, float &elevation, Point3D &uhat)
Definition: AuvMath.cpp:583
static T Min(const T &a, const T &b)
Returns the lesser of the two arguments If either input is nan, nan is returned Unit tests in AuvMath...
Definition: AuvMath.h:91
static bool TriangleContains(float x, float y, float x0, float y0, float x1, float y1, float x2, float y2)
Definition: AuvMath.cpp:568
Contains the Matrix3x3 class definition.
static void ShellSort(T array[], int arraySize)
Definition: AuvMath.h:282
static T Square(T number)
Square function (opposite of sqrt(T))
Definition: AuvMath.h:107
static bool InLimit(const T &value, const T &limit1, const T &limit2)
Tests if a variable falls within defined limits...
Definition: AuvMath.cpp:65
static T Round(const T &value, const T &divisor)
Makes sure a variable is divisible by specified divisor...
Definition: AuvMath.cpp:202
static T Interpolate1D(const T &x, const T &data0, const T &data1, const T &x0, const T &x1)
Examines a 2 element data field for the value, where x0 <= x <= x1 Unit tests in AuvMath_Test.testInterpolate1D.
Definition: AuvMath.cpp:279
static double OceanDepth(const double &pressure, const double &latitude)
OceanDepth (pressure, latitude)
Definition: AuvMath.cpp:508