GteContCylinder3.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 
14 namespace gte
15 {
16 
17 // Compute the cylinder axis segment using least-squares fit. The radius is
18 // the maximum distance from points to the axis. The height is determined by
19 // projection of points onto the axis and determining the containing interval.
20 template <typename Real>
21 bool GetContainer(int numPoints, Vector3<Real> const* points,
22  Cylinder3<Real>& cylinder);
23 
24 // Test for containment of a point by a cylinder.
25 template <typename Real>
26 bool InContainer(Vector3<Real> const& point, Cylinder3<Real> const& cylinder);
27 
28 
29 template <typename Real>
30 bool GetContainer(int numPoints, Vector3<Real> const* points,
31  Cylinder3<Real>& cylinder)
32 {
34  fitter.Fit(numPoints, points);
35  Line3<Real> line = fitter.GetParameters();
36 
38  Real maxRadiusSqr = (Real)0;
39  for (int i = 0; i < numPoints; ++i)
40  {
41  auto result = plQuery(points[i], line);
42  if (result.sqrDistance > maxRadiusSqr)
43  {
44  maxRadiusSqr = result.sqrDistance;
45  }
46  }
47 
48  Vector3<Real> diff = points[0] - line.origin;
49  Real wMin = Dot(line.direction, diff);
50  Real wMax = wMin;
51  for (int i = 1; i < numPoints; ++i)
52  {
53  diff = points[i] - line.origin;
54  Real w = Dot(line.direction, diff);
55  if (w < wMin)
56  {
57  wMin = w;
58  }
59  else if (w > wMax)
60  {
61  wMax = w;
62  }
63  }
64 
65  cylinder.axis.origin = line.origin +
66  (((Real)0.5)*(wMax + wMin))*line.direction;
67  cylinder.axis.direction = line.direction;
68  cylinder.radius = sqrt(maxRadiusSqr);
69  cylinder.height = wMax - wMin;
70  return true;
71 }
72 
73 template <typename Real>
74 bool InContainer(Vector3<Real> const& point, Cylinder3<Real> const& cylinder)
75 {
76  Vector3<Real> diff = point - cylinder.axis.origin;
77  Real zProj = Dot(diff, cylinder.axis.direction);
78  if (std::abs(zProj)*(Real)2 > cylinder.height)
79  {
80  return false;
81  }
82 
83  Vector3<Real> xyProj = diff - zProj*cylinder.axis.direction;
84  return Length(xyProj) <= cylinder.radius;
85 }
86 
87 
88 }
gte::BSNumber< UIntegerType > abs(gte::BSNumber< UIntegerType > const &number)
Definition: GteBSNumber.h:966
bool GetContainer(int numPoints, Vector3< Real > const *points, Capsule3< Real > &capsule)
bool InContainer(Vector3< Real > const &point, Capsule3< Real > const &capsule)
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:852
Vector< N, Real > direction
Definition: GteLine.h:29
GLfixed GLfixed GLint GLint GLfixed points
Definition: glext.h:4927
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
DualQuaternion< Real > Length(DualQuaternion< Real > const &d, bool robust=false)
bool Fit(int numPoints, Vector3< Real > const *points)
Vector< N, Real > origin
Definition: GteLine.h:29
Line3< Real > const & GetParameters() const
GLuint64EXT * result
Definition: glext.h:10003
Line3< Real > axis
Definition: GteCylinder3.h:31


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