From 827b227ae53884109b6885ccd1424dda8c0b3725 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 21 May 2013 20:53:09 +0300 Subject: [PATCH] Properly sort intersection points for complex shapes Code structure in simpler shapes have been adjusted to keep it similar. --- source/geometry/compositeshape.h | 21 +++++++-------------- source/geometry/extrudedshape.h | 17 ++++++----------- source/geometry/hyperbox.h | 11 ++++++----- source/geometry/hypersphere.h | 6 ++---- source/geometry/surfacepoint.h | 15 +++++++++++++++ 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/source/geometry/compositeshape.h b/source/geometry/compositeshape.h index fd55c8e..50b04e8 100644 --- a/source/geometry/compositeshape.h +++ b/source/geometry/compositeshape.h @@ -105,14 +105,13 @@ template inline unsigned CompositeShape::get_intersections(const Ray &ray, SurfacePoint *points, unsigned size) const { unsigned n = 0; - for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i) + for(typename ShapeArray::const_iterator i=shapes.begin(); (nget_intersections(ray, points+base, size-base); - for(unsigned j=0; j pt = points[base+j]; + SurfacePoint &pt = points[base+j]; bool surface = Ops::init_surface(); for(typename ShapeArray::const_iterator k=shapes.begin(); k!=shapes.end(); ++k) @@ -121,22 +120,16 @@ inline unsigned CompositeShape::get_intersections(const Ray &ray, if(surface) { - if(points) - { - unsigned k; - for(k=n; (k>0 && points[k-1].distance>pt.distance); --k) - points[k] = points[k-1]; - if(base+j!=k) - points[k] = pt; - } + if(points && base+j!=n) + points[n] = pt; ++n; - if(n==size) - return n; } } } + sort_points(points, n); + return n; } diff --git a/source/geometry/extrudedshape.h b/source/geometry/extrudedshape.h index 0d895ac..7aeedb0 100644 --- a/source/geometry/extrudedshape.h +++ b/source/geometry/extrudedshape.h @@ -141,7 +141,7 @@ inline unsigned ExtrudedShape::get_intersections(const Ray &ray, Sur base_points = reinterpret_cast *>(points+size)-size; unsigned count = base->get_intersections(base_ray, base_points, size); - for(unsigned i=0; i::get_intersections(const Ray &ray, Sur } ++n; - if(n==size) - return 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(LinAl::Vector(p))) { if(points) { @@ -176,16 +174,13 @@ 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::get_intersections(const Ray &ray, SurfaceP { using std::abs; + if(size>2) + size = 2; + LinAl::Vector half_dim = dimensions/T(2); unsigned n = 0; - for(unsigned i=0; i::get_intersections(const Ray &ray, SurfaceP for(unsigned k=0; (inside && k::get_intersections(const Ray &ray, SurfaceP } ++n; - if(n==size || n==2) - return n; } } } diff --git a/source/geometry/hypersphere.h b/source/geometry/hypersphere.h index 35cddd1..cbec2d9 100644 --- a/source/geometry/hypersphere.h +++ b/source/geometry/hypersphere.h @@ -89,10 +89,10 @@ inline unsigned HyperSphere::get_intersections(const Ray &ray, Surfa T offset = sqrt(offset_sq); unsigned n = 0; - for(int i=-1; i<=1; i+=2) + for(int i=-1; (n::get_intersections(const Ray &ray, Surfa } ++n; - if(n==size) - return n; } } diff --git a/source/geometry/surfacepoint.h b/source/geometry/surfacepoint.h index a5bd794..57a66d1 100644 --- a/source/geometry/surfacepoint.h +++ b/source/geometry/surfacepoint.h @@ -1,6 +1,7 @@ #ifndef MSP_GEOMETRY_SURFACEPOINT_H_ #define MSP_GEOMETRY_SURFACEPOINT_H_ +#include #include namespace Msp { @@ -17,6 +18,20 @@ struct SurfacePoint T distance; }; +template +void sort_points(SurfacePoint *points, unsigned size) +{ + for(unsigned i=0; i