1 #ifndef MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
2 #define MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
4 #include "affinetransformation.h"
5 #include "boundingbox.h"
13 A shape modified by an affine transformation.
15 template<typename T, unsigned D>
16 class TransformedShape: public Shape<T, D>
20 AffineTransformation<T, D> transformation;
21 AffineTransformation<T, D> inverse_trans;
24 TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
25 TransformedShape(const TransformedShape &);
26 TransformedShape &operator=(const TransformedShape &);
29 virtual TransformedShape *clone() const;
31 const Shape<T, D> &get_shape() const { return *shape; }
32 const AffineTransformation<T, D> &get_transformation() const { return transformation; }
34 virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
35 virtual bool contains(const LinAl::Vector<T, D> &) const;
37 Ray<T, D> make_local_ray(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 BoundingBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
81 BoundingBox<T, D> inner_bbox = shape->get_axis_aligned_bounding_box();
83 LinAl::Vector<T, D> min_pt;
84 LinAl::Vector<T, D> max_pt;
85 for(unsigned i=0; i<(1<<D); ++i)
87 LinAl::Vector<T, D> point;
88 for(unsigned j=0; j<D; ++j)
89 point[j] = ((i>>j)&1 ? inner_bbox.get_maximum_coordinate(j) : inner_bbox.get_minimum_coordinate(j));
91 point = transformation.transform(point);
93 for(unsigned j=0; j<D; ++j)
95 min_pt[j] = std::min(min_pt[j], point[j]);
96 max_pt[j] = std::max(min_pt[j], point[j]);
100 return BoundingBox<T, D>(min_pt, max_pt);
103 template<typename T, unsigned D>
104 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
106 return shape->contains(inverse_trans.transform(point));
109 template<typename T, unsigned D>
110 inline Ray<T, D> TransformedShape<T, D>::make_local_ray(const Ray<T, D> &ray) const
112 LinAl::Vector<T, D> local_dir = inverse_trans.transform_linear(ray.get_direction());
113 float distortion = local_dir.norm();
114 return Ray<T, D>(inverse_trans.transform(ray.get_start()), local_dir, ray.get_limit()*distortion);
117 template<typename T, unsigned D>
118 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
120 Ray<T, D> local_ray = make_local_ray(ray);
122 unsigned count = shape->get_intersections(local_ray, points, size);
125 for(unsigned i=0; i<count; ++i)
127 points[i].position = transformation.transform(points[i].position);
128 /* XXX This is not correct for nonuniform scaling. Inverse of the
129 transpose of the upper DxD part of the matrix should be used. */
130 points[i].normal = transformation.transform_linear(points[i].normal);
131 points[i].distance = inner_product(points[i].position-ray.get_start(), ray.get_direction());
137 } // namespace Geometry