vmmlib  1.11.0
Templatized C++ vector and matrix math library
ray.hpp
1 /*
2  * Copyright (c) 2006-2016, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /* @author Jafet Villafranca
34  *
35  * Implements ray-object intersection, storing the ray parameters as attributes
36  * and implementing intersection tests against different types of objects
37  * (e.g. sphere)
38  */
39 
40 #ifndef __VMML__RAY__HPP__
41 #define __VMML__RAY__HPP__
42 
43 #include <vmmlib/vector.hpp>
44 
45 namespace vmml
46 {
47 template< typename T > class Ray
48 {
49 public:
50  typedef vector< 3, T > vec3;
51  typedef vector< 4, T > vec4;
52 
59  Ray( const vec3& origin, const vec3& direction )
60  : _origin ( origin )
61  , _direction ( vmml::normalize( direction ))
62  {}
63  ~Ray() {}
64 
76  T test( const vec4& sphere ) const;
77 
78 private:
79  const vec3 _origin;
80  const vec3 _direction;
81 
82 };
83 
84 
85 template< typename T >
86 T Ray< T >::test( const vec4& sphere ) const
87 {
88  const vec3 center = vec3(sphere.x(), sphere.y(), sphere.z());
89  const T radius = sphere.w();
90 
91  const vec3 centerVec = center - _origin;
92  const T vecProjection = centerVec.dot(_direction);
93 
94  const T sqDistance = centerVec.squared_length();
95  const T sqRadius = radius * radius;
96 
98  if( vecProjection < 0 && sqDistance > sqRadius )
99  return -1.f;
100 
102  const T sqCenterToProj = sqDistance - vecProjection * vecProjection;
103 
104  if( sqCenterToProj > sqRadius )
105  return -1.f;
106 
108  const T distSurface = sqrt( sqRadius - sqCenterToProj );
109 
110  if(sqDistance > sqRadius)
111  return vecProjection - distSurface;
112 
113  return vecProjection + distSurface;
114 }
115 
116 } // namespace vmml
117 
118 #endif
T test(const vec4 &sphere) const
Ray-Sphere Intersection.
Definition: ray.hpp:86
heavily inspired by boost::enable_if http://www.boost.org, file: boost/utility/enable_if.hpp, Copyright 2003 Jaakko Järvi, Jeremiah Willcock, Andrew Lumsdaine
Definition: aabb.hpp:40
Ray(const vec3 &origin, const vec3 &direction)
Constructor.
Definition: ray.hpp:59