X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgeometry%2Fextrudedshape.h;h=bacea7c3e54e208f6ef246bfa4dd719aa2aeb8ac;hb=fb9e6b5ff48e10ea2217cf539e0461956abf02cf;hp=7aeedb0cd146e30e761e3c88b41d54dde680cdfb;hpb=827b227ae53884109b6885ccd1424dda8c0b3725;p=libs%2Fmath.git diff --git a/source/geometry/extrudedshape.h b/source/geometry/extrudedshape.h index 7aeedb0..bacea7c 100644 --- a/source/geometry/extrudedshape.h +++ b/source/geometry/extrudedshape.h @@ -31,9 +31,8 @@ public: const Shape &get_base() const { return *base; } T get_length() const { return length; } - virtual HyperBox get_axis_aligned_bounding_box() const; + virtual BoundingBox get_axis_aligned_bounding_box() const; virtual bool contains(const LinAl::Vector &) const; - virtual bool check_intersection(const Ray &) const; virtual unsigned get_max_ray_intersections() const; virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; }; @@ -42,7 +41,7 @@ template inline ExtrudedShape::ExtrudedShape(const Shape &b, T l): length(l) { - if(l<=0) + if(l<=T(0)) throw std::invalid_argument("ExtrudedShape::ExtrudedShape"); base = b.clone(); @@ -75,10 +74,12 @@ inline ExtrudedShape *ExtrudedShape::clone() const } template -inline HyperBox ExtrudedShape::get_axis_aligned_bounding_box() const +inline BoundingBox ExtrudedShape::get_axis_aligned_bounding_box() const { - HyperBox base_bbox = base->get_axis_aligned_bounding_box(); - return HyperBox(LinAl::Vector(base_bbox.get_dimensions(), length)); + BoundingBox base_bbox = base->get_axis_aligned_bounding_box(); + T half_length = length/T(2); + return BoundingBox(compose(base_bbox.get_minimum_point(), -half_length), + compose(base_bbox.get_maximum_point(), half_length)); } template @@ -89,13 +90,7 @@ inline bool ExtrudedShape::contains(const LinAl::Vector &point) cons if(abs(point[D-1])>length/T(2)) return false; - return base->contains(LinAl::Vector(point)); -} - -template -inline bool ExtrudedShape::check_intersection(const Ray &ray) const -{ - return get_intersections(ray, 0, 1); + return base->contains(point.template slice(0)); } template @@ -113,7 +108,7 @@ inline unsigned ExtrudedShape::get_intersections(const Ray &ray, Sur T half_length = length/T(2); const LinAl::Vector &ray_start = ray.get_start(); const LinAl::Vector &ray_direction = ray.get_direction(); - LinAl::Vector base_dir(ray_direction); + LinAl::Vector base_dir = ray_direction.template slice(0); /* If the ray does not degenerate to a point in the base space, it could intersect the base shape. */ @@ -130,28 +125,32 @@ inline unsigned ExtrudedShape::get_intersections(const Ray &ray, Sur if(offset base_ray(LinAl::Vector(ray_start+ray_direction*offset), - base_dir, (limit-offset)*distortion); - - SurfacePoint *base_points = 0; - if(points) - /* Shamelessly reuse the provided storage. Align to the end of the array - so processing can start from the first (nearest) point. */ - base_points = reinterpret_cast *>(points+size)-size; - - unsigned count = base->get_intersections(base_ray, base_points, size); - for(unsigned i=0; (n=offset) { + T distortion = base_dir.norm(); + Ray base_ray((ray_start+ray_direction*offset).template slice(0), + base_dir, (limit-offset)*distortion); + + SurfacePoint *base_points = 0; if(points) + /* Shamelessly reuse the provided storage. Align to the end of the array + so processing can start from the first (nearest) point. */ + base_points = reinterpret_cast *>(points+size)-size; + + unsigned count = base->get_intersections(base_ray, base_points, size); + for(unsigned i=0; (n(base_points[i].normal, T(0)); - points[n].distance = x; - } + if(points) + { + T x = offset+base_points[i].distance/distortion; + points[n].position = ray_start+ray_direction*x; + points[n].normal = compose(base_points[i].normal, T(0)); + points[n].distance = x; + } - ++n; + ++n; + } } } @@ -166,7 +165,7 @@ inline unsigned ExtrudedShape::get_intersections(const Ray &ray, Sur continue; LinAl::Vector p = ray_start+ray_direction*x; - if(base->contains(LinAl::Vector(p))) + if(base->contains(p.template slice(0))) { if(points) {