]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/halfspace.h
Add an entry flag to SharedPoint
[libs/math.git] / source / geometry / halfspace.h
index f671629e56194b0a93a00220c5ef17ef0c6ad0f7..b439851bd5adaf3a7067c7f91f5bf8308bb01166 100644 (file)
@@ -7,8 +7,8 @@ namespace Msp {
 namespace Geometry {
 
 /**
-An infinite shape consisting of the space on one side of a plane.  Mostly
-useful when composited with other shapes.
+An unbounded shape consisting of the space on one side of a plane.  Mostly
+useful when intersected with other shapes.
 */
 template<typename T, unsigned D>
 class HalfSpace: public Shape<T, D>
@@ -63,7 +63,12 @@ inline bool HalfSpace<T, D>::contains(const LinAl::Vector<T, D> &point) const
 template<typename T, unsigned D>
 inline unsigned HalfSpace<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
 {
-       T x = -inner_product(ray.get_start(), normal)/inner_product(ray.get_direction(), normal);
+       T d = inner_product(ray.get_start(), normal);
+       T c = inner_product(ray.get_direction(), normal);
+       if(c==T(0))
+               return 0;
+
+       T x = -d/c;
        if(ray.check_limits(x))
        {
                if(points && size>0)
@@ -71,6 +76,7 @@ inline unsigned HalfSpace<T, D>::get_intersections(const Ray<T, D> &ray, Surface
                        points[0].position = ray.get_start()+ray.get_direction()*x;
                        points[0].normal = normal;
                        points[0].distance = x;
+                       points[0].entry = (c<T(0));
                }
 
                return 1;