]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/transformedshape.h
Optimize bounding box bisection with more early culling
[libs/math.git] / source / geometry / transformedshape.h
index 9a98f3139d85ef56edcd3fcdb1d8ab47116b6fee..9387123508dcf081341c076f8c4e41970a10ead0 100644 (file)
@@ -2,18 +2,21 @@
 #define MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
 
 #include "affinetransformation.h"
-#include "ray.h"
 #include "shape.h"
 
 namespace Msp {
 namespace Geometry {
 
+/**
+A shape modified by an affine transformation.
+*/
 template<typename T, unsigned D>
-class TransformedShape
+class TransformedShape: public Shape<T, D>
 {
 private:
        Shape<T, D> *shape;
        AffineTransformation<T, D> transformation;
+       AffineTransformation<T, D> inverse_trans;
 
 public:
        TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
@@ -26,19 +29,25 @@ public:
        const Shape<T, D> &get_shape() const { return *shape; }
        const AffineTransformation<T, D> &get_transformation() const { return transformation; }
 
-       virtual bool check_intersection(const Ray<T, D> &) 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 { return shape->get_max_ray_intersections(); }
+       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>
 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
        shape(s.clone()),
-       transformation(t)
+       transformation(t),
+       inverse_trans(invert(t))
 { }
 
 template<typename T, unsigned D>
-inline TransformedShape<T, D>::TransformedShape(const TransformedShape &other):
+inline TransformedShape<T, D>::TransformedShape(const TransformedShape<T, D> &other):
        shape(other.shape->clone()),
-       transformation(other.transformation)
+       transformation(other.transformation),
+       inverse_trans(other.inverse_trans)
 { }
 
 template<typename T, unsigned D>
@@ -46,7 +55,8 @@ inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const Transform
 {
        delete shape;
        shape = other.shape->clone();
-       transformation = other.transformation();
+       transformation = other.transformation;
+       inverse_trans = other.inverse_trans;
 }
 
 template<typename T, unsigned D>
@@ -62,13 +72,67 @@ inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
 }
 
 template<typename T, unsigned D>
-inline bool TransformedShape<T, D>::check_intersection(const Ray<T, D> &ray) const
+inline BoundingBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box(unsigned detail) const
 {
-       // TODO cache the inverse transformation for performance
-       LinAl::SquareMatrix<T, D+1> inverse_trans = LinAl::invert(transformation.get_matrix());
-       Ray<T, D> trans_ray(reduce_vector(inverse_trans*augment_vector(ray.get_start(), T(1))),
-               reduce_vector(inverse_trans*augment_vector(ray.get_direction(), T(0))));
-       return shape->check_intersection(trans_ray);
+       if(detail)
+               return this->bisect_axis_aligned_bounding_box(detail);
+
+       return transformation.transform(shape->get_axis_aligned_bounding_box());
+}
+
+template<typename T, unsigned D>
+inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
+{
+       return shape->contains(inverse_trans.transform(point));
+}
+
+template<typename T, unsigned D>
+inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
+{
+       Ray<T, D> local_ray = inverse_trans.transform(ray);
+
+       unsigned count = shape->get_intersections(local_ray, points, size);
+       if(points)
+       {
+               for(unsigned i=0; i<count; ++i)
+               {
+                       points[i].position = transformation.transform(points[i].position);
+                       /* XXX This is not correct for nonuniform scaling.  Inverse of the
+                       transpose of the upper DxD part of the matrix should be used. */
+                       points[i].normal = transformation.transform_linear(points[i].normal);
+                       points[i].distance = inner_product(points[i].position-ray.get_start(), ray.get_direction());
+               }
+       }
+       return count;
+}
+
+template<typename T, unsigned D>
+inline Coverage TransformedShape<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
+{
+       BoundingBox<T, D> local_bbox = inverse_trans.transform(bbox);
+       Coverage coverage = shape->get_coverage(local_bbox);
+       if(coverage==PARTIAL_COVERAGE)
+       {
+               BoundingBox<T, D> outer_bbox = transformation.transform(local_bbox);
+               LinAl::Vector<T, D> min_pt = local_bbox.get_minimum_point();
+               LinAl::Vector<T, D> max_pt = local_bbox.get_maximum_point();
+               for(unsigned i=0; i<D; ++i)
+               {
+                       T scale_ratio = (1-bbox.get_dimension(i)/outer_bbox.get_dimension(i))*local_bbox.get_dimension(i);
+                       T low_gap = bbox.get_minimum_coordinate(i)-outer_bbox.get_minimum_coordinate(i);
+                       T high_gap = outer_bbox.get_maximum_coordinate(i)-bbox.get_maximum_coordinate(i);
+                       min_pt[i] += low_gap*scale_ratio;
+                       max_pt[i] -= high_gap-scale_ratio;
+               }
+
+               local_bbox = BoundingBox<T, D>(min_pt, max_pt);
+               if(shape->get_coverage(local_bbox)>=PARTIAL_COVERAGE)
+                       return PARTIAL_COVERAGE;
+               else
+                       return UNCERTAIN_COVERAGE;
+       }
+       else
+               return coverage;
 }
 
 } // namespace Geometry