1 #ifndef MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
2 #define MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
4 #include "affinetransformation.h"
12 A shape modified by an affine transformation.
14 template<typename T, unsigned D>
15 class TransformedShape: public Shape<T, D>
19 AffineTransformation<T, D> transformation;
20 AffineTransformation<T, D> inverse_trans;
23 TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
24 TransformedShape(const TransformedShape &);
25 TransformedShape &operator=(const TransformedShape &);
28 virtual TransformedShape *clone() const;
30 const Shape<T, D> &get_shape() const { return *shape; }
31 const AffineTransformation<T, D> &get_transformation() const { return transformation; }
33 virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
34 virtual bool contains(const LinAl::Vector<T, D> &) const;
35 virtual bool check_intersection(const Ray<T, D> &) const;
36 virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
37 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
40 template<typename T, unsigned D>
41 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
44 inverse_trans(invert(t))
47 template<typename T, unsigned D>
48 inline TransformedShape<T, D>::TransformedShape(const TransformedShape &other):
49 shape(other.shape->clone()),
50 transformation(other.transformation),
51 inverse_trans(other.inverse_trans)
54 template<typename T, unsigned D>
55 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
58 shape = other.shape->clone();
59 transformation = other.transformation;
60 inverse_trans = other.inverse_trans;
63 template<typename T, unsigned D>
64 inline TransformedShape<T, D>::~TransformedShape()
69 template<typename T, unsigned D>
70 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
72 return new TransformedShape<T, D>(*this);
75 template<typename T, unsigned D>
76 inline HyperBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
78 // XXX This is not correct for most shapes
79 return shape->get_axis_aligned_bounding_box();
82 template<typename T, unsigned D>
83 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
85 return shape->contains(inverse_trans.transform(point));
88 template<typename T, unsigned D>
89 inline bool TransformedShape<T, D>::check_intersection(const Ray<T, D> &ray) const
91 Ray<T, D> local_ray(inverse_trans.transform(ray.get_start()),
92 inverse_trans.transform_linear(ray.get_direction()));
93 return shape->check_intersection(local_ray);
96 template<typename T, unsigned D>
97 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
99 Ray<T, D> local_ray(inverse_trans.transform(ray.get_start()),
100 inverse_trans.transform_linear(ray.get_direction()));
101 unsigned count = shape->get_intersections(local_ray, points, size);
104 for(unsigned i=0; i<count; ++i)
106 points[i].position = transformation.transform(points[i].position);
107 /* XXX This is not correct for nonuniform scaling. Inverse of the
108 transpose of the upper DxD part of the matrix should be used. */
109 points[i].normal = transformation.transform(points[i].normal);
115 } // namespace Geometry