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 BoundingBox<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 BoundingBox<T, D> ExtrudedShape<T, D>::get_axis_aligned_bounding_box() const
79 BoundingBox<T, D-1> base_bbox = base->get_axis_aligned_bounding_box();
80 T half_length = length/T(2);
81 return BoundingBox<T, D>(LinAl::Vector<T, D>(base_bbox.get_minimum_point(), -half_length),
82 LinAl::Vector<T, D>(base_bbox.get_maximum_point(), half_length));
85 template<typename T, unsigned D>
86 inline bool ExtrudedShape<T, D>::contains(const LinAl::Vector<T, D> &point) const
90 if(abs(point[D-1])>length/T(2))
93 return base->contains(LinAl::Vector<T, D-1>(point));
96 template<typename T, unsigned D>
97 inline unsigned ExtrudedShape<T, D>::get_max_ray_intersections() const
99 return std::max(base->get_max_ray_intersections(), 2U);
102 template<typename T, unsigned D>
103 inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
108 T half_length = length/T(2);
109 const LinAl::Vector<T, D> &ray_start = ray.get_start();
110 const LinAl::Vector<T, D> &ray_direction = ray.get_direction();
111 LinAl::Vector<T, D-1> base_dir(ray_direction);
113 /* If the ray does not degenerate to a point in the base space, it could
114 intersect the base shape. */
115 if(inner_product(base_dir, base_dir)!=T(0))
119 if(ray.get_direction()[D-1]!=T(0))
121 offset = (half_length-ray_start[D-1])/ray_direction[D-1];
122 limit = (-half_length-ray_start[D-1])/ray_direction[D-1];
131 T distortion = base_dir.norm();
132 Ray<T, D-1> base_ray(LinAl::Vector<T, D-1>(ray_start+ray_direction*offset),
133 base_dir, (limit-offset)*distortion);
135 SurfacePoint<T, D-1> *base_points = 0;
137 /* Shamelessly reuse the provided storage. Align to the end of the array
138 so processing can start from the first (nearest) point. */
139 base_points = reinterpret_cast<SurfacePoint<T, D-1> *>(points+size)-size;
141 unsigned count = base->get_intersections(base_ray, base_points, size);
142 for(unsigned i=0; (n<size && i<count); ++i)
146 T x = offset+base_points[i].distance/distortion;
147 points[n].position = ray_start+ray_direction*x;
148 points[n].normal = LinAl::Vector<T, D>(base_points[i].normal, T(0));
149 points[n].distance = x;
157 /* If the ray is not parallel to the base space, it may pass through the
159 if(n<size && ray_direction[D-1])
161 for(int i=-1; (n<size && i<=1); i+=2)
163 T x = (half_length*i-ray_start[D-1])/ray_direction[D-1];
164 if(!ray.check_limits(x))
167 LinAl::Vector<T, D> p = ray_start+ray_direction*x;
168 if(base->contains(LinAl::Vector<T, D-1>(p)))
172 points[n].position = p;
173 points[n].normal = LinAl::Vector<T, D>();
174 points[n].normal[D-1] = i;
175 points[n].distance = x;
182 sort_points(points, n);
188 } // namespace Geometry