ai_math.h
Go to the documentation of this file.
1// Copyright 2025 Autodesk, Inc. All rights reserved.
2//
3// Use of this software is subject to the terms of the Autodesk license
4// agreement provided at the time of installation or download, or which
5// otherwise accompanies this software in either electronic or hard copy form.
6
12#pragma once
13#include "ai_api.h"
14#include "ai_constants.h"
15#include <cmath>
16#include <cstdlib> // for int abs(int)
17#include <cstring> // for memcpy
18#include <type_traits>
19
30template <typename T>
31AI_DEVICE inline constexpr T AiMin(T a, T b)
32{
33#ifdef AI_CPU_COMPILER
34 return (a < b) ? a : b;
35#else
36 return min(a,b);
37#endif
38}
39
43template <typename T>
44AI_DEVICE inline constexpr T AiMax(T a, T b)
45{
46#ifdef AI_CPU_COMPILER
47 return (a > b) ? a : b;
48#else
49 return max(a,b);
50#endif
51}
52
56template <typename T>
57AI_DEVICE inline constexpr T AiMin(T a, T b, T c)
58{
59 return AiMin(AiMin(a, b), c);
60}
61
65template <typename T>
66AI_DEVICE inline constexpr T AiMax(T a, T b, T c)
67{
68 return AiMax(AiMax(a, b), c);
69}
70
74template <typename T>
75AI_DEVICE inline constexpr T AiMin(T a, T b, T c, T d)
76{
77 return AiMin(AiMin(a, b), AiMin(c, d));
78}
79
83template <typename T>
84AI_DEVICE inline constexpr T AiMax(T a, T b, T c, T d)
85{
86 return AiMax(AiMax(a, b), AiMax(c, d));
87}
88
92template <typename T>
93AI_DEVICE inline constexpr T AiSqr(T a)
94{
95 return (a * a);
96}
97
101template <typename T>
102AI_DEVICE inline constexpr T AiClamp(T v, T lo, T hi)
103{
104 return AiMax(lo, AiMin(v, hi));
105}
106
110template <typename T>
111AI_DEVICE inline constexpr T AiSafeAcos(T x)
112{
113 return (x >= 1) ? 0 : ((x <= -1) ? static_cast<T>(AI_PI) : std::acos(x));
114}
115
119template <typename T1, typename T2>
120AI_DEVICE inline constexpr T1 AiLerp(T2 t, T1 a, T1 b)
121{
122 return ((T2(1) - t) * a) + (b * t);
123}
124
128template <typename T>
129AI_DEVICE inline constexpr T AiHerp01(T t)
130{
131 return t * t * ((T) 3 - (T) 2 * t);
132}
133
137template <typename T1, typename T2>
138AI_DEVICE inline constexpr T1 AiHerp(T2 t, T1 a, T1 b)
139{
140 return AiLerp(AiHerp01(t), a, b);
141}
142
146template <typename T1, typename T2>
147AI_DEVICE inline T1 AiBiLerp(T2 s, T2 t, T1 c00, T1 c10, T1 c01, T1 c11)
148{
149 T1 c0x = AiLerp(t, c00, c01);
150 T1 c1x = AiLerp(t, c10, c11);
151 return AiLerp(s, c0x, c1x);
152}
153
157template <typename T>
158AI_DEVICE inline constexpr T AiBias(T a, T b)
159{
160 return (a > 0) ? ((b == (T) 0.5) ? a : std::pow(a, (std::log(b) * (T)-1.442695041))) : 0;
161}
162
166template <typename T>
167AI_DEVICE inline constexpr T AiGain(T a, T g)
168{
169 return (g == (T) 0.5) ? a : ((a < (T) 0.5) ? (AiBias(a + a, (T) 1 - g) * (T) 0.5) : ((T) 1 - AiBias((T) 2 - a - a, (T) 1 - g) * (T) 0.5));
170}
171
175template <typename T>
176AI_DEVICE inline constexpr T AiStep(T x, T e)
177{
178 return (x < e) ? (T) 0 : (T) 1;
179}
180
184template <typename T>
185AI_DEVICE inline constexpr T AiFilterStep(T x, T e, T w)
186{
187 return AiClamp((x + w * (T) 0.5 - e) / w, (T) 0, (T) 1);
188}
189#ifdef AI_GPU_COMPILER
190AI_DEVICE inline float AiFilterStep(float x, float e, float w)
191{
192 return __saturatef((x + w * 0.5f - e) / w);
193}
194#endif
195
196
202template <typename T>
203AI_DEVICE inline constexpr T AiLinearStep(T lo, T hi, T t)
204{
205 return AiClamp((t - lo) / (hi - lo), (T) 0, (T) 1);
206}
207#ifdef AI_GPU_COMPILER
208AI_DEVICE inline float AiLinearStep(float lo, float hi, float t)
209{
210 return __saturatef((t - lo) / (hi - lo));
211}
212#endif
213
219template <typename T>
220AI_DEVICE inline constexpr T AiSmoothStep(T e0, T e1, T t)
221{
222 return AiHerp01(AiClamp((t - e0) / (e1 - e0), (T) 0, (T) 1));
223}
224#ifdef AI_GPU_COMPILER
225AI_DEVICE inline float AiSmoothStep(float e0, float e1, float t)
226{
227 return AiHerp01(__saturatef((t - e0) / (e1 - e0)));
228}
229#endif
230
240template <typename OUT_T, typename IN_T>
241AI_DEVICE inline OUT_T reinterpret_type(const IN_T in)
242{
243 static_assert(!std::is_pointer<IN_T>::value,
244 "Do not reinterpret pointers. Reinterpret the underlying data");
245 static_assert(sizeof(OUT_T) <= sizeof(IN_T), "Reinterpretation must not increase size of type");
246 OUT_T out;
247 std::memcpy(&out, &in, sizeof(OUT_T));
248 return out;
249}
250
251AI_API AI_DEVICE AI_CONST bool AiIsFinite(float x);
252AI_API AI_CONST float AiFastExp(float x);
253AI_API AI_DEVICE AI_CONST float AiFastPow(float x, float y);
254
255/*\}*/
DLL export prefix for API functions (necessary for multi-platform development)
Various useful constants.
#define AI_PI
pi
Definition: ai_constants.h:27
AI_DEVICE constexpr T1 AiLerp(T2 t, T1 a, T1 b)
Linear interpolation between 'a' and 'b' using 't' (0<=t<=1)
Definition: ai_math.h:120
AI_DEVICE constexpr T AiBias(T a, T b)
Bias function.
Definition: ai_math.h:158
AI_DEVICE constexpr T AiHerp01(T t)
Hermite interpolation between 0 and 1 using 't' (0<=t<=1)
Definition: ai_math.h:129
AI_DEVICE constexpr T AiStep(T x, T e)
Step function.
Definition: ai_math.h:176
AI_API AI_DEVICE AI_CONST float AiFastPow(float x, float y)
Fast, approximate powf() suitable for cases where performance is more critical than accuracy.
Definition: ai_tools.cpp:28
AI_DEVICE constexpr T AiClamp(T v, T lo, T hi)
Clamp the input to the specified range.
Definition: ai_math.h:102
AI_DEVICE constexpr T AiLinearStep(T lo, T hi, T t)
Linearly interpolated step from 'lo' to 'hi'.
Definition: ai_math.h:203
AI_DEVICE constexpr T AiMin(T a, T b)
Minimum of 'a' and 'b'.
Definition: ai_math.h:31
AI_DEVICE OUT_T reinterpret_type(const IN_T in)
This function can be used to go between different interpretations of the same bits.
Definition: ai_math.h:241
AI_DEVICE constexpr T AiSqr(T a)
Square of 'a'.
Definition: ai_math.h:93
AI_DEVICE constexpr T AiFilterStep(T x, T e, T w)
Filtered-step function.
Definition: ai_math.h:185
AI_DEVICE T1 AiBiLerp(T2 s, T2 t, T1 c00, T1 c10, T1 c01, T1 c11)
Bilinear interpolation between four float values using 's' and 't' (0<=st<=1)
Definition: ai_math.h:147
AI_DEVICE constexpr T1 AiHerp(T2 t, T1 a, T1 b)
Hermite interpolation between 'a' and 'b' using 't' (0<=t<=1)
Definition: ai_math.h:138
AI_API AI_CONST float AiFastExp(float x)
Fast, approximate expf() suitable for cases where performance is more critical than accuracy.
Definition: ai_tools.cpp:14
AI_DEVICE constexpr T AiSafeAcos(T x)
Safe arc cosine of 'x' (acos() returns NaN if x<-1 or x>1)
Definition: ai_math.h:111
AI_API AI_DEVICE AI_CONST bool AiIsFinite(float x)
Test to see if the given floating point number is finite (not NaN and not infinite) as defined by the...
Definition: ai_tools.cpp:48
AI_DEVICE constexpr T AiSmoothStep(T e0, T e1, T t)
RenderMan's smoothstep() function.
Definition: ai_math.h:220
AI_DEVICE constexpr T AiGain(T a, T g)
Gain function.
Definition: ai_math.h:167
AI_DEVICE constexpr T AiMax(T a, T b)
Maximum of 'a' and 'b'.
Definition: ai_math.h:44

© 2023 Autodesk, Inc. · All rights reserved · www.arnoldrenderer.com