]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/compositeshape.h
Implement the missing assignment operator in CompositeShape
[libs/math.git] / source / geometry / compositeshape.h
index fd55c8e6e090875cd147f57697a35190fa26206e..55467d762993add17cbc4fe74ae7bfda2f776c22 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_GEOMETRY_COMPOSITESHAPE_H_
 #define MSP_GEOMETRY_COMPOSITESHAPE_H_
 
+#include <stdexcept>
 #include <vector>
 #include "shape.h"
 
@@ -19,16 +20,17 @@ protected:
 
        ShapeArray shapes;
 
+       CompositeShape() { }
        CompositeShape(const Shape<T, D> &, const Shape<T, D> &);
-       CompositeShape(const ShapeArray &);
+       template<typename Iter>
+       void init_from_iter_range(const Iter &, const Iter &);
        CompositeShape(const CompositeShape &);
        CompositeShape &operator=(const CompositeShape &);
 public:
        virtual ~CompositeShape();
 
-       virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
+       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;
        virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
 };
@@ -42,16 +44,35 @@ inline CompositeShape<T, D, O>::CompositeShape(const Shape<T, D> &s1, const Shap
 }
 
 template<typename T, unsigned D, typename O>
-inline CompositeShape<T, D, O>::CompositeShape(const ShapeArray &s)
+template<typename Iter>
+inline void CompositeShape<T, D, O>::init_from_iter_range(const Iter &begin, const Iter &end)
 {
-       if(s.empty())
-               throw std::invalid_argument("CompositeShape::CompositeShape");
+       if(begin==end)
+               throw std::invalid_argument("CompositeShape::init_from_iter_range");
 
-       shapes.reserve(s.size());
-       for(typename ShapeArray::const_iterator i=s.begin(); i!=s.end(); ++i)
+       for(Iter i=begin; i!=end; ++i)
                shapes.push_back((*i)->clone());
 }
 
+template<typename T, unsigned D, typename O>
+inline CompositeShape<T, D, O>::CompositeShape(const CompositeShape<T, D, O> &other):
+       shapes(other.shapes)
+{
+       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();
+}
+
 template<typename T, unsigned D, typename O>
 inline CompositeShape<T, D, O>::~CompositeShape()
 {
@@ -60,9 +81,9 @@ inline CompositeShape<T, D, O>::~CompositeShape()
 }
 
 template<typename T, unsigned D, typename O>
-inline HyperBox<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() const
 {
-       HyperBox<T, D> aabb;
+       BoundingBox<T, D> aabb;
        for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i)
        {
                if(i==shapes.begin())
@@ -86,12 +107,6 @@ inline bool CompositeShape<T, D, O>::contains(const LinAl::Vector<T, D> &point)
        return inside;
 }
 
-template<typename T, unsigned D, typename O>
-inline bool CompositeShape<T, D, O>::check_intersection(const Ray<T, D> &ray) const
-{
-       return get_intersections(ray, 0, 1);
-}
-
 template<typename T, unsigned D, typename O>
 inline unsigned CompositeShape<T, D, O>::get_max_ray_intersections() const
 {
@@ -105,14 +120,13 @@ 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
 {
        unsigned n = 0;
-       for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i)
+       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);
-               for(unsigned j=0; j<count; ++j)
+               for(unsigned j=0; (n<size && j<count); ++j)
                {
-                       // XXX Devise a way to reduce copies here
-                       SurfacePoint<T, D> pt = points[base+j];
+                       SurfacePoint<T, D> &pt = points[base+j];
 
                        bool surface = Ops::init_surface();
                        for(typename ShapeArray::const_iterator k=shapes.begin(); k!=shapes.end(); ++k)
@@ -121,22 +135,16 @@ inline unsigned CompositeShape<T, D, O>::get_intersections(const Ray<T, D> &ray,
 
                        if(surface)
                        {
-                               if(points)
-                               {
-                                       unsigned k;
-                                       for(k=n; (k>0 && points[k-1].distance>pt.distance); --k)
-                                               points[k] = points[k-1];
-                                       if(base+j!=k)
-                                               points[k] = pt;
-                               }
+                               if(points && base+j!=n)
+                                       points[n] = pt;
 
                                ++n;
-                               if(n==size)
-                                       return n;
                        }
                }
        }
 
+       sort_points(points, n);
+
        return n;
 }