X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgeometry%2Fextrudedshape.h;h=fd4597b9b1a5c935f50a3eb3a6e02296cd3f3a9f;hb=44bd1d1ab256d397be4e2169c4ca5efdd0569d31;hp=0d895ac5bc0aecbac9d6de46f5e93c9c932ca0dd;hpb=643aa7b2317f88463f66da11e595ebe0f6c9621d;p=libs%2Fmath.git diff --git a/source/geometry/extrudedshape.h b/source/geometry/extrudedshape.h index 0d895ac..fd4597b 100644 --- a/source/geometry/extrudedshape.h +++ b/source/geometry/extrudedshape.h @@ -31,18 +31,18 @@ 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(unsigned = 0) 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; + virtual Coverage get_coverage(const BoundingBox &) const; }; 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 +75,12 @@ inline ExtrudedShape *ExtrudedShape::clone() const } template -inline HyperBox ExtrudedShape::get_axis_aligned_bounding_box() const +inline BoundingBox ExtrudedShape::get_axis_aligned_bounding_box(unsigned detail) 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(detail); + 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 +91,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 +109,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. */ @@ -121,7 +117,7 @@ inline unsigned ExtrudedShape::get_intersections(const Ray &ray, Sur { T offset = T(); T limit = T(); - if(ray.get_direction()[D-1]!=T(0)) + if(ray_direction[D-1]!=T(0)) { offset = (half_length-ray_start[D-1])/ray_direction[D-1]; limit = (-half_length-ray_start[D-1])/ray_direction[D-1]; @@ -130,45 +126,48 @@ 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; i=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; + points[n].entry = base_points[i].entry; + } - ++n; - if(n==size) - return n; + ++n; + } } } /* If the ray is not parallel to the base space, it may pass through the caps. */ - if(ray_direction[D-1]) + if(n p = ray_start+ray_direction*x; - if(base->contains(LinAl::Vector(p)) && ncontains(p.template slice(0))) { if(points) { @@ -176,21 +175,37 @@ inline unsigned ExtrudedShape::get_intersections(const Ray &ray, Sur points[n].normal = LinAl::Vector(); points[n].normal[D-1] = i; points[n].distance = x; - - if(n==1 && x +inline Coverage ExtrudedShape::get_coverage(const BoundingBox &bbox) const +{ + T half_length = length/T(2); + if(bbox.get_maximum_coordinate(D-1)<-half_length || bbox.get_minimum_coordinate(D-1)>half_length) + return NO_COVERAGE; + + BoundingBox base_bbox(bbox.get_minimum_point().template slice(0), bbox.get_maximum_point().template slice(0)); + Coverage coverage = base->get_coverage(base_bbox); + if(coverage==NO_COVERAGE) + return NO_COVERAGE; + + if(bbox.get_minimum_coordinate(D-1)<-half_length || bbox.get_maximum_coordinate(D-1)>half_length) + return PARTIAL_COVERAGE; + else + return coverage; +} + } // namespace Geometry } // namespace Msp