]> git.tdb.fi Git - libs/math.git/commitdiff
Make CompositeShape::get_intersections work with null points
authorMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2013 20:03:07 +0000 (23:03 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2013 20:03:07 +0000 (23:03 +0300)
This was a bit tricky, since it's necessary to inspect the points returned
by component shapes to see if they're actually on the composite surface.
To that end, a temporary buffer is allocated.  This is most likely used in
interactive point-and-pick situations, so the performance loss should not
be significant.

source/geometry/compositeshape.h

index 55467d762993add17cbc4fe74ae7bfda2f776c22..f0b1f529544c12913202640c1e22604e5e5d5ded 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GEOMETRY_COMPOSITESHAPE_H_
 #define MSP_GEOMETRY_COMPOSITESHAPE_H_
 
+#include <algorithm>
 #include <stdexcept>
 #include <vector>
 #include "shape.h"
@@ -19,11 +20,16 @@ protected:
        typedef std::vector<Shape<T, D> *> ShapeArray;
 
        ShapeArray shapes;
+       unsigned max_isect;
+       unsigned min_scratch;
 
        CompositeShape() { }
        CompositeShape(const Shape<T, D> &, const Shape<T, D> &);
        template<typename Iter>
        void init_from_iter_range(const Iter &, const Iter &);
+private:
+       void init();
+protected:
        CompositeShape(const CompositeShape &);
        CompositeShape &operator=(const CompositeShape &);
 public:
@@ -31,7 +37,7 @@ public:
 
        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;
+       virtual unsigned get_max_ray_intersections() const { return max_isect; }
        virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
 };
 
@@ -41,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>
@@ -52,11 +59,27 @@ 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 void CompositeShape<T, D, O>::init()
+{
+       max_isect = 0;
+       min_scratch = 0;
+       for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i)
+       {
+               unsigned mi = (*i)->get_max_ray_intersections();
+               max_isect += mi;
+               min_scratch = std::max(min_scratch, mi);
+       }
 }
 
 template<typename T, unsigned D, typename O>
 inline CompositeShape<T, D, O>::CompositeShape(const CompositeShape<T, D, O> &other):
-       shapes(other.shapes)
+       shapes(other.shapes),
+       max_isect(other.max_isect),
+       min_scratch(other.min_scratch)
 {
        for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i)
                *i = (*i)->clone();
@@ -71,6 +94,9 @@ inline CompositeShape<T, D, O> &CompositeShape<T, D, O>::operator=(const Composi
        shapes = other.shapes;
        for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i)
                *i = (*i)->clone();
+
+       max_isect = other.max_isect;
+       min_scratch = other.min_scratch;
 }
 
 template<typename T, unsigned D, typename O>
@@ -107,26 +133,25 @@ inline bool CompositeShape<T, D, O>::contains(const LinAl::Vector<T, D> &point)
        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)
+       {
+               buffer = new SurfacePoint<T, D>[min_scratch];
+               buf_size = min_scratch;
+       }
+
        unsigned n = 0;
        for(typename ShapeArray::const_iterator i=shapes.begin(); (n<size && i!=shapes.end()); ++i)
        {
-               unsigned base = n;
-               unsigned count = (*i)->get_intersections(ray, points+base, size-base);
+               unsigned base = (points ? n : 0);
+               unsigned count = (*i)->get_intersections(ray, buffer+base, buf_size-base);
                for(unsigned j=0; (n<size && j<count); ++j)
                {
-                       SurfacePoint<T, D> &pt = points[base+j];
+                       SurfacePoint<T, D> &pt = buffer[base+j];
 
                        bool surface = Ops::init_surface();
                        for(typename ShapeArray::const_iterator k=shapes.begin(); k!=shapes.end(); ++k)
@@ -143,7 +168,10 @@ inline unsigned CompositeShape<T, D, O>::get_intersections(const Ray<T, D> &ray,
                }
        }
 
-       sort_points(points, n);
+       if(points)
+               sort_points(points, n);
+       else
+               delete[] buffer;
 
        return n;
 }