1 #ifndef MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
2 #define MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
4 #include "affinetransformation.h"
11 A shape modified by an affine transformation.
13 template<typename T, unsigned D>
14 class TransformedShape: public Shape<T, D>
18 AffineTransformation<T, D> transformation;
19 AffineTransformation<T, D> inverse_trans;
22 TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
23 TransformedShape(const TransformedShape &);
24 TransformedShape &operator=(const TransformedShape &);
27 virtual TransformedShape *clone() const;
29 const Shape<T, D> &get_shape() const { return *shape; }
30 const AffineTransformation<T, D> &get_transformation() const { return transformation; }
32 virtual BoundingBox<T, D> get_axis_aligned_bounding_box(unsigned = 0) const;
33 virtual bool contains(const LinAl::Vector<T, D> &) const;
34 virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
35 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
36 virtual Coverage get_coverage(const BoundingBox<T, D> &) const;
39 template<typename T, unsigned D>
40 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
43 inverse_trans(invert(t))
46 template<typename T, unsigned D>
47 inline TransformedShape<T, D>::TransformedShape(const TransformedShape<T, D> &other):
48 shape(other.shape->clone()),
49 transformation(other.transformation),
50 inverse_trans(other.inverse_trans)
53 template<typename T, unsigned D>
54 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
57 shape = other.shape->clone();
58 transformation = other.transformation;
59 inverse_trans = other.inverse_trans;
62 template<typename T, unsigned D>
63 inline TransformedShape<T, D>::~TransformedShape()
68 template<typename T, unsigned D>
69 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
71 return new TransformedShape<T, D>(*this);
74 template<typename T, unsigned D>
75 inline BoundingBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box(unsigned detail) const
78 return this->bisect_axis_aligned_bounding_box(detail);
80 return transformation.transform(shape->get_axis_aligned_bounding_box());
83 template<typename T, unsigned D>
84 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
86 return shape->contains(inverse_trans.transform(point));
89 template<typename T, unsigned D>
90 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
92 Ray<T, D> local_ray = inverse_trans.transform(ray);
94 unsigned count = shape->get_intersections(local_ray, points, size);
97 for(unsigned i=0; i<count; ++i)
99 points[i].position = transformation.transform(points[i].position);
100 /* XXX This is not correct for nonuniform scaling. Inverse of the
101 transpose of the upper DxD part of the matrix should be used. */
102 points[i].normal = transformation.transform_linear(points[i].normal);
103 points[i].distance = inner_product(points[i].position-ray.get_start(), ray.get_direction());
109 template<typename T, unsigned D>
110 inline Coverage TransformedShape<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
112 BoundingBox<T, D> local_bbox = inverse_trans.transform(bbox);
113 Coverage coverage = shape->get_coverage(local_bbox);
114 if(coverage==PARTIAL_COVERAGE)
116 BoundingBox<T, D> outer_bbox = transformation.transform(local_bbox);
117 LinAl::Vector<T, D> min_pt = local_bbox.get_minimum_point();
118 LinAl::Vector<T, D> max_pt = local_bbox.get_maximum_point();
119 for(unsigned i=0; i<D; ++i)
121 T scale_ratio = (1-bbox.get_dimension(i)/outer_bbox.get_dimension(i))*local_bbox.get_dimension(i);
122 T low_gap = bbox.get_minimum_coordinate(i)-outer_bbox.get_minimum_coordinate(i);
123 T high_gap = outer_bbox.get_maximum_coordinate(i)-bbox.get_maximum_coordinate(i);
124 min_pt[i] += low_gap*scale_ratio;
125 max_pt[i] -= high_gap-scale_ratio;
128 local_bbox = BoundingBox<T, D>(min_pt, max_pt);
129 if(shape->get_coverage(local_bbox)>=PARTIAL_COVERAGE)
130 return PARTIAL_COVERAGE;
132 return UNCERTAIN_COVERAGE;
138 } // namespace Geometry