]> git.tdb.fi Git - libs/math.git/blob - source/geometry/transformedshape.h
Add basic description for all classes
[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         virtual bool check_intersection(const Ray<T, D> &) const;
36         virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); }
37         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
38 };
39
40 template<typename T, unsigned D>
41 inline TransformedShape<T, D>::TransformedShape(const Shape<T, D> &s, const AffineTransformation<T, D> &t):
42         shape(s.clone()),
43         transformation(t),
44         inverse_trans(invert(t))
45 { }
46
47 template<typename T, unsigned D>
48 inline TransformedShape<T, D>::TransformedShape(const TransformedShape &other):
49         shape(other.shape->clone()),
50         transformation(other.transformation),
51         inverse_trans(other.inverse_trans)
52 { }
53
54 template<typename T, unsigned D>
55 inline TransformedShape<T, D> &TransformedShape<T, D>::operator=(const TransformedShape<T, D> &other)
56 {
57         delete shape;
58         shape = other.shape->clone();
59         transformation = other.transformation;
60         inverse_trans = other.inverse_trans;
61 }
62
63 template<typename T, unsigned D>
64 inline TransformedShape<T, D>::~TransformedShape()
65 {
66         delete shape;
67 }
68
69 template<typename T, unsigned D>
70 inline TransformedShape<T, D> *TransformedShape<T, D>::clone() const
71 {
72         return new TransformedShape<T, D>(*this);
73 }
74
75 template<typename T, unsigned D>
76 inline HyperBox<T, D> TransformedShape<T, D>::get_axis_aligned_bounding_box() const
77 {
78         // XXX This is not correct for most shapes
79         return shape->get_axis_aligned_bounding_box();
80 }
81
82 template<typename T, unsigned D>
83 inline bool TransformedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
84 {
85         return shape->contains(inverse_trans.transform(point));
86 }
87
88 template<typename T, unsigned D>
89 inline bool TransformedShape<T, D>::check_intersection(const Ray<T, D> &ray) const
90 {
91         Ray<T, D> local_ray(inverse_trans.transform(ray.get_start()),
92                 inverse_trans.transform_linear(ray.get_direction()));
93         return shape->check_intersection(local_ray);
94 }
95
96 template<typename T, unsigned D>
97 inline unsigned TransformedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
98 {
99         Ray<T, D> local_ray(inverse_trans.transform(ray.get_start()),
100                 inverse_trans.transform_linear(ray.get_direction()));
101         unsigned count = shape->get_intersections(local_ray, points, size);
102         if(points)
103         {
104                 for(unsigned i=0; i<count; ++i)
105                 {
106                         points[i].position = transformation.transform(points[i].position);
107                         /* XXX This is not correct for nonuniform scaling.  Inverse of the
108                         transpose of the upper DxD part of the matrix should be used. */
109                         points[i].normal = transformation.transform(points[i].normal);
110                 }
111         }
112         return count;
113 }
114
115 } // namespace Geometry
116 } // namespace Msp
117
118 #endif