math.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 namespace iv
4 {
5 
6 template< class Float >
7 Float mix_max( Float from, Float to, Float maxDist, std::enable_if_t< std::is_floating_point< Float >::value, std::nullptr_t > = nullptr )
8 {
9  Float result;
10  if( to > from )
11  {
12  result = from + maxDist;
13  if( result > to )
14  result = to;
15  }
16  else
17  {
18  result = from - maxDist;
19  if( result < to )
20  result = to;
21  }
22 
23  return result;
24 }
25 
26 template< class Float >
27 Float abs( Float val )
28 {
29  return ( val > 0 ) ? ( val ) : ( Float( 0 ) - val );
30 }
31 
32 template< class Float >
33 Float clamp( Float val, Float min, Float max )
34 {
35  return std::max( min, std::min( max, val ) );
36 }
37 
38 //------------ math --------------------
39 /*
40 template< class P, class Type >
41 inline Type lerp( double factor, Vector< P, Type, 2 > const & bounds )
42 {
43  return bounds.template get< 0 >() + factor * ( bounds.template get< 1 >() - bounds.template get< 0 >() );
44 }
45 
46 template< class Type >
47 inline Type lerp( double factor, Type const & bound_min, Type const & bound_max )
48 {
49  return bound_min + factor * ( bound_max - bound_min );
50 }
51 
52 
53 
54 template< class P, class Type >
55 inline double compute_factor( Type value, Vector< P, Type, 2 > const & bounds )
56 {
57  return double( value - bounds.template get< 0 >() ) / double( bounds.template get< 1 >() - bounds.template get< 0 >() );
58 }
59 
60 template< class Type >
61 inline double compute_factor( Type value, Type bound_min, Type bound_max )
62 {
63  return double( value - bound_min ) / double( bound_max - bound_min );
64 }
65 
66 
67 template< class Float >
68 inline Float angle_normalize( Float angle )
69 {
70  if( angle > Float( 2.0*math_pi ) )
71  {
72  double divisor = angle / Float( 2.0*math_pi );
73  return angle - int( divisor ) * Float( 2.0*math_pi );
74  }
75  else if( angle < Float( 0.0 ) )
76  {
77  double divisor = angle / Float( -2.0*math_pi );
78  angle = angle + ( int( divisor )+1 ) * Float( 2.0*math_pi );
79  if( angle == Float( 2.0*math_pi ) )
80  return Float( 0.0 );
81  else
82  return angle;
83  }
84  else
85  {
86  return angle;
87  }
88 }
89 
90 
91 
94 template< class _P, class Float >
95 inline Float linear_remap( Float const & original_value, Vector< _P, Float, 2 > const & original_range, Vector< _P, Float, 2 > const & new_range )
96 {
97  auto factor = compute_factor( original_value, original_range );
98  return lerp( factor, new_range );
99 }
100 
101 */
102 
103 }