18 #ifndef __H__OCULAR_ENGINE_MATH_COMMON__H__
19 #define __H__OCULAR_ENGINE_MATH_COMMON__H__
21 #include "Definitions.hpp"
22 #include "Interpolation.hpp"
46 enum class IntersectionType : int8_t
62 static int32_t Floor(
const float value)
64 return (value > 0) ?
static_cast<int>(value) : static_cast<int>(value) - 1;
72 static int32_t Floor(
const double value)
74 return (value > 0) ?
static_cast<int>(value) : static_cast<int>(value) - 1;
83 static T InverseSqrt(T
const& value)
85 return static_cast<T
>(1) / sqrt(value);
98 static T Normalize(T
const& value, T
const& rangeStart, T
const& rangeEnd)
104 T width = rangeEnd - rangeStart;
106 while(result > rangeEnd)
111 while(result < rangeStart)
125 static T RadiansToDegrees(T
const& radians)
127 double dRads =
static_cast<double>(radians);
128 dRads = Normalize<double>(dRads, -PI_TWO, PI_TWO);
130 return static_cast<T
>(dRads * PI_UNDER_180);
139 static T DegreesToRadians(T
const& degrees)
141 double dDegs =
static_cast<double>(degrees);
142 dDegs = Normalize<double>(dDegs, -360.0, 360.0);
144 return static_cast<T
>(dDegs * PI_OVER_180);
155 static T Clamp(T
const value, T
const lower = static_cast<T>(0), T
const upper = static_cast<T>(1))
157 return ((value < lower) ? lower : (value > upper) ? upper : value);
173 static T RoundUpDecimal(T value,
int const precision)
175 double result =
static_cast<double>(value);
177 result *= std::pow(10, precision);
178 result = std::ceil(result);
179 result /= std::pow(10, precision);
181 return static_cast<T
>(result);
200 static T RoundUpPowTen(T value,
int const precision)
202 double result =
static_cast<double>(value);
204 result /= std::pow(10, precision);
205 result = std::ceil(result);
206 result *= std::pow(10, precision);
208 return static_cast<T
>(result);
224 static T RoundDownDecimal(T value,
int const precision)
226 double result =
static_cast<double>(value);
228 result *= std::pow(10, precision);
229 result = std::floor(result);
230 result /= std::pow(10, precision);
232 return static_cast<T
>(result);
251 static T RoundDownPowTen(T value,
int const precision)
253 double result =
static_cast<double>(value);
255 result /= std::pow(10, precision);
256 result = std::floor(value);
257 result *= std::pow(10, precision);
259 return static_cast<T
>(result);
277 static T RoundDecimal(T value,
int const precision)
281 const double up = RoundUpDecimal(value, precision);
282 const double down = RoundDownDecimal(value, precision);
284 const double upDiff = std::abs(static_cast<double>(value) - static_cast<double>(up));
285 const double downDiff = std::abs(static_cast<double>(value) - static_cast<double>(down));
287 if(upDiff < downDiff)
289 result =
static_cast<T
>(up);
293 result =
static_cast<T
>(down);
315 static T RoundPowTen(T value,
int const precision)
317 T up = RoundUpPowTen(value, precision);
318 T down = RoundDownPowTen(value, precision);
320 T upDiff =
static_cast<T
>(std::abs(static_cast<double>(value)-static_cast<double>(up)));
321 T downDiff =
static_cast<T
>(std::abs(static_cast<double>(value)-static_cast<double>(down)));
323 if(upDiff < downDiff)
339 static inline uint32_t Clz(uint32_t
const value)
346 static uint8_t
const table[] = {
347 32U, 31U, 30U, 30U, 29U, 29U, 29U, 29U,
348 28U, 28U, 28U, 28U, 28U, 28U, 28U, 28U,
349 27U, 27U, 27U, 27U, 27U, 27U, 27U, 27U,
350 27U, 27U, 27U, 27U, 27U, 27U, 27U, 27U,
351 26U, 26U, 26U, 26U, 26U, 26U, 26U, 26U,
352 26U, 26U, 26U, 26U, 26U, 26U, 26U, 26U,
353 26U, 26U, 26U, 26U, 26U, 26U, 26U, 26U,
354 26U, 26U, 26U, 26U, 26U, 26U, 26U, 26U,
355 25U, 25U, 25U, 25U, 25U, 25U, 25U, 25U,
356 25U, 25U, 25U, 25U, 25U, 25U, 25U, 25U,
357 25U, 25U, 25U, 25U, 25U, 25U, 25U, 25U,
358 25U, 25U, 25U, 25U, 25U, 25U, 25U, 25U,
359 25U, 25U, 25U, 25U, 25U, 25U, 25U, 25U,
360 25U, 25U, 25U, 25U, 25U, 25U, 25U, 25U,
361 25U, 25U, 25U, 25U, 25U, 25U, 25U, 25U,
362 25U, 25U, 25U, 25U, 25U, 25U, 25U, 25U,
363 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
364 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
365 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
366 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
367 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
368 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
369 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
370 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
371 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
372 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
373 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
374 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
375 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
376 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
377 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U,
378 24U, 24U, 24U, 24U, 24U, 24U, 24U, 24U
383 if(value >= (1U << 16))
385 if(value >= (1U << 24))
396 if(value >= (1U << 8))
406 return (uint32_t)(table[value >> n]) - n;
415 static inline uint32_t Clz(uint64_t value)
437 result = (64 - (result + 1));
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70