GteContSphere3.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>
12 
13 namespace gte
14 {
15 
16 // Compute the smallest sphere whose center is the average of the input
17 // points.
18 template <typename Real>
19 bool GetContainer(int numPoints, Vector3<Real> const* points,
20  Sphere3<Real>& sphere);
21 
22 // Test for containment of a point inside a sphere.
23 template <typename Real>
24 bool InContainer(Vector3<Real> const& point, Sphere3<Real> const& sphere);
25 
26 // Compute the smallest sphere that contains the input spheres.
27 template <typename Real>
28 bool MergeContainers(Sphere3<Real> const& sphere0,
29  Sphere3<Real> const& sphere1, Sphere3<Real>& merge);
30 
31 
32 template <typename Real>
33 bool GetContainer(int numPoints, Vector3<Real> const* points,
34  Sphere3<Real>& sphere)
35 {
36  sphere.center = points[0];
37  for (int i = 1; i < numPoints; ++i)
38  {
39  sphere.center += points[i];
40  }
41  sphere.center /= (Real)numPoints;
42 
43  for (int i = 0; i < numPoints; ++i)
44  {
45  Vector3<Real> diff = points[i] - sphere.center;
46  Real radiusSqr = Dot(diff, diff);
47  if (radiusSqr > sphere.radius)
48  {
49  sphere.radius = radiusSqr;
50  }
51  }
52 
53  sphere.radius = sqrt(sphere.radius);
54  return true;
55 }
56 
57 template <typename Real>
58 bool InContainer(Vector3<Real> const& point, Sphere3<Real> const& sphere)
59 {
60  Vector3<Real> diff = point - sphere.center;
61  return Length(diff) <= sphere.radius;
62 }
63 
64 template <typename Real>
65 bool MergeContainers(Sphere3<Real> const& sphere0,
66  Sphere3<Real> const& sphere1, Sphere3<Real>& merge)
67 {
68  Vector3<Real> cenDiff = sphere1.center - sphere0.center;
69  Real lenSqr = Dot(cenDiff, cenDiff);
70  Real rDiff = sphere1.radius - sphere0.radius;
71  Real rDiffSqr = rDiff*rDiff;
72 
73  if (rDiffSqr >= lenSqr)
74  {
75  merge = (rDiff >= (Real)0 ? sphere1 : sphere0);
76  }
77  else
78  {
79  Real length = sqrt(lenSqr);
80  if (length > (Real)0)
81  {
82  Real coeff = (length + rDiff) / (((Real)2)*length);
83  merge.center = sphere0.center + coeff*cenDiff;
84  }
85  else
86  {
87  merge.center = sphere0.center;
88  }
89 
90  merge.radius = ((Real)0.5)*(length + sphere0.radius + sphere1.radius);
91  }
92 
93  return true;
94 }
95 
96 
97 }
bool GetContainer(int numPoints, Vector3< Real > const *points, Capsule3< Real > &capsule)
bool MergeContainers(Capsule3< Real > const &capsule0, Capsule3< Real > const &capsule1, Capsule3< Real > &merge)
bool InContainer(Vector3< Real > const &point, Capsule3< Real > const &capsule)
GLfixed GLfixed GLint GLint GLfixed points
Definition: glext.h:4927
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:790
DualQuaternion< Real > Length(DualQuaternion< Real > const &d, bool robust=false)
Vector< N, Real > center


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