Lumiverse  2.5
A framework for creating lighting control applications
LumiverseOrientation.h
Go to the documentation of this file.
1 
4 #ifndef _LumiverseORIENTATION_H_
5 #define _LumiverseORIENTATION_H_
6 #pragma once
7 
8 #include "../LumiverseType.h"
9 #include <string>
10 #include <stdio.h>
11 #include <unordered_map>
12 #define _USE_MATH_DEFINES
13 #include <math.h>
14 
15 namespace Lumiverse {
18  DEGREE, RADIAN
19  };
20 
21 #ifdef USE_C11_MAPS
22 
23  static unordered_map<int, string> oriToString = {
24  { DEGREE, "degree" }, { RADIAN, "radian" }
25  };
26 
28  static unordered_map<string, int> stringToOri = {
29  { "degree", DEGREE }, { "radian", RADIAN }
30  };
31 #else
32  static string oriToString(ORIENTATION_UNIT u) {
33  switch(u) {
34  case DEGREE:
35  return "degree";
36  case RADIAN:
37  return "radian";
38  default:
39  return "";
40  }
41  }
42 
43  static ORIENTATION_UNIT stringToOri(string u) {
44  if (u == "degree") return DEGREE;
45  if (u == "radian") return RADIAN;
46 
47  return RADIAN;
48  }
49 #endif
50 
62  {
63  public:
73  LumiverseOrientation(float val = 0.0f, ORIENTATION_UNIT unit = DEGREE, float def = 0.0f, float max = 360.0f, float min = 0.0f);
74 
80 
88 
93 
98  virtual string getTypeName() { return "orientation"; }
99 
100  // Override for =
101  void operator=(float val);
102  void operator=(LumiverseOrientation val);
103 
104  // Arithmetic overrides
105  LumiverseOrientation& operator+=(float val);
106  LumiverseOrientation& operator+=(LumiverseOrientation& val);
107 
108  LumiverseOrientation& operator-=(float val);
109  LumiverseOrientation& operator-=(LumiverseOrientation& val);
110 
111  LumiverseOrientation& operator*=(float val);
112  LumiverseOrientation& operator*=(LumiverseOrientation& val);
113 
114  LumiverseOrientation& operator/=(float val);
115  LumiverseOrientation& operator/=(LumiverseOrientation& val);
116 
120  float getVal() { return m_val; }
121 
126  void setVal(float val, ORIENTATION_UNIT unit = DEGREE) { m_val = asUnit(unit, val, m_unit); clamp(); }
127 
131  void setVals(float val, float def, float min, float max);
132 
137  void setUnit(ORIENTATION_UNIT unit);
138 
144 
149  void setMax(float val, ORIENTATION_UNIT unit = DEGREE) { m_max = asUnit(unit, val, m_unit); }
150 
155  float getMax() { return m_max; }
156 
161  void setMin(float val, ORIENTATION_UNIT unit = DEGREE) { m_min = asUnit(unit, val, m_unit); }
162 
167  float getMin() { return m_min; }
168 
173  void setDefault(float val, ORIENTATION_UNIT unit = DEGREE) { m_default = asUnit(unit, val, m_unit); }
174 
179  float getDefault() { return m_default; }
180 
184  virtual void reset() { m_val = m_default; }
185 
189  void setValAsPercent(float val);
190 
195  float asPercent();
196 
200  float valAsUnit(ORIENTATION_UNIT unit) { return asUnit(m_unit, m_val, unit); }
201 
205  float maxAsUnit(ORIENTATION_UNIT unit) { return asUnit(m_unit, m_max, unit); }
206 
210  float minAsUnit(ORIENTATION_UNIT unit) { return asUnit(m_unit, m_min, unit); }
211 
215  float defaultAsUnit(ORIENTATION_UNIT unit) { return asUnit(m_unit, m_default, unit); }
216 
217  // Converts a float to a JSON object with specified name.
218  virtual JSONNode toJSON(string name);
219 
225  virtual string asString();
226 
227  virtual bool isDefault();
228 
229  private:
233  void clamp();
234 
244  float asUnit(ORIENTATION_UNIT valUnit, float val, ORIENTATION_UNIT targetUnit);
245 
249  float m_val;
250 
252  float m_default;
253 
255  float m_max;
256 
258  float m_min;
259 
262  };
263 
264  // Ops ops ops all overloaded woo
265 
266  // Compares two LumiverseOrientations. Uses normal float comparison
267  inline bool operator==(LumiverseOrientation& a, LumiverseOrientation& b) {
268  if (a.getTypeName() != "orientation" || b.getTypeName() != "orientation")
269  return false;
270 
271  // Equality/inequality shouldn't change based on a unit conversion.
272  return a.getVal() == b.valAsUnit(a.getUnit());
273  }
274 
275  inline bool operator!=(LumiverseOrientation& a, LumiverseOrientation& b) {
276  return !(a == b);
277  }
278 
279  // LumiverseOrientation uses the normal < op for floats.
280  inline bool operator<(LumiverseOrientation& a, LumiverseOrientation& b) {
281  if (a.getTypeName() != "orientation" || b.getTypeName() != "orientation")
282  return false;
283 
284  // Equality/inequality shouldn't change based on a unit conversion.
285  return a.getVal() < b.valAsUnit(a.getUnit());
286  }
287 
288  inline bool operator>(LumiverseOrientation& a, LumiverseOrientation& b) {
289  return b < a;
290  }
291 
292  inline bool operator<=(LumiverseOrientation& a, LumiverseOrientation& b) {
293  return !(a > b);
294  }
295 
296  inline bool operator>=(LumiverseOrientation& a, LumiverseOrientation b) {
297  return !(a < b);
298  }
299 
300  // Arithmetic overrides
301  inline LumiverseOrientation operator+(LumiverseOrientation& lhs, float rhs) {
302  LumiverseOrientation val = LumiverseOrientation(lhs);
303  val += rhs;
304  return val;
305  }
306 
307  //inline LumiverseOrientation operator+(LumiverseOrientation& lhs, LumiverseOrientation& rhs) {
308  // LumiverseOrientation val = LumiverseOrientation(lhs);
309  // val += rhs.valAsUnit(val.getUnit());
310  // return val;
311  //}
312 
313  // Apparently clang needs this form of the overload to make it happy
314  inline LumiverseOrientation operator+(LumiverseOrientation lhs, LumiverseOrientation rhs) {
315  LumiverseOrientation val = LumiverseOrientation(lhs);
316  val += rhs.valAsUnit(val.getUnit());
317  return val;
318  }
319 
320  inline LumiverseOrientation operator-(LumiverseOrientation& lhs, float rhs) {
321  LumiverseOrientation val = LumiverseOrientation(lhs);
322  val -= rhs;
323  return val;
324  }
325 
326  inline LumiverseOrientation operator-(LumiverseOrientation& lhs, LumiverseOrientation& rhs) {
327  LumiverseOrientation val = LumiverseOrientation(lhs);
328  val -= rhs.valAsUnit(val.getUnit());
329  return val;
330  }
331 
332  inline LumiverseOrientation operator*(LumiverseOrientation& lhs, float rhs) {
333  LumiverseOrientation val = LumiverseOrientation(lhs);
334  val *= rhs;
335  return val;
336  }
337 
338  inline LumiverseOrientation operator*(LumiverseOrientation& lhs, LumiverseOrientation& rhs) {
339  LumiverseOrientation val = LumiverseOrientation(lhs);
340  val *= rhs.valAsUnit(val.getUnit());;
341  return val;
342  }
343 
344  inline LumiverseOrientation operator/(LumiverseOrientation& lhs, float rhs) {
345  LumiverseOrientation val = LumiverseOrientation(lhs);
346  val /= rhs;
347  return val;
348  }
349 
350  inline LumiverseOrientation operator/(LumiverseOrientation& lhs, LumiverseOrientation& rhs) {
351  LumiverseOrientation val = LumiverseOrientation(lhs);
352  val /= rhs.valAsUnit(val.getUnit());
353  return val;
354  }
355 }
356 
357 #endif
ORIENTATION_UNIT m_unit
Indicates the type of angle measurement used in the object.
Definition: LumiverseOrientation.h:261
virtual void reset()
Resets the value to the default value.
Definition: LumiverseOrientation.h:184
float valAsUnit(ORIENTATION_UNIT unit)
Returns the value of this orientation with the specified units.
Definition: LumiverseOrientation.h:200
float asUnit(ORIENTATION_UNIT valUnit, float val, ORIENTATION_UNIT targetUnit)
Returns the specified value as the specified unit.
Definition: LumiverseOrientation.cpp:116
This class is a wapper around a variety of different possible data types that might be needed by a De...
Definition: LumiverseType.h:33
void setDefault(float val, ORIENTATION_UNIT unit=DEGREE)
Set the default value for the orientation.
Definition: LumiverseOrientation.h:173
float maxAsUnit(ORIENTATION_UNIT unit)
Returns the max value of the orientation with the specified units.
Definition: LumiverseOrientation.h:205
float getMax()
Get the maximum value.
Definition: LumiverseOrientation.h:155
float m_max
Maximum value for the orientation (default 1.0)
Definition: LumiverseOrientation.h:255
float m_val
the value of this object
Definition: LumiverseOrientation.h:249
float m_default
Default value for this orientation.
Definition: LumiverseOrientation.h:252
float getMin()
Get the minimum value.
Definition: LumiverseOrientation.h:167
float getVal()
Gets the value of the orientation.
Definition: LumiverseOrientation.h:120
void clamp()
Ensures that the value of this float is between min and max.
Definition: LumiverseOrientation.cpp:92
virtual JSONNode toJSON(string name)
Converts the type to a JSON object with the specified name.
Definition: LumiverseOrientation.cpp:32
void setUnit(ORIENTATION_UNIT unit)
Set unit.
Definition: LumiverseOrientation.cpp:75
ORIENTATION_UNIT
Enumeration indicating the type of angular unit stored.
Definition: LumiverseOrientation.h:17
Defines an orientation in Lumiverse.
Definition: LumiverseOrientation.h:61
static unordered_map< string, int > stringToOri
Definition: LumiverseOrientation.h:28
float asPercent()
Returns the value of this orientation as a percentage.
Definition: LumiverseOrientation.cpp:109
LumiverseOrientation(float val=0.0f, ORIENTATION_UNIT unit=DEGREE, float def=0.0f, float max=360.0f, float min=0.0f)
Constructs an orientation, default value is 0.
Definition: LumiverseOrientation.cpp:5
void setVals(float val, float def, float min, float max)
Sets the value of all orientation values except the unit.
Definition: LumiverseOrientation.cpp:66
static unordered_map< int, string > oriToString
Converts orientation to string.
Definition: LumiverseOrientation.h:23
ORIENTATION_UNIT getUnit()
Get the unit.
Definition: LumiverseOrientation.h:143
void setMin(float val, ORIENTATION_UNIT unit=DEGREE)
Set miniumum value.
Definition: LumiverseOrientation.h:161
Contains all core Lumiverse functions and variables.
Definition: Device.cpp:2
~LumiverseOrientation()
Destroys the orientation.
Definition: LumiverseOrientation.cpp:30
virtual bool isDefault()
Returns true if the value is equal to the default value for the type.
Definition: LumiverseOrientation.cpp:88
float getDefault()
Gets the default value for the orientation.
Definition: LumiverseOrientation.h:179
void setMax(float val, ORIENTATION_UNIT unit=DEGREE)
Set maximum value.
Definition: LumiverseOrientation.h:149
void setValAsPercent(float val)
Sets the value of the LumiverseOrientation proportionally.
Definition: LumiverseOrientation.cpp:103
float defaultAsUnit(ORIENTATION_UNIT unit)
Returns the default value of the orientation with the specified units.
Definition: LumiverseOrientation.h:215
virtual string getTypeName()
Says that this object is an orientation.
Definition: LumiverseOrientation.h:98
virtual string asString()
Returns the value of the LumiverseOrientation as a string.
Definition: LumiverseOrientation.cpp:50
float minAsUnit(ORIENTATION_UNIT unit)
Returns the min value of the orientation with the specified units.
Definition: LumiverseOrientation.h:210
void setVal(float val, ORIENTATION_UNIT unit=DEGREE)
Sets the value of the orientation.
Definition: LumiverseOrientation.h:126
float m_min
Minimum value for the orientation (default 0.0)
Definition: LumiverseOrientation.h:258