Viskores  1.0
Geometry.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 
19 #ifndef viskores_Geometry_h
20 #define viskores_Geometry_h
21 
23 
24 namespace viskores
25 {
26 
27 // Forward declarations of geometric types:
28 template <typename CoordType, int Dim, bool IsTwoSided>
29 struct Ray;
30 template <typename CoordType, int Dim>
31 struct LineSegment;
32 template <typename CoordType>
33 struct Plane;
34 template <typename CoordType, int Dim>
35 struct Sphere;
36 
42 template <typename CoordType = viskores::FloatDefault, int Dim = 3, bool IsTwoSided = false>
43 struct Ray
44 {
45  static constexpr int Dimension = Dim;
47  static constexpr bool TwoSided = IsTwoSided;
48 
50  Vector Direction; // always stored as a unit length.
51 
53  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
55 
57  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 3, int>::type = 0>
59 
62  Ray(const Vector& point, const Vector& direction);
63 
66  Ray(const LineSegment<CoordType, Dim>& segment);
67 
75  bool IsValid() const;
76 
79  Vector Evaluate(CoordType param) const;
80 
89  CoordType DistanceTo(const Vector& point) const;
90 
93  CoordType DistanceTo(const Vector& point, CoordType& param, Vector& projectedPoint) const;
94 
112  template <bool OtherTwoSided, int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
114  Vector& point,
115  CoordType tol = 0.f);
116 };
117 
119 template <typename CoordType = viskores::FloatDefault, int Dim = 3>
120 struct LineSegment
121 {
122  static constexpr int Dimension = Dim;
125 
127  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
129 
131  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 3, int>::type = 0>
133 
136  LineSegment(const Vector& p0, const Vector& p1);
137 
140  bool IsSingular(CoordType tol2 = static_cast<CoordType>(1.0e-6f)) const;
141 
143  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 3, int>::type = 0>
145 
147  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
149 
152  Vector Center() const { return this->Evaluate(0.5f); }
153 
159  Vector Direction() const { return this->Endpoints[1] - this->Endpoints[0]; }
160 
163  Vector Evaluate(CoordType param) const;
164 
173  CoordType DistanceTo(const Vector& point) const;
174 
177  CoordType DistanceTo(const Vector& point, CoordType& param, Vector& projectedPoint) const;
178 
191  template <int Dim_ = Dim, typename std::enable_if<Dim_ == 2, int>::type = 0>
193  Vector& point,
194  CoordType tol = 0.f);
195 };
196 
198 template <typename CoordType = viskores::FloatDefault>
199 struct Plane
200 {
204 
207  Plane();
208 
211  Plane(const Vector& origin, const Vector& normal, CoordType tol2 = static_cast<CoordType>(1e-8f));
212 
215  bool IsValid() const { return !viskores::IsInf(this->Normal[0]); }
216 
219  CoordType DistanceTo(const Vector& point) const;
220 
223  Vector ClosestPoint(const Vector& point) const;
224 
235  template <bool IsTwoSided>
237  CoordType& parameter,
238  Vector& point,
239  bool& lineInPlane,
240  CoordType tol = CoordType(1e-6f)) const;
241 
252  bool Intersect(const LineSegment<CoordType>& segment,
253  CoordType& parameter,
254  bool& lineInPlane) const;
255 
266  bool Intersect(const LineSegment<CoordType>& segment,
267  CoordType& parameter,
268  Vector& point,
269  bool& lineInPlane) const;
270 
289  bool Intersect(const Plane<CoordType>& other,
291  bool& coincident,
292  CoordType tol2 = static_cast<CoordType>(1e-6f)) const;
293 };
294 
298 template <typename CoordType = viskores::FloatDefault, int Dim = 3>
299 struct Sphere
300 {
301  static constexpr int Dimension = Dim;
304  CoordType Radius;
305 
308 
310  VISKORES_EXEC_CONT Sphere(const Vector& center, CoordType radius);
311 
314  bool IsValid() const { return this->Radius > 0.f; }
315 
318  bool Contains(const Vector& point, CoordType tol2 = 0.f) const;
319 
326  int Classify(const Vector& point, CoordType tol2 = 0.f) const;
327 };
328 
329 // -----------------------------------------------------------------------------
330 // Synonyms
331 //
332 // These "using" statements aim to make it easier to use the templated
333 // structs above when working with a particular dimension and/or the
334 // default floating-point type.
335 
337 template <typename CoordType, int Dim = 3>
339 
340 // Shortcuts for 2D and 3D rays, lines, and line segments:
341 template <typename CoordType>
343 template <typename CoordType>
345 template <typename CoordType>
347 template <typename CoordType>
349 template <typename CoordType>
351 template <typename CoordType>
353 
355 template <typename T>
357 
358 // Aliases for d-dimensional spheres.
359 template <typename T>
361 template <typename T>
363 
364 // Shortcuts for default floating-point types
375 
376 // -----------------------------------------------------------------------------
377 // Construction techniques
378 //
379 // These are free functions that create instances of geometric structs by taking
380 // in data that is not identical to the state of the struct and converting it
381 // into state for the struct.
382 
390 template <typename CoordType, bool IsTwoSided>
392  const viskores::Vec<CoordType, 3>& point,
394  CoordType tol2 = static_cast<CoordType>(1e-8f));
395 
396 template <typename CoordType>
398  const viskores::Vec<CoordType, 3>& point,
399  const viskores::LineSegment3<CoordType>& segment,
400  CoordType tol2 = static_cast<CoordType>(1e-8f));
401 
403 template <typename CoordType>
405  const typename viskores::Vec<CoordType, 2>& p0,
406  const typename viskores::Vec<CoordType, 2>& p1,
407  const typename viskores::Vec<CoordType, 2>& p2,
408  CoordType tol = static_cast<CoordType>(1e-6f));
409 
411 template <typename CoordType>
413  const viskores::Vec<CoordType, 3>& a0,
414  const viskores::Vec<CoordType, 3>& a1,
415  const viskores::Vec<CoordType, 3>& a2,
416  const viskores::Vec<CoordType, 3>& a3,
417  CoordType tol = static_cast<CoordType>(1e-6f));
418 
419 } // namespace viskores
420 
421 #include <viskores/Geometry.hxx>
422 
423 #endif // viskores_Geometry_h
viskores::make_CircleFrom3Points
viskores::Circle< CoordType > make_CircleFrom3Points(const typename viskores::Vec< CoordType, 2 > &p0, const typename viskores::Vec< CoordType, 2 > &p1, const typename viskores::Vec< CoordType, 2 > &p2, CoordType tol=static_cast< CoordType >(1e-6f))
Construct a circle from 3 points.
viskores::Plane::IsValid
bool IsValid() const
Return true if the plane's normal is well-defined to within the given tolerance.
Definition: Geometry.h:215
viskores::LineSegment::IsSingular
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...
viskores::LineSegment::Center
Vector Center() const
Return the midpoint of the line segment.
Definition: Geometry.h:152
viskores::Ray::IsValid
bool IsValid() const
Return whether the ray is valid or not.
viskores::Sphere::Vector
viskores::Vec< CoordType, Dim > Vector
Definition: Geometry.h:302
viskores::Plane::Plane
Plane()
Construct a default plane whose base point is the origin and whose normal is (0,0,...
viskores::LineSegment::Evaluate
Vector Evaluate(CoordType param) const
Compute a point along the line. param values in [0,1] lie on the line segment.
viskores::make_PlaneFromPointAndLine
viskores::Plane< CoordType > make_PlaneFromPointAndLine(const viskores::Vec< CoordType, 3 > &point, const viskores::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.
viskores::Ray::Intersect
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.
viskores::make_SphereFrom4Points
viskores::Sphere< CoordType, 3 > make_SphereFrom4Points(const viskores::Vec< CoordType, 3 > &a0, const viskores::Vec< CoordType, 3 > &a1, const viskores::Vec< CoordType, 3 > &a2, const viskores::Vec< CoordType, 3 > &a3, CoordType tol=static_cast< CoordType >(1e-6f))
Construct a sphere from 4 points.
viskores::Sphere::Contains
bool Contains(const Vector &point, CoordType tol2=0.f) const
Return whether the point lies strictly inside the sphere.
viskores::LineSegment::Direction
Vector Direction() const
Return the vector pointing to endpoint 1 from endpoint 0.
Definition: Geometry.h:159
viskores::LineSegment::Dimension
static constexpr int Dimension
Definition: Geometry.h:122
viskores::Plane::Vector
viskores::Vec< CoordType, 3 > Vector
Definition: Geometry.h:201
VISKORES_EXEC_CONT
#define VISKORES_EXEC_CONT
Definition: ExportMacros.h:60
viskores::Sphere::Sphere
Sphere()
Construct a default sphere (unit radius at the origin).
viskores::Plane::DistanceTo
CoordType DistanceTo(const Vector &point) const
Return the signed distance from the plane to the point.
VectorAnalysis.h
viskores::LineSegment::IntersectInfinite
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,...
viskores::LineSegment
Represent a finite line segment with a pair of points.
Definition: Geometry.h:31
viskores::Ray::Direction
Vector Direction
Definition: Geometry.h:50
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::Ray::TwoSided
static constexpr bool TwoSided
Definition: Geometry.h:47
viskores::LineSegment::Endpoints
Vector Endpoints[2]
Definition: Geometry.h:124
viskores::Sphere::Radius
CoordType Radius
Definition: Geometry.h:304
viskores::LineSegment::PerpendicularBisector
Plane< CoordType > PerpendicularBisector() const
Construct a plane bisecting this line segment (only when Dimension is 3).
viskores::Ray::Evaluate
Vector Evaluate(CoordType param) const
Compute a point along the line. param values > 0 lie on the ray.
viskores::LineSegment::LineSegment
LineSegment()
Construct a default segment from (0,0) to (1,0).
viskores::Sphere::IsValid
bool IsValid() const
Return true if the sphere is valid (i.e., has a strictly positive radius).
Definition: Geometry.h:314
viskores::Ray::Dimension
static constexpr int Dimension
Definition: Geometry.h:45
viskores::LineSegment::DistanceTo
CoordType DistanceTo(const Vector &point) const
Return the minmum distance from point to this line segment.
viskores::Ray
Represent an infinite or semi-infinite line segment with a point and a direction.
Definition: Geometry.h:29
viskores::Ray::Origin
Vector Origin
Definition: Geometry.h:49
viskores::Sphere::Dimension
static constexpr int Dimension
Definition: Geometry.h:301
viskores::Plane::Intersect
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).
viskores::Sphere::Center
Vector Center
Definition: Geometry.h:303
viskores::LineSegment::Vector
viskores::Vec< CoordType, Dim > Vector
Definition: Geometry.h:123
viskores::make_PlaneFromPointAndLineSegment
viskores::Plane< CoordType > make_PlaneFromPointAndLineSegment(const viskores::Vec< CoordType, 3 > &point, const viskores::LineSegment3< CoordType > &segment, CoordType tol2=static_cast< CoordType >(1e-8f))
viskores::Plane::Origin
Vector Origin
Definition: Geometry.h:202
viskores::Ray::Ray
Ray()
Construct a default 2-D ray, from (0,0) pointing along the +x axis.
viskores::Vec< CoordType, Dim >
viskores::Plane::ClosestPoint
Vector ClosestPoint(const Vector &point) const
Return the closest point in the plane to the given point.
viskores::Sphere::Classify
int Classify(const Vector &point, CoordType tol2=0.f) const
Classify a point as inside (-1), on (0), or outside (+1) of the sphere.
viskores::Ray::DistanceTo
CoordType DistanceTo(const Vector &point) const
Return the minmum distance from point to this line/ray.
viskores::Plane::Normal
Vector Normal
Definition: Geometry.h:203
viskores::Plane
Represent a plane with a base point (origin) and normal vector.
Definition: Geometry.h:33
viskores::Sphere
Represent a sphere of the given Dimension.
Definition: Geometry.h:35