VTK-m  2.0
Geometry.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 
11 #ifndef vtk_m_Geometry_h
12 #define vtk_m_Geometry_h
13 
14 #include <vtkm/VectorAnalysis.h>
15 
16 namespace vtkm
17 {
18 
19 // Forward declarations of geometric types:
20 template <typename CoordType, int Dim, bool IsTwoSided>
21 struct Ray;
22 template <typename CoordType, int Dim>
23 struct LineSegment;
24 template <typename CoordType>
25 struct Plane;
26 template <typename CoordType, int Dim>
27 struct Sphere;
28 
34 template <typename CoordType = vtkm::FloatDefault, int Dim = 3, bool IsTwoSided = false>
35 struct Ray
36 {
37  static constexpr int Dimension = Dim;
39  static constexpr bool TwoSided = IsTwoSided;
40 
42  Vector Direction; // always stored as a unit length.
43 
45  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
47 
49  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 3, int>::type = 0>
51 
54  Ray(const Vector& point, const Vector& direction);
55 
58  Ray(const LineSegment<CoordType, Dim>& segment);
59 
67  bool IsValid() const;
68 
71  Vector Evaluate(CoordType param) const;
72 
81  CoordType DistanceTo(const Vector& point) const;
82 
85  CoordType DistanceTo(const Vector& point, CoordType& param, Vector& projectedPoint) const;
86 
104  template <bool OtherTwoSided, int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
106  Vector& point,
107  CoordType tol = 0.f);
108 };
109 
111 template <typename CoordType = vtkm::FloatDefault, int Dim = 3>
112 struct LineSegment
113 {
114  static constexpr int Dimension = Dim;
117 
119  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
121 
123  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 3, int>::type = 0>
125 
128  LineSegment(const Vector& p0, const Vector& p1);
129 
132  bool IsSingular(CoordType tol2 = static_cast<CoordType>(1.0e-6f)) const;
133 
135  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 3, int>::type = 0>
137 
139  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
141 
144  Vector Center() const { return this->Evaluate(0.5f); }
145 
151  Vector Direction() const { return this->Endpoints[1] - this->Endpoints[0]; }
152 
155  Vector Evaluate(CoordType param) const;
156 
165  CoordType DistanceTo(const Vector& point) const;
166 
169  CoordType DistanceTo(const Vector& point, CoordType& param, Vector& projectedPoint) const;
170 
183  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
185  Vector& point,
186  CoordType tol = 0.f);
187 };
188 
190 template <typename CoordType = vtkm::FloatDefault>
191 struct Plane
192 {
196 
199  Plane();
200 
203  Plane(const Vector& origin, const Vector& normal, CoordType tol2 = static_cast<CoordType>(1e-8f));
204 
207  bool IsValid() const { return !vtkm::IsInf(this->Normal[0]); }
208 
211  CoordType DistanceTo(const Vector& point) const;
212 
215  Vector ClosestPoint(const Vector& point) const;
216 
227  template <bool IsTwoSided>
229  CoordType& parameter,
230  Vector& point,
231  bool& lineInPlane,
232  CoordType tol = CoordType(1e-6f)) const;
233 
244  bool Intersect(const LineSegment<CoordType>& segment,
245  CoordType& parameter,
246  bool& lineInPlane) const;
247 
258  bool Intersect(const LineSegment<CoordType>& segment,
259  CoordType& parameter,
260  Vector& point,
261  bool& lineInPlane) const;
262 
281  bool Intersect(const Plane<CoordType>& other,
283  bool& coincident,
284  CoordType tol2 = static_cast<CoordType>(1e-6f)) const;
285 };
286 
290 template <typename CoordType = vtkm::FloatDefault, int Dim = 3>
291 struct Sphere
292 {
293  static constexpr int Dimension = Dim;
296  CoordType Radius;
297 
300 
302  VTKM_EXEC_CONT Sphere(const Vector& center, CoordType radius);
303 
306  bool IsValid() const { return this->Radius > 0.f; }
307 
310  bool Contains(const Vector& point, CoordType tol2 = 0.f) const;
311 
318  int Classify(const Vector& point, CoordType tol2 = 0.f) const;
319 };
320 
321 // -----------------------------------------------------------------------------
322 // Synonyms
323 //
324 // These "using" statements aim to make it easier to use the templated
325 // structs above when working with a particular dimension and/or the
326 // default floating-point type.
327 
329 template <typename CoordType, int Dim = 3>
331 
332 // Shortcuts for 2D and 3D rays, lines, and line segments:
333 template <typename CoordType>
335 template <typename CoordType>
337 template <typename CoordType>
339 template <typename CoordType>
341 template <typename CoordType>
343 template <typename CoordType>
345 
347 template <typename T>
349 
350 // Aliases for d-dimensional spheres.
351 template <typename T>
353 template <typename T>
355 
356 // Shortcuts for default floating-point types
367 
368 // -----------------------------------------------------------------------------
369 // Construction techniques
370 //
371 // These are free functions that create instances of geometric structs by taking
372 // in data that is not identical to the state of the struct and converting it
373 // into state for the struct.
374 
382 template <typename CoordType, bool IsTwoSided>
384  const vtkm::Vec<CoordType, 3>& point,
386  CoordType tol2 = static_cast<CoordType>(1e-8f));
387 
388 template <typename CoordType>
390  const vtkm::Vec<CoordType, 3>& point,
391  const vtkm::LineSegment3<CoordType>& segment,
392  CoordType tol2 = static_cast<CoordType>(1e-8f));
393 
395 template <typename CoordType>
397  const typename vtkm::Vec<CoordType, 2>& p0,
398  const typename vtkm::Vec<CoordType, 2>& p1,
399  const typename vtkm::Vec<CoordType, 2>& p2,
400  CoordType tol = static_cast<CoordType>(1e-6f));
401 
403 template <typename CoordType>
405  const vtkm::Vec<CoordType, 3>& a0,
406  const vtkm::Vec<CoordType, 3>& a1,
407  const vtkm::Vec<CoordType, 3>& a2,
408  const vtkm::Vec<CoordType, 3>& a3,
409  CoordType tol = static_cast<CoordType>(1e-6f));
410 
411 } // namespace vtkm
412 
413 #include <vtkm/Geometry.hxx>
414 
415 #endif // vtk_m_Geometry_h
vtkm::Ray
Represent an infinite or semi-infinite line segment with a point and a direction.
Definition: Geometry.h:21
vtkm::Sphere::Sphere
VTKM_EXEC_CONT Sphere()
Construct a default sphere (unit radius at the origin).
vtkm::Ray::DistanceTo
VTKM_EXEC_CONT CoordType DistanceTo(const Vector &point) const
Return the minmum distance from point to this line/ray.
vtkm::LineSegment::Evaluate
VTKM_EXEC_CONT Vector Evaluate(CoordType param) const
Compute a point along the line. param values in [0,1] lie on the line segment.
vtkm::Ray::Dimension
static constexpr int Dimension
Definition: Geometry.h:37
vtkm::Ray::TwoSided
static constexpr bool TwoSided
Definition: Geometry.h:39
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::LineSegment::Dimension
static constexpr int Dimension
Definition: Geometry.h:114
VTKM_EXEC_CONT
#define VTKM_EXEC_CONT
Definition: ExportMacros.h:52
vtkm::Ray::Origin
Vector Origin
Definition: Geometry.h:41
vtkm::LineSegment::PerpendicularBisector
VTKM_EXEC_CONT Plane< CoordType > PerpendicularBisector() const
Construct a plane bisecting this line segment (only when Dimension is 3).
vtkm::make_SphereFrom4Points
VTKM_EXEC_CONT vtkm::Sphere< CoordType, 3 > make_SphereFrom4Points(const vtkm::Vec< CoordType, 3 > &a0, const vtkm::Vec< CoordType, 3 > &a1, const vtkm::Vec< CoordType, 3 > &a2, const vtkm::Vec< CoordType, 3 > &a3, CoordType tol=static_cast< CoordType >(1e-6f))
Construct a sphere from 4 points.
vtkm::Sphere::Radius
CoordType Radius
Definition: Geometry.h:296
vtkm::LineSegment::LineSegment
VTKM_EXEC_CONT LineSegment()
Construct a default segment from (0,0) to (1,0).
vtkm::Sphere::Dimension
static constexpr int Dimension
Definition: Geometry.h:293
vtkm::LineSegment::Vector
vtkm::Vec< CoordType, Dim > Vector
Definition: Geometry.h:115
vtkm::Sphere::IsValid
VTKM_EXEC_CONT bool IsValid() const
Return true if the sphere is valid (i.e., has a strictly positive radius).
Definition: Geometry.h:306
vtkm::LineSegment::DistanceTo
VTKM_EXEC_CONT CoordType DistanceTo(const Vector &point) const
Return the minmum distance from point to this line segment.
vtkm::make_PlaneFromPointAndLineSegment
VTKM_EXEC_CONT vtkm::Plane< CoordType > make_PlaneFromPointAndLineSegment(const vtkm::Vec< CoordType, 3 > &point, const vtkm::LineSegment3< CoordType > &segment, CoordType tol2=static_cast< CoordType >(1e-8f))
vtkm::LineSegment::Center
VTKM_EXEC_CONT Vector Center() const
Return the midpoint of the line segment.
Definition: Geometry.h:144
VectorAnalysis.h
vtkm::Plane::Intersect
VTKM_EXEC_CONT bool Intersect(const Ray< CoordType, 3, IsTwoSided > &ray, CoordType &parameter, Vector &point, bool &lineInPlane, CoordType tol=CoordType(1e-6f)) const
Intersect this plane with the ray (or line if the ray is two-sided).
vtkm::Sphere::Vector
vtkm::Vec< CoordType, Dim > Vector
Definition: Geometry.h:294
vtkm::Plane::IsValid
VTKM_EXEC_CONT bool IsValid() const
Return true if the plane's normal is well-defined to within the given tolerance.
Definition: Geometry.h:207
vtkm::Ray::Evaluate
VTKM_EXEC_CONT Vector Evaluate(CoordType param) const
Compute a point along the line. param values > 0 lie on the ray.
vtkm::Plane::Plane
VTKM_EXEC_CONT Plane()
Construct a default plane whose base point is the origin and whose normal is (0,0,...
vtkm::Sphere::Contains
VTKM_EXEC_CONT bool Contains(const Vector &point, CoordType tol2=0.f) const
Return whether the point lies strictly inside the sphere.
vtkm::Ray::IsValid
VTKM_EXEC_CONT bool IsValid() const
Return whether the ray is valid or not.
vtkm::Plane::Normal
Vector Normal
Definition: Geometry.h:195
vtkm::LineSegment
Represent a finite line segment with a pair of points.
Definition: Geometry.h:23
vtkm::Ray::Ray
VTKM_EXEC_CONT Ray()
Construct a default 2-D ray, from (0,0) pointing along the +x axis.
vtkm::Ray::Intersect
VTKM_EXEC_CONT bool Intersect(const Ray< CoordType, Dim, OtherTwoSided > &other, Vector &point, CoordType tol=0.f)
Compute the non-degenerate point where two 2-D rays intersect, or return false.
vtkm::LineSegment::Direction
VTKM_EXEC_CONT Vector Direction() const
Return the vector pointing to endpoint 1 from endpoint 0.
Definition: Geometry.h:151
vtkm::Sphere
Represent a sphere of the given Dimension.
Definition: Geometry.h:27
vtkm::Vec< CoordType, Dim >
vtkm::LineSegment::IsSingular
VTKM_EXEC_CONT bool IsSingular(CoordType tol2=static_cast< CoordType >(1.0e-6f)) const
Return whether this line segment has an infinitesimal extent (i.e., whether the endpoints are coincid...
vtkm::Plane::Vector
vtkm::Vec< CoordType, 3 > Vector
Definition: Geometry.h:193
vtkm::make_PlaneFromPointAndLine
VTKM_EXEC_CONT vtkm::Plane< CoordType > make_PlaneFromPointAndLine(const vtkm::Vec< CoordType, 3 > &point, const vtkm::Ray< CoordType, 3, IsTwoSided > &ray, CoordType tol2=static_cast< CoordType >(1e-8f))
Construct a plane from a point plus one of: a line, a ray, or a line segment.
vtkm::Plane::DistanceTo
VTKM_EXEC_CONT CoordType DistanceTo(const Vector &point) const
Return the signed distance from the plane to the point.
vtkm::Plane::ClosestPoint
VTKM_EXEC_CONT Vector ClosestPoint(const Vector &point) const
Return the closest point in the plane to the given point.
vtkm::Ray::Direction
Vector Direction
Definition: Geometry.h:42
vtkm::Sphere::Center
Vector Center
Definition: Geometry.h:295
vtkm::Sphere::Classify
VTKM_EXEC_CONT int Classify(const Vector &point, CoordType tol2=0.f) const
Classify a point as inside (-1), on (0), or outside (+1) of the sphere.
vtkm::make_CircleFrom3Points
VTKM_EXEC_CONT vtkm::Circle< CoordType > make_CircleFrom3Points(const typename vtkm::Vec< CoordType, 2 > &p0, const typename vtkm::Vec< CoordType, 2 > &p1, const typename vtkm::Vec< CoordType, 2 > &p2, CoordType tol=static_cast< CoordType >(1e-6f))
Construct a circle from 3 points.
vtkm::Plane::Origin
Vector Origin
Definition: Geometry.h:194
vtkm::Plane
Represent a plane with a base point (origin) and normal vector.
Definition: Geometry.h:25
vtkm::LineSegment::Endpoints
Vector Endpoints[2]
Definition: Geometry.h:116
vtkm::LineSegment::IntersectInfinite
VTKM_EXEC_CONT bool IntersectInfinite(const LineSegment< CoordType, Dim > &other, Vector &point, CoordType tol=0.f)
Compute the non-degenerate point where two (infinite) 2-D line segments intersect,...