1 #ifndef MSP_GEOMETRY_EXTRUDEDSHAPE_H_
2 #define MSP_GEOMETRY_EXTRUDEDSHAPE_H_
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.
16 template<typename T, unsigned D>
17 class ExtrudedShape: public Shape<T, D>
24 ExtrudedShape(const Shape<T, D-1> &, T);
25 ExtrudedShape(const ExtrudedShape &);
26 ExtrudedShape &operator=(const ExtrudedShape &);
27 virtual ~ExtrudedShape();
29 virtual ExtrudedShape *clone() const;
31 const Shape<T, D-1> &get_base() const { return *base; }
32 T get_length() const { return length; }
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;
40 template<typename T, unsigned D>
41 inline ExtrudedShape<T, D>::ExtrudedShape(const Shape<T, D-1> &b, T l):
45 throw std::invalid_argument("ExtrudedShape::ExtrudedShape");
50 template<typename T, unsigned D>
51 inline ExtrudedShape<T, D>::ExtrudedShape(const ExtrudedShape<T, D> &other):
52 base(other.base.clone()),
56 template<typename T, unsigned D>
57 inline ExtrudedShape<T, D> &ExtrudedShape<T, D>::operator=(const ExtrudedShape<T, D> &other)
60 base = other.base.clone();
61 length = other.length;
64 template<typename T, unsigned D>
65 inline ExtrudedShape<T, D>::~ExtrudedShape()
70 template<typename T, unsigned D>
71 inline ExtrudedShape<T, D> *ExtrudedShape<T, D>::clone() const
73 return new ExtrudedShape<T, D>(*base, length);
76 template<typename T, unsigned D>
77 inline HyperBox<T, D> ExtrudedShape<T, D>::get_axis_aligned_bounding_box() const
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));
83 template<typename T, unsigned D>
84 inline bool ExtrudedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
88 if(abs(point[D-1])>length/T(2))
91 return base->contains(LinAl::Vector<T, D-1>(point));
94 template<typename T, unsigned D>
95 inline unsigned ExtrudedShape<T, D>::get_max_ray_intersections() const
97 return std::max(base->get_max_ray_intersections(), 2U);
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
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);
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))
117 if(ray.get_direction()[D-1]!=T(0))
119 offset = (half_length-ray_start[D-1])/ray_direction[D-1];
120 limit = (-half_length-ray_start[D-1])/ray_direction[D-1];
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);
130 SurfacePoint<T, D-1> *base_points = 0;
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;
136 unsigned count = base->get_intersections(base_ray, base_points, size);
137 for(unsigned i=0; (n<size && i<count); ++i)
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;
151 /* If the ray is not parallel to the base space, it may pass through the
153 if(n<size && ray_direction[D-1])
155 for(int i=-1; (n<size && i<=1); i+=2)
157 T x = (half_length*i-ray_start[D-1])/ray_direction[D-1];
158 if(!ray.check_limits(x))
161 LinAl::Vector<T, D> p = ray_start+ray_direction*x;
162 if(base->contains(LinAl::Vector<T, D-1>(p)))
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;
176 sort_points(points, n);
182 } // namespace Geometry