GteSinEstimate.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
11 
12 // Minimax polynomial approximations to sin(x). The polynomial p(x) of
13 // degree D has only odd-power terms, is required to have linear term x,
14 // and p(pi/2) = sin(pi/2) = 1. It minimizes the quantity
15 // maximum{|sin(x) - p(x)| : x in [-pi/2,pi/2]} over all polynomials of
16 // degree D subject to the constraints mentioned.
17 
18 namespace gte
19 {
20 
21 template <typename Real>
23 {
24 public:
25  // The input constraint is x in [-pi/2,pi/2]. For example,
26  // float x; // in [-pi/2,pi/2]
27  // float result = SinEstimate<float>::Degree<3>(x);
28  template <int D>
29  inline static Real Degree(Real x);
30 
31  // The input x can be any real number. Range reduction is used to
32  // generate a value y in [-pi/2,pi/2] for which sin(y) = sin(x).
33  // For example,
34  // float x; // x any real number
35  // float result = SinEstimate<float>::DegreeRR<3>(x);
36  template <int D>
37  inline static Real DegreeRR(Real x);
38 
39 private:
40  // Metaprogramming and private implementation to allow specialization of
41  // a template member function.
42  template <int D> struct degree {};
43  inline static Real Evaluate(degree<3>, Real x);
44  inline static Real Evaluate(degree<5>, Real x);
45  inline static Real Evaluate(degree<7>, Real x);
46  inline static Real Evaluate(degree<9>, Real x);
47  inline static Real Evaluate(degree<11>, Real x);
48 
49  // Support for range reduction.
50  inline static Real Reduce(Real x);
51 };
52 
53 
54 template <typename Real>
55 template <int D>
56 inline Real SinEstimate<Real>::Degree(Real x)
57 {
58  return Evaluate(degree<D>(), x);
59 }
60 
61 template <typename Real>
62 template <int D>
63 inline Real SinEstimate<Real>::DegreeRR(Real x)
64 {
65  return Degree<D>(Reduce(x));
66 }
67 
68 template <typename Real>
70 {
71  Real xsqr = x * x;
72  Real poly;
73  poly = (Real)GTE_C_SIN_DEG3_C1;
74  poly = (Real)GTE_C_SIN_DEG3_C0 + poly * xsqr;
75  poly = poly * x;
76  return poly;
77 }
78 
79 template <typename Real>
81 {
82  Real xsqr = x * x;
83  Real poly;
84  poly = (Real)GTE_C_SIN_DEG5_C2;
85  poly = (Real)GTE_C_SIN_DEG5_C1 + poly * xsqr;
86  poly = (Real)GTE_C_SIN_DEG5_C0 + poly * xsqr;
87  poly = poly * x;
88  return poly;
89 }
90 
91 template <typename Real>
93 {
94  Real xsqr = x * x;
95  Real poly;
96  poly = (Real)GTE_C_SIN_DEG7_C3;
97  poly = (Real)GTE_C_SIN_DEG7_C2 + poly * xsqr;
98  poly = (Real)GTE_C_SIN_DEG7_C1 + poly * xsqr;
99  poly = (Real)GTE_C_SIN_DEG7_C0 + poly * xsqr;
100  poly = poly * x;
101  return poly;
102 }
103 
104 template <typename Real>
106 {
107  Real xsqr = x * x;
108  Real poly;
109  poly = (Real)GTE_C_SIN_DEG9_C4;
110  poly = (Real)GTE_C_SIN_DEG9_C3 + poly * xsqr;
111  poly = (Real)GTE_C_SIN_DEG9_C2 + poly * xsqr;
112  poly = (Real)GTE_C_SIN_DEG9_C1 + poly * xsqr;
113  poly = (Real)GTE_C_SIN_DEG9_C0 + poly * xsqr;
114  poly = poly * x;
115  return poly;
116 }
117 
118 template <typename Real>
120 {
121  Real xsqr = x * x;
122  Real poly;
123  poly = (Real)GTE_C_SIN_DEG11_C5;
124  poly = (Real)GTE_C_SIN_DEG11_C4 + poly * xsqr;
125  poly = (Real)GTE_C_SIN_DEG11_C3 + poly * xsqr;
126  poly = (Real)GTE_C_SIN_DEG11_C2 + poly * xsqr;
127  poly = (Real)GTE_C_SIN_DEG11_C1 + poly * xsqr;
128  poly = (Real)GTE_C_SIN_DEG11_C0 + poly * xsqr;
129  poly = poly * x;
130  return poly;
131 }
132 
133 template <typename Real>
134 inline Real SinEstimate<Real>::Reduce(Real x)
135 {
136  // Map x to y in [-pi,pi], x = 2*pi*quotient + remainder.
137  Real quotient = (Real)GTE_C_INV_TWO_PI * x;
138  if (x >= (Real)0)
139  {
140  quotient = (Real)((int)(quotient + (Real)0.5));
141  }
142  else
143  {
144  quotient = (Real)((int)(quotient - (Real)0.5));
145  }
146  Real y = x - (Real)GTE_C_TWO_PI * quotient;
147 
148  // Map y to [-pi/2,pi/2] with sin(y) = sin(x).
149  if (y > (Real)GTE_C_HALF_PI)
150  {
151  y = (Real)GTE_C_PI - y;
152  }
153  else if (y < (Real)-GTE_C_HALF_PI)
154  {
155  y = (Real)-GTE_C_PI - y;
156  }
157  return y;
158 }
159 
160 
161 }
#define GTE_C_SIN_DEG7_C0
Definition: GteConstants.h:172
#define GTE_C_SIN_DEG11_C2
Definition: GteConstants.h:187
#define GTE_C_INV_TWO_PI
Definition: GteConstants.h:22
#define GTE_C_SIN_DEG5_C1
Definition: GteConstants.h:168
#define GTE_C_SIN_DEG9_C0
Definition: GteConstants.h:178
#define GTE_C_SIN_DEG3_C0
Definition: GteConstants.h:163
#define GTE_C_SIN_DEG11_C3
Definition: GteConstants.h:188
#define GTE_C_SIN_DEG7_C1
Definition: GteConstants.h:173
#define GTE_C_SIN_DEG5_C2
Definition: GteConstants.h:169
#define GTE_C_SIN_DEG7_C3
Definition: GteConstants.h:175
GLint GLenum GLint x
Definition: glcorearb.h:404
#define GTE_C_SIN_DEG11_C5
Definition: GteConstants.h:190
#define GTE_C_SIN_DEG11_C1
Definition: GteConstants.h:186
#define GTE_C_SIN_DEG5_C0
Definition: GteConstants.h:167
#define GTE_C_PI
Definition: GteConstants.h:17
static Real Reduce(Real x)
#define GTE_C_SIN_DEG3_C1
Definition: GteConstants.h:164
static Real Evaluate(degree< 3 >, Real x)
#define GTE_C_SIN_DEG9_C4
Definition: GteConstants.h:182
#define GTE_C_SIN_DEG7_C2
Definition: GteConstants.h:174
#define GTE_C_SIN_DEG9_C1
Definition: GteConstants.h:179
#define GTE_C_SIN_DEG11_C4
Definition: GteConstants.h:189
#define GTE_C_SIN_DEG9_C3
Definition: GteConstants.h:181
#define GTE_C_SIN_DEG9_C2
Definition: GteConstants.h:180
#define GTE_C_HALF_PI
Definition: GteConstants.h:18
static Real DegreeRR(Real x)
static Real Degree(Real x)
#define GTE_C_SIN_DEG11_C0
Definition: GteConstants.h:185
#define GTE_C_TWO_PI
Definition: GteConstants.h:20
GLint y
Definition: glcorearb.h:98


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01