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 unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
39 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
42 template<typename T, unsigned D>
43 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
46 inverse_trans(invert(t))
49 template<typename T, unsigned D>
50 inline TransformedShape<T, D>::TransformedShape(const TransformedShape<T, D> &other):
51 shape(other.shape->clone()),
52 transformation(other.transformation),
53 inverse_trans(other.inverse_trans)
56 template<typename T, unsigned D>
57 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
60 shape = other.shape->clone();
61 transformation = other.transformation;
62 inverse_trans = other.inverse_trans;
65 template<typename T, unsigned D>
66 inline TransformedShape<T, D>::~TransformedShape()
71 template<typename T, unsigned D>
72 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
74 return new TransformedShape<T, D>(*this);
77 template<typename T, unsigned D>
78 inline HyperBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
80 // XXX This is not correct for most shapes
81 return shape->get_axis_aligned_bounding_box();
84 template<typename T, unsigned D>
85 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
87 return shape->contains(inverse_trans.transform(point));
90 template<typename T, unsigned D>
91 inline Ray<T, D> TransformedShape<T, D>::make_local_ray(const Ray<T, D> &ray) const
93 LinAl::Vector<T, D> local_dir = inverse_trans.transform_linear(ray.get_direction());
94 float distortion = local_dir.norm();
95 return Ray<T, D>(inverse_trans.transform(ray.get_start()), local_dir, ray.get_limit()*distortion);
98 template<typename T, unsigned D>
99 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
101 Ray<T, D> local_ray = make_local_ray(ray);
103 unsigned count = shape->get_intersections(local_ray, points, size);
106 for(unsigned i=0; i<count; ++i)
108 points[i].position = transformation.transform(points[i].position);
109 /* XXX This is not correct for nonuniform scaling. Inverse of the
110 transpose of the upper DxD part of the matrix should be used. */
111 points[i].normal = transformation.transform_linear(points[i].normal);
112 points[i].distance = inner_product(points[i].position-ray.get_start(), ray.get_direction());
118 } // namespace Geometry