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;
36 Ray<T, D> make_local_ray(const Ray<T, D> &) const;
38 virtual bool check_intersection(const Ray<T, D> &) const;
39 virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
40 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
43 template<typename T, unsigned D>
44 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
47 inverse_trans(invert(t))
50 template<typename T, unsigned D>
51 inline TransformedShape<T, D>::TransformedShape(const TransformedShape<T, D> &other):
52 shape(other.shape->clone()),
53 transformation(other.transformation),
54 inverse_trans(other.inverse_trans)
57 template<typename T, unsigned D>
58 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
61 shape = other.shape->clone();
62 transformation = other.transformation;
63 inverse_trans = other.inverse_trans;
66 template<typename T, unsigned D>
67 inline TransformedShape<T, D>::~TransformedShape()
72 template<typename T, unsigned D>
73 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
75 return new TransformedShape<T, D>(*this);
78 template<typename T, unsigned D>
79 inline HyperBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
81 // XXX This is not correct for most shapes
82 return shape->get_axis_aligned_bounding_box();
85 template<typename T, unsigned D>
86 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
88 return shape->contains(inverse_trans.transform(point));
91 template<typename T, unsigned D>
92 inline Ray<T, D> TransformedShape<T, D>::make_local_ray(const Ray<T, D> &ray) const
94 LinAl::Vector<T, D> local_dir = inverse_trans.transform_linear(ray.get_direction());
95 float distortion = local_dir.norm();
96 return Ray<T, D>(inverse_trans.transform(ray.get_start()), local_dir, ray.get_limit()*distortion);
99 template<typename T, unsigned D>
100 inline bool TransformedShape<T, D>::check_intersection(const Ray<T, D> &ray) const
102 return shape->check_intersection(make_local_ray(ray));
105 template<typename T, unsigned D>
106 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
108 Ray<T, D> local_ray = make_local_ray(ray);
110 unsigned count = shape->get_intersections(local_ray, points, size);
113 for(unsigned i=0; i<count; ++i)
115 points[i].position = transformation.transform(points[i].position);
116 /* XXX This is not correct for nonuniform scaling. Inverse of the
117 transpose of the upper DxD part of the matrix should be used. */
118 points[i].normal = transformation.transform_linear(points[i].normal);
119 points[i].distance = inner_product(points[i].position-ray.get_start(), ray.get_direction());
125 } // namespace Geometry