UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MathUtil.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "GeometryBase.h"
6#include "HAL/Platform.h"
7#include "EngineDefines.h"
8
9#include <cmath>
10#include <cfloat>
11
15template<typename RealType>
17
18template<>
20{
22 static constexpr float Epsilon = FLT_EPSILON;
24 static constexpr float ZeroTolerance = 1e-06f;
25
27 static constexpr float MaxReal = FLT_MAX;
28
30 static constexpr float SafeLargeValue = UE_LARGE_WORLD_MAX;
31
33 static constexpr float Pi = 3.1415926535897932384626433832795f;
34 static constexpr float FourPi = 4.0f * Pi;
35 static constexpr float TwoPi = 2.0f*Pi;
36 static constexpr float HalfPi = 0.5f*Pi;
37
39 static constexpr float InvPi = 1.0f / Pi;
41 static constexpr float InvTwoPi = 1.0f / TwoPi;
42
44 static constexpr float DegToRad = Pi / 180.0f;
46 static constexpr float RadToDeg = 180.0f / Pi;
47
48 //static constexpr float LN_2;
49 //static constexpr float LN_10;
50 //static constexpr float INV_LN_2;
51 //static constexpr float INV_LN_10;
52
53 static constexpr float Sqrt2 = 1.4142135623730950488016887242097f;
54 static constexpr float InvSqrt2 = 1.0f / Sqrt2;
55 static constexpr float Sqrt3 = 1.7320508075688772935274463415059f;
56 static constexpr float InvSqrt3 = 1.0f / Sqrt3;
57};
58
59template<>
61{
63 static constexpr double Epsilon = DBL_EPSILON;
65 static constexpr double ZeroTolerance = 1e-08;
66
68 static constexpr double MaxReal = DBL_MAX;
69
71 static constexpr double SafeLargeValue = (double)FLT_MAX;
72
74 static constexpr double Pi = 3.1415926535897932384626433832795;
75 static constexpr double FourPi = 4.0 * Pi;
76 static constexpr double TwoPi = 2.0 * Pi;
77 static constexpr double HalfPi = 0.5 * Pi;
78
80 static constexpr double InvPi = 1.0 / Pi;
82 static constexpr double InvTwoPi = 1.0 / TwoPi;
83
85 static constexpr double DegToRad = Pi / 180.0;
87 static constexpr double RadToDeg = 180.0 / Pi;
88
89 //static constexpr double LN_2;
90 //static constexpr double LN_10;
91 //static constexpr double INV_LN_2;
92 //static constexpr double INV_LN_10;
93
94 static constexpr double Sqrt2 = 1.4142135623730950488016887242097;
95 static constexpr double InvSqrt2 = 1.0 / Sqrt2;
96 static constexpr double Sqrt3 = 1.7320508075688772935274463415059;
97 static constexpr double InvSqrt3 = 1.0 / Sqrt3;
98};
99
100
101// we use TMathUtil<int> so we need to define these nonsense constants
102template<>
104{
105 static constexpr int32 Epsilon = 0;
106 static constexpr int32 ZeroTolerance = 0;
107 static constexpr int32 MaxReal = MAX_int32;
108 static constexpr int32 SafeLargeValue = MAX_int32/2;
109 static constexpr int32 Pi = 3;
110 static constexpr int32 FourPi = 4 * Pi;
111 static constexpr int32 TwoPi = 2 * Pi;
112 static constexpr int32 HalfPi = 1;
113 static constexpr int32 InvPi = 1;
114 static constexpr int32 InvTwoPi = 1;
115 static constexpr int32 DegToRad = 1;
116 static constexpr int32 RadToDeg = 1;
117 static constexpr int32 Sqrt2 = 1;
118 static constexpr int32 InvSqrt2 = 1;
119 static constexpr int32 Sqrt3 = 2;
120 static constexpr int32 InvSqrt3 = 1;
121};
122
123
124// we use TMathUtil<int> so we need to define these nonsense constants
125template<>
127{
128 static constexpr int64 Epsilon = 0;
129 static constexpr int64 ZeroTolerance = 0;
130 static constexpr int64 MaxReal = MAX_int64;
131 static constexpr int64 SafeLargeValue = MAX_int64/2;
132 static constexpr int64 Pi = 3;
133 static constexpr int64 FourPi = 4 * Pi;
134 static constexpr int64 TwoPi = 2 * Pi;
135 static constexpr int64 HalfPi = 1;
136 static constexpr int64 InvPi = 1;
137 static constexpr int64 InvTwoPi = 1;
138 static constexpr int64 DegToRad = 1;
139 static constexpr int64 RadToDeg = 1;
140 static constexpr int64 Sqrt2 = 1;
141 static constexpr int64 InvSqrt2 = 1;
142 static constexpr int64 Sqrt3 = 2;
143 static constexpr int64 InvSqrt3 = 1;
144};
145
146
147
148template<typename RealType>
149class TMathUtil : public TMathUtilConstants<RealType>
150{
151public:
152 static inline bool IsNaN(const RealType Value);
153 static inline bool IsFinite(const RealType Value);
154 static inline RealType Abs(const RealType Value);
155 static inline RealType Clamp(const RealType Value, const RealType ClampMin, const RealType ClampMax);
156 static inline RealType Sign(const RealType Value);
157 static inline int32 SignAsInt(const RealType Value);
158 static inline RealType SignNonZero(const RealType Value);
159 static inline RealType Max(const RealType A, const RealType B);
160 static inline RealType Max3(const RealType A, const RealType B, const RealType C);
161 static inline int32 Max3Index(const RealType A, const RealType B, const RealType C);
162 static inline RealType Min(const RealType A, const RealType B);
163 static inline RealType Min3(const RealType A, const RealType B, const RealType C);
164 static inline int32 Min3Index(const RealType A, const RealType B, const RealType C);
166 static inline void MinMax(RealType A, RealType B, RealType C, RealType& MinOut, RealType& MaxOut);
168 static inline RealType SmoothMin(RealType A, RealType B, RealType BlendExtent);
170 static inline RealType SmoothMax(RealType A, RealType B, RealType BlendExtent);
171 static inline RealType Sqrt(const RealType Value);
173 static inline RealType Cbrt(const RealType Value);
174 static inline RealType Tan(const RealType Value);
175 static inline RealType Atan2(const RealType ValueY, const RealType ValueX);
176 static inline RealType Sin(const RealType Value);
177 static inline RealType Cos(const RealType Value);
178 static inline RealType ACos(const RealType Value);
179 static inline RealType Floor(const RealType Value);
180 static inline RealType Ceil(const RealType Value);
181 static inline RealType Round(const RealType Value);
182 static inline RealType Pow(const RealType Value, const RealType Power);
183 static inline RealType Exp(const RealType Power);
184 static inline RealType Log(const RealType Value);
185 static inline RealType Lerp(const RealType A, const RealType B, RealType Alpha);
186
187
191 static inline RealType Atan2Positive(const RealType Y, const RealType X);
192
193private:
194 TMathUtil() = delete;
195};
198
199
200template<typename RealType>
202{
203 return std::isnan(Value);
204}
205
206
207template<typename RealType>
209{
210 return std::isfinite(Value);
211}
212
213
214template<typename RealType>
215RealType TMathUtil<RealType>::Abs(const RealType Value)
216{
217 return (Value >= (RealType)0) ? Value : -Value;
218}
219
220
221template<typename RealType>
222RealType TMathUtil<RealType>::Clamp(const RealType Value, const RealType ClampMin, const RealType ClampMax)
223{
224 return (Value < ClampMin) ? ClampMin : ((Value > ClampMax) ? ClampMax : Value);
225}
226
227template<typename RealType>
229{
230 return (Value > (RealType)0) ? 1 : ((Value < (RealType)0) ? -1 : 0);
231}
232
233template<typename RealType>
234RealType TMathUtil<RealType>::Sign(const RealType Value)
235{
236 return (RealType)SignAsInt(Value);
237}
238
239template<typename RealType>
241{
242 return (Value < (RealType)0) ? (RealType)-1 : (RealType)1;
243}
244
245template<typename RealType>
246RealType TMathUtil<RealType>::Max(const RealType A, const RealType B)
247{
248 return (A >= B) ? A : B;
249}
250
251template<typename RealType>
252RealType TMathUtil<RealType>::Max3(const RealType A, const RealType B, const RealType C)
253{
254 return Max(Max(A, B), C);
255}
256
257template<typename RealType>
258int32 TMathUtil<RealType>::Max3Index(const RealType A, const RealType B, const RealType C)
259{
260 if (A >= B)
261 {
262 return (A >= C) ? 0 : 2;
263 }
264 else
265 {
266 return (B >= C) ? 1 : 2;
267 }
268}
269
270template<typename RealType>
271RealType TMathUtil<RealType>::Min(const RealType A, const RealType B)
272{
273 return (A <= B) ? A : B;
274}
275
276template<typename RealType>
277RealType TMathUtil<RealType>::Min3(const RealType A, const RealType B, const RealType C)
278{
279 return Min(Min(A, B), C);
280}
281
282template<typename RealType>
283int32 TMathUtil<RealType>::Min3Index(const RealType A, const RealType B, const RealType C)
284{
285 if (A <= B)
286 {
287 return (A <= C) ? 0 : 2;
288 }
289 else
290 {
291 return (B <= C) ? 1 : 2;
292 }
293}
294
295// compute min and max of a,b,c with max 3 comparisons (sometimes 2)
296template<typename RealType>
297void TMathUtil<RealType>::MinMax(RealType A, RealType B, RealType C, RealType& MinOut, RealType& MaxOut)
298{
299 if (A < B) {
300 if (A < C) {
302 }
303 else {
304 MinOut = C; MaxOut = B;
305 }
306 }
307 else {
308 if (A > C) {
310 }
311 else {
312 MinOut = B; MaxOut = C;
313 }
314 }
315}
316
317template<typename RealType>
318RealType TMathUtil<RealType>::SmoothMin(RealType A, RealType B, RealType BlendExtent)
319{
320 if (BlendExtent == 0)
321 {
322 return Min(A, B);
323 }
324
325 double H = (BlendExtent + B - A) / 2;
326 return Min(B - H * Clamp(H / BlendExtent, 0.0, 1.0), A);
327}
328
329template<typename RealType>
330RealType TMathUtil<RealType>::SmoothMax(RealType A, RealType B, RealType BlendExtent)
331{
332 if (BlendExtent == 0)
333 {
334 return Max(A, B);
335 }
336
337 double H = (BlendExtent + A - B) / 2;
338 return Max(B + H * Clamp(H / BlendExtent, 0.0, 1.0), A);
339}
340
341template<typename RealType>
342RealType TMathUtil<RealType>::Sqrt(const RealType Value)
343{
344 return sqrt(Value);
345}
346
347template<typename RealType>
348RealType TMathUtil<RealType>::Cbrt(const RealType Value)
349{
350 return cbrt(Value);
351}
352
353template<typename RealType>
354RealType TMathUtil<RealType>::Tan(const RealType Value)
355{
356 return tan(Value);
357}
358
359template<typename RealType>
360RealType TMathUtil<RealType>::Atan2(const RealType ValueY, const RealType ValueX)
361{
362 return atan2(ValueY, ValueX);
363}
364
365template<typename RealType>
366RealType TMathUtil<RealType>::Sin(const RealType Value)
367{
368 return sin(Value);
369}
370
371template<typename RealType>
372RealType TMathUtil<RealType>::Cos(const RealType Value)
373{
374 return cos(Value);
375}
376
377template<typename RealType>
378RealType TMathUtil<RealType>::ACos(const RealType Value)
379{
380 return acos(Value);
381}
382
383template<typename RealType>
384RealType TMathUtil<RealType>::Floor(const RealType Value)
385{
386 return floor(Value);
387}
388
389template<typename RealType>
390RealType TMathUtil<RealType>::Ceil(const RealType Value)
391{
392 return ceil(Value);
393}
394
395template<typename RealType>
396RealType TMathUtil<RealType>::Round(const RealType Value)
397{
398 return round(Value);
399}
400
401template<typename RealType>
402RealType TMathUtil<RealType>::Pow(const RealType Value, const RealType Power)
403{
404 return pow(Value, Power);
405}
406
407template<typename RealType>
408RealType TMathUtil<RealType>::Exp(const RealType Power)
409{
410 return exp(Power);
411}
412
413template<typename RealType>
414RealType TMathUtil<RealType>::Log(const RealType Power)
415{
416 return log(Power);
417}
418
419
420
421template<typename RealType>
422RealType TMathUtil<RealType>::Lerp(const RealType A, const RealType B, RealType Alpha)
423{
424 Alpha = Clamp(Alpha, (RealType)0, (RealType)1);
425 return ((RealType)1 - Alpha)*A + (Alpha)*B;
426}
427
428template<typename RealType>
429RealType TMathUtil<RealType>::Atan2Positive(const RealType Y, const RealType X)
430{
431 // @todo this is a float atan2 !!
432 RealType Theta = TMathUtil<RealType>::Atan2(Y, X);
433 if (Theta < 0)
434 {
435 return ((RealType)2 * TMathUtil<RealType>::Pi) + Theta;
436 }
437 return Theta;
438}
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define UE_LARGE_WORLD_MAX
Definition EngineDefines.h:41
#define X(Name, Desc)
Definition FormatStringSan.h:47
TMathUtil< double > FMathd
Definition MathUtil.h:197
TMathUtil< float > FMathf
Definition MathUtil.h:196
#define MAX_int32
Definition NumericLimits.h:25
#define MAX_int64
Definition NumericLimits.h:26
USkinnedMeshComponent float
Definition SkinnedMeshComponent.h:60
float Sqrt2(float X)
Definition SubsurfaceProfile.cpp:428
Definition MathUtil.h:150
static RealType SmoothMin(RealType A, RealType B, RealType BlendExtent)
Definition MathUtil.h:318
static RealType Log(const RealType Value)
Definition MathUtil.h:414
static RealType Cos(const RealType Value)
Definition MathUtil.h:372
static RealType Cbrt(const RealType Value)
Definition MathUtil.h:348
static RealType Atan2(const RealType ValueY, const RealType ValueX)
Definition MathUtil.h:360
static bool IsFinite(const RealType Value)
Definition MathUtil.h:208
static RealType Sign(const RealType Value)
Definition MathUtil.h:234
static void MinMax(RealType A, RealType B, RealType C, RealType &MinOut, RealType &MaxOut)
Definition MathUtil.h:297
static RealType Lerp(const RealType A, const RealType B, RealType Alpha)
Definition MathUtil.h:422
static bool IsNaN(const RealType Value)
Definition MathUtil.h:201
static RealType Min3(const RealType A, const RealType B, const RealType C)
Definition MathUtil.h:277
static RealType Sin(const RealType Value)
Definition MathUtil.h:366
static RealType Clamp(const RealType Value, const RealType ClampMin, const RealType ClampMax)
Definition MathUtil.h:222
static RealType Floor(const RealType Value)
Definition MathUtil.h:384
static int32 Min3Index(const RealType A, const RealType B, const RealType C)
Definition MathUtil.h:283
static RealType Tan(const RealType Value)
Definition MathUtil.h:354
static int32 Max3Index(const RealType A, const RealType B, const RealType C)
Definition MathUtil.h:258
static RealType Max(const RealType A, const RealType B)
Definition MathUtil.h:246
static RealType Max3(const RealType A, const RealType B, const RealType C)
Definition MathUtil.h:252
static RealType Sqrt(const RealType Value)
Definition MathUtil.h:342
static RealType Abs(const RealType Value)
Definition MathUtil.h:215
static RealType ACos(const RealType Value)
Definition MathUtil.h:378
static RealType SignNonZero(const RealType Value)
Definition MathUtil.h:240
static int32 SignAsInt(const RealType Value)
Definition MathUtil.h:228
static RealType Atan2Positive(const RealType Y, const RealType X)
Definition MathUtil.h:429
static RealType Pow(const RealType Value, const RealType Power)
Definition MathUtil.h:402
static RealType Ceil(const RealType Value)
Definition MathUtil.h:390
static RealType SmoothMax(RealType A, RealType B, RealType BlendExtent)
Definition MathUtil.h:330
static RealType Exp(const RealType Power)
Definition MathUtil.h:408
static RealType Min(const RealType A, const RealType B)
Definition MathUtil.h:271
static RealType Round(const RealType Value)
Definition MathUtil.h:396
Definition MathUtil.h:16