]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/hyperbox.h
Make the check_intersection function non-virtual
[libs/math.git] / source / geometry / hyperbox.h
index 10c28d5d1f4c20a15937e7b0aa696f3074151aec..11c197e72ebb059814e0c92f685ac533b72acaa2 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <algorithm>
 #include <cmath>
+#include <stdexcept>
 #include <msp/linal/vector.h>
 #include "ray.h"
 #include "shape.h"
@@ -32,7 +33,6 @@ public:
 
        virtual HyperBox<T, D> get_axis_aligned_bounding_box() const { return *this; }
        virtual bool contains(const LinAl::Vector<T, D> &) const;
-       virtual bool check_intersection(const Ray<T, D> &) const;
        virtual unsigned get_max_ray_intersections() const { return 2; }
        virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
 };
@@ -47,7 +47,11 @@ inline HyperBox<T, D>::HyperBox()
 template<typename T, unsigned D>
 inline HyperBox<T, D>::HyperBox(const LinAl::Vector<T, D> &d):
        dimensions(d)
-{ }
+{
+       for(unsigned i=0; i<D; ++i)
+               if(dimensions[i]<=T(0))
+                       throw std::invalid_argument("HyperBox::HyperBox");
+}
 
 template<typename T, unsigned D>
 inline HyperBox<T, D> *HyperBox<T, D>::clone() const
@@ -64,34 +68,33 @@ inline T HyperBox<T, D>::get_dimension(unsigned i) const
 template<typename T, unsigned D>
 inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
 {
+       using std::abs;
+
        for(unsigned i=0; i<D; ++i)
-               if(abs(point[i])>dimensions[i]/2)
+               if(abs(point[i])>dimensions[i]/T(2))
                        return false;
        return true;
 }
 
-template<typename T, unsigned D>
-inline bool HyperBox<T, D>::check_intersection(const Ray<T, D> &ray) const
-{
-       return get_intersections(ray, 0, 1);
-}
-
 template<typename T, unsigned D>
 inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
 {
        using std::abs;
 
+       if(size>2)
+               size = 2;
+
        LinAl::Vector<T, D> half_dim = dimensions/T(2);
        unsigned n = 0;
-       for(unsigned i=0; i<D; ++i)
+       for(unsigned i=0; (n<size && i<D); ++i)
        {
                if(!ray.get_direction()[i])
                        continue;
 
-               for(int j=-1; j<=1; j+=2)
+               for(int j=-1; (n<size && j<=1); j+=2)
                {
                        T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
-                       if(x<0)
+                       if(!ray.check_limits(x))
                                continue;
 
                        LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
@@ -100,7 +103,7 @@ inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfaceP
                        for(unsigned k=0; (inside && k<D); ++k)
                                inside = (k==i || abs(p[k])<=half_dim[k]);
 
-                       if(inside && n<size)
+                       if(inside)
                        {
                                if(points)
                                {
@@ -114,8 +117,6 @@ inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfaceP
                                }
 
                                ++n;
-                               if(n==size || n==2)
-                                       return n;
                        }
                }
        }