GteCosEstimate.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 cos(x). The polynomial p(x) of
13 // degree D has only even-power terms, is required to have constant term 1,
14 // and p(pi/2) = cos(pi/2) = 0. It minimizes the quantity
15 // maximum{|cos(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 = CosEstimate<float>::Degree<4>(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] and a sign s for which
33  // cos(y) = s*cos(x). For example,
34  // float x; // x any real number
35  // float result = CosEstimate<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<2>, Real x);
44  inline static Real Evaluate(degree<4>, Real x);
45  inline static Real Evaluate(degree<6>, Real x);
46  inline static Real Evaluate(degree<8>, Real x);
47  inline static Real Evaluate(degree<10>, Real x);
48 
49  // Support for range reduction.
50  inline static void Reduce(Real x, Real& y, Real& sign);
51 };
52 
53 
54 template <typename Real>
55 template <int D>
56 inline Real CosEstimate<Real>::Degree(Real x)
57 {
58  return Evaluate(degree<D>(), x);
59 }
60 
61 template <typename Real>
62 template <int D>
63 inline Real CosEstimate<Real>::DegreeRR(Real x)
64 {
65  Real y, sign;
66  Reduce(x, y, sign);
67  Real poly = sign * Degree<D>(y);
68  return poly;
69 }
70 
71 template <typename Real>
73 {
74  Real xsqr = x * x;
75  Real poly;
76  poly = (Real)GTE_C_COS_DEG2_C1;
77  poly = (Real)GTE_C_COS_DEG2_C0 + poly * xsqr;
78  return poly;
79 }
80 
81 template <typename Real>
83 {
84  Real xsqr = x * x;
85  Real poly;
86  poly = (Real)GTE_C_COS_DEG4_C2;
87  poly = (Real)GTE_C_COS_DEG4_C1 + poly * xsqr;
88  poly = (Real)GTE_C_COS_DEG4_C0 + poly * xsqr;
89  return poly;
90 }
91 
92 template <typename Real>
94 {
95  Real xsqr = x * x;
96  Real poly;
97  poly = (Real)GTE_C_COS_DEG6_C3;
98  poly = (Real)GTE_C_COS_DEG6_C2 + poly * xsqr;
99  poly = (Real)GTE_C_COS_DEG6_C1 + poly * xsqr;
100  poly = (Real)GTE_C_COS_DEG6_C0 + poly * xsqr;
101  return poly;
102 }
103 
104 template <typename Real>
106 {
107  Real xsqr = x * x;
108  Real poly;
109  poly = (Real)GTE_C_COS_DEG8_C4;
110  poly = (Real)GTE_C_COS_DEG8_C3 + poly * xsqr;
111  poly = (Real)GTE_C_COS_DEG8_C2 + poly * xsqr;
112  poly = (Real)GTE_C_COS_DEG8_C1 + poly * xsqr;
113  poly = (Real)GTE_C_COS_DEG8_C0 + poly * xsqr;
114  return poly;
115 }
116 
117 template <typename Real>
119 {
120  Real xsqr = x * x;
121  Real poly;
122  poly = (Real)GTE_C_COS_DEG10_C5;
123  poly = (Real)GTE_C_COS_DEG10_C4 + poly * xsqr;
124  poly = (Real)GTE_C_COS_DEG10_C3 + poly * xsqr;
125  poly = (Real)GTE_C_COS_DEG10_C2 + poly * xsqr;
126  poly = (Real)GTE_C_COS_DEG10_C1 + poly * xsqr;
127  poly = (Real)GTE_C_COS_DEG10_C0 + poly * xsqr;
128  return poly;
129 }
130 
131 template <typename Real>
132 inline void CosEstimate<Real>::Reduce(Real x, Real& y, Real& sign)
133 {
134  // Map x to y in [-pi,pi], x = 2*pi*quotient + remainder.
135  Real quotient = (Real)GTE_C_INV_TWO_PI * x;
136  if (x >= (Real)0)
137  {
138  quotient = (Real)((int)(quotient + (Real)0.5));
139  }
140  else
141  {
142  quotient = (Real)((int)(quotient - (Real)0.5));
143  }
144  y = x - (Real)GTE_C_TWO_PI * quotient;
145 
146  // Map y to [-pi/2,pi/2] with cos(y) = sign*cos(x).
147  if (y > (Real)GTE_C_HALF_PI)
148  {
149  y = (Real)GTE_C_PI - y;
150  sign = (Real)-1;
151  }
152  else if (y < (Real)-GTE_C_HALF_PI)
153  {
154  y = (Real)-GTE_C_PI - y;
155  sign = (Real)-1;
156  }
157  else
158  {
159  sign = (Real)1;
160  }
161 }
162 
163 
164 }
static void Reduce(Real x, Real &y, Real &sign)
#define GTE_C_COS_DEG10_C5
Definition: GteConstants.h:222
#define GTE_C_COS_DEG6_C0
Definition: GteConstants.h:204
#define GTE_C_INV_TWO_PI
Definition: GteConstants.h:22
#define GTE_C_COS_DEG4_C2
Definition: GteConstants.h:201
#define GTE_C_COS_DEG8_C4
Definition: GteConstants.h:214
static Real DegreeRR(Real x)
#define GTE_C_COS_DEG6_C2
Definition: GteConstants.h:206
#define GTE_C_COS_DEG8_C3
Definition: GteConstants.h:213
#define GTE_C_COS_DEG4_C0
Definition: GteConstants.h:199
GLint GLenum GLint x
Definition: glcorearb.h:404
#define GTE_C_COS_DEG8_C0
Definition: GteConstants.h:210
#define GTE_C_COS_DEG10_C2
Definition: GteConstants.h:219
#define GTE_C_PI
Definition: GteConstants.h:17
#define GTE_C_COS_DEG8_C1
Definition: GteConstants.h:211
#define GTE_C_COS_DEG10_C0
Definition: GteConstants.h:217
#define GTE_C_COS_DEG2_C1
Definition: GteConstants.h:196
#define GTE_C_COS_DEG6_C3
Definition: GteConstants.h:207
static Real Evaluate(degree< 2 >, Real x)
#define GTE_C_COS_DEG6_C1
Definition: GteConstants.h:205
static Real Degree(Real x)
#define GTE_C_HALF_PI
Definition: GteConstants.h:18
#define GTE_C_COS_DEG10_C1
Definition: GteConstants.h:218
#define GTE_C_COS_DEG10_C4
Definition: GteConstants.h:221
#define GTE_C_COS_DEG2_C0
Definition: GteConstants.h:195
#define GTE_C_COS_DEG8_C2
Definition: GteConstants.h:212
#define GTE_C_TWO_PI
Definition: GteConstants.h:20
GLint y
Definition: glcorearb.h:98
#define GTE_C_COS_DEG4_C1
Definition: GteConstants.h:200
#define GTE_C_COS_DEG10_C3
Definition: GteConstants.h:220


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59