NDEVR
API Documentation
misc.h
Go to the documentation of this file.
1#pragma once
2#include "Base/Headers/Angle.h"
3#include "macros.h"
4#include <cmath>
5
6/* utility helpers */
7
13
14namespace NDEVR
15{
16
20 template <typename T>
21 inline T square(T x)
22 {
23 return x * x;
24 }
25
29 template <typename T>
30 inline T hypot(T x, T y)
31 {
32 return (T)(sqrt(x * x + y * y));
33 }
34
38 template <typename T>
39 inline T hypot_sqr(T x, T y)
40 {
41 return x * x + y * y;
42 }
43
47 inline g_type deg2rad(g_type degree)
48 {
49 return degree * g_type(0.01745329251994329576);
50 }
51
55 inline g_type rad2deg(g_type rad)
56 {
57 return rad * g_type(57.29577951308232087721);
58 }
59
62 inline g_type average_angle(g_type theta1, g_type theta2)
63 {
64 g_type x, y;
65
66 x = cos(theta1) + cos(theta2);
67 y = sin(theta1) + sin(theta2);
68 if (x == 0 && y == 0)
69 return 0;
70 else
71 return std::atan2(y, x);
72 }
73
78 template <typename T>
79 inline int sign(T x)
80 {
81 if (x > 0)
82 return 1;
83 else if (x < 0)
84 return -1;
85 else
86 return 0;
87 }
88
92 template <typename T>
93 inline T clamp(T l, T x, T u)
94 {
95 if (x < l)
96 return l;
97 if (x > u)
98 return u;
99 return x;
100 }
101
105 template <typename T>
106 inline T wrap(T l, T x, T u)
107 {
108 T intervalWidth = u - l;
109 while (x < l)
110 x += intervalWidth;
111 while (x > u)
112 x -= intervalWidth;
113 return x;
114 }
115
119 inline bool arrayHasInvalid(const g_type* array, int size, int* nanIndex = 0)
120 {
121 for (int i = 0; i < size; ++i)
122 if (IsInvalid(array[i])) {
123 if (nanIndex)
124 *nanIndex = i;
125 return true;
126 }
127 return false;
128 }
129
133 extern "C"
134 {
135 typedef void (*ForceLinkFunction) (void);
136 }
137
140 {
143 ForceLinker(ForceLinkFunction function) { (function)(); }
144 };
145
146
147}
The primary namespace for the NDEVR SDK.
g_type rad2deg(g_type rad)
convert from radian to degree
Definition misc.h:55
T square(T x)
return the square value
Definition misc.h:21
bool arrayHasInvalid(const g_type *array, int size, int *nanIndex=0)
tests whether there is a Invalid in the array
Definition misc.h:119
constexpr t_type sign(t_type value)
A simple function that returns 1 for all values greater than or equal to 0 and -1 for all values less...
T wrap(T l, T x, T u)
wrap x to be in the interval [l, u]
Definition misc.h:106
g_type average_angle(g_type theta1, g_type theta2)
average two angles
Definition misc.h:62
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
T hypot(T x, T y)
return the hypot of x and y
Definition misc.h:30
T clamp(T l, T x, T u)
clamp x to the interval [l, u]
Definition misc.h:93
t_type sqrt(const t_type &value)
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type sin(const Angle< t_type > &angle)
Performs optimized sine operation on the given angle using pre-computed lookup table for optimal spee...
void(* ForceLinkFunction)(void)
The following two functions are used to force linkage with static libraries.
Definition misc.h:135
g_type deg2rad(g_type degree)
convert from degree to radian
Definition misc.h:47
T hypot_sqr(T x, T y)
return the squared hypot of x and y
Definition misc.h:39
std::enable_if<!ObjectInfo< t_type >::Float, fltp08 >::type cos(const Angle< t_type > &angle)
Performs optimized cosine operation on the given angle using pre-computed lookup table for optimal sp...
ForceLinker(ForceLinkFunction function)
Calls the provided function to force the linker to include the associated object file.
Definition misc.h:143