]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/compositeshape.h
Use the coverage function to calculate tighter bounding boxes
[libs/math.git] / source / geometry / compositeshape.h
index 4c640be22f4c5045076b9659ce7734d517f1127c..5d0cd8c59532ed3b782c3cd60908c8ce96778b6c 100644 (file)
@@ -1,9 +1,9 @@
 #ifndef MSP_GEOMETRY_COMPOSITESHAPE_H_
 #define MSP_GEOMETRY_COMPOSITESHAPE_H_
 
+#include <algorithm>
 #include <stdexcept>
 #include <vector>
-#include "boundingbox.h"
 #include "shape.h"
 
 namespace Msp {
@@ -20,20 +20,25 @@ protected:
        typedef std::vector<Shape<T, D> *> ShapeArray;
 
        ShapeArray shapes;
+       unsigned max_isect;
 
        CompositeShape() { }
        CompositeShape(const Shape<T, D> &, const Shape<T, D> &);
-       CompositeShape(const CompositeShape &);
-       CompositeShape &operator=(const CompositeShape &);
        template<typename Iter>
        void init_from_iter_range(const Iter &, const Iter &);
+private:
+       void init();
+protected:
+       CompositeShape(const CompositeShape &);
+       CompositeShape &operator=(const CompositeShape &);
 public:
        virtual ~CompositeShape();
 
-       virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
+       virtual BoundingBox<T, D> get_axis_aligned_bounding_box(unsigned = 0) const;
        virtual bool contains(const LinAl::Vector<T, D> &) const;
-       virtual unsigned get_max_ray_intersections() const;
+       virtual unsigned get_max_ray_intersections() const { return max_isect; }
        virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
+       virtual Coverage get_coverage(const BoundingBox<T, D> &) const;
 };
 
 template<typename T, unsigned D, typename O>
@@ -42,6 +47,7 @@ inline CompositeShape<T, D, O>::CompositeShape(const Shape<T, D> &s1, const Shap
        shapes.reserve(2);
        shapes.push_back(s1.clone());
        shapes.push_back(s2.clone());
+       init();
 }
 
 template<typename T, unsigned D, typename O>
@@ -53,14 +59,37 @@ inline void CompositeShape<T, D, O>::init_from_iter_range(const Iter &begin, con
 
        for(Iter i=begin; i!=end; ++i)
                shapes.push_back((*i)->clone());
+       init();
 }
 
 template<typename T, unsigned D, typename O>
-inline CompositeShape<T, D, O>::CompositeShape(const CompositeShape<T, D, O> &other)
+inline void CompositeShape<T, D, O>::init()
 {
-       shapes.reserve(other.shapes.size());
-       for(typename ShapeArray::const_iterator i=other.shapes.begin(); i!=other.shapes.end(); ++i)
-               shapes.push_back((*i)->clone());
+       max_isect = 0;
+       for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i)
+               max_isect += (*i)->get_max_ray_intersections();
+}
+
+template<typename T, unsigned D, typename O>
+inline CompositeShape<T, D, O>::CompositeShape(const CompositeShape<T, D, O> &other):
+       shapes(other.shapes),
+       max_isect(other.max_isect)
+{
+       for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i)
+               *i = (*i)->clone();
+}
+
+template<typename T, unsigned D, typename O>
+inline CompositeShape<T, D, O> &CompositeShape<T, D, O>::operator=(const CompositeShape<T, D, O> &other)
+{
+       for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i)
+               delete *i;
+
+       shapes = other.shapes;
+       for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i)
+               *i = (*i)->clone();
+
+       max_isect = other.max_isect;
 }
 
 template<typename T, unsigned D, typename O>
@@ -71,8 +100,11 @@ inline CompositeShape<T, D, O>::~CompositeShape()
 }
 
 template<typename T, unsigned D, typename O>
-inline BoundingBox<T, D> CompositeShape<T, D, O>::get_axis_aligned_bounding_box() const
+inline BoundingBox<T, D> CompositeShape<T, D, O>::get_axis_aligned_bounding_box(unsigned detail) const
 {
+       if(detail)
+               return this->bisect_axis_aligned_bounding_box(detail);
+
        BoundingBox<T, D> aabb;
        for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i)
        {
@@ -87,57 +119,98 @@ inline BoundingBox<T, D> CompositeShape<T, D, O>::get_axis_aligned_bounding_box(
 template<typename T, unsigned D, typename O>
 inline bool CompositeShape<T, D, O>::contains(const LinAl::Vector<T, D> &point) const
 {
-       bool inside = Ops::init_inside();
+       bool inside = false;
        for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i)
        {
-               inside = Ops::combine_inside(inside, (*i)->contains(point));
-               if(Ops::is_inside_decided(inside))
-                       break;
+               inside = (*i)->contains(point);
+               if(Ops::shortcircuit(inside))
+                       return inside;
        }
        return inside;
 }
 
-template<typename T, unsigned D, typename O>
-inline unsigned CompositeShape<T, D, O>::get_max_ray_intersections() const
-{
-       unsigned max_isect = 0;
-       for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i)
-               max_isect += (*i)->get_max_ray_intersections();
-       return max_isect;
-}
-
 template<typename T, unsigned D, typename O>
 inline unsigned CompositeShape<T, D, O>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
 {
+       SurfacePoint<T, D> *buffer = points;
+       unsigned buf_size = size;
+       if(!points && !Ops::shortcircuit(true))
+       {
+               buffer = new SurfacePoint<T, D>[max_isect];
+               buf_size = max_isect;
+       }
+
+       int start_nesting = 0;
        unsigned n = 0;
-       for(typename ShapeArray::const_iterator i=shapes.begin(); (n<size && i!=shapes.end()); ++i)
+       for(typename ShapeArray::const_iterator i=shapes.begin(); (n<buf_size && i!=shapes.end()); ++i)
        {
                unsigned base = n;
-               unsigned count = (*i)->get_intersections(ray, points+base, size-base);
-               for(unsigned j=0; (n<size && j<count); ++j)
+               unsigned count = (*i)->get_intersections(ray, buffer+base, buf_size-base);
+               bool start_inside = (*i)->contains(ray.get_start());
+
+               if(!count && !start_inside)
+               {
+                       if(Ops::shortcircuit(false))
+                               return 0;
+                       continue;
+               }
+
+               start_nesting += start_inside;
+
+               if(!base)
                {
-                       SurfacePoint<T, D> &pt = points[base+j];
+                       n = count;
+                       continue;
+               }
 
-                       bool surface = Ops::init_surface();
-                       for(typename ShapeArray::const_iterator k=shapes.begin(); k!=shapes.end(); ++k)
-                               if(k!=i)
-                                       surface = Ops::combine_surface(surface, (*k)->contains(pt.position));
+               sort_points(buffer, base+count);
 
-                       if(surface)
+               int nesting = start_nesting;
+               unsigned k = 0;
+               for(unsigned j=0; j<base+count; ++j)
+               {
+                       if(Ops::shortcircuit(nesting+buffer[j].entry<2))
                        {
-                               if(points && base+j!=n)
-                                       points[n] = pt;
-
-                               ++n;
+                               if(j!=k)
+                                       buffer[k] = buffer[j];
+                               ++k;
                        }
+
+                       nesting += buffer[j].entry*2-1;
                }
+
+               if(!k && Ops::shortcircuit(false))
+                       return 0;
+
+               n = k;
+
+               if(i!=shapes.begin())
+                       start_nesting = (start_nesting>!Ops::shortcircuit(true));
        }
 
-       sort_points(points, n);
+       if(buffer!=points)
+               delete[] buffer;
 
        return n;
 }
 
+template<typename T, unsigned D, typename O>
+inline Coverage CompositeShape<T, D, O>::get_coverage(const BoundingBox<T, D> &bbox) const
+{
+       Coverage coverage = NO_COVERAGE;
+       for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i)
+       {
+               Coverage c = (*i)->get_coverage(bbox);
+               if(i==shapes.begin() || Ops::shortcircuit(c>coverage))
+                       coverage = c;
+
+               if(coverage!=PARTIAL_COVERAGE && Ops::shortcircuit(coverage==FULL_COVERAGE))
+                       break;
+       }
+
+       return coverage;
+}
+
 } // namespace Geometry
 } // namespace Msp