1 #ifndef MSP_GEOMETRY_SHAPE_H_
2 #define MSP_GEOMETRY_SHAPE_H_
6 #include <msp/linal/vector.h>
7 #include "boundingbox.h"
9 #include "surfacepoint.h"
22 Base class and interface for geometric shapes. Shapes may be bounded or
23 unbounded. They are always considered to be solid, i.e. have a distinct inside
26 template<typename T, unsigned D>
33 BoundingBox<T, D> bounding_box;
41 virtual Shape *clone() const = 0;
43 /** Returns the bounding box of the shape. The detail parameter controls
44 the tightness of the box. Higher detail will take more time to compute. */
45 virtual BoundingBox<T, D> get_axis_aligned_bounding_box(unsigned detail = 0) const = 0;
47 BoundingBox<T, D> bisect_axis_aligned_bounding_box(unsigned) const;
50 /** Checks if a point is contained within the shape. */
51 virtual bool contains(const LinAl::Vector<T, D> &) const = 0;
53 bool check_intersection(const Ray<T, D> &) const;
54 virtual unsigned get_max_ray_intersections() const = 0;
56 /** Determines intersection points between the shape and a ray. */
57 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const = 0;
59 /** Returns a vector with all of the intersections between the shape and a
61 std::vector<SurfacePoint<T, D> > get_intersections(const Ray<T, D> &) const;
63 /** Determines whether the shape covers a bounding box. */
64 virtual Coverage get_coverage(const BoundingBox<T, D> &) const = 0;
67 template<typename T, unsigned D>
68 inline BoundingBox<T, D> Shape<T, D>::bisect_axis_aligned_bounding_box(unsigned detail) const
71 throw std::invalid_argument("Shape::bisect_axis_aligned_bounding_box");
73 // Form the root cell from the loosest approximation of a bounding box.
74 std::list<CoverageCell> queue;
75 queue.push_back(CoverageCell());
76 CoverageCell &root = queue.front();
78 root.bounding_box = get_axis_aligned_bounding_box();
79 // There's no point bisecting if the bounding box fills the entire space.
80 if(root.bounding_box.is_space())
81 return root.bounding_box;
83 root.coverage = get_coverage(root.bounding_box);
84 // If the bounding box is fully covered it's already tight.
85 if(root.coverage==FULL_COVERAGE)
86 return root.bounding_box;
88 /* Initialize bounds to the opposite edges because we don't yet know which
89 part of the bounding box the shape occupies. */
90 LinAl::Vector<T, D> tight_min_pt = root.bounding_box.get_maximum_point();
91 LinAl::Vector<T, D> tight_max_pt = root.bounding_box.get_minimum_point();
95 CoverageCell &cell = queue.front();
97 const LinAl::Vector<T, D> &min_pt = cell.bounding_box.get_minimum_point();
98 const LinAl::Vector<T, D> &max_pt = cell.bounding_box.get_maximum_point();
99 LinAl::Vector<T, D> center = (min_pt+max_pt)/T(2);
101 // Bisect each dimension.
102 for(unsigned i=0; i<(1<<D); ++i)
105 child.level = cell.level+1;
107 LinAl::Vector<T, D> child_min_pt = min_pt;
108 LinAl::Vector<T, D> child_max_pt = max_pt;
109 for(unsigned j=0; j<D; ++j)
112 child_min_pt[j] = center[j];
114 child_max_pt[j] = center[j];
116 child.bounding_box = BoundingBox<T, D>(child_min_pt, child_max_pt);
118 child.coverage = get_coverage(child.bounding_box);
119 if(child.coverage==FULL_COVERAGE || (child.level==detail && child.coverage!=NO_COVERAGE))
121 /* Adjust the bounds when we are certain that it's covered by at
122 least part of the shape. Also adjust for uncertain results if
123 we're on the last level. */
124 for(unsigned j=0; j<D; ++j)
126 tight_min_pt[j] = std::min(tight_min_pt[j], child_min_pt[j]);
127 tight_max_pt[j] = std::max(tight_max_pt[j], child_max_pt[j]);
130 else if(child.coverage==PARTIAL_COVERAGE)
131 queue.push_back(child);
137 return BoundingBox<T, D>(tight_min_pt, tight_max_pt);
140 template<typename T, unsigned D>
141 inline bool Shape<T, D>::check_intersection(const Ray<T, D> &ray) const
143 return get_intersections(ray, 0, 1);
146 template<typename T, unsigned D>
147 inline std::vector<SurfacePoint<T, D> > Shape<T, D>::get_intersections(const Ray<T, D> &ray) const
149 unsigned max_isect = get_max_ray_intersections();
150 std::vector<SurfacePoint<T, D> > points(max_isect);
151 unsigned count = get_intersections(ray, &points[0], max_isect);
152 points.resize(count);
156 } // namespace Geometry