From 4fc002747e8138501d08942be247e60cd99ac404 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 2 Jul 2016 18:34:13 +0300 Subject: [PATCH] Rework CompositeShape algorithms The old version had problems with coplanar surfaces and sometimes failed to produce intersections when it should have. --- source/geometry/compositeshape.h | 77 ++++++++++++++++++-------------- source/geometry/intersection.h | 6 +-- source/geometry/union.h | 6 +-- 3 files changed, 46 insertions(+), 43 deletions(-) diff --git a/source/geometry/compositeshape.h b/source/geometry/compositeshape.h index f0b1f52..0c2c838 100644 --- a/source/geometry/compositeshape.h +++ b/source/geometry/compositeshape.h @@ -21,7 +21,6 @@ protected: ShapeArray shapes; unsigned max_isect; - unsigned min_scratch; CompositeShape() { } CompositeShape(const Shape &, const Shape &); @@ -66,20 +65,14 @@ template inline void CompositeShape::init() { max_isect = 0; - min_scratch = 0; for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i) - { - unsigned mi = (*i)->get_max_ray_intersections(); - max_isect += mi; - min_scratch = std::max(min_scratch, mi); - } + max_isect += (*i)->get_max_ray_intersections(); } template inline CompositeShape::CompositeShape(const CompositeShape &other): shapes(other.shapes), - max_isect(other.max_isect), - min_scratch(other.min_scratch) + max_isect(other.max_isect) { for(typename ShapeArray::iterator i=shapes.begin(); i!=shapes.end(); ++i) *i = (*i)->clone(); @@ -96,7 +89,6 @@ inline CompositeShape &CompositeShape::operator=(const Composi *i = (*i)->clone(); max_isect = other.max_isect; - min_scratch = other.min_scratch; } template @@ -123,12 +115,12 @@ inline BoundingBox CompositeShape::get_axis_aligned_bounding_box( template inline bool CompositeShape::contains(const LinAl::Vector &point) const { - bool inside = Ops::init_inside(); + bool inside = false; for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i) { - inside = Ops::combine_inside(inside, (*i)->contains(point)); - if(Ops::is_inside_decided(inside)) - break; + inside = (*i)->contains(point); + if(Ops::shortcircuit(inside)) + return inside; } return inside; } @@ -138,39 +130,58 @@ inline unsigned CompositeShape::get_intersections(const Ray &ray, { SurfacePoint *buffer = points; unsigned buf_size = size; - if(!points) + if(!points && !Ops::shortcircuit(true)) { - buffer = new SurfacePoint[min_scratch]; - buf_size = min_scratch; + buffer = new SurfacePoint[max_isect]; + buf_size = max_isect; } + int start_nesting = 0; unsigned n = 0; - for(typename ShapeArray::const_iterator i=shapes.begin(); (nget_intersections(ray, buffer+base, buf_size-base); - for(unsigned j=0; (ncontains(ray.get_start()); + + if(!count && !start_inside) { - SurfacePoint &pt = buffer[base+j]; + if(Ops::shortcircuit(false)) + return 0; + continue; + } - bool surface = Ops::init_surface(); - for(typename ShapeArray::const_iterator k=shapes.begin(); k!=shapes.end(); ++k) - if(k!=i) - surface = Ops::combine_surface(surface, (*k)->contains(pt.position)); + start_nesting += start_inside; - if(surface) - { - if(points && base+j!=n) - points[n] = pt; + if(!base) + { + n = count; + continue; + } - ++n; + sort_points(buffer, base+count); + + int nesting = start_nesting; + unsigned k = 0; + for(unsigned j=0; j struct IntersectionOps { static BoundingBox combine_aabb(const BoundingBox &a, const BoundingBox &b) { return a&b; } - static bool init_inside() { return true; } - static bool combine_inside(bool a, bool b) { return a && b; } - static bool is_inside_decided(bool a) { return !a; } - static bool init_surface() { return true; } - static bool combine_surface(bool a, bool b) { return a && b; } + static bool shortcircuit(bool c) { return !c; } }; /** diff --git a/source/geometry/union.h b/source/geometry/union.h index a1562cb..738f1f2 100644 --- a/source/geometry/union.h +++ b/source/geometry/union.h @@ -10,11 +10,7 @@ template struct UnionOps { static BoundingBox combine_aabb(const BoundingBox &a, const BoundingBox &b) { return a|b; } - static bool init_inside() { return false; } - static bool combine_inside(bool a, bool b) { return a || b; } - static bool is_inside_decided(bool a) { return a; } - static bool init_surface() { return true; } - static bool combine_surface(bool a, bool b) { return a && !b; } + static bool shortcircuit(bool c) { return c; } }; /** -- 2.45.2