Carna  Version 3.3.2
RayPlaneHitTest.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 - 2015 Leonid Kostrykin
3  *
4  * Chair of Medical Engineering (mediTEC)
5  * RWTH Aachen University
6  * Pauwelsstr. 20
7  * 52074 Aachen
8  * Germany
9  *
10  */
11 
12 #ifndef RAYPLANEHITTEST_H_6014714286
13 #define RAYPLANEHITTEST_H_6014714286
14 
15 #include <Carna/Carna.h>
16 #include <Carna/base/math.h>
17 #include <Carna/base/math/Ray.h>
19 
24 namespace Carna
25 {
26 
27 namespace base
28 {
29 
30 namespace math
31 {
32 
33 
34 
35 // ----------------------------------------------------------------------------------
36 // RayPlaneHitTest
37 // ----------------------------------------------------------------------------------
38 
48 template< typename VectorType, typename ScalarType >
50 {
51 
52  bool myHitExists;
53  VectorType myHitLocation;
54 
55 public:
56 
63 
69  void compute( const Ray< VectorType >& ray, const VectorType& planeNormal, ScalarType planeOriginOffset );
70 
74  bool hitExists() const;
75 
81  const VectorType& hitLocation() const;
82 
83 }; // RayPlaneHitTest
84 
85 
86 template< typename VectorType, typename ScalarType >
88  : myHitExists( false )
89 {
90 }
91 
92 
93 template< typename VectorType, typename ScalarType >
95 {
96  return myHitExists;
97 }
98 
99 
100 template< typename VectorType, typename ScalarType >
102 {
103  CARNA_ASSERT( hitExists() );
104  return myHitLocation;
105 }
106 
107 
108 template< typename VectorType, typename ScalarType >
110  ( const Ray< VectorType >& ray
111  , const VectorType& planeNormal
112  , ScalarType planeOriginOffset )
113 {
114  CARNA_ASSERT( isEqual< ScalarType >( ray.direction.norm(), 1 ) );
115  CARNA_ASSERT( isEqual< ScalarType >( planeNormal.norm(), 1 ) );
116  CARNA_ASSERT( ray.direction.rows() == ray.origin.rows() && ray.origin.rows() == planeNormal.rows() );
117  CARNA_ASSERT( ray.direction.cols() == ray.origin.cols() && ray.origin.cols() == planeNormal.cols() && planeNormal.cols() == 1 );
118 
119  if( isEqual< ScalarType >( ray.direction.dot( planeNormal ), 0 ) )
120  {
121  myHitExists = false;
122  }
123  else
124  {
125  const ScalarType rayLength = ( planeOriginOffset - ray.origin.dot( planeNormal ) ) / ray.direction.dot( planeNormal );
126  if( rayLength < 0 )
127  {
128  myHitExists = false;
129  }
130  else
131  {
132  myHitExists = true;
133  myHitLocation = ray.origin + ray.direction * rayLength;
134  CARNA_ASSERT( isEqual( planeNormal.dot( myHitLocation ), planeOriginOffset ) );
135  }
136  }
137 }
138 
139 
140 
141 } // namespace Carna :: base :: math
142 
143 } // namespace Carna :: base
144 
145 } // namespace Carna
146 
147 #endif // RAYPLANEHITTEST_H_6014714286
Defines Carna::base::math namespace and CARNA_FOR_VECTOR3UI.
VectorType origin
Holds the origin of this ray.
Definition: Ray.h:62
void compute(const Ray< VectorType > &ray, const VectorType &planeNormal, ScalarType planeOriginOffset)
Performs a hit test of ray with the plane with planeNormal and planeOriginOffset. Use hitExists and h...
Defines ray in where is the Scalar type derived from VectorType and is dimension of VectorType...
Definition: Ray.h:47
const VectorType & hitLocation() const
References the location of the hit.
VectorType direction
Holds the direction of this ray.
Definition: Ray.h:67
Defines Carna::base::CarnaException, Carna::base::AssertionFailure.
bool isEqual(const InputType &x, const InputType &y)
Tells whether two objects are equal respectively to epsilon.
Definition: math.h:174
bool hitExists() const
Tells whether a hit exists.
Defines Carna::base::math::Ray.
#define CARNA_ASSERT(expression)
If the given expression is false, a break point is raised in debug mode and an AssertionFailure throw...
Tests whether particular plane is hit by a Ray object.