GteTorus3.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 
10 #include <Mathematics/GteVector3.h>
11 
12 // The torus has center C with plane of symmetry containing C and having
13 // directions D0 and D1. The axis of symmetry is the line containing C
14 // and having direction N (the plane normal). The radius from the center
15 // of the torus is r0 (outer radius) and the radius of the tube of the
16 // torus is r1 (inner radius). It must be that r0 >= r1. A point X may
17 // be written as X = C + y0*D0 + y1*D1 + y2*N, where matrix [U V N] is
18 // orthonormal and has determinant 1. Thus, y0 = Dot(D0,X-C),
19 // y1 = Dot(D1,X-C), and y2 = Dot(N,X-C). The implicit form is
20 // [|X-C|^2 - (r0^2 + r1^2)]^2 + 4*r0^2*((Dot(N,X-C))^2 - r1^2) = 0
21 // Note that D0 and D1 are not present in the equation, which is to be
22 // expected by the symmetry. The parametric form is
23 // X(u,v) = (r0 + r1*cos(v))*(cos(u)*D0 + sin(u)*D1) + r1*sin(v)*N
24 // for -pi <= u < pi, -pi <= v < pi. The member 'center' is C, 'direction0'
25 // is D0, 'direction1' is D1, 'normal' is N, 'radius0' is r0, and 'radius1'
26 // is r1.
27 
28 namespace gte
29 {
30 
31 template <typename Real>
32 class Torus3
33 {
34 public:
35  // Construction and destruction. The default constructor sets center to
36  // (0,0,0), direction0 to (1,0,0), direction1 to (0,1,0), normal to
37  // (0,0,1), radius0 to 2, and radius1 to 1.
38  Torus3();
39  Torus3(Vector3<Real> const& inCenter, Vector3<Real> const& inDirection0,
40  Vector3<Real> const& inDirection1, Vector3<Real> const& inNormal,
41  Real inRadius0, Real inRadius1);
42 
43  // Evaluation of the surface. The function supports derivative
44  // calculation through order 2; that is, maxOrder <= 2 is required. If
45  // you want only the position, pass in maxOrder of 0. If you want the
46  // position and first-order derivatives, pass in maxOrder of 1, and so on.
47  // The output 'values' are ordered as: position X; first-order derivatives
48  // dX/du, dX/dv; second-order derivatives d2X/du2, d2X/dudv, d2X/dv2.
49  void Evaluate(Real u, Real v, unsigned int maxOrder,
50  Vector3<Real> values[6]) const;
51 
52  // Reverse lookup of parameters from position.
53  void GetParameters(Vector3<Real> const& X, Real& u, Real& v) const;
54 
57 
58 public:
59  // Comparisons to support sorted containers.
60  bool operator==(Torus3 const& torus) const;
61  bool operator!=(Torus3 const& torus) const;
62  bool operator< (Torus3 const& torus) const;
63  bool operator<=(Torus3 const& torus) const;
64  bool operator> (Torus3 const& torus) const;
65  bool operator>=(Torus3 const& torus) const;
66 };
67 
68 
69 template <typename Real>
71  :
72  center(Vector3<Real>::Zero()),
73  direction0(Vector3<Real>::Unit(0)),
74  direction1(Vector3<Real>::Unit(1)),
75  normal(Vector3<Real>::Unit(2)),
76  radius0((Real)2),
77  radius1((Real)1)
78 {
79 }
80 
81 template <typename Real>
83  Vector3<Real> const& inDirection0, Vector3<Real> const& inDirection1,
84  Vector3<Real> const& inNormal, Real inRadius0, Real inRadius1)
85  :
86  center(inCenter),
87  direction0(inDirection0),
88  direction1(inDirection1),
89  normal(inNormal),
90  radius0(inRadius0),
91  radius1(inRadius1)
92 {
93 }
94 
95 template <typename Real>
96 void Torus3<Real>::Evaluate(Real u, Real v, unsigned int maxOrder,
97  Vector3<Real> values[6]) const
98 {
99  // Compute position.
100  Real csu = cos(u), snu = sin(u), csv = cos(v), snv = sin(v);
101  Real r1csv = radius1 * csv;
102  Real r1snv = radius1 * snv;
103  Real r0pr1csv = radius0 + r1csv;
104  Vector3<Real> combo0 = csu * direction0 + snu * direction1;
105  Vector3<Real> r0pr1csvcombo0 = r0pr1csv * combo0;
106  Vector3<Real> r1snvnormal = r1snv * normal;
107  values[0] = center + r0pr1csvcombo0 + r1snvnormal;
108 
109  if (maxOrder >= 1)
110  {
111  // Compute first-order derivatives.
112  Vector3<Real> combo1 = -snu * direction0 + csu * direction1;
113  values[1] = r0pr1csv * combo1;
114  values[2] = -r1snv * combo0 + r1csv * normal;
115 
116  if (maxOrder >= 2)
117  {
118  // Compute second-order derivatives.
119  values[3] = -r0pr1csvcombo0;
120  values[4] = -r1snv * combo1;
121  values[5] = -r1csv * combo0 - r1snvnormal;
122 
123  if (maxOrder >= 3)
124  {
125  // These orders are not supported.
126  for (int i = 0; i < 6; ++i)
127  {
128  values[i] = Vector3<float>::Zero();
129  }
130  }
131  }
132  }
133 }
134 
135 template <typename Real>
136 void Torus3<Real>::GetParameters(Vector3<Real> const& X, Real& u, Real& v)
137 const
138 {
139  Vector3<Real> delta = X - center;
140  Real dot0 = Dot(direction0, delta); // (r0 + r1*cos(v))*cos(u)
141  Real dot1 = Dot(direction1, delta); // (r0 + r1*cos(v))*sin(u)
142  Real dot2 = Dot(normal, delta); // r1*sin(v)
143  Real r1csv = sqrt(dot0 * dot0 + dot1 * dot1) - radius0; // r1*cos(v)
144  u = atan2(dot1, dot0);
145  v = atan2(dot2, r1csv);
146 }
147 
148 template <typename Real>
149 bool Torus3<Real>::operator==(Torus3 const& torus) const
150 {
151  return center == torus.center
152  && direction0 == torus.direction0
153  && direction1 == torus.direction1
154  && normal == torus.normal
155  && radius0 == torus.radius0
156  && radius1 == torus.radius1;
157 }
158 
159 template <typename Real>
160 bool Torus3<Real>::operator!=(Torus3 const& torus) const
161 {
162  return !operator==(torus);
163 }
164 
165 template <typename Real>
166 bool Torus3<Real>::operator<(Torus3 const& torus) const
167 {
168  if (center < torus.center)
169  {
170  return true;
171  }
172 
173  if (center > torus.center)
174  {
175  return false;
176  }
177 
178  if (direction0 < torus.direction0)
179  {
180  return true;
181  }
182 
183  if (direction0 > torus.direction0)
184  {
185  return false;
186  }
187 
188  if (direction1 < torus.direction1)
189  {
190  return true;
191  }
192 
193  if (direction1 > torus.direction1)
194  {
195  return false;
196  }
197 
198  if (normal < torus.normal)
199  {
200  return true;
201  }
202 
203  if (normal > torus.normal)
204  {
205  return false;
206  }
207 
208  if (radius0 < torus.radius0)
209  {
210  return true;
211  }
212 
213  if (radius0 > torus.radius0)
214  {
215  return false;
216  }
217 
218  return radius1 < torus.radius1;
219 }
220 
221 template <typename Real>
222 bool Torus3<Real>::operator<=(Torus3 const& torus) const
223 {
224  return operator<(torus) || operator==(torus);
225 }
226 
227 template <typename Real>
228 bool Torus3<Real>::operator>(Torus3 const& torus) const
229 {
230  return !operator<=(torus);
231 }
232 
233 template <typename Real>
234 bool Torus3<Real>::operator>=(Torus3 const& torus) const
235 {
236  return !operator<(torus);
237 }
238 
239 
240 }
void Evaluate(Real u, Real v, unsigned int maxOrder, Vector3< Real > values[6]) const
Definition: GteTorus3.h:96
Vector3< float > direction0
Definition: GteTorus3.h:55
Vector3< float > center
Definition: GteTorus3.h:55
bool operator==(Torus3 const &torus) const
Definition: GteTorus3.h:149
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1597
Vector3< float > normal
Definition: GteTorus3.h:55
void GetParameters(Vector3< Real > const &X, Real &u, Real &v) const
Definition: GteTorus3.h:136
bool operator!=(Torus3 const &torus) const
Definition: GteTorus3.h:160
Vector3< float > direction1
Definition: GteTorus3.h:55
static Vector Zero()
Definition: GteVector.h:295
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
bool operator>(Torus3 const &torus) const
Definition: GteTorus3.h:228
bool operator<(Torus3 const &torus) const
Definition: GteTorus3.h:166
const GLdouble * v
Definition: glcorearb.h:832
bool operator>=(Torus3 const &torus) const
Definition: GteTorus3.h:234
Real radius0
Definition: GteTorus3.h:56
bool operator<=(Torus3 const &torus) const
Definition: GteTorus3.h:222
Real radius1
Definition: GteTorus3.h:56


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