]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/hyperbox.h
Implement bounding boxes with a separate class
[libs/math.git] / source / geometry / hyperbox.h
index 42e4d61a75a49ce0824aaf231388013246c1413f..da500910e4f100d57f865b644bd719d71a3ee34c 100644 (file)
@@ -5,6 +5,7 @@
 #include <cmath>
 #include <stdexcept>
 #include <msp/linal/vector.h>
+#include "boundingbox.h"
 #include "ray.h"
 #include "shape.h"
 #include "surfacepoint.h"
@@ -31,9 +32,8 @@ public:
        const LinAl::Vector<T, D> &get_dimensions() const { return dimensions; }
        T get_dimension(unsigned) const;
 
-       virtual HyperBox<T, D> get_axis_aligned_bounding_box() const { return *this; }
+       virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
        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;
 };
@@ -66,6 +66,13 @@ inline T HyperBox<T, D>::get_dimension(unsigned i) const
        return dimensions[i];
 }
 
+template<typename T, unsigned D>
+inline BoundingBox<T, D> HyperBox<T, D>::get_axis_aligned_bounding_box() const
+{
+       LinAl::Vector<T, D> half_dim = dimensions/T(2);
+       return BoundingBox<T, D>(-half_dim, half_dim);
+}
+
 template<typename T, unsigned D>
 inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
 {
@@ -77,25 +84,22 @@ inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
        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(!ray.check_limits(x))
@@ -107,7 +111,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)
                                {
@@ -121,8 +125,6 @@ inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfaceP
                                }
 
                                ++n;
-                               if(n==size || n==2)
-                                       return n;
                        }
                }
        }