ai_math.h
Go to the documentation of this file.
1// Copyright 2021 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
29template <typename T>
30AI_DEVICE inline constexpr T AiMin(T a, T b)
31{
32#ifdef AI_CPU_COMPILER
33 return (a < b) ? a : b;
34#else
35 return min(a,b);
36#endif
37}
38
42template <typename T>
43AI_DEVICE inline constexpr T AiMax(T a, T b)
44{
45#ifdef AI_CPU_COMPILER
46 return (a > b) ? a : b;
47#else
48 return max(a,b);
49#endif
50}
51
55template <typename T>
56AI_DEVICE inline constexpr T AiMin(T a, T b, T c)
57{
58 return AiMin(AiMin(a, b), c);
59}
60
64template <typename T>
65AI_DEVICE inline constexpr T AiMax(T a, T b, T c)
66{
67 return AiMax(AiMax(a, b), c);
68}
69
73template <typename T>
74AI_DEVICE inline constexpr T AiMin(T a, T b, T c, T d)
75{
76 return AiMin(AiMin(a, b), AiMin(c, d));
77}
78
82template <typename T>
83AI_DEVICE inline constexpr T AiMax(T a, T b, T c, T d)
84{
85 return AiMax(AiMax(a, b), AiMax(c, d));
86}
87
91template <typename T>
92AI_DEVICE inline constexpr T AiSqr(T a)
93{
94 return (a * a);
95}
96
100template <typename T>
101AI_DEVICE inline constexpr T AiClamp(T v, T lo, T hi)
102{
103 return AiMax(lo, AiMin(v, hi));
104}
105
109template <typename T>
110AI_DEVICE inline constexpr T AiSafeAcos(T x)
111{
112 return (x >= 1) ? 0 : ((x <= -1) ? static_cast<T>(AI_PI) : std::acos(x));
113}
114
118template <typename T1, typename T2>
119AI_DEVICE inline constexpr T1 AiLerp(T2 t, T1 a, T1 b)
120{
121 return ((T2(1) - t) * a) + (b * t);
122}
123
127template <typename T>
128AI_DEVICE inline constexpr T AiHerp01(T t)
129{
130 return t * t * ((T) 3 - (T) 2 * t);
131}
132
136template <typename T1, typename T2>
137AI_DEVICE inline constexpr T1 AiHerp(T2 t, T1 a, T1 b)
138{
139 return AiLerp(AiHerp01(t), a, b);
140}
141
145template <typename T1, typename T2>
146AI_DEVICE inline T1 AiBiLerp(T2 s, T2 t, T1 c00, T1 c10, T1 c01, T1 c11)
147{
148 T1 c0x = AiLerp(t, c00, c01);
149 T1 c1x = AiLerp(t, c10, c11);
150 return AiLerp(s, c0x, c1x);
151}
152
156template <typename T>
157AI_DEVICE inline constexpr T AiBias(T a, T b)
158{
159 return (a > 0) ? ((b == (T) 0.5) ? a : std::pow(a, (std::log(b) * (T)-1.442695041))) : 0;
160}
161
165template <typename T>
166AI_DEVICE inline constexpr T AiGain(T a, T g)
167{
168 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));
169}
170
174template <typename T>
175AI_DEVICE inline constexpr T AiStep(T x, T e)
176{
177 return (x < e) ? (T) 0 : (T) 1;
178}
179
183template <typename T>
184AI_DEVICE inline constexpr T AiFilterStep(T x, T e, T w)
185{
186 return AiClamp((x + w * (T) 0.5 - e) / w, (T) 0, (T) 1);
187}
188#ifdef AI_GPU_COMPILER
189AI_DEVICE inline float AiFilterStep(float x, float e, float w)
190{
191 return __saturatef((x + w * 0.5f - e) / w);
192}
193#endif
194
195
201template <typename T>
202AI_DEVICE inline constexpr T AiLinearStep(T lo, T hi, T t)
203{
204 return AiClamp((t - lo) / (hi - lo), (T) 0, (T) 1);
205}
206#ifdef AI_GPU_COMPILER
207AI_DEVICE inline float AiLinearStep(float lo, float hi, float t)
208{
209 return __saturatef((t - lo) / (hi - lo));
210}
211#endif
212
218template <typename T>
219AI_DEVICE inline constexpr T AiSmoothStep(T e0, T e1, T t)
220{
221 return AiHerp01(AiClamp((t - e0) / (e1 - e0), (T) 0, (T) 1));
222}
223#ifdef AI_GPU_COMPILER
224AI_DEVICE inline float AiSmoothStep(float e0, float e1, float t)
225{
226 return AiHerp01(__saturatef((t - e0) / (e1 - e0)));
227}
228#endif
229
239template <typename OUT_T, typename IN_T>
240AI_DEVICE inline OUT_T reinterpret_type(const IN_T in)
241{
242 OUT_T out;
243 std::memcpy(&out, &in, sizeof(OUT_T));
244 return out;
245}
246
247AI_API AI_DEVICE AI_CONST bool AiIsFinite(float x);
248AI_API AI_CONST float AiFastExp(float x);
249AI_API AI_DEVICE AI_CONST float AiFastPow(float x, float y);
250
251/*\}*/
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:119
AI_DEVICE constexpr T AiBias(T a, T b)
Bias function.
Definition: ai_math.h:157
AI_DEVICE constexpr T AiHerp01(T t)
Hermite interpolation between 0 and 1 using 't' (0<=t<=1)
Definition: ai_math.h:128
AI_DEVICE constexpr T AiStep(T x, T e)
Step function.
Definition: ai_math.h:175
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:101
AI_DEVICE constexpr T AiLinearStep(T lo, T hi, T t)
Linearly interpolated step from 'lo' to 'hi'.
Definition: ai_math.h:202
AI_DEVICE constexpr T AiMin(T a, T b)
Minimum of 'a' and 'b'.
Definition: ai_math.h:30
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:240
AI_DEVICE constexpr T AiSqr(T a)
Square of 'a'.
Definition: ai_math.h:92
AI_DEVICE constexpr T AiFilterStep(T x, T e, T w)
Filtered-step function.
Definition: ai_math.h:184
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:146
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:137
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:110
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:219
AI_DEVICE constexpr T AiGain(T a, T g)
Gain function.
Definition: ai_math.h:166
AI_DEVICE constexpr T AiMax(T a, T b)
Maximum of 'a' and 'b'.
Definition: ai_math.h:43

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