]> git.tdb.fi Git - libs/math.git/blob - source/geometry/transformedshape.h
Use the coverage function to calculate tighter bounding boxes
[libs/math.git] / source / geometry / transformedshape.h
1 #ifndef MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
2 #define MSP_GEOMETRY_TRANSFORMEDSHAPE_H_
3
4 #include "affinetransformation.h"
5 #include "shape.h"
6
7 namespace Msp {
8 namespace Geometry {
9
10 /**
11 A shape modified by an affine transformation.
12 */
13 template<typename T, unsigned D>
14 class TransformedShape: public Shape<T, D>
15 {
16 private:
17         Shape<T, D> *shape;
18         AffineTransformation<T, D> transformation;
19         AffineTransformation<T, D> inverse_trans;
20
21 public:
22         TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
23         TransformedShape(const TransformedShape &);
24         TransformedShape &operator=(const TransformedShape &);
25         ~TransformedShape();
26
27         virtual TransformedShape *clone() const;
28
29         const Shape<T, D> &get_shape() const { return *shape; }
30         const AffineTransformation<T, D> &get_transformation() const { return transformation; }
31
32         virtual BoundingBox<T, D> get_axis_aligned_bounding_box(unsigned = 0) const;
33         virtual bool contains(const LinAl::Vector<T, D> &) const;
34         virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
35         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
36         virtual Coverage get_coverage(const BoundingBox<T, D> &) const;
37 };
38
39 template<typename T, unsigned D>
40 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
41         shape(s.clone()),
42         transformation(t),
43         inverse_trans(invert(t))
44 { }
45
46 template<typename T, unsigned D>
47 inline TransformedShape<T, D>::TransformedShape(const TransformedShape<T, D> &other):
48         shape(other.shape->clone()),
49         transformation(other.transformation),
50         inverse_trans(other.inverse_trans)
51 { }
52
53 template<typename T, unsigned D>
54 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
55 {
56         delete shape;
57         shape = other.shape->clone();
58         transformation = other.transformation;
59         inverse_trans = other.inverse_trans;
60 }
61
62 template<typename T, unsigned D>
63 inline TransformedShape<T, D>::~TransformedShape()
64 {
65         delete shape;
66 }
67
68 template<typename T, unsigned D>
69 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
70 {
71         return new TransformedShape<T, D>(*this);
72 }
73
74 template<typename T, unsigned D>
75 inline BoundingBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box(unsigned detail) const
76 {
77         if(detail)
78                 return this->bisect_axis_aligned_bounding_box(detail);
79
80         return transformation.transform(shape->get_axis_aligned_bounding_box());
81 }
82
83 template<typename T, unsigned D>
84 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
85 {
86         return shape->contains(inverse_trans.transform(point));
87 }
88
89 template<typename T, unsigned D>
90 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
91 {
92         Ray<T, D> local_ray = inverse_trans.transform(ray);
93
94         unsigned count = shape->get_intersections(local_ray, points, size);
95         if(points)
96         {
97                 for(unsigned i=0; i<count; ++i)
98                 {
99                         points[i].position = transformation.transform(points[i].position);
100                         /* XXX This is not correct for nonuniform scaling.  Inverse of the
101                         transpose of the upper DxD part of the matrix should be used. */
102                         points[i].normal = transformation.transform_linear(points[i].normal);
103                         points[i].distance = inner_product(points[i].position-ray.get_start(), ray.get_direction());
104                 }
105         }
106         return count;
107 }
108
109 template<typename T, unsigned D>
110 inline Coverage TransformedShape<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
111 {
112         return shape->get_coverage(inverse_trans.transform(bbox));
113 }
114
115 } // namespace Geometry
116 } // namespace Msp
117
118 #endif