Vuo  2.3.2
VuoRange.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include "VuoReal.h"
13 #include <float.h>
14 
16 #define VuoRange_NoMinimum -INFINITY
17 
19 #define VuoRange_NoMaximum INFINITY
20 
24 typedef struct
25 {
26  // The minimum value of a range. (VuoRange_NoMinimum >= minimum >= maximum)
27  VuoReal minimum;
28 
29  // The maximum value of a range. (minimum >= maximum >= VuoRange_NoMaximum)
30  VuoReal maximum;
31 } VuoRange;
32 
34 struct json_object * VuoRange_getJson(const VuoRange value);
35 char * VuoRange_getSummary(const VuoRange value);
36 
38 #define VuoRange_SUPPORTS_COMPARISON
39 bool VuoRange_areEqual(const VuoRange a, const VuoRange b);
40 bool VuoRange_isLessThan(const VuoRange a, const VuoRange b);
41 
43 
47 char * VuoRange_getString(const VuoRange value);
51 
55 static inline VuoRange VuoRange_make(VuoReal _minimum, VuoReal _maximum) __attribute__((const));
56 static inline VuoRange VuoRange_make(VuoReal _minimum, VuoReal _maximum)
57 {
58  return (VuoRange){_minimum, _maximum};
59 }
60 
64 static inline bool VuoRange_isInverted(VuoRange range) __attribute__((const));
65 static inline bool VuoRange_isInverted(VuoRange range)
66 {
67  return range.minimum > range.maximum;
68 }
69 
73 static inline VuoRange VuoRange_getOrderedRange(VuoRange range) __attribute__((const));
75 {
76  if (VuoRange_isInverted(range))
77  return (VuoRange){range.maximum, range.minimum};
78  else
79  return range;
80 }
81 
85 static inline VuoRange VuoRange_makeNonzero(VuoRange a) __attribute__((const));
87 {
88  if (fabs(a.maximum - a.minimum) < FLT_EPSILON)
89  a.maximum = a.minimum + FLT_EPSILON;
90  return a;
91 }
92 
98 static inline VuoReal VuoRange_clamp(VuoRange range, VuoReal value) __attribute__((const));
99 static inline VuoReal VuoRange_clamp(VuoRange range, VuoReal value)
100 {
101  VuoReal lower = fmin(range.minimum, range.maximum);
102  VuoReal upper = fmax(range.minimum, range.maximum);
103  return fmin(fmax(value, lower), upper);
104 }
105 
113 static inline VuoReal VuoRange_scale(VuoRange from, VuoRange to, VuoReal value) __attribute__((const));
114 static inline VuoReal VuoRange_scale(VuoRange from, VuoRange to, VuoReal value)
115 {
116  VuoReal from_lower = fmin(from.minimum, from.maximum);
117  VuoReal from_upper = fmax(from.minimum, from.maximum);
118  VuoReal to_lower = fmin(to.minimum, to.maximum);
119  VuoReal to_upper = fmax(to.minimum, to.maximum);
120 
121  if( from_lower == VuoRange_NoMinimum || from_upper == VuoRange_NoMaximum ||
122  to_lower == VuoRange_NoMinimum || to_upper == VuoRange_NoMaximum )
123  return value;
124 
125  VuoReal from_range = from_upper - from_lower;
126  VuoReal to_range = to_upper - to_lower;
127  VuoReal v = fmax(fmin(value, from_upper), from_lower);
128  VuoReal n = (v - from_lower) / from_range;
129 
130  return to_lower + (n * to_range);
131 }