OGLplus (0.52.0) a C++ wrapper for OpenGL

angle.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_MATH_ANGLE_1107121519_HPP
14 #define OGLPLUS_MATH_ANGLE_1107121519_HPP
15 
16 #include <oglplus/config/compiler.hpp>
18 
19 #include <cassert>
20 #include <type_traits>
21 
22 namespace oglplus {
23 
25 
42 template <typename T>
43 class Angle
44 {
45 private:
46  // the angle value in radians
47  T _val_rad;
48 
49  struct Radians_ { };
50  Angle(T val_rad, Radians_)
51  : _val_rad(val_rad)
52  { }
53 
54  struct Degrees_ { };
55  Angle(T val_deg, Degrees_)
56  : _val_rad(T(val_deg * (math::Pi() / T(180))))
57  { }
58 public:
60  Angle(void)
61  : _val_rad(T(0))
62  { }
63 
64 #if !OGLPLUS_NO_DEFAULTED_FUNCTIONS || OGLPLUS_DOCUMENTATION_ONLY
65  Angle(const Angle&) = default;
67 
68  Angle& operator = (const Angle&) = default;
69 
71  Angle(Angle&&) = default;
72 #endif
73 
75  template <typename U>
76  Angle(const Angle<U>& other)
77  : _val_rad(T(other.Value()))
78  { }
79 
81  static inline Angle Radians(T val_rad)
82  {
83  return Angle(val_rad, Radians_());
84  }
85 
87  static inline Angle Degrees(T val_deg)
88  {
89  return Angle(val_deg, Degrees_());
90  }
91 
93  static inline Angle ArcSin(T x)
94  {
95  assert(-1.0f <= x && x <= 1.0f);
96  return Angle(::std::asin(x), Radians_());
97  }
98 
100  static inline Angle ArcCos(T x)
101  {
102  assert(-1.0f <= x && x <= 1.0f);
103  return Angle(::std::acos(x), Radians_());
104  }
105 
107  inline T Value(void) const
108  {
109  return _val_rad;
110  }
111 
113  inline T ValueInDegrees(void) const
114  {
115  return _val_rad * T(180 / math::Pi());
116  }
117 
119  inline T ValueInRightAngles(void) const
120  {
121  return _val_rad * T(2.0 / math::Pi());
122  }
123 
125  inline T ValueInFullCircles(void) const
126  {
127  return _val_rad * T(0.5 / math::Pi());
128  }
129 
131  friend bool operator == (const Angle& a, const Angle& b)
132  {
133  return a._val_rad == b._val_rad;
134  }
135 
137  friend bool operator != (const Angle& a, const Angle& b)
138  {
139  return a._val_rad != b._val_rad;
140  }
141 
143  friend bool operator < (const Angle& a, const Angle& b)
144  {
145  return a._val_rad < b._val_rad;
146  }
147 
149  friend bool operator > (const Angle& a, const Angle& b)
150  {
151  return a._val_rad > b._val_rad;
152  }
153 
155  friend bool operator <= (const Angle& a, const Angle& b)
156  {
157  return a._val_rad <= b._val_rad;
158  }
159 
161  friend bool operator >= (const Angle& a, const Angle& b)
162  {
163  return a._val_rad >= b._val_rad;
164  }
165 
166 #if OGLPLUS_DOCUMENTATION_ONLY
167  friend Angle Negate(const Angle& angle);
169 #endif
170 
172  Angle Negated(void) const
173  {
174  return Angle(-this->_val_rad, Radians_());
175  }
176 
178  friend Angle operator - (const Angle& angle)
179  {
180  return angle.Negated();
181  }
182 
183 #if OGLPLUS_DOCUMENTATION_ONLY
184  friend Angle Add(const Angle& a, const Angle& b);
186 #endif
187 
188  static Angle Added(const Angle& a, const Angle& b)
189  {
190  return Angle(a._val_rad + b._val_rad, Radians_());
191  }
192 
194  friend Angle operator + (const Angle& a, const Angle& b)
195  {
196  return Added(a, b);
197  }
198 
201  {
202  *this = Add(*this, b);
203  return *this;
204  }
205 
206 #if OGLPLUS_DOCUMENTATION_ONLY
207  friend Angle Subtract(const Angle& a, const Angle& b);
209 #endif
210 
211  static Angle Subtracted(const Angle& a, const Angle& b)
212  {
213  return Angle(a._val_rad - b._val_rad, Radians_());
214  }
215 
217  friend Angle operator - (const Angle& a, const Angle& b)
218  {
219  return Subtracted(a, b);
220  }
221 
224  {
225  *this = Subtracted(*this, b);
226  return *this;
227  }
228 
229 #if OGLPLUS_DOCUMENTATION_ONLY
230  friend Angle Multiply(const Angle& a, T mult);
232 #endif
233 
234  static Angle Multiplied(const Angle& a, T mult)
235  {
236  return Angle(a._val_rad * mult, Radians_());
237  }
238 
240  friend Angle operator * (const Angle& a, T mult)
241  {
242  return Multiplied(a, mult);
243  }
244 
246  friend Angle operator * (T mult, const Angle& a)
247  {
248  return Multiplied(a, mult);
249  }
250 
253  {
254  *this = Multiplied(*this, mult);
255  return *this;
256  }
257 
258 #if OGLPLUS_DOCUMENTATION_ONLY
259  friend Angle Divide(const Angle& a, T div);
261 #endif
262 
263  static Angle Divided(const Angle& a, T div)
264  {
265  assert(div != T(0));
266  return Angle(a._val_rad / div, Radians_());
267  }
268 
270  friend Angle operator / (const Angle& a, T div)
271  {
272  return Divided(a, div);
273  }
274 
277  {
278  *this = Divided(*this, div);
279  return *this;
280  }
281 
282 #if OGLPLUS_DOCUMENTATION_ONLY
283  friend T Ratio(const Angle& a, const Angle& b);
285 #endif
286 
287  static T Ratio(const Angle& a, const Angle& b)
288  {
289  assert(b._val_rad != T(0));
290  return a._val_rad / b._val_rad;
291  }
292 
294  friend T operator / (const Angle& a, const Angle& b)
295  {
296  return Ratio(a, b);
297  }
298 
299 #if OGLPLUS_DOCUMENTATION_ONLY
300  friend inline T Sin(const Angle& a);
302 #endif
303 
305  T Sin(void) const
306  {
307  return ::std::sin(this->_val_rad);
308  }
309 
310 #if OGLPLUS_DOCUMENTATION_ONLY
311  friend inline T Cos(const Angle& a);
313 #endif
314 
316  T Cos(void) const
317  {
318  return ::std::cos(this->_val_rad);
319  }
320 
321 #if OGLPLUS_DOCUMENTATION_ONLY
322  friend inline T Tan(const Angle& a);
324 #endif
325 
327  T Tan(void) const
328  {
329  return ::std::tan(this->_val_rad);
330  }
331 };
332 
333 template <typename T>
334 inline Angle<T> Negate(const Angle<T>& a)
335 {
336  return a.Negated();
337 }
338 
339 template <typename T>
340 inline Angle<T> Add(const Angle<T>& a, const Angle<T>& b)
341 {
342  return Angle<T>::Added(a, b);
343 }
344 
345 template <typename T>
346 inline Angle<T> Subtract(const Angle<T>& a, const Angle<T>& b)
347 {
348  return Angle<T>::Subtracted(a, b);
349 }
350 
351 template <typename T>
352 inline Angle<T> Multiply(const Angle<T>& a, T v)
353 {
354  return Angle<T>::Multiplied(a, v);
355 }
356 
357 template <typename T>
358 inline Angle<T> Divide(const Angle<T>& a, T v)
359 {
360  return Angle<T>::Divided(a, v);
361 }
362 
363 template <typename T>
364 inline T Ratio(const Angle<T>& a, const Angle<T>& b)
365 {
366  return Angle<T>::Ratio(a, b);
367 }
368 
369 template <typename T>
370 inline T Radians(const Angle<T>& a)
371 {
372  return a.Value();
373 }
374 
375 template <typename T>
376 inline T Degrees(const Angle<T>& a)
377 {
378  return a.ValueInDegrees();
379 }
380 
381 template <typename T>
382 inline T Sin(const Angle<T>& a)
383 {
384  return a.Sin();
385 }
386 
387 template <typename T>
388 inline T Cos(const Angle<T>& a)
389 {
390  return a.Cos();
391 }
392 
393 template <typename T>
394 inline T Tan(const Angle<T>& a)
395 {
396  return a.Tan();
397 }
398 
399 #if OGLPLUS_DOCUMENTATION_ONLY || defined(GL_FLOAT)
400 typedef Angle<GLfloat> Anglef;
402 
403 typedef GLfloat AngleValueType;
404 #elif defined(AL_VERSION_1_1)
405 typedef ALfloat AngleValueType;
406 #else
407 typedef double AngleValueType;
408 #endif
409 
411 
425 inline Angle<AngleValueType> Radians(AngleValueType val_rad)
426 {
427  return Angle<AngleValueType>::Radians(val_rad);
428 }
429 
431 
452 inline Angle<AngleValueType> Degrees(AngleValueType val_deg)
453 {
454  return Angle<AngleValueType>::Degrees(val_deg);
455 }
456 
458 
480 inline Angle<AngleValueType> FullCircles(AngleValueType value)
481 {
483  AngleValueType(value * math::TwoPi())
484  );
485 }
486 
487 inline Angle<AngleValueType> FullCircle(void)
488 {
489  return Angle<AngleValueType>::Radians(AngleValueType(math::TwoPi()));
490 }
491 
493 
515 inline Angle<AngleValueType> RightAngles(AngleValueType value)
516 {
518  AngleValueType(value * math::HalfPi())
519  );
520 }
521 
522 inline Angle<AngleValueType> RightAngle(void)
523 {
524  return Angle<AngleValueType>::Radians(AngleValueType(math::HalfPi()));
525 }
526 
528 
540 inline Angle<AngleValueType> ArcSin(AngleValueType x)
541 {
543 }
544 
546 
558 inline Angle<AngleValueType> ArcCos(AngleValueType x)
559 {
561 }
562 
564 
575 inline Angle<AngleValueType> ArcTan(AngleValueType x)
576 {
577  return Angle<AngleValueType>::Radians(::std::atan(x));
578 }
579 
581 
592 inline Angle<AngleValueType> ArcTan(AngleValueType y, AngleValueType x)
593 {
594  return Angle<AngleValueType>::Radians(::std::atan2(y, x));
595 }
596 
598 
618 template <typename T>
619 inline T SineWave(T t)
620 {
621  return ::std::sin(T(math::TwoPi() * t));
622 }
623 
625 
645 template <typename T>
646 inline T SineWave01(T t)
647 {
648  return (SineWave(t)+T(1))/T(2);
649 }
650 
652 
672 template <typename T>
673 inline T CosineWave(T t)
674 {
675  return ::std::cos(T(math::TwoPi() * t));
676 }
677 
679 
699 template <typename T>
700 inline T CosineWave01(T t)
701 {
702  return (CosineWave(t)+T(1))/T(2);
703 }
704 
705 } // namespace oglplus
706 
707 #endif // include guard
Angle & operator+=(const Angle &b)
Addition operator.
Definition: angle.hpp:200
T Value(void) const
Returns the value of the angle in radians.
Definition: angle.hpp:107
Class implementing planar angle-related functionality.
Definition: fwd.hpp:24
Angle Negated(void) const
Negation.
Definition: angle.hpp:172
T ValueInDegrees(void) const
Returns the value of the angle in degrees.
Definition: angle.hpp:113
Angle & operator-=(const Angle &b)
Subtraction operator.
Definition: angle.hpp:223
friend Angle operator/(const Angle &a, T div)
Division by constant operator.
Definition: angle.hpp:270
friend Angle Multiply(const Angle &a, T mult)
Multiplication by constant.
Definition: angle.hpp:352
T SineWave(T t)
Returns a value on a sine wave at the specified point.
Definition: angle.hpp:619
friend Angle operator+(const Angle &a, const Angle &b)
Addition operator.
Definition: angle.hpp:194
friend Angle Subtract(const Angle &a, const Angle &b)
Subtraction.
Definition: angle.hpp:346
Angle & operator*=(T mult)
Multiplication by constant operator.
Definition: angle.hpp:252
static Angle ArcSin(T x)
Constructs a new angle using arc sine.
Definition: angle.hpp:93
friend Angle Add(const Angle &a, const Angle &b)
Addition.
Definition: angle.hpp:340
friend bool operator>=(const Angle &a, const Angle &b)
Greater than/equal comparison.
Definition: angle.hpp:161
Angle< GLfloat > Anglef
Instantiation of Angle using GL floating-point as underlying type.
Definition: angle.hpp:401
Angle(void)
Constructs a zero angle.
Definition: angle.hpp:60
friend bool operator!=(const Angle &a, const Angle &b)
Inequality comparison.
Definition: angle.hpp:137
friend bool operator<=(const Angle &a, const Angle &b)
Less than/equal comparison.
Definition: angle.hpp:155
T CosineWave01(T t)
Returns a value on a cosine wave transformed to range <0, 1>
Definition: angle.hpp:700
Angle< AngleValueType > ArcCos(AngleValueType x)
Creates a new angle using the arc cosine function.
Definition: angle.hpp:558
T ValueInFullCircles(void) const
Returns the value of the angle in number of full circles.
Definition: angle.hpp:125
Angle & operator/=(T div)
Division by constant operator.
Definition: angle.hpp:276
friend T Ratio(const Angle &a, const Angle &b)
Angle ratio.
Definition: angle.hpp:364
static Angle ArcCos(T x)
Constructs a new angle using arc cosine.
Definition: angle.hpp:100
static Angle Radians(T val_rad)
Constructs a new angle from value in radians.
Definition: angle.hpp:81
friend bool operator>(const Angle &a, const Angle &b)
Greater than comparison.
Definition: angle.hpp:149
friend bool operator<(const Angle &a, const Angle &b)
Less than comparison.
Definition: angle.hpp:143
static Angle Degrees(T val_deg)
Constructs a new angle from value in degrees.
Definition: angle.hpp:87
T Tan(void) const
Returns the tangent of the angle.
Definition: angle.hpp:327
T CosineWave(T t)
Returns a value on a cosine wave at the specified point.
Definition: angle.hpp:673
T SineWave01(T t)
Returns a value on a sine wave transformed to range <0, 1>
Definition: angle.hpp:646
Math constants.
Angle< AngleValueType > RightAngles(AngleValueType value)
Creates a new angle from a value in "right angles" (i.e. 90 deg.)
Definition: angle.hpp:515
T Sin(void) const
Returns the sine of the angle.
Definition: angle.hpp:305
friend Angle Divide(const Angle &a, T div)
Division by constant.
Definition: angle.hpp:358
friend Angle operator*(const Angle &a, T mult)
Multiplication by constant operator.
Definition: angle.hpp:240
friend Angle operator-(const Angle &angle)
Negation operator.
Definition: angle.hpp:178
Angle< AngleValueType > ArcTan(AngleValueType x)
Creates a new angle using the arc tangent function.
Definition: angle.hpp:575
friend Angle Negate(const Angle &angle)
Negation.
Definition: angle.hpp:334
Angle< AngleValueType > ArcSin(AngleValueType x)
Creates a new angle using the arc sine function.
Definition: angle.hpp:540
friend bool operator==(const Angle &a, const Angle &b)
Equality comparison.
Definition: angle.hpp:131
Angle< AngleValueType > FullCircles(AngleValueType value)
Creates a new angle from a value in "full circles" (i.e. 360 degrees)
Definition: angle.hpp:480
T ValueInRightAngles(void) const
Returns the value of the angle in number of right angles.
Definition: angle.hpp:119
T Cos(void) const
Returns the cosine of the angle.
Definition: angle.hpp:316
Angle(const Angle< U > &other)
Copy construction from angles using different underlying type.
Definition: angle.hpp:76

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).