]> git.tdb.fi Git - libs/math.git/blob - source/geometry/extrudedshape.h
Make the check_intersection function non-virtual
[libs/math.git] / source / geometry / extrudedshape.h
1 #ifndef MSP_GEOMETRY_EXTRUDEDSHAPE_H_
2 #define MSP_GEOMETRY_EXTRUDEDSHAPE_H_
3
4 #include <algorithm>
5 #include <cmath>
6 #include "shape.h"
7
8 namespace Msp {
9 namespace Geometry {
10
11 /**
12 A shape embedded in space of dimension higher by one and extruded towards the
13 highest dimension.  As an example, extruding a circle creates a cylinder.  The
14 base shape's orientation is not changed.
15 */
16 template<typename T, unsigned D>
17 class ExtrudedShape: public Shape<T, D>
18 {
19 private:
20         Shape<T, D-1> *base;
21         T length;
22
23 public:
24         ExtrudedShape(const Shape<T, D-1> &, T);
25         ExtrudedShape(const ExtrudedShape &);
26         ExtrudedShape &operator=(const ExtrudedShape &);
27         virtual ~ExtrudedShape();
28
29         virtual ExtrudedShape *clone() const;
30
31         const Shape<T, D-1> &get_base() const { return *base; }
32         T get_length() const { return length; }
33
34         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
35         virtual bool contains(const LinAl::Vector<T, D> &) const;
36         virtual unsigned get_max_ray_intersections() const;
37         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
38 };
39
40 template<typename T, unsigned D>
41 inline ExtrudedShape<T, D>::ExtrudedShape(const Shape<T, D-1> &b, T l):
42         length(l)
43 {
44         if(l<=0)
45                 throw std::invalid_argument("ExtrudedShape::ExtrudedShape");
46
47         base = b.clone();
48 }
49
50 template<typename T, unsigned D>
51 inline ExtrudedShape<T, D>::ExtrudedShape(const ExtrudedShape<T, D> &other):
52         base(other.base.clone()),
53         length(other.length)
54 { }
55
56 template<typename T, unsigned D>
57 inline ExtrudedShape<T, D> &ExtrudedShape<T, D>::operator=(const ExtrudedShape<T, D> &other)
58 {
59         delete base;
60         base = other.base.clone();
61         length = other.length;
62 }
63
64 template<typename T, unsigned D>
65 inline ExtrudedShape<T, D>::~ExtrudedShape()
66 {
67         delete base;
68 }
69
70 template<typename T, unsigned D>
71 inline ExtrudedShape<T, D> *ExtrudedShape<T, D>::clone() const
72 {
73         return new ExtrudedShape<T, D>(*base, length);
74 }
75
76 template<typename T, unsigned D>
77 inline HyperBox<T, D> ExtrudedShape<T, D>::get_axis_aligned_bounding_box() const
78 {
79         HyperBox<T, D-1> base_bbox = base->get_axis_aligned_bounding_box();
80         return HyperBox<T, D>(LinAl::Vector<T, D>(base_bbox.get_dimensions(), length));
81 }
82
83 template<typename T, unsigned D>
84 inline bool ExtrudedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
85 {
86         using std::abs;
87
88         if(abs(point[D-1])>length/T(2))
89                 return false;
90
91         return base->contains(LinAl::Vector<T, D-1>(point));
92 }
93
94 template<typename T, unsigned D>
95 inline unsigned ExtrudedShape<T, D>::get_max_ray_intersections() const
96 {
97         return std::max(base->get_max_ray_intersections(), 2U);
98 }
99
100 template<typename T, unsigned D>
101 inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
102 {
103         using std::swap;
104
105         unsigned n = 0;
106         T half_length = length/T(2);
107         const LinAl::Vector<T, D> &ray_start = ray.get_start();
108         const LinAl::Vector<T, D> &ray_direction = ray.get_direction();
109         LinAl::Vector<T, D-1> base_dir(ray_direction);
110
111         /* If the ray does not degenerate to a point in the base space, it could
112         intersect the base shape. */
113         if(inner_product(base_dir, base_dir)!=T(0))
114         {
115                 T offset = T();
116                 T limit = T();
117                 if(ray.get_direction()[D-1]!=T(0))
118                 {
119                         offset = (half_length-ray_start[D-1])/ray_direction[D-1];
120                         limit = (-half_length-ray_start[D-1])/ray_direction[D-1];
121                         if(offset>limit)
122                                 swap(offset, limit);
123                         if(offset<T(0))
124                                 offset = T(0);
125                 }
126                 T distortion = base_dir.norm();
127                 Ray<T, D-1> base_ray(LinAl::Vector<T, D-1>(ray_start+ray_direction*offset),
128                         base_dir, (limit-offset)*distortion);
129
130                 SurfacePoint<T, D-1> *base_points = 0;
131                 if(points)
132                         /* Shamelessly reuse the provided storage.  Align to the end of the array
133                         so processing can start from the first (nearest) point. */
134                         base_points = reinterpret_cast<SurfacePoint<T, D-1> *>(points+size)-size;
135
136                 unsigned count = base->get_intersections(base_ray, base_points, size);
137                 for(unsigned i=0; (n<size && i<count); ++i)
138                 {
139                         if(points)
140                         {
141                                 T x = offset+base_points[i].distance/distortion;
142                                 points[n].position = ray_start+ray_direction*x;
143                                 points[n].normal = LinAl::Vector<T, D>(base_points[i].normal, T(0));
144                                 points[n].distance = x;
145                         }
146
147                         ++n;
148                 }
149         }
150
151         /* If the ray is not parallel to the base space, it may pass through the
152         caps. */
153         if(n<size && ray_direction[D-1])
154         {
155                 for(int i=-1; (n<size && i<=1); i+=2)
156                 {
157                         T x = (half_length*i-ray_start[D-1])/ray_direction[D-1];
158                         if(!ray.check_limits(x))
159                                 continue;
160
161                         LinAl::Vector<T, D> p = ray_start+ray_direction*x;
162                         if(base->contains(LinAl::Vector<T, D-1>(p)))
163                         {
164                                 if(points)
165                                 {
166                                         points[n].position = p;
167                                         points[n].normal = LinAl::Vector<T, D>();
168                                         points[n].normal[D-1] = i;
169                                         points[n].distance = x;
170                                 }
171
172                                 ++n;
173                         }
174                 }
175
176                 sort_points(points, n);
177         }
178
179         return n;
180 }
181
182 } // namespace Geometry
183 } // namespace Msp
184
185 #endif