GteContScribeCircle3Sphere3.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/GteCircle3.h>
13 
14 namespace gte
15 {
16 
17 // All functions return 'true' if circle/sphere has been constructed,
18 // 'false' otherwise (input points are linearly dependent).
19 
20 // Circle circumscribing a triangle in 3D.
21 template <typename Real>
22 bool Circumscribe(Vector3<Real> const& v0, Vector3<Real> const& v1,
23  Vector3<Real> const& v2, Circle3<Real>& circle);
24 
25 // Sphere circumscribing a tetrahedron.
26 template <typename Real>
27 bool Circumscribe(Vector3<Real> const& v0, Vector3<Real> const& v1,
28  Vector3<Real> const& v2, Vector3<Real> const& v3, Sphere3<Real>& sphere);
29 
30 // Circle inscribing a triangle in 3D.
31 template <typename Real>
32 bool Inscribe(Vector3<Real> const& v0, Vector3<Real> const& v1,
33  Vector3<Real> const& v2, Circle3<Real>& circle);
34 
35 // Sphere inscribing tetrahedron.
36 template <typename Real>
37 bool Inscribe(Vector3<Real> const& v0, Vector3<Real> const& v1,
38  Vector3<Real> const& v2, Vector3<Real> const& v3, Sphere3<Real>& sphere);
39 
40 
41 template <typename Real>
43  Vector3<Real> const& v2, Circle3<Real>& circle)
44 {
45  Vector3<Real> E02 = v0 - v2;
46  Vector3<Real> E12 = v1 - v2;
47  Real e02e02 = Dot(E02, E02);
48  Real e02e12 = Dot(E02, E12);
49  Real e12e12 = Dot(E12, E12);
50  Real det = e02e02*e12e12 - e02e12*e02e12;
51  if (det != (Real)0)
52  {
53  Real halfInvDet = ((Real)0.5) / det;
54  Real u0 = halfInvDet*e12e12*(e02e02 - e02e12);
55  Real u1 = halfInvDet*e02e02*(e12e12 - e02e12);
56  Vector3<Real> tmp = u0*E02 + u1*E12;
57  circle.center = v2 + tmp;
58  circle.normal = UnitCross(E02, E12);
59  circle.radius = Length(tmp);
60  return true;
61  }
62  return false;
63 }
64 
65 template <typename Real>
67  Vector3<Real> const& v2, Vector3<Real> const& v3, Sphere3<Real>& sphere)
68 {
69  Vector3<Real> E10 = v1 - v0;
70  Vector3<Real> E20 = v2 - v0;
71  Vector3<Real> E30 = v3 - v0;
72 
74  A.SetRow(0, E10);
75  A.SetRow(1, E20);
76  A.SetRow(2, E30);
77 
78  Vector3<Real> B{
79  ((Real)0.5)*Dot(E10, E10),
80  ((Real)0.5)*Dot(E20, E20),
81  ((Real)0.5)*Dot(E30, E30) };
82 
83  Vector3<Real> solution;
84  if (LinearSystem<Real>::Solve(A, B, solution))
85  {
86  sphere.center = v0 + solution;
87  sphere.radius = Length(solution);
88  return true;
89  }
90  return false;
91 }
92 
93 template <typename Real>
95  Vector3<Real> const& v2, Circle3<Real>& circle)
96 {
97  // Edges.
98  Vector3<Real> E0 = v1 - v0;
99  Vector3<Real> E1 = v2 - v1;
100  Vector3<Real> E2 = v0 - v2;
101 
102  // Plane normal.
103  circle.normal = Cross(E1, E0);
104 
105  // Edge normals within the plane.
106  Vector3<Real> N0 = UnitCross(circle.normal, E0);
107  Vector3<Real> N1 = UnitCross(circle.normal, E1);
108  Vector3<Real> N2 = UnitCross(circle.normal, E2);
109 
110  Real a0 = Dot(N1, E0);
111  if (a0 == (Real)0)
112  {
113  return false;
114  }
115 
116  Real a1 = Dot(N2, E1);
117  if (a1 == (Real)0)
118  {
119  return false;
120  }
121 
122  Real a2 = Dot(N0, E2);
123  if (a2 == (Real)0)
124  {
125  return false;
126  }
127 
128  Real invA0 = ((Real)1) / a0;
129  Real invA1 = ((Real)1) / a1;
130  Real invA2 = ((Real)1) / a2;
131 
132  circle.radius = ((Real)1) / (invA0 + invA1 + invA2);
133  circle.center = circle.radius*(invA0*v0 + invA1*v1 + invA2*v2);
134  Normalize(circle.normal);
135  return true;
136 }
137 
138 template <typename Real>
140  Vector3<Real> const& v2, Vector3<Real> const& v3, Sphere3<Real>& sphere)
141 {
142  // Edges.
143  Vector3<Real> E10 = v1 - v0;
144  Vector3<Real> E20 = v2 - v0;
145  Vector3<Real> E30 = v3 - v0;
146  Vector3<Real> E21 = v2 - v1;
147  Vector3<Real> E31 = v3 - v1;
148 
149  // Normals.
150  Vector3<Real> N0 = Cross(E31, E21);
151  Vector3<Real> N1 = Cross(E20, E30);
152  Vector3<Real> N2 = Cross(E30, E10);
153  Vector3<Real> N3 = Cross(E10, E20);
154 
155  // Normalize the normals.
156  if (Normalize(N0) == (Real)0)
157  {
158  return false;
159  }
160  if (Normalize(N1) == (Real)0)
161  {
162  return false;
163  }
164  if (Normalize(N2) == (Real)0)
165  {
166  return false;
167  }
168  if (Normalize(N3) == (Real)0)
169  {
170  return false;
171  }
172 
173  Matrix3x3<Real> A;
174  A.SetRow(0, N1 - N0);
175  A.SetRow(1, N2 - N0);
176  A.SetRow(2, N3 - N0);
177  Vector3<Real> B{ (Real)0, (Real)0, -Dot(N3, E30) };
178  Vector3<Real> solution;
179  if (LinearSystem<Real>::Solve(A, B, solution))
180  {
181  sphere.center = v3 + solution;
182  sphere.radius = std::abs(Dot(N0, solution));
183  return true;
184  }
185  return false;
186 }
187 
188 
189 }
bool Inscribe(Vector2< Real > const &v0, Vector2< Real > const &v1, Vector2< Real > const &v2, Circle2< Real > &circle)
GLfixed u1
Definition: glext.h:4927
gte::BSNumber< UIntegerType > abs(gte::BSNumber< UIntegerType > const &number)
Definition: GteBSNumber.h:966
GLfloat GLfloat v1
Definition: glcorearb.h:812
Vector< N, Real > UnitCross(Vector< N, Real > const &v0, Vector< N, Real > const &v1, bool robust=false)
Definition: GteVector3.h:130
Vector3< Real > normal
Definition: GteCircle3.h:31
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
GLfloat GLfloat GLfloat GLfloat v3
Definition: glcorearb.h:814
Real Normalize(GVector< Real > &v, bool robust=false)
Definition: GteGVector.h:454
GLfloat v0
Definition: glcorearb.h:811
DualQuaternion< Real > Cross(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
DualQuaternion< Real > Length(DualQuaternion< Real > const &d, bool robust=false)
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:813
bool Circumscribe(Vector2< Real > const &v0, Vector2< Real > const &v1, Vector2< Real > const &v2, Circle2< Real > &circle)
void SetRow(int r, Vector< NumCols, Real > const &vec)
Definition: GteMatrix.h:352
Vector3< Real > center
Definition: GteCircle3.h:31
Vector< N, Real > center


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