Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
MathHelper.inl
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#ifndef DVD_CORE_MATH_MATH_HELPER_INL_
33#define DVD_CORE_MATH_MATH_HELPER_INL_
34
35namespace Divide
36{
37
38 namespace customRNG
39 {
40 namespace detail
41 {
42 template<typename Engine>
44 {
45 thread_local std::random_device rnddev{};
46 thread_local Engine rndeng = Engine( rnddev() );
47 return rndeng;
48 }
49 } // namespace detail
50
51 template<typename Engine>
52 void srand( const U32 seed )
53 {
54 detail::getEngine<Engine>().seed( seed );
55 }
56
57 template<typename Engine>
58 void srand()
59 {
60 std::random_device rnddev{};
61 srand<Engine>( rnddev() );
62 }
63
64 template<typename T, typename Engine, typename Distribution> requires std::is_arithmetic_v<T> && std::is_fundamental_v<T>
65 T rand( T min, T max )
66 {
67 return static_cast<T>(Distribution{ min, max }(detail::getEngine<Engine>()));
68 }
69
70 template<typename T, typename Engine, typename Distribution> requires std::is_arithmetic_v<T>
71 T rand()
72 {
73 return rand<T, Engine, Distribution>( 0, std::numeric_limits<T>::max() );
74 }
75 } // namespace customRNG
76
77 template <typename T, typename Engine, typename Distribution> requires std::is_arithmetic_v<T>
78 T Random( T min, T max )
79 {
80 if ( min > max ) [[unlikely]]
81 {
82 std::swap( min, max );
83 }
84
85 return customRNG::rand<T, Engine, Distribution>( min, max );
86 }
87
88 template <typename T, typename Engine, typename Distribution> requires std::is_arithmetic_v<T>
89 T Random( T max )
90 {
91 return Random<T, Engine, Distribution>( max < 0 ? std::numeric_limits<T>::lowest() : 0, max );
92 }
93
94 template <typename T, typename Engine, typename Distribution> requires std::is_arithmetic_v<T>
96 {
97 return Random<T, Engine, Distribution>( std::numeric_limits<T>::max() );
98 }
99
100 template<typename Engine>
102 {
103 customRNG::srand<Engine>();
104 }
105
106 template<typename Engine>
107 void SeedRandom( const U32 seed )
108 {
109 customRNG::srand<Engine>( seed );
110 }
111
113 template <typename T> requires std::is_arithmetic<T>::value
114 constexpr void CLAMP( T& n, const T min, const T max ) noexcept
115 {
116 n = std::min( std::max( n, min), max);
117 }
118
119 template <typename T>
120 constexpr void CLAMP_01( T& n ) noexcept
121 {
122 return CLAMP( n, static_cast<T>(0), static_cast<T>(1) );
123 }
124
125 template <typename T> requires std::is_arithmetic<T>::value
126 constexpr T CLAMPED( const T n, const T min, const T max ) noexcept
127 {
128 T ret = n;
129 CLAMP( ret, min, max );
130 return ret;
131 }
132
133 template <typename T> requires std::is_arithmetic<T>::value
134 constexpr T CLAMPED_01( const T n ) noexcept
135 {
136 return CLAMPED( n, static_cast<T>( 0 ), static_cast<T>( 1 ) );
137 }
138
139
140 template <typename T> requires std::is_arithmetic<T>::value
141 constexpr T MAP(const T input, const T in_min, const T in_max, const T out_min, const T out_max, D64& slopeOut ) noexcept
142 {
143 const D64 diff = in_max > in_min ? to_D64( in_max - in_min ) : EPSILON_D64;
144 slopeOut = 1.0 * (out_max - out_min) / diff;
145 return static_cast<T>(out_min + slopeOut * (input - in_min));
146 }
147
148 template <typename T>
149 constexpr void REMAP( T& input, T in_min, T in_max, T out_min, T out_max, D64& slopeOut ) noexcept
150 {
151 input = MAP( input, in_min, in_max, out_min, out_max, slopeOut );
152 }
153
154 template <typename T>
155 requires std::is_arithmetic<T>::value
156 constexpr T SQUARED( T input ) noexcept
157 {
158 return input * input;
159 }
160
161 template<typename T>
162 void CLAMP_IN_RECT( T& inout_x, T& inout_y, T rect_x, T rect_y, T rect_z, T rect_w ) noexcept
163 {
164 CLAMP( inout_x, rect_x, rect_z + rect_x );
165 CLAMP( inout_y, rect_y, rect_w + rect_y );
166 }
167
168 template<typename T>
169 void CLAMP_IN_RECT( T& inout_x, T& inout_y, const Rect<T>& rect ) noexcept
170 {
171 CLAMP_IN_RECT( inout_x, inout_y, rect.x, rect.y, rect.z, rect.w );
172 }
173
174 template<typename T>
175 void CLAMP_IN_RECT( T& inout_x, T& inout_y, const vec4<T>& rect ) noexcept
176 {
177 CLAMP_IN_RECT( inout_x, inout_y, rect.x, rect.y, rect.z, rect.w );
178 }
179
180 template <typename T>
181 bool COORDS_IN_RECT( T input_x, T input_y, T rect_x, T rect_y, T rect_z, T rect_w ) noexcept
182 {
183 return IS_IN_RANGE_INCLUSIVE( input_x, rect_x, rect_z + rect_x ) &&
184 IS_IN_RANGE_INCLUSIVE( input_y, rect_y, rect_w + rect_y );
185 }
186
187 template<typename T>
188 bool COORDS_IN_RECT( T input_x, T input_y, const Rect<T>& rect ) noexcept
189 {
190 return COORDS_IN_RECT( input_x, input_y, rect.x, rect.y, rect.z, rect.w );
191 }
192
193 template <typename T>
194 bool COORDS_IN_RECT( T input_x, T input_y, const vec4<T>& rect ) noexcept
195 {
196 return COORDS_IN_RECT( input_x, input_y, rect.x, rect.y, rect.z, rect.w );
197 }
198
199 template<typename T> requires std::is_integral<T>::value && std::is_unsigned<T>::value
200 constexpr T roundup( T value, unsigned maxb = sizeof( T ) * CHAR_BIT, unsigned curb = 1 )
201 {
202 return maxb <= curb
203 ? value
204 : roundup( ((value - 1) | ((value - 1) >> curb)) + 1, maxb, curb << 1 );
205 }
206
207 constexpr U32 nextPOW2( U32 n ) noexcept
208 {
209 n--;
210 n |= n >> 1;
211 n |= n >> 2;
212 n |= n >> 4;
213 n |= n >> 8;
214 n |= n >> 16;
215
216 return ++n;
217 }
218
219 constexpr U32 prevPOW2( U32 n ) noexcept
220 {
221 n = n | n >> 1;
222 n = n | n >> 2;
223 n = n | n >> 4;
224 n = n | n >> 8;
225 n = n | n >> 16;
226 return n - (n >> 1);
227 }
228
229 constexpr U32 minSquareMatrixSize( const U32 elementCount ) noexcept
230 {
231 U32 result = 1u;
232 while ( result * result < elementCount )
233 {
234 ++result;
235 }
236 return result;
237 }
238
239 template <typename T, typename U>
240 T Lerp( const T v1, const T v2, const U t ) noexcept
241 {
242 return v1 * (static_cast<U>(1) - t) + v2 * t;
243 }
244
245 template <typename T, typename U>
246 T FastLerp( const T v1, const T v2, const U t ) noexcept
247 {
248 return v1 + t * (v2 - v1);
249 }
250
251 template <typename T>
252 T Sqrt( const T input ) noexcept
253 {
254 return static_cast<T>(std::sqrt( input ));
255 }
256
257 template <typename T, typename U>
258 T Sqrt( const U input ) noexcept
259 {
260 return static_cast<T>(std::sqrt( input ));
261 }
262
263 template <>
264 inline F32 Sqrt( const __m128 input ) noexcept
265 {
266 return _mm_cvtss_f32( _mm_sqrt_ss( input ) );
267 }
269
270 // Helper method to emulate GLSL
271 inline F32 FRACT( const F32 floatValue ) noexcept
272 {
273 return to_F32( fmod( floatValue, 1.0f ) );
274 }
275
276 constexpr I8 FLOAT_TO_CHAR_SNORM( const F32_SNORM value ) noexcept
277 {
278 assert( value >= -1.f && value <= 1.f );
279 return to_I8( (value >= 1.0f ? 127 : (value <= -1.0f ? -127 : to_I32( SIGN( value ) * std::floor( std::abs( value ) * 128.0f ) ))) );
280 }
281
282 constexpr F32_SNORM SNORM_CHAR_TO_FLOAT( const I8 value ) noexcept
283 {
284 return value / 127.f;
285 }
286
287 constexpr U8 PACKED_FLOAT_TO_CHAR_UNORM( const F32_SNORM value ) noexcept
288 {
289 assert( value >= -1.f && value <= 1.f );
290
291 const F32_NORM normValue = (value + 1) * 0.5f;
292 return FLOAT_TO_CHAR_UNORM( normValue );
293 }
294
295 constexpr F32_SNORM UNORM_CHAR_TO_PACKED_FLOAT( const U8 value ) noexcept
296 {
297 const F32_NORM normValue = UNORM_CHAR_TO_FLOAT( value );
298
299 return 2 * normValue - 1;
300 }
301
302 constexpr F32_NORM UNORM_CHAR_TO_FLOAT( const U8 value ) noexcept
303 {
304 return value == 255 ? 1.0f : value / 256.0f;
305 }
306
307 constexpr U8 FLOAT_TO_CHAR_UNORM( const F32_NORM value ) noexcept
308 {
309 assert( value >= 0.f && value <= 1.f );
310 return to_U8( (value >= 1.0f ? 255 : (value <= 0.0f ? 0 : to_I32( std::floor( value * 256.0f ) ))) );
311 }
312
313 namespace Util
314 {
315 // Pack 3 values into 1 float
316 inline F32 PACK_VEC3( const F32_SNORM x, const F32_SNORM y, const F32_SNORM z ) noexcept
317 {
318 assert( x >= -1.0f && x <= 1.0f );
319 assert( y >= -1.0f && y <= 1.0f );
320 assert( z >= -1.0f && z <= 1.0f );
321
322 //Scale and bias
323 return PACK_VEC3( to_U8( ((x + 1.0f) * 0.5f) * 255.0f ),
324 to_U8( ((y + 1.0f) * 0.5f) * 255.0f ),
325 to_U8( ((z + 1.0f) * 0.5f) * 255.0f ) );
326 }
327
328 inline F32 PACK_VEC3( const U8 x, const U8 y, const U8 z ) noexcept
329 {
330 const U32 packed = x << 16 | y << 8 | z;
331 return to_F32( to_D64( packed ) / to_D64( 1 << 24 ) );
332 }
333
334 // UnPack 3 values from 1 float
335 inline void UNPACK_VEC3( const F32 src, F32_SNORM& x, F32_SNORM& y, F32_SNORM& z ) noexcept
336 {
337 x = FRACT( src );
338 y = FRACT( src * 256.0f );
339 z = FRACT( src * 65536.0f );
340
341 // Unpack to the -1..1 range
342 x = x * 2.0f - 1.0f;
343 y = y * 2.0f - 1.0f;
344 z = z * 2.0f - 1.0f;
345 }
346 } //namespace Util
347
348 namespace Angle
349 {
350
351 template <typename T>
352 constexpr DEGREES<T> to_VerticalFoV( const DEGREES<T> horizontalFoV, D64 aspectRatio ) noexcept
353 {
354 return static_cast<DEGREES<T>>(
355 to_DEGREES( 2 * std::atan( std::tan( Angle::to_RADIANS( horizontalFoV ) * 0.5f ) / aspectRatio ) )
356 );
357 }
358
359 template <typename T>
360 constexpr DEGREES<T> to_HorizontalFoV( const DEGREES<T> verticalFoV, D64 aspectRatio ) noexcept
361 {
362 return static_cast<DEGREES<T>>(
363 to_DEGREES( 2 * std::atan( std::tan( Angle::to_RADIANS( verticalFoV ) * 0.5f ) ) * aspectRatio )
364 );
365 }
366
367 template <typename T>
368 constexpr RADIANS<T> to_RADIANS( const DEGREES<T> angle ) noexcept
369 {
370 return static_cast<RADIANS<T>>(angle * M_PIDIV180);
371 }
372
373 template <>
374 constexpr RADIANS<F32> to_RADIANS( const DEGREES<F32> angle ) noexcept
375 {
376 return static_cast<RADIANS<F32>>(angle * M_PIDIV180_f);
377 }
378
379 template <typename T>
380 constexpr DEGREES<T> to_DEGREES( const RADIANS<T> angle ) noexcept
381 {
382 return static_cast<DEGREES<T>>(angle * M_180DIVPI);
383 }
384
385 template <>
386 constexpr DEGREES<F32> to_DEGREES( const RADIANS<F32> angle ) noexcept
387 {
388 return static_cast<DEGREES<F32>>(angle * M_180DIVPI_f);
389 }
390
391 template <typename T>
392 constexpr vec2<RADIANS<T>> to_RADIANS( const vec2<DEGREES<T>> angle ) noexcept
393 {
394 return vec2<RADIANS<T>>( angle * M_PIDIV180 );
395 }
396
397 template <typename T>
398 constexpr vec2<DEGREES<T>> to_DEGREES( const vec2<RADIANS<T>> angle ) noexcept
399 {
400 return vec2<RADIANS<T>>( angle * M_180DIVPI );
401 }
402
403 template <typename T>
404 constexpr vec3<RADIANS<T>> to_RADIANS( const vec3<DEGREES<T>>& angle ) noexcept
405 {
406 return vec3<RADIANS<T>>( angle * M_PIDIV180 );
407 }
408
409 template <typename T>
410 constexpr vec3<DEGREES<T>> to_DEGREES( const vec3<RADIANS<T>>& angle ) noexcept
411 {
412 return vec3<DEGREES<T>>( angle * M_180DIVPI );
413 }
414
415 template <typename T>
416 constexpr vec4<RADIANS<T>> to_RADIANS( const vec4<DEGREES<T>>& angle ) noexcept
417 {
418 return vec4<RADIANS<T>>( angle * M_PIDIV180 );
419 }
420
421 template <typename T>
422 constexpr vec4<DEGREES<T>> to_DEGREES( const vec4<RADIANS<T>>& angle ) noexcept
423 {
424 return vec4<DEGREES<T>>( angle * M_180DIVPI );
425 }
426
428 template <typename T>
429 constexpr T DegreesToRadians( const T angleDegrees ) noexcept
430 {
431 return to_RADIANS( angleDegrees );
432 }
433
435 template <typename T>
436 constexpr T RadiansToDegrees( const T angleRadians ) noexcept
437 {
438 return to_DEGREES( angleRadians );
439 }
441 template <typename T>
442 constexpr T Degrees( const T degrees ) noexcept
443 {
444 return degrees;
445 }
446
448 template <typename T>
449 constexpr T Radians( const T radians ) noexcept
450 {
451 return radians;
452 }
453
454 } // namespace Angle
455
456 namespace Metric
457 {
458
460 template <typename T>
461 constexpr T Tera( const T a )
462 {
463 return Tera<T, T>( a );
464 }
465
467 template <typename T>
468 constexpr T Giga( const T a )
469 {
470 return Giga<T, T>( a );
471 }
472
474 template <typename T>
475 constexpr T Mega( const T a )
476 {
477 return Mega<T, T>( a );
478 }
479
481 template <typename T>
482 constexpr T Kilo( const T a )
483 {
484 return Kilo<T, T>( a );
485 }
486
488 template <typename T>
489 constexpr T Hecto( const T a )
490 {
491 return Hecto<T, T>( a );
492 }
493
495 template <typename T>
496 constexpr T Deca( const T a )
497 {
498 return Deca<T, T>( a );
499 }
500
502 template <typename T>
503 constexpr T Base( const T a )
504 {
505 return Base<T, T>( a );
506 }
507
509 template <typename T>
510 constexpr T Deci( const T a )
511 {
512 return Deci<T, T>( a );
513 }
514
516 template <typename T>
517 constexpr T Centi( const T a )
518 {
519 return Centi<T, T>( a );
520 }
521
523 template <typename T>
524 constexpr T Milli( const T a )
525 {
526 return Milli<T, T>( a );
527 }
528
530 template <typename T>
531 constexpr T Micro( const T a )
532 {
533 return Micro<T, T>( a );
534 }
535
537 template <typename T>
538 constexpr T Nano( const T a )
539 {
540 return Nano<T, T>( a );
541 }
542
544 template <typename T>
545 constexpr T Pico( const T a )
546 {
547 return Pico<T, T>( a );
548 }
549
550
551 template <typename T, typename U>
552 constexpr T Tera( const U a )
553 {
554 return static_cast<T>(multiply( a, 1'000'000'000'000 ));
555 }
556
557 template <typename T, typename U>
558 constexpr T Giga( const U a )
559 {
560 return static_cast<T>(multiply( a, 1'000'000'000 ));
561 }
562
563 template <typename T, typename U>
564 constexpr T Mega( const U a )
565 {
566 return static_cast<T>(multiply( a, 1'000'000 ));
567 }
568
569 template <typename T, typename U>
570 constexpr T Kilo( const U a )
571 {
572 return static_cast<T>(multiply( a, 1'000 ));
573 }
574
575 template <typename T, typename U>
576 constexpr T Hecto( const U a )
577 {
578 return static_cast<T>(multiply( a, 100 ));
579 }
580
581 template <typename T, typename U>
582 constexpr T Deca( const U a )
583 {
584 return static_cast<T>(multiply( a, 10 ));
585 }
586
587 template <typename T, typename U>
588 constexpr T Base( const U a )
589 {
590 return static_cast<T>(a);
591 }
592
593 template <typename T, typename U>
594 constexpr T Deci( const U a )
595 {
596 return static_cast<T>(divide( a, 10.0 ));
597 }
598
599 template <typename T, typename U>
600 constexpr T Centi( const U a )
601 {
602 return static_cast<T>(divide( a, 100.0 ));
603 }
604
605 template <typename T, typename U>
606 constexpr T Milli( const U a )
607 {
608 return static_cast<T>(divide( a, 1000.0 ));
609 }
610
611 template <typename T, typename U>
612 constexpr T Micro( const U a )
613 {
614 return static_cast<T>(divide( a, 1e6 ));
615 }
616
617 template <typename T, typename U>
618 constexpr T Nano( const U a )
619 {
620 return static_cast<T>(divide( a, 1e9 ));
621 }
622
623 template <typename T, typename U>
624 constexpr T Pico( const U a )
625 {
626 return static_cast<T>(divide( a, 1e12 ));
627 }
628
629 } // namespace Metric
630
631 namespace Time
632 {
633
634 template <typename T>
635 constexpr T Hours( const T a )
636 {
637 return a;
638 }
639
640 template <typename T>
641 constexpr T Minutes( const T a )
642 {
643 return a;
644 }
645
646 template <typename T>
647 constexpr T Seconds( const T a )
648 {
649 return a;
650 }
651
652 template <typename T>
653 constexpr T Milliseconds( const T a )
654 {
655 return a;
656 }
657
658 template <typename T>
659 constexpr T Microseconds( const T a )
660 {
661 return a;
662 }
663
664 template <typename T>
665 constexpr T Nanoseconds( const T a )
666 {
667 return a;
668 }
669
670 template <typename T, typename U>
671 constexpr T Hours( const U a )
672 {
673 return static_cast<T>(a);
674 }
675
676 template <typename T, typename U>
677 constexpr T Minutes( const U a )
678 {
679 return static_cast<T>(a);
680 }
681
682 template <typename T, typename U>
683 constexpr T Seconds( const U a )
684 {
685 return static_cast<T>(a);
686 }
687
688 template <typename T, typename U>
689 constexpr T Milliseconds( const U a )
690 {
691 return static_cast<T>(a);
692 }
693
694 template <typename T, typename U>
695 constexpr T Microseconds( const U a )
696 {
697 return static_cast<T>(a);
698 }
699
700 template <typename T, typename U>
701 constexpr T Nanoseconds( const U a )
702 {
703 return static_cast<T>(a);
704 }
705
706 template <typename T, typename U>
707 constexpr T NanosecondsToSeconds( const U a ) noexcept
708 {
709 return Metric::Nano<T, U>( a );
710 }
711
712 template <typename T, typename U>
713 constexpr T NanosecondsToMilliseconds( const U a ) noexcept
714 {
715 return Metric::Micro<T, U>( a );
716 }
717
718 template <typename T, typename U>
719 constexpr T NanosecondsToMicroseconds( const U a ) noexcept
720 {
721 return Metric::Milli<T, U>( a );
722 }
723
724 template <typename T, typename U>
725 constexpr T MicrosecondsToSeconds( const U a ) noexcept
726 {
727 return Metric::Micro<T, U>( a );
728 }
729
730 template <typename T, typename U>
731 constexpr T MicrosecondsToMilliseconds( const U a ) noexcept
732 {
733 return Metric::Milli<T, U>( a );
734 }
735
736 template <typename T, typename U>
737 constexpr T MicrosecondsToNanoseconds( const U a ) noexcept
738 {
739 return Metric::Kilo<T, U>( a );
740 }
741
742 template <typename T, typename U>
743 constexpr T MillisecondsToSeconds( const U a ) noexcept
744 {
745 return Metric::Milli<T, U>( a );
746 }
747
748 template <typename T, typename U>
749 constexpr T MillisecondsToMicroseconds( const U a ) noexcept
750 {
751 return Metric::Kilo<T, U>( a );
752 }
753
754 template <typename T, typename U>
755 constexpr T MillisecondsToNanoseconds( const U a ) noexcept
756 {
757 return Metric::Mega<T, U>( a );
758 }
759
760 template <typename T, typename U>
761 constexpr T SecondsToMilliseconds( const U a ) noexcept
762 {
763 return Metric::Kilo<T, U>( a );
764 }
765
766 template <typename T, typename U>
767 constexpr T SecondsToMicroseconds( const U a ) noexcept
768 {
769 return Metric::Mega<T, U>( a );
770 }
771
772 template <typename T, typename U>
773 constexpr T SecondsToNanoseconds( const U a ) noexcept
774 {
775 return Metric::Giga<T, U>( a );
776 }
777
778 } // namespace Time
779
780 namespace Util
781 {
782
783 FORCE_INLINE size_t GetAlignmentCorrected( const size_t value, const size_t alignment ) noexcept
784 {
785 return value % alignment == 0u
786 ? value
787 : ((value + alignment - 1u) / alignment) * alignment;
788 }
789
791 template<typename T, typename... Rest>
792 FORCE_INLINE void Hash_combine( std::size_t& seed, const T& v, const Rest&... rest ) noexcept
793 {
794 seed ^= std::hash<T>{}(v)+0x9e3779b9 + (seed << 6) + (seed >> 2);
795 (Hash_combine( seed, rest ), ...);
796 };
797
798 template<typename T, typename... Rest> requires std::is_integral<T>::value && std::is_unsigned<T>::value
799 FORCE_INLINE void Hash_combine( size_t& seed, const T& v, const Rest&... rest ) noexcept
800 {
801 seed ^= v + 0x9e3779b9 + (seed << 6) + (seed >> 2);
802 (Hash_combine( seed, rest ), ...);
803 }
804
805 template <typename TargetType, typename SourceType>
806 TargetType ConvertData( const SourceType& data )
807 {
808 TargetType targetValue;
809 std::istringstream iss( data );
810 iss >> targetValue;
811 DIVIDE_ASSERT( !iss.fail(), "Util::convertData error : invalid conversion!" );
812 return targetValue;
813 }
814
815 template<class FwdIt, class Compare>
816 void InsertionSort( FwdIt first, FwdIt last, Compare cmp )
817 {
818 for ( auto it = first; it != last; ++it )
819 {
820 auto const insertion = eastl::upper_bound( first, it, *it, cmp );
821 eastl::rotate( insertion, it, eastl::next( it ) );
822 }
823 }
824
825 } // namespace Util
826} // namespace Divide
827
828#endif //DVD_CORE_MATH_MATH_HELPER_INL_
#define DIVIDE_ASSERT(...)
#define FORCE_INLINE
constexpr T Degrees(T degrees) noexcept
Returns the specified value. Used only for emphasis.
Definition: MathHelper.inl:442
constexpr T DegreesToRadians(T angleDegrees) noexcept
Return the radian equivalent of the given degree value.
Definition: MathHelper.inl:429
constexpr DEGREES< T > to_DEGREES(RADIANS< T > angle) noexcept
Definition: MathHelper.inl:380
constexpr T RadiansToDegrees(T angleRadians) noexcept
Return the degree equivalent of the given radian value.
Definition: MathHelper.inl:436
constexpr RADIANS< T > to_RADIANS(DEGREES< T > angle) noexcept
Definition: MathHelper.inl:368
constexpr DEGREES< T > to_VerticalFoV(DEGREES< T > horizontalFoV, D64 aspectRatio) noexcept
Definition: MathHelper.inl:352
constexpr T Radians(T radians) noexcept
Returns the specified value. Used only for emphasis.
Definition: MathHelper.inl:449
constexpr DEGREES< T > to_HorizontalFoV(DEGREES< T > verticalFoV, D64 aspectRatio) noexcept
Definition: MathHelper.inl:360
constexpr T Nano(T a)
Base value * 0.000000001.
Definition: MathHelper.inl:538
constexpr T Milli(T a)
Base value * 0.001.
Definition: MathHelper.inl:524
constexpr T Kilo(T a)
Base value * 1000.
Definition: MathHelper.inl:482
constexpr T Mega(T a)
Base value * 1000000.
Definition: MathHelper.inl:475
constexpr T Tera(T a)
Base value * 1000000000000.
Definition: MathHelper.inl:461
constexpr T Centi(T a)
Base value * 0.01.
Definition: MathHelper.inl:517
constexpr T Deci(T a)
Base value * 0.1.
Definition: MathHelper.inl:510
constexpr T Pico(T a)
Base value * 0.000000000001.
Definition: MathHelper.inl:545
constexpr T Deca(T a)
Base value * 10.
Definition: MathHelper.inl:496
constexpr T Giga(T a)
Base value * 1000000000.
Definition: MathHelper.inl:468
constexpr T Base(T a)
Base value.
Definition: MathHelper.inl:503
constexpr T Micro(T a)
Base value * 0.000001.
Definition: MathHelper.inl:531
constexpr T Hecto(T a)
Base value * 100.
Definition: MathHelper.inl:489
constexpr T MillisecondsToSeconds(U a) noexcept
Definition: MathHelper.inl:743
constexpr T MicrosecondsToSeconds(U a) noexcept
Definition: MathHelper.inl:725
constexpr T SecondsToNanoseconds(U a) noexcept
Definition: MathHelper.inl:773
constexpr T MicrosecondsToMilliseconds(U a) noexcept
Definition: MathHelper.inl:731
constexpr T NanosecondsToMilliseconds(U a) noexcept
Definition: MathHelper.inl:713
constexpr T Milliseconds(T a)
Definition: MathHelper.inl:653
constexpr T Hours(T a)
Definition: MathHelper.inl:635
constexpr T SecondsToMicroseconds(U a) noexcept
Definition: MathHelper.inl:767
constexpr T Minutes(T a)
Definition: MathHelper.inl:641
constexpr T NanosecondsToSeconds(U a) noexcept
Definition: MathHelper.inl:707
constexpr T Microseconds(T a)
Definition: MathHelper.inl:659
constexpr T NanosecondsToMicroseconds(U a) noexcept
Definition: MathHelper.inl:719
constexpr T SecondsToMilliseconds(U a) noexcept
Definition: MathHelper.inl:761
constexpr T MicrosecondsToNanoseconds(U a) noexcept
Definition: MathHelper.inl:737
constexpr T MillisecondsToMicroseconds(U a) noexcept
Definition: MathHelper.inl:749
constexpr T MillisecondsToNanoseconds(U a) noexcept
Definition: MathHelper.inl:755
constexpr T Nanoseconds(T a)
Definition: MathHelper.inl:665
constexpr T Seconds(T a)
Definition: MathHelper.inl:647
U ConvertData(const T &data)
F32 PACK_VEC3(F32_SNORM x, F32_SNORM y, F32_SNORM z) noexcept
Definition: MathHelper.inl:316
FORCE_INLINE size_t GetAlignmentCorrected(const size_t value, const size_t alignment) noexcept
Definition: MathHelper.inl:783
void UNPACK_VEC3(F32 src, F32_SNORM &x, F32_SNORM &y, F32_SNORM &z) noexcept
Definition: MathHelper.inl:335
void Hash_combine(size_t &seed, const T &v, const Rest &... rest) noexcept
a la Boost
Definition: MathHelper.inl:799
void InsertionSort(FwdIt first, FwdIt last, Compare cmp=Compare())
Definition: MathHelper.inl:816
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
FORCE_INLINE bool Compare(const GUIDWrapper *const lhs, const GUIDWrapper *const rhs) noexcept
Definition: GUIDWrapper.h:73
constexpr U8 FLOAT_TO_CHAR_UNORM(F32_NORM value) noexcept
Returns round(value * 255)
Definition: MathHelper.inl:307
::value constexpr void CLAMP_01(T &n) noexcept
Definition: MathHelper.inl:120
constexpr I8 FLOAT_TO_CHAR_SNORM(F32_SNORM value) noexcept
Returns clamped value * 128.
Definition: MathHelper.inl:276
constexpr F32_SNORM UNORM_CHAR_TO_PACKED_FLOAT(U8 value) noexcept
Definition: MathHelper.inl:295
constexpr U32 nextPOW2(U32 n) noexcept
Definition: MathHelper.inl:207
T Lerp(T v1, T v2, U t) noexcept
Definition: MathHelper.inl:240
TextureType TargetType(const ImageView &imageView) noexcept
::value &&std::is_unsigned< T >::value constexpr T roundup(T value, unsigned maxb=sizeof(T) *CHAR_BIT, unsigned curb=1)
Definition: MathHelper.inl:200
constexpr T SQUARED(T input) noexcept
Definition: MathHelper.inl:156
constexpr F32_NORM UNORM_CHAR_TO_FLOAT(U8 value) noexcept
Returns value / 255.f.
Definition: MathHelper.inl:302
T Sqrt(T input) noexcept
Definition: MathHelper.inl:252
constexpr D64 EPSILON_D64
bool IS_IN_RANGE_INCLUSIVE(const T x, const U min, const U max) noexcept
F32 FRACT(F32 floatValue) noexcept
Helper method to emulate GLSL.
Definition: MathHelper.inl:271
constexpr void REMAP(T &input, T in_min, T in_max, T out_min, T out_max, D64 &slopeOut) noexcept
Definition: MathHelper.inl:149
uint8_t U8
void CLAMP_IN_RECT(T &inout_x, T &inout_y, T rect_x, T rect_y, T rect_z, T rect_w) noexcept
Definition: MathHelper.inl:162
constexpr F32 to_F32(const T value)
constexpr D64 to_D64(const T value)
T Random()
Definition: MathHelper.inl:95
constexpr resolve_uac< A, B >::return_type divide(const A &a, const B &b) noexcept
constexpr I32 SIGN(const T val)
Definition: MathHelper.h:133
constexpr U32 prevPOW2(U32 n) noexcept
Definition: MathHelper.inl:219
constexpr F32 M_180DIVPI_f
Definition: MathHelper.h:91
void SeedRandom()
Definition: MathHelper.inl:101
constexpr U8 PACKED_FLOAT_TO_CHAR_UNORM(F32_SNORM value) noexcept
Helper methods to go from an snorm float (-1...1) to packed unorm char (value => (value + 1) * 0....
Definition: MathHelper.inl:287
constexpr I8 to_I8(const T value)
::value constexpr T MAP(T input, T in_min, T in_max, T out_min, T out_max, D64 &slopeOut) noexcept
Definition: MathHelper.inl:141
constexpr U8 to_U8(const T value)
::value constexpr T CLAMPED(T n, T min, T max) noexcept
Definition: MathHelper.inl:126
constexpr D64 M_PIDIV180
Definition: MathHelper.h:81
double D64
constexpr F32 M_PIDIV180_f
Definition: MathHelper.h:90
constexpr F32_SNORM SNORM_CHAR_TO_FLOAT(I8 value) noexcept
Definition: MathHelper.inl:282
constexpr U32 minSquareMatrixSize(U32 elementCount) noexcept
Definition: MathHelper.inl:229
constexpr D64 M_180DIVPI
Definition: MathHelper.h:82
bool COORDS_IN_RECT(T input_x, T input_y, T rect_x, T rect_y, T rect_z, T rect_w) noexcept
Definition: MathHelper.inl:181
::value constexpr void CLAMP(T &n, T min, T max) noexcept
Clamps value n between min and max.
Definition: MathHelper.inl:114
T FastLerp(T v1, T v2, U t) noexcept
Definition: MathHelper.inl:246
constexpr I32 to_I32(const T value)
uint32_t U32
constexpr resolve_uac< A, B >::return_type multiply(const A &a, const B &b) noexcept
::value constexpr T CLAMPED_01(T n) noexcept
Definition: MathHelper.inl:134