Thea
RayN.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2011
12 //
13 //============================================================================
14 
15 #ifndef __Thea_RayN_hpp__
16 #define __Thea_RayN_hpp__
17 
18 #include "Common.hpp"
19 #include "AffineTransformN.hpp"
20 #include "CoordinateFrameN.hpp"
21 #include "MatVec.hpp"
22 
23 namespace Thea {
24 
26 template <int N, typename T = Real>
27 class /* THEA_API */ RayN
28 {
29  public:
30  THEA_DECL_SMART_POINTERS(RayN)
31 
32  typedef Vector<N, T> VectorT;
33 
35  RayN() {}
36 
38  RayN(VectorT const & origin_, VectorT const & direction_) : origin(origin_), direction(direction_) {}
39 
41  RayN(RayN const & src) : origin(src.origin), direction(src.direction) {}
42 
44  template <typename U> RayN<N, U> cast() const
45  {
46  return RayN<N, U>(origin.template cast<U>(), direction.template cast<U>());
47  }
48 
50  VectorT const & getOrigin() const { return origin; }
51 
53  void setOrigin(VectorT const & origin_) { origin = origin_; }
54 
56  VectorT const & getDirection() const { return direction; }
57 
59  void setDirection(VectorT const & direction_) { direction = direction_; }
60 
62  void normalizeDirection() { direction.normalize(); }
63 
66  {
67  return RayN(frame.pointToWorldSpace(origin), frame.vectorToWorldSpace(direction));
68  }
69 
72  {
73  return RayN(frame.pointToObjectSpace(origin), frame.vectorToObjectSpace(direction));
74  }
75 
78  {
79  return RayN(tr * origin, tr.getLinear() * direction);
80  }
81 
87  VectorT getPoint(T const & t) const { return origin + t * direction; }
88 
90  T distance(VectorT const & p) const { return (closestPoint(p) - p).norm(); }
91 
93  T squaredDistance(VectorT const & p) const { return (closestPoint(p) - p).squaredNorm(); }
94 
96  VectorT closestPoint(VectorT const & p) const
97  {
98  T t = (p - origin).dot(direction); // we'll normalize direction later
99  if (t < 0)
100  return origin;
101  else
102  {
103  T dir_sqlen = direction.squaredNorm();
104  if (Math::fuzzyEq(dir_sqlen, static_cast<T>(0)))
105  return origin;
106 
107  // Direction has to be normalized twice -- once during the dot product above and once in the scaling below. We can
108  // combine the two divisions and avoid the square roots.
109  return origin + (t / dir_sqlen) * direction;
110  }
111  }
112 
114  std::string toString() const
115  {
116  return "[origin: " + Thea::toString(origin) + ", direction: " + Thea::toString(direction) + ']';
117  }
118 
119  private:
120  VectorT origin;
121  VectorT direction;
122 
123 }; // class RayN
124 
125 } // namespace Thea
126 
127 #include "Ray3.hpp"
128 
129 #endif
Eigen::Matrix< T, Size, 1, Options|((Options &Eigen::DontAlign)==0 &&Size==Eigen::Dynamic?Eigen::AutoAlign:Eigen::DontAlign), MaxRowsAtCompileTime, 1 > Vector
General 1D dense column vector template, alias for Eigen::Matrix<T, Size, 1,...>, with a custom align...
Definition: MatVec.hpp:146
RayN()
Default constructor.
Definition: RayN.hpp:35
std::string toString(char const *s)
Convert a C-string to a std::string object, returning an empty string if the input is a null pointer...
VectorT const & getOrigin() const
Get the origin of the ray.
Definition: RayN.hpp:50
A ray in N-dimensional space, having an originating point and a direction vector (not necessarily uni...
Definition: RayN.hpp:27
Root namespace for the Thea library.
VectorT vectorToObjectSpace(VectorT const &v) const
Transform a direction vector from world space to the local space of the coordinate frame...
void normalizeDirection()
Make the direction vector unit length.
Definition: RayN.hpp:62
RayN toObjectSpace(CoordinateFrameN< N, T > const &frame) const
Transform the ray from world space into a local frame.
Definition: RayN.hpp:71
void setOrigin(VectorT const &origin_)
Set the origin of the ray.
Definition: RayN.hpp:53
void setDirection(VectorT const &direction_)
Set the direction of the ray.
Definition: RayN.hpp:59
T distance(VectorT const &p) const
Get the distance of a point from the ray.
Definition: RayN.hpp:90
bool fuzzyEq(T const &a, T const &b, T const &tol)
Check if two numbers are approximately equal, with a given tolerance.
Definition: Math.hpp:210
RayN(RayN const &src)
Copy constructor.
Definition: RayN.hpp:41
VectorT closestPoint(VectorT const &p) const
Get the the point on the ray closest to a given point.
Definition: RayN.hpp:96
MatrixT const & getLinear() const
Get linear transform component.
RayN< N, U > cast() const
Cast the ray to a different scalar type.
Definition: RayN.hpp:44
VectorT pointToWorldSpace(VectorT const &p) const
Transform a point from the local space of the coordinate frame to world space.
RayN(VectorT const &origin_, VectorT const &direction_)
Initialize with an origin and a direction.
Definition: RayN.hpp:38
T squaredDistance(VectorT const &p) const
Get the square of the distance of a point from the ray.
Definition: RayN.hpp:93
VectorT getPoint(T const &t) const
Get a parametrized point on the ray.
Definition: RayN.hpp:87
RayN transform(AffineTransformN< N, T > const &tr) const
Apply an arbitrary affine transform to the ray.
Definition: RayN.hpp:77
A coordinate frame in N-space, defined by N orthonormal vectors.
VectorT pointToObjectSpace(VectorT const &p) const
Transform a point from world space to the local space of the coordinate frame.
VectorT vectorToWorldSpace(VectorT const &v) const
Transform a direction vector from the local space of the coordinate frame to world space...
VectorT const & getDirection() const
Get the direction of the ray.
Definition: RayN.hpp:56
RayN toWorldSpace(CoordinateFrameN< N, T > const &frame) const
Transform the ray out of a local frame to world space.
Definition: RayN.hpp:65
Vector< N, T > VectorT
N-dimensional vector.
Definition: RayN.hpp:32
std::string toString() const
Get a textual representation of the ray.
Definition: RayN.hpp:114
An affine transform in N-dimensional space, where N is any positive (non-zero) integer and T is a fie...