]> git.tdb.fi Git - libs/math.git/blob - source/geometry/transformedshape.h
Make the check_intersection function non-virtual
[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 "ray.h"
6 #include "shape.h"
7
8 namespace Msp {
9 namespace Geometry {
10
11 /**
12 A shape modified by an affine transformation.
13 */
14 template<typename T, unsigned D>
15 class TransformedShape: public Shape<T, D>
16 {
17 private:
18         Shape<T, D> *shape;
19         AffineTransformation<T, D> transformation;
20         AffineTransformation<T, D> inverse_trans;
21
22 public:
23         TransformedShape(const Shape<T, D> &, const AffineTransformation<T, D> &);
24         TransformedShape(const TransformedShape &);
25         TransformedShape &operator=(const TransformedShape &);
26         ~TransformedShape();
27
28         virtual TransformedShape *clone() const;
29
30         const Shape<T, D> &get_shape() const { return *shape; }
31         const AffineTransformation<T, D> &get_transformation() const { return transformation; }
32
33         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
34         virtual bool contains(const LinAl::Vector<T, D> &) const;
35 private:
36         Ray<T, D> make_local_ray(const Ray<T, D> &) const;
37 public:
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;
40 };
41
42 template<typename T, unsigned D>
43 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
44         shape(s.clone()),
45         transformation(t),
46         inverse_trans(invert(t))
47 { }
48
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)
54 { }
55
56 template<typename T, unsigned D>
57 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
58 {
59         delete shape;
60         shape = other.shape->clone();
61         transformation = other.transformation;
62         inverse_trans = other.inverse_trans;
63 }
64
65 template<typename T, unsigned D>
66 inline TransformedShape<T, D>::~TransformedShape()
67 {
68         delete shape;
69 }
70
71 template<typename T, unsigned D>
72 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
73 {
74         return new TransformedShape<T, D>(*this);
75 }
76
77 template<typename T, unsigned D>
78 inline HyperBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
79 {
80         // XXX This is not correct for most shapes
81         return shape->get_axis_aligned_bounding_box();
82 }
83
84 template<typename T, unsigned D>
85 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
86 {
87         return shape->contains(inverse_trans.transform(point));
88 }
89
90 template<typename T, unsigned D>
91 inline Ray<T, D> TransformedShape<T, D>::make_local_ray(const Ray<T, D> &ray) const
92 {
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);
96 }
97
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
100 {
101         Ray<T, D> local_ray = make_local_ray(ray);
102
103         unsigned count = shape->get_intersections(local_ray, points, size);
104         if(points)
105         {
106                 for(unsigned i=0; i<count; ++i)
107                 {
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());
113                 }
114         }
115         return count;
116 }
117
118 } // namespace Geometry
119 } // namespace Msp
120
121 #endif