]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/halfspace.h
Avoid division by zero in HalfSpace::get_intersections
[libs/math.git] / source / geometry / halfspace.h
index 9ed80ff71b85052e6525bb46fb800f775c04fe3f..4adc0dff45694726f399b7a07a3afff0cedcdb2c 100644 (file)
@@ -24,7 +24,7 @@ public:
 
        const LinAl::Vector<T, D> &get_normal() const { return normal; }
 
-       virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
+       virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
        virtual bool contains(const LinAl::Vector<T, D> &) const;
        virtual unsigned get_max_ray_intersections() const { return 1; }
        virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
@@ -33,7 +33,7 @@ public:
 template<typename T, unsigned D>
 inline HalfSpace<T, D>::HalfSpace()
 {
-       normal[0] = 1;
+       normal[0] = T(1);
 }
 
 template<typename T, unsigned D>
@@ -48,22 +48,27 @@ inline HalfSpace<T, D> *HalfSpace<T, D>::clone() const
 }
 
 template<typename T, unsigned D>
-inline HyperBox<T, D> HalfSpace<T, D>::get_axis_aligned_bounding_box() const
+inline BoundingBox<T, D> HalfSpace<T, D>::get_axis_aligned_bounding_box() const
 {
-       // XXX Implement this properly
-       return HyperBox<T, D>();
+       // XXX If the normal is aligned to an axis, should the bounding box reflect that?
+       return ~BoundingBox<T, D>();
 }
 
 template<typename T, unsigned D>
 inline bool HalfSpace<T, D>::contains(const LinAl::Vector<T, D> &point) const
 {
-       return inner_product(point, normal)<=0;
+       return inner_product(point, normal)<=T(0);
 }
 
 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)