VR-Link API Documentation for HLA Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vlMath.h
Go to the documentation of this file.
1 /*********************************************************************
2 ** Copyright (c) 1992-2026 MAK Technologies, Inc
3 ** All rights reserved.
4 *********************************************************************/
5 
6 
7 #ifndef vlMath_H
8 #define vlMath_H
9 
14 
15 #if !defined(_WIN32) && !defined(VXWORKS)
16 #include <sys/param.h>
17 #endif
18 
19 
20 #include <vlutil/vlMachineTypes.h>
21 #include <math.h>
22 #include <vlutil/vlExceptions.h>
23 
24 #ifdef DtUSE_UTILITIES_NAMESPACE
25 namespace DtUSE_UTILITIES_NAMESPACE
26 {
27 #endif
28 
31 #define DtEPico 1.0E-12
32 #define DtENano 1.0E-09
33 #define DtEMicro 1.0E-06
34 #define DtEMilli 1.0E-03
35 #define DtECenti 1.0E-02
36 #define DtEDeci 1.0E-01
37 #define DtEHalf 5.0E-01
38 #define DtENineTenths 9.0E-01
39 #define DtEOne 1.0
40 #define DtEDeca 1.0E01
41 #define DtEHecto 1.0E02
42 #define DtEKilo 1.0E03
43 #define DtEMega 1.0E06
44 #define DtEGiga 1.0E09
45 
47 #define DtEq(x, y, e) ((DtAbs((x) - (y)) <= (e)) ? true : false)
48 
49 #define M_2PI 6.28318530717958647693 /* 2Pi */
50 
51 #ifndef __darwin__
52 #define M_E 2.7182818284590452354 /* Euler constant "e" */
53 #define M_1_E 0.36787944117144232160 /* 1/e */
54 #define M_LN2 0.69314718055994530942 /* ln(2) */
55 #define M_LN10 2.30258509299404568402 /* ln(10) */
56 #define M_LOG2E 1.4426950408889634074 /* log2(e) = 1/ln(2) */
57 #define M_LOG10E 0.43429448190325182765 /* log10(e) = 1/ln(10) */
58 #define M_PI 3.14159265358979323846 /* Pythagorean constant "Pi" */
59 #define M_PI_2 1.57079632679489661923 /* Pi/2 */
60 #define M_PI_3 1.04719755119659774615 /* Pi/3 */
61 #define M_PI_4 0.78539816339744830962 /* Pi/4 */
62 #define M_1_PI 0.31830988618379067154 /* 1/Pi */
63 #define M_2_PI 0.63661977236758134308 /* 2/Pi */
64 #define M_3_PI 0.954929658551372014613 /* 3/Pi */
65 #define M_4_PI 1.27323954473516268615 /* 4/Pi */
66 #define M_SQRTPI 1.77245385090551602730 /* Sqrt(Pi) */
67 #define M_1_SQRTPI 0.564189583547756286948 /* 1/Sqrt(Pi) */
68 #define M_2_SQRTPI 1.12837916709551257390 /* 2/Sqrt(Pi) */
69 #define M_3_SQRTPI 1.69256875064326886084 /* 3/Sqrt(Pi) */
70 #define M_4_SQRTPI 2.25675833419102514779 /* 4/Sqrt(Pi) */
71 #define M_SQRT2PI 2.50662827463100050242 /* Sqrt(2Pi) */
72 #define M_1_SQRT2PI 0.39894228040143267794 /* 1/Sqrt(2Pi) */
73 #define M_RAD2DEG 57.2957795130823208768 /* 180/Pi radians --> degrees */
74 #define M_DEG2RAD 0.0174532925199432957692 /* Pi/180 degrees --> radians */
75 #define M_SQRT2 1.41421356237309504880 /* Sqrt(2) */
76 #define M_SQRT_2 0.707106781186547524401 /* 1/Sqrt(2) */
77 #define M_GOLDEN_R 0.6180339887498948482046 /* GoldenRatio: (Sqrt(5) - 1)/2 */
78 #endif
79 
81 inline DT_DLL_MATRIX double DtRad2Deg(double rad)
82 {
83  return ((rad / M_PI) * 180.0);
84 }
85 
87 inline DT_DLL_MATRIX double DtDeg2Rad(double deg)
88 {
89  return ((deg * M_PI) / 180.0);
90 }
91 
93 template<typename T>
94 T DtAbs(T x)
95 {
96  return (((x) >= 0) ? (x) : (-(x)));
97 }
98 
100 template<typename T>
101 int DtSgn(T x)
102 {
103  return (((x) >= 0) ? 1 : -1);
104 }
105 
107 template<typename T>
108 T DtPos(T x)
109 {
110  return (((x) >= 0) ? (x) : 0);
111 }
112 
114 template <typename T>
115 T DtNeg(const T&val)
116 {
117  if (val >= 0) return 0;
118  else return val;
119 }
120 
125 template <typename T>
127 {
128  static const T theTwoPiRad = static_cast<T>(2.0) * static_cast<T>(M_PI);
129  while (deltaRad >= static_cast<T>(M_PI))
130  {
131  deltaRad -= theTwoPiRad;
132  }
133  while (deltaRad < -static_cast<T>(M_PI))
134  {
135  deltaRad += theTwoPiRad;
136  }
137  return deltaRad;
138 }
139 
141 template <typename T>
142 T DtSquare(const T& val)
143 {
144  return val * val;
145 }
146 
148 template <typename T>
149 T DtLimit(T x,T lo ,T hi)
150 {
151  if(lo > hi)
152  {
154  return DtLimit(x,hi,lo);
155  }
156  if(x > hi)
157  {
158  return hi;
159  }
160  if(x < lo)
161  {
162  return lo;
163  }
164  return x;
165 }
166 
168 template <typename T>
169 inline T DtMod(T a, T b)
170 {
171  return a % b;
172 }
173 
175 template <>
176 inline float DtMod(float a, float b)
177 {
178  return (float)fmod((float)a,(float)b);
179 }
180 
182 template <>
183 inline double DtMod(double a,double b)
184 {
185  return fmod(a,b);
186 }
187 
189 template <typename T>
190 T DtGCD(T a, T b)
191 {
192  if(a == 0) DtTHROW_NEW(DtInvalidInput,"Can't divide by zero");
193  if(b == 0) DtTHROW_NEW(DtInvalidInput,"Can't divide by zero");
194  while( true )
195  {
196  a = DtMod(a,b);
197  if( a == 0 ) return b;
198  b = DtMod(b,a);
199  if( b == 0 ) return a;
200  }
201 }
202 
204 template <typename T>
205 T DtLCM(T a,T b)
206 {
207  if(a == 0) DtTHROW_NEW(DtInvalidInput,"Can't divide by zero");
208  if(b == 0) DtTHROW_NEW(DtInvalidInput,"Can't divide by zero");
209  return (a * b) / DtGCD(a,b);
210 }
211 
212 #ifdef DtUSE_UTILITIES_NAMESPACE
213 }
214 #endif
215 #endif
216 
This file declares vlException.
T DtSquare(const T &val)
Returns the square of x for type T.
Definition: vlMath.h:142
double DtRad2Deg(double rad)
Converts Radians to Degrees.
Definition: vlMath.h:81
T DtShortestAngleDeltaRad(T deltaRad)
Normalizes an angle delta in radians to the range [-Pi, Pi).
Definition: vlMath.h:126
A Templated version of DtException.
Definition: vlExceptions.h:104
int DtSgn(T x)
Returns the sign of X for type T.
Definition: vlMath.h:101
T DtLimit(T x, T lo, T hi)
if x is outside the limits (hi and lo) then x is set to the nearest limit
Definition: vlMath.h:149
T DtGCD(T a, T b)
Returns the greatest common divisor of a and b for type T.
Definition: vlMath.h:190
#define DtTHROW_NEW(exceptionClass, errorMsg)
Throws a new DtException using the class name and the error message.
Definition: vlExceptions.h:189
#define DT_DLL_MATRIX
Definition: vlMachineTypes.h:119
T DtNeg(const T &val)
Returns x if x is less than zero for type T.
Definition: vlMath.h:115
T DtLCM(T a, T b)
Returns the least common multiple of a and b for Type T.
Definition: vlMath.h:205
T DtPos(T x)
Returns x if greater than zero.
Definition: vlMath.h:108
double DtDeg2Rad(double deg)
Converts Degrees to Radians.
Definition: vlMath.h:87
#define M_PI
Definition: vlMath.h:58
T DtMod(T a, T b)
Returns the remainder of a / b for type T.
Definition: vlMath.h:169
T DtAbs(T x)
Returns the absolute value of x for type T.
Definition: vlMath.h:94
This file contains typedefs for machine dependant types.

Document ID: Generated on Mon Sep 28 00:09:32 EDT 2026 from SVN revision 295353
Copyright © 1992-2025 MAK Technologies. All Rights Reserved (www.mak.com)