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() 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;
38 template<typename T, unsigned D>
39 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
42 inverse_trans(invert(t))
45 template<typename T, unsigned D>
46 inline TransformedShape<T, D>::TransformedShape(const TransformedShape<T, D> &other):
47 shape(other.shape->clone()),
48 transformation(other.transformation),
49 inverse_trans(other.inverse_trans)
52 template<typename T, unsigned D>
53 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
56 shape = other.shape->clone();
57 transformation = other.transformation;
58 inverse_trans = other.inverse_trans;
61 template<typename T, unsigned D>
62 inline TransformedShape<T, D>::~TransformedShape()
67 template<typename T, unsigned D>
68 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
70 return new TransformedShape<T, D>(*this);
73 template<typename T, unsigned D>
74 inline BoundingBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
76 return transformation.transform(shape->get_axis_aligned_bounding_box());
79 template<typename T, unsigned D>
80 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
82 return shape->contains(inverse_trans.transform(point));
85 template<typename T, unsigned D>
86 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
88 Ray<T, D> local_ray = inverse_trans.transform(ray);
90 unsigned count = shape->get_intersections(local_ray, points, size);
93 for(unsigned i=0; i<count; ++i)
95 points[i].position = transformation.transform(points[i].position);
96 /* XXX This is not correct for nonuniform scaling. Inverse of the
97 transpose of the upper DxD part of the matrix should be used. */
98 points[i].normal = transformation.transform_linear(points[i].normal);
99 points[i].distance = inner_product(points[i].position-ray.get_start(), ray.get_direction());
105 } // namespace Geometry