]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/hyperbox.h
Add more collision check functions for shapes
[libs/math.git] / source / geometry / hyperbox.h
index 2ade28acddd42ba9da6eefcb8cbefe14040ee535..6248d6b0920e906f5bad1ee1ce1b9ba6fbb97fc4 100644 (file)
@@ -1,9 +1,12 @@
 #ifndef MSP_GEOMETRY_HYPERBOX_H_
 #define MSP_GEOMETRY_HYPERBOX_H_
 
+#include <algorithm>
+#include <cmath>
 #include <msp/linal/vector.h>
 #include "ray.h"
 #include "shape.h"
+#include "surfacepoint.h"
 
 namespace Msp {
 namespace Geometry {
@@ -24,7 +27,10 @@ public:
        T get_dimension(unsigned) const;
 
        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;
 };
 
 template<typename T, unsigned D>
@@ -51,26 +57,67 @@ inline T HyperBox<T, D>::get_dimension(unsigned i) const
        return dimensions[i];
 }
 
+template<typename T, unsigned D>
+inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
+{
+       for(unsigned i=0; i<D; ++i)
+               if(abs(point[i])>dimensions[i]/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;
+
        LinAl::Vector<T, D> half_dim = dimensions/T(2);
+       unsigned n = 0;
+       T first_depth = T();
        for(unsigned i=0; i<D; ++i)
+       {
+               if(!ray.get_direction()[i])
+                       continue;
+
                for(int j=-1; j<=1; j+=2)
                {
                        T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
-                       if(x>0)
+                       if(x<0)
+                               continue;
+
+                       LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
+
+                       bool inside = true;
+                       for(unsigned k=0; (inside && k<D); ++k)
+                               inside = (k==i || abs(p[k])<=half_dim[k]);
+
+                       if(inside && n<size)
                        {
-                               LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
-                               bool inside = true;
-                               for(unsigned k=0; (inside && k<D); ++k)
-                                       inside = (k==i || (p[k]>=-half_dim[k] && p[k]<half_dim[k]));
-                               if(inside)
-                                       return true;
+                               if(points)
+                               {
+                                       points[n].position = p;
+                                       points[n].normal = LinAl::Vector<T, D>();
+                                       points[n].normal[i] = j;
+                                       if(n==0)
+                                               first_depth = x;
+                                       else if(n==1 && x<first_depth)
+                                               std::swap(points[0], points[1]);
+                               }
+
+                               ++n;
+                               if(n==size || n==2)
+                                       return n;
                        }
                }
+       }
 
-       return false;
+       return n;
 }
 
 } // namespace Geometry