PeriDyno 1.0.0
Loading...
Searching...
No Matches
SimpleMath.h
Go to the documentation of this file.
1#pragma once
2#ifdef CUDA_BACKEND
3 #include <cuda_runtime.h>
4#endif
5
6#include "Vector.h"
7#include "Matrix.h"
8
9namespace dyno
10{
11 // Only floating point should call these functions.
12 template <typename T>
13 inline DYN_FUNC int isless (T const& a, T const& b, T const EPS = EPSILON) { return (a + EPS < b); }
14 template <typename T, typename D, typename std::enable_if<!std::is_same<T, D>::value, int>::type = 0>
15 inline DYN_FUNC int isless (T const& a, D const& b, T const EPS = EPSILON) { return isless(a, static_cast<T>(b), EPS); }
16
17 template <typename T>
18 inline DYN_FUNC int isleq (T const& a, T const& b, T const EPS = EPSILON) { return (a < b + EPS); }
19 template <typename T, typename D, typename std::enable_if<!std::is_same<T, D>::value, int>::type = 0>
20 inline DYN_FUNC int isleq (T const& a, D const& b, T const EPS = EPSILON) { return isleq(a, static_cast<T>(b), EPS); }
21
22 template <typename T>
23 inline DYN_FUNC int isgreat (T const& a, T const& b, T const EPS = EPSILON) { return (a > b + EPS); }
24 template <typename T, typename D, typename std::enable_if<!std::is_same<T, D>::value, int>::type = 0>
25 inline DYN_FUNC int isgreat (T const& a, D const& b, T const EPS = EPSILON) { return isgreat(a, static_cast<T>(b), EPS); }
26
27 template <typename T>
28 inline DYN_FUNC int isgeq (T const& a, T const& b, T const EPS = EPSILON) { return (a + EPS > b); }
29 template <typename T, typename D, typename std::enable_if<!std::is_same<T, D>::value, int>::type = 0>
30 inline DYN_FUNC int isgeq (T const& a, D const& b, T const EPS = EPSILON) { return isgeq(a, static_cast<T>(b), EPS); }
31
32 template <typename T>
33 inline DYN_FUNC int iseq (T const& a, T const& b, T const EPS = EPSILON) { return (b < a + EPS && a < b + EPS); }
34 template <typename T, typename D, typename std::enable_if<!std::is_same<T, D>::value, int>::type = 0>
35 inline DYN_FUNC int iseq (T const& a, D const& b, T const EPS = EPSILON) { return iseq(a, static_cast<T>(b), EPS); }
36
37 // <:-1 =:0 >:1
38 template <typename T>
39 inline DYN_FUNC int sign (T const& a, T const EPS = EPSILON) { return isgreat(a, static_cast<T>(0.f), EPS) - isless(a, static_cast<T>(0.f), EPS);}
40
41 template <typename T>
42 inline DYN_FUNC T clamp(const T& v, const T& lo, const T& hi)
43 {
44 return (v < lo) ? lo : (hi < v) ? hi : v;
45 }
46
47 template <typename T>
48 inline DYN_FUNC Vector<T,2> clamp(const Vector<T,2>& v, const Vector<T,2>& lo, const Vector<T,2>& hi)
49 {
50 Vector<T,2> ret;
51 ret[0] = (v[0] < lo[0]) ? lo[0] : (hi[0] < v[0]) ? hi[0] : v[0];
52 ret[1] = (v[1] < lo[1]) ? lo[1] : (hi[1] < v[1]) ? hi[1] : v[1];
53
54 return ret;
55 }
56
57 template <typename T>
58 inline DYN_FUNC Vector<T, 3> clamp(const Vector<T, 3>& v, const Vector<T, 3>& lo, const Vector<T, 3>& hi)
59 {
60 Vector<T, 3> ret;
61 ret[0] = (v[0] < lo[0]) ? lo[0] : (hi[0] < v[0]) ? hi[0] : v[0];
62 ret[1] = (v[1] < lo[1]) ? lo[1] : (hi[1] < v[1]) ? hi[1] : v[1];
63 ret[2] = (v[2] < lo[2]) ? lo[2] : (hi[2] < v[2]) ? hi[2] : v[2];
64
65 return ret;
66 }
67
68 template <typename T>
69 inline DYN_FUNC Vector<T, 4> clamp(const Vector<T, 4>& v, const Vector<T, 4>& lo, const Vector<T, 4>& hi)
70 {
71 Vector<T, 3> ret;
72 ret[0] = (v[0] < lo[0]) ? lo[0] : (hi[0] < v[0]) ? hi[0] : v[0];
73 ret[1] = (v[1] < lo[1]) ? lo[1] : (hi[1] < v[1]) ? hi[1] : v[1];
74 ret[2] = (v[2] < lo[2]) ? lo[2] : (hi[2] < v[2]) ? hi[2] : v[2];
75 ret[3] = (v[3] < lo[3]) ? lo[3] : (hi[3] < v[3]) ? hi[3] : v[3];
76
77 return ret;
78 }
79
80 template <typename T>
81 inline DYN_FUNC T abs(const T& v)
82 {
83 return v < T(0) ? - v : v;
84 }
85
86 template <typename T>
87 inline DYN_FUNC Vector<T, 2> abs(const Vector<T, 2>& v)
88 {
89 Vector<T, 2> ret;
90 ret[0] = (v[0] < T(0)) ? -v[0] : v[0];
91 ret[1] = (v[1] < T(0)) ? -v[1] : v[1];
92
93 return ret;
94 }
95
96 template <typename T>
97 inline DYN_FUNC Vector<T, 3> abs(const Vector<T, 3>& v)
98 {
99 Vector<T, 3> ret;
100 ret[0] = (v[0] < T(0)) ? -v[0] : v[0];
101 ret[1] = (v[1] < T(0)) ? -v[1] : v[1];
102 ret[2] = (v[2] < T(0)) ? -v[2] : v[2];
103
104 return ret;
105 }
106
107 template <typename T>
108 inline DYN_FUNC Vector<T, 4> abs(const Vector<T, 4>& v)
109 {
110 Vector<T, 3> ret;
111 ret[0] = (v[0] < T(0)) ? -v[0] : v[0];
112 ret[1] = (v[1] < T(0)) ? -v[1] : v[1];
113 ret[2] = (v[2] < T(0)) ? -v[2] : v[2];
114 ret[3] = (v[3] < T(0)) ? -v[3] : v[3];
115
116 return ret;
117 }
118
119 template <typename T>
120 inline DYN_FUNC T minimum(const T& v0, const T& v1)
121 {
122 return v0 < v1 ? v0 : v1;
123 }
124
125 template <typename T>
126 inline DYN_FUNC Vector<T, 2> minimum(const Vector<T, 2>& v0, const Vector<T, 2>& v1)
127 {
128 Vector<T, 2> ret;
129 ret[0] = (v0[0] < v1[0]) ? v0[0] : v1[0];
130 ret[1] = (v0[1] < v1[1]) ? v0[1] : v1[1];
131
132 return ret;
133 }
134
135 template <typename T>
136 inline DYN_FUNC Vector<T, 3> minimum(const Vector<T, 3>& v0, const Vector<T, 3>& v1)
137 {
138 Vector<T, 3> ret;
139 ret[0] = (v0[0] < v1[0]) ? v0[0] : v1[0];
140 ret[1] = (v0[1] < v1[1]) ? v0[1] : v1[1];
141 ret[2] = (v0[2] < v1[2]) ? v0[2] : v1[2];
142
143 return ret;
144 }
145
146 template <typename T>
147 inline DYN_FUNC Vector<T, 4> minimum(const Vector<T, 4>& v0, const Vector<T, 4>& v1)
148 {
149 Vector<T, 4> ret;
150 ret[0] = (v0[0] < v1[0]) ? v0[0] : v1[0];
151 ret[1] = (v0[1] < v1[1]) ? v0[1] : v1[1];
152 ret[2] = (v0[2] < v1[2]) ? v0[2] : v1[2];
153 ret[3] = (v0[3] < v1[3]) ? v0[3] : v1[3];
154
155 return ret;
156 }
157
158
159 template <typename T>
160 inline DYN_FUNC T maximum(const T& v0, const T& v1)
161 {
162 return v0 > v1 ? v0 : v1;
163 }
164
165 template <typename T>
166 inline DYN_FUNC Vector<T, 2> maximum(const Vector<T, 2>& v0, const Vector<T, 2>& v1)
167 {
168 Vector<T, 2> ret;
169 ret[0] = (v0[0] > v1[0]) ? v0[0] : v1[0];
170 ret[1] = (v0[1] > v1[1]) ? v0[1] : v1[1];
171
172 return ret;
173 }
174
175 template <typename T>
176 inline DYN_FUNC Vector<T, 3> maximum(const Vector<T, 3>& v0, const Vector<T, 3>& v1)
177 {
178 Vector<T, 3> ret;
179 ret[0] = (v0[0] > v1[0]) ? v0[0] : v1[0];
180 ret[1] = (v0[1] > v1[1]) ? v0[1] : v1[1];
181 ret[2] = (v0[2] > v1[2]) ? v0[2] : v1[2];
182
183 return ret;
184 }
185
186 template <typename T>
187 inline DYN_FUNC Vector<T, 4> maximum(const Vector<T, 4>& v0, const Vector<T, 4>& v1)
188 {
189 Vector<T, 4> ret;
190 ret[0] = (v0[0] > v1[0]) ? v0[0] : v1[0];
191 ret[1] = (v0[1] > v1[1]) ? v0[1] : v1[1];
192 ret[2] = (v0[2] > v1[2]) ? v0[2] : v1[2];
193 ret[3] = (v0[3] > v1[3]) ? v0[3] : v1[3];
194
195 return ret;
196 }
197
198 template <typename T>
199 inline DYN_FUNC T dot(Vector<T, 2> const& U, Vector<T, 2> const& V)
200 {
201 return U[0] * V[0] + U[1] * V[1];
202 }
203
204 template <typename T>
205 inline DYN_FUNC T dot(Vector<T, 3> const& U, Vector<T, 3> const& V)
206 {
207 return U[0] * V[0] + U[1] * V[1] + U[2] * V[2];
208 }
209
210 template <typename T>
211 inline DYN_FUNC Vector<T, 3> cross(Vector<T, 3> const& U, Vector<T, 3> const& V)
212 {
213 return Vec3f{
214 U[1] * V[2] - U[2] * V[1],
215 U[2] * V[0] - U[0] * V[2],
216 U[0] * V[1] - U[1] * V[0]
217 };
218 }
219
220 template <typename T>
221 inline DYN_FUNC T dotcross(Vector<T, 3> const& U, Vector<T, 3> const& V, Vector<T, 3> const& W)
222 {
223 // U (V x W)
224 return dot(U, cross(V, W));
225 }
226
227 template <typename T>
228 inline DYN_FUNC Vector<T, 2> perp(Vector<T, 2> const& v)
229 {
230 return Vector<T, 2> {v[1], -v[0]};
231 }
232
233 template <typename T>
234 inline DYN_FUNC T dotperp(Vector<T, 2> const& v0, Vector<T, 2> const& v1)
235 {
236 return dot(v0, perp(v1));
237 }
238
239 // return the lowest bit of x (1<<y)
240 inline DYN_FUNC unsigned int lowbit(unsigned int x) {return x & (-x);}
241
242 // return how many 1-bit in x
243 inline DYN_FUNC unsigned int countbit(unsigned int x) { unsigned int cnt = 0; while(x) {x -= lowbit(x); cnt++;} return cnt;}
244
245 // Count Leading Zeros
246 inline DYN_FUNC unsigned int __builtin_clz(unsigned int x) {
247 unsigned int r = 0;
248 if (!(x & 0xFFFF0000)) r += 16, x <<= 16;
249 if (!(x & 0xFF000000)) r += 8, x <<= 8;
250 if (!(x & 0xF0000000)) r += 4, x <<= 4;
251 if (!(x & 0xC0000000)) r += 2, x <<= 2;
252 if (!(x & 0x80000000)) r += 1, x <<= 1;
253 return r;
254 }
255
256 // Most Significant Bit: return y for 1 << y ( assert unsigned int is 32-bits)
257 inline DYN_FUNC unsigned int MSB(unsigned int x) {return 32 - __builtin_clz(x);}
258
259 // return the id of lowest bit of x
260 inline DYN_FUNC unsigned int lownum(unsigned int x) {return MSB(lowbit(x));}
261
262 // check big (x & 1<<y)
263 inline DYN_FUNC int checkbit(unsigned int const&x, unsigned int const& y) {return (x >> y) & 1u;}
264
265}
#define V(a, b, c)
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
DYN_FUNC int sign(T const &a, T const EPS=EPSILON)
Definition SimpleMath.h:39
DYN_FUNC int isleq(T const &a, T const &b, T const EPS=EPSILON)
Definition SimpleMath.h:18
DYN_FUNC Vector< T, 2 > perp(Vector< T, 2 > const &v)
Definition SimpleMath.h:228
DYN_FUNC T dotperp(Vector< T, 2 > const &v0, Vector< T, 2 > const &v1)
Definition SimpleMath.h:234
DYN_FUNC Vector< T, 3 > cross(Vector< T, 3 > const &U, Vector< T, 3 > const &V)
Definition SimpleMath.h:211
DYN_FUNC T dot(Vector< T, 2 > const &U, Vector< T, 2 > const &V)
Definition SimpleMath.h:199
DYN_FUNC int isless(T const &a, T const &b, T const EPS=EPSILON)
Definition SimpleMath.h:13
DYN_FUNC T abs(const T &v)
Definition SimpleMath.h:81
DYN_FUNC unsigned int lowbit(unsigned int x)
Definition SimpleMath.h:240
DYN_FUNC unsigned int __builtin_clz(unsigned int x)
Definition SimpleMath.h:246
constexpr Real EPSILON
Definition Typedef.inl:42
DYN_FUNC T minimum(const T &v0, const T &v1)
Definition SimpleMath.h:120
DYN_FUNC T dotcross(Vector< T, 3 > const &U, Vector< T, 3 > const &V, Vector< T, 3 > const &W)
Definition SimpleMath.h:221
DYN_FUNC unsigned int MSB(unsigned int x)
Definition SimpleMath.h:257
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
DYN_FUNC int isgreat(T const &a, T const &b, T const EPS=EPSILON)
Definition SimpleMath.h:23
DYN_FUNC T clamp(const T &v, const T &lo, const T &hi)
Definition SimpleMath.h:42
DYN_FUNC T maximum(const T &v0, const T &v1)
Definition SimpleMath.h:160
DYN_FUNC int isgeq(T const &a, T const &b, T const EPS=EPSILON)
Definition SimpleMath.h:28
DYN_FUNC int checkbit(unsigned int const &x, unsigned int const &y)
Definition SimpleMath.h:263
DYN_FUNC unsigned int countbit(unsigned int x)
Definition SimpleMath.h:243
DYN_FUNC int iseq(T const &a, T const &b, T const EPS=EPSILON)
Definition SimpleMath.h:33
DYN_FUNC unsigned int lownum(unsigned int x)
Definition SimpleMath.h:260