Honeycomb  0.1
Component-Model Framework
Trig.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Math/Alge/Alge.h"
5 
6 namespace honey
7 {
8 
9 template<class Real> class Interp_;
10 
12 template<class Real>
13 class SinTable
14 {
15  typedef typename Numeral<Real>::Real_ Real_;
16  typedef Alge_<Real> Alge;
17  typedef Interp_<Real> Interp;
18 
19 public:
20  SinTable() : _size(0) {}
21 
22  void resize(int size);
23  int size() const { return _size; }
24 
25  Real sin(Real x) const;
26  Real asin(Real x) const;
27  Real cos(Real x) const { return sin(Real_::piHalf - x); }
28  Real acos(Real x) const { return Real_::piHalf - asin(x); }
29  Real tan(Real x) const { return sin(x) / cos(x); }
30  Real atan(Real x) const { return asin(x / Alge::sqrt(1 + x*x)); }
31  Real atan2(Real y, Real x) const;
32 
33 private:
34  typedef vector<Real> List;
35 
36  Real linear(const List& list, Real idx) const { int cur = idx; return Interp::linear(idx-cur, list[cur], list[(cur+1)%_size]); }
37 
38  List _sin;
39  List _asin;
40  int _size;
41  int _radToSin;
42  int _xToAsin;
43 };
44 
45 extern template class SinTable<Float>;
46 extern template class SinTable<Double>;
47 extern template class SinTable<Quad>;
48 
49 
51 template<class Real>
52 class Trig_
53 {
54  typedef typename Numeral<Real>::Real_ Real_;
55  typedef Alge_<Real> Alge;
56 
57 public:
59  static void enableSinTable(bool enable, int size = -1);
60 
62  static Real sin(Real x) { auto& t = inst(); return t._tableEnable ? t._table.sin(x) : Real_::sin(x); }
64  static Real asin(Real x) { auto& t = inst(); return t._tableEnable ? t._table.asin(x) : Real_::asin(x); }
65 
67  static Real cos(Real x) { auto& t = inst(); return t._tableEnable ? t._table.cos(x) : Real_::cos(x); }
69  static Real acos(Real x) { auto& t = inst(); return t._tableEnable ? t._table.acos(x) : Real_::acos(x); }
70 
72  static Real tan(Real x) { auto& t = inst(); return t._tableEnable ? t._table.tan(x) : Real_::tan(x); }
74  static Real atan(Real x) { auto& t = inst(); return t._tableEnable ? t._table.atan(x) : Real_::atan(x); }
76  static Real atan2(Real y, Real x) { auto& t = inst(); return t._tableEnable ? t._table.atan2(y,x) : Real_::atan2(y,x); }
77 
79  static Real radian(Real degree) { return degree * Real_::pi / 180; }
81  static Real degree(Real radian) { return radian * 180 / Real_::pi; }
82 
84  static Real normalizeAngle(Real angle) { return Alge::modNormalize(Real_::pi, angle); }
86  static Real alignAngle(Real angleFrom, Real angleTo) { return Alge::modDistSigned(Real_::pi, angleFrom, angleTo); }
88  static Real distanceAngle(Real angle, Real angle2) { return Alge::abs(alignAngle(angle, angle2)); }
89 
90 private:
91  Trig_() : _tableEnable(false) {}
92 
94  static mt_global(Trig_, inst,);
95 
96  SinTable<Real> _table;
97  bool _tableEnable;
98 
99  static const int tableSizeDefault = 1 << 13;
100 };
101 
106 
107 extern template class Trig_<Float>;
108 extern template class Trig_<Double>;
109 extern template class Trig_<Quad>;
110 
111 }
112 
#define mt_global(Class, Func, Ctor)
Create a global object that will be initialized upon first access, so it can be accessed safely from ...
Definition: Meta.h:283
Trig_< Float > Trig_f
Definition: Trig.h:103
static Real alignAngle(Real angleFrom, Real angleTo)
Calc smallest angle to align angleFrom with angleTo. Angles must be normalized. Result is in range [-...
Definition: Trig.h:86
Algebra.
Definition: Alge.h:13
Real acos(Real x) const
Definition: Trig.h:28
Real atan(Real x) const
Definition: Trig.h:30
static Real sin(Real x)
Sin of radian angle.
Definition: Trig.h:62
Speeds up all trig functions at the cost of precision. Precision is roughly 1 / size.
Definition: Trig.h:13
Interpolation math.
Definition: Quat.h:10
static Real tan(Real x)
Tan of radian angle.
Definition: Trig.h:72
int size() const
Definition: Trig.h:23
Trig_< Double > Trig_d
Definition: Trig.h:104
Alge_< Real > Alge
Definition: Alge.h:130
static Real sqrt(Real x)
Square Root.
Definition: Alge.h:63
static Real atan(Real x)
Convert tangent ratio [-inf, inf] to radian angle [-pi/2, pi/2].
Definition: Trig.h:74
static Real distanceAngle(Real angle, Real angle2)
Calc shortest angular distance between angle and angle2. Angles must be normalized. Result is in range [0, pi].
Definition: Trig.h:88
Real asin(Real x) const
Definition: Trig.cpp:41
static Real cos(Real x)
Cos of radian angle.
Definition: Trig.h:67
Real sin(Real x) const
Definition: Trig.cpp:28
static Int abs(Int x)
Get absolute value of signed integer.
Definition: Alge.h:21
static Real degree(Real radian)
Convert angle in radians to angle in degrees.
Definition: Trig.h:81
Trig_< Real > Trig
Definition: Trig.h:102
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
Real atan2(Real y, Real x) const
Definition: Trig.cpp:49
void resize(int size)
Definition: Trig.cpp:10
Numeral< Real >::Real_ Real_
Operations and constants for Real type. See Float_, Double_.
Definition: Real.h:25
Real tan(Real x) const
Definition: Trig.h:29
static T linear(Real t, const T &a, const T &b)
Linear interpolation. t range is [0,1].
Definition: Interp.h:28
static Real modDistSigned(Real mod, Real x, Real y)
Calc smallest signed distance between two normalized values in a modular field.
Definition: Alge.h:53
int size(const StdContainer &cont)
Safely get the size of a std container as a signed integer.
Definition: StdUtil.h:19
static Real radian(Real degree)
Convert angle in degrees to angle in radians.
Definition: Trig.h:79
static Real asin(Real x)
Convert sine ratio [-1, 1] to radian angle [-pi/2, pi/2].
Definition: Trig.h:64
static Real normalizeAngle(Real angle)
Get an equivalent angle in the normalized range [-pi, pi].
Definition: Trig.h:84
Real cos(Real x) const
Definition: Trig.h:27
Trig_< Quad > Trig_q
Definition: Trig.h:105
static Real modNormalize(Real mod, Real val)
Get an equivalent value in the normalized modular interval [-mod, mod].
Definition: Alge.h:47
SinTable()
Definition: Trig.h:20
Global Honeycomb namespace.
static Real acos(Real x)
Convert cosine ratio [-1, 1] to radian angle [0, pi].
Definition: Trig.h:69
Trigonometry.
Definition: Trig.h:52
static Real atan2(Real y, Real x)
Converts Cartesian(x,y) to Polar(r, theta), and returns radian angle theta [-pi, pi].
Definition: Trig.h:76