1 #ifndef MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
2 #define MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
4 #include "affinetransformation.h"
11 template<typename T, unsigned D>
12 class TransformedShape: public Shape<T, D>
16 AffineTransformation<T, D> transformation;
17 AffineTransformation<T, D> inverse_trans;
20 TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
21 TransformedShape(const TransformedShape &);
22 TransformedShape &operator=(const TransformedShape &);
25 virtual TransformedShape *clone() const;
27 const Shape<T, D> &get_shape() const { return *shape; }
28 const AffineTransformation<T, D> &get_transformation() const { return transformation; }
30 virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
31 virtual bool contains(const LinAl::Vector<T, D> &) const;
32 virtual bool check_intersection(const Ray<T, D> &) const;
33 virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
34 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
37 template<typename T, unsigned D>
38 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
41 inverse_trans(invert(t))
44 template<typename T, unsigned D>
45 inline TransformedShape<T, D>::TransformedShape(const TransformedShape &other):
46 shape(other.shape->clone()),
47 transformation(other.transformation),
48 inverse_trans(other.inverse_trans)
51 template<typename T, unsigned D>
52 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
55 shape = other.shape->clone();
56 transformation = other.transformation;
57 inverse_trans = other.inverse_trans;
60 template<typename T, unsigned D>
61 inline TransformedShape<T, D>::~TransformedShape()
66 template<typename T, unsigned D>
67 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
69 return new TransformedShape<T, D>(*this);
72 template<typename T, unsigned D>
73 inline HyperBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
75 // XXX This is not correct for most shapes
76 return 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 bool TransformedShape<T, D>::check_intersection(const Ray<T, D> &ray) const
88 Ray<T, D> local_ray(inverse_trans.transform(ray.get_start()),
89 inverse_trans.transform_linear(ray.get_direction()));
90 return shape->check_intersection(local_ray);
93 template<typename T, unsigned D>
94 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
96 Ray<T, D> local_ray(inverse_trans.transform(ray.get_start()),
97 inverse_trans.transform_linear(ray.get_direction()));
98 unsigned count = shape->get_intersections(local_ray, points, size);
101 for(unsigned i=0; i<count; ++i)
103 points[i].position = transformation.transform(points[i].position);
104 /* XXX This is not correct for nonuniform scaling. Inverse of the
105 transpose of the upper DxD part of the matrix should be used. */
106 points[i].normal = transformation.transform(points[i].normal);
112 } // namespace Geometry