GteIntpSphere2.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 
13 #include <memory>
14 
15 // Interpolation of a scalar-valued function defined on a sphere. Although
16 // the sphere lives in 3D, the interpolation is a 2D method whose input
17 // points are angles (theta,phi) from spherical coordinates. The domains of
18 // the angles are -pi <= theta <= pi and 0 <= phi <= pi.
19 
20 namespace gte
21 {
22 
23 template <typename InputType, typename ComputeType, typename RationalType>
25 {
26 public:
27  // Construction and destruction. For complete spherical coverage, include
28  // the two antipodal (theta,phi) points (-pi,0,F(-pi,0)) and
29  // (-pi,pi,F(-pi,pi)) in the input data. These correspond to the sphere
30  // poles x = 0, y = 0, and |z| = 1.
31  ~IntpSphere2();
32  IntpSphere2(int numPoints, InputType const* theta, InputType const* phi,
33  InputType const* F);
34 
35  // Spherical coordinates are
36  // x = cos(theta)*sin(phi)
37  // y = sin(theta)*sin(phi)
38  // z = cos(phi)
39  // for -pi <= theta <= pi, 0 <= phi <= pi. The application can use this
40  // function to convert unit length vectors (x,y,z) to (theta,phi).
41  static void GetSphericalCoordinates(InputType x, InputType y, InputType z,
42  InputType& theta, InputType& phi);
43 
44  // The return value is 'true' if and only if the input point is in the
45  // convex hull of the input (theta,pi) array, in which case the
46  // interpolation is valid.
47  bool operator()(InputType theta, InputType phi, InputType& F) const;
48 
49 private:
51 
52  std::vector<Vector2<InputType>> mWrapAngles;
54  TriangleMesh mMesh;
55  std::vector<InputType> mWrapF;
56  std::unique_ptr<IntpQuadraticNonuniform2<InputType, TriangleMesh>> mInterp;
57 };
58 
59 
60 template <typename InputType, typename ComputeType, typename RationalType>
62 {
63 }
64 
65 template <typename InputType, typename ComputeType, typename RationalType>
67  InputType const* theta, InputType const* phi, InputType const* F)
68  :
70 {
71  // Copy the input data. The larger arrays are used to support wrap-around
72  // in the Delaunay triangulation for the interpolator.
73  int totalPoints = 3 * numPoints;
74  mWrapAngles.resize(totalPoints);
75  mWrapF.resize(totalPoints);
76  for (int i = 0; i < numPoints; ++i)
77  {
78  mWrapAngles[i][0] = theta[i];
79  mWrapAngles[i][1] = phi[i];
80  mWrapF[i] = F[i];
81  }
82 
83  // Use periodicity to get wrap-around in the Delaunay triangulation.
84  int i0 = 0, i1 = numPoints, i2 = 2 * numPoints;
85  for (; i0 < numPoints; ++i0, ++i1, ++i2)
86  {
87  mWrapAngles[i1][0] = mWrapAngles[i0][0] + (InputType)GTE_C_TWO_PI;
88  mWrapAngles[i2][0] = mWrapAngles[i0][0] - (InputType)GTE_C_TWO_PI;
89  mWrapAngles[i1][1] = mWrapAngles[i0][1];
90  mWrapAngles[i2][1] = mWrapAngles[i0][1];
91  mWrapF[i1] = mWrapF[i0];
92  mWrapF[i2] = mWrapF[i0];
93  }
94 
95  mDelaunay(totalPoints, &mWrapAngles[0], (ComputeType)0);
96  mInterp = std::make_unique<IntpQuadraticNonuniform2<InputType, TriangleMesh>>(
97  mMesh, &mWrapF[0], (InputType)1);
98 }
99 
100 template <typename InputType, typename ComputeType, typename RationalType>
102 GetSphericalCoordinates(InputType x, InputType y, InputType z,
103 InputType& theta, InputType& phi)
104 {
105  // Assumes (x,y,z) is unit length. Returns -pi <= theta <= pi and
106  // 0 <= phiAngle <= pi.
107 
108  if (z < (InputType)1)
109  {
110  if (z > -(InputType)1)
111  {
112  theta = atan2(y, x);
113  phi = acos(z);
114  }
115  else
116  {
117  theta = -(InputType)GTE_C_PI;
118  phi = (InputType)GTE_C_PI;
119  }
120  }
121  else
122  {
123  theta = -(InputType)GTE_C_PI;
124  phi = (InputType)0;
125  }
126 }
127 
128 template <typename InputType, typename ComputeType, typename RationalType>
130  InputType theta, InputType phi, InputType& F) const
131 {
132  Vector2<InputType> angles{ theta, phi };
133  InputType thetaDeriv, phiDeriv;
134  return (*mInterp)(angles, F, thetaDeriv, phiDeriv);
135 }
136 
137 }
std::unique_ptr< IntpQuadraticNonuniform2< InputType, TriangleMesh > > mInterp
static void GetSphericalCoordinates(InputType x, InputType y, InputType z, InputType &theta, InputType &phi)
GLint GLenum GLint x
Definition: glcorearb.h:404
Delaunay2< InputType, ComputeType > mDelaunay
#define GTE_C_PI
Definition: GteConstants.h:17
std::vector< InputType > mWrapF
IntpSphere2(int numPoints, InputType const *theta, InputType const *phi, InputType const *F)
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:843
bool operator()(InputType theta, InputType phi, InputType &F) const
Delaunay2Mesh< InputType, ComputeType, RationalType > TriangleMesh
std::vector< Vector2< InputType > > mWrapAngles
TriangleMesh mMesh
#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:00