Astaroth  2.2
math_utils.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2020, Johannes Pekkila, Miikka Vaisala.
3 
4  This file is part of Astaroth.
5 
6  Astaroth is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Astaroth is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Astaroth. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
27 #pragma once
28 #include <math.h> // isnan, isinf
29 #include <stdlib.h> // rand
30 
31 #if AC_DOUBLE_PRECISION != 1
32 #define exp(x) expf(x)
33 #define sin(x) sinf(x)
34 #define cos(x) cosf(x)
35 #define sqrt(x) sqrtf(x)
36 #endif
37 
38 template <class T>
39 static inline const T
40 max(const T& a, const T& b)
41 {
42  return a > b ? a : b;
43 }
44 
45 template <class T>
46 static inline const T
47 min(const T& a, const T& b)
48 {
49  return a < b ? a : b;
50 }
51 
52 static inline const int3
53 max(const int3& a, const int3& b)
54 {
55  return (int3){max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)};
56 }
57 
58 static inline const int3
59 min(const int3& a, const int3& b)
60 {
61  return (int3){min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)};
62 }
63 
64 template <class T>
65 static inline const T
66 sum(const T& a, const T& b)
67 {
68  return a + b;
69 }
70 
71 template <class T>
72 static inline const T
73 clamp(const T& val, const T& min, const T& max)
74 {
75  return val < min ? min : val > max ? max : val;
76 }
77 
78 static inline AcReal
79 randr()
80 {
81  return AcReal(rand()) / AcReal(RAND_MAX);
82 }
83 
84 static inline bool
85 is_power_of_two(const unsigned val)
86 {
87  return val && !(val & (val - 1));
88 }
89 
90 #ifdef __CUDACC__
91 #define HOST_DEVICE_INLINE __host__ __device__ __forceinline__
92 #else
93 #define HOST_DEVICE_INLINE inline
94 #endif // __CUDACC__
95 
97 operator+(const AcReal3& a, const AcReal3& b)
98 {
99  return (AcReal3){a.x + b.x, a.y + b.y, a.z + b.z};
100 }
101 
102 static HOST_DEVICE_INLINE int3
103 operator+(const int3& a, const int3& b)
104 {
105  return (int3){a.x + b.x, a.y + b.y, a.z + b.z};
106 }
107 
108 static HOST_DEVICE_INLINE int3 operator*(const int3& a, const int3& b)
109 {
110  return (int3){a.x * b.x, a.y * b.y, a.z * b.z};
111 }
112 
113 static HOST_DEVICE_INLINE void
114 operator+=(AcReal3& lhs, const AcReal3& rhs)
115 {
116  lhs.x += rhs.x;
117  lhs.y += rhs.y;
118  lhs.z += rhs.z;
119 }
120 
122 operator-(const AcReal3& a, const AcReal3& b)
123 {
124  return (AcReal3){a.x - b.x, a.y - b.y, a.z - b.z};
125 }
126 
127 static HOST_DEVICE_INLINE int3
128 operator-(const int3& a, const int3& b)
129 {
130  return (int3){a.x - b.x, a.y - b.y, a.z - b.z};
131 }
132 
134 operator-(const AcReal3& a)
135 {
136  return (AcReal3){-a.x, -a.y, -a.z};
137 }
138 
139 static HOST_DEVICE_INLINE void
140 operator-=(AcReal3& lhs, const AcReal3& rhs)
141 {
142  lhs.x -= rhs.x;
143  lhs.y -= rhs.y;
144  lhs.z -= rhs.z;
145 }
146 
147 static HOST_DEVICE_INLINE AcReal3 operator*(const AcReal& a, const AcReal3& b)
148 {
149  return (AcReal3){a * b.x, a * b.y, a * b.z};
150 }
151 
152 static HOST_DEVICE_INLINE AcReal3 operator*(const AcReal3& b, const AcReal& a)
153 {
154  return (AcReal3){a * b.x, a * b.y, a * b.z};
155 }
156 
158 dot(const AcReal3& a, const AcReal3& b)
159 {
160  return a.x * b.x + a.y * b.y + a.z * b.z;
161 }
162 
164 mul(const AcMatrix& aa, const AcReal3& x)
165 {
166  return (AcReal3){dot(aa.row[0], x), dot(aa.row[1], x), dot(aa.row[2], x)};
167 }
168 
170 cross(const AcReal3& a, const AcReal3& b)
171 {
172  AcReal3 c;
173 
174  c.x = a.y * b.z - a.z * b.y;
175  c.y = a.z * b.x - a.x * b.z;
176  c.z = a.x * b.y - a.y * b.x;
177 
178  return c;
179 }
180 
181 static HOST_DEVICE_INLINE bool
182 is_valid(const AcReal& a)
183 {
184  return !isnan(a) && !isinf(a);
185 }
186 
187 static HOST_DEVICE_INLINE bool
188 is_valid(const AcReal3& a)
189 {
190  return is_valid(a.x) && is_valid(a.y) && is_valid(a.z);
191 }
HOST_DEVICE_INLINE
#define HOST_DEVICE_INLINE
Definition: math_utils.h:93
AcReal
float AcReal
Definition: astaroth.h:38
max
#define max(a, b)
Definition: verification.c:6
AcMatrix::row
AcReal3 row[3]
Definition: astaroth.h:46
min
#define min(a, b)
Definition: verification.c:7
AcMatrix
Definition: astaroth.h:45
AcReal3
float3 AcReal3
Definition: astaroth.h:39