From: Mikko Rasa Date: Tue, 21 May 2013 18:41:15 +0000 (+0300) Subject: Make the check_intersection function non-virtual X-Git-Url: http://git.tdb.fi/?p=libs%2Fmath.git;a=commitdiff_plain;h=313e10c1dcf5504789cc145166aece93d8141212 Make the check_intersection function non-virtual Almost all shapes were implementing it with a call to get_intersections, so might as well make the base class do that. Performance loss is likely negligible. --- diff --git a/source/geometry/compositeshape.h b/source/geometry/compositeshape.h index 50b04e8..9476f99 100644 --- a/source/geometry/compositeshape.h +++ b/source/geometry/compositeshape.h @@ -28,7 +28,6 @@ public: virtual HyperBox 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; }; @@ -86,12 +85,6 @@ inline bool CompositeShape::contains(const LinAl::Vector &point) return inside; } -template -inline bool CompositeShape::check_intersection(const Ray &ray) const -{ - return get_intersections(ray, 0, 1); -} - template inline unsigned CompositeShape::get_max_ray_intersections() const { diff --git a/source/geometry/extrudedshape.h b/source/geometry/extrudedshape.h index 7aeedb0..83933b0 100644 --- a/source/geometry/extrudedshape.h +++ b/source/geometry/extrudedshape.h @@ -33,7 +33,6 @@ public: virtual HyperBox 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; }; @@ -92,12 +91,6 @@ inline bool ExtrudedShape::contains(const LinAl::Vector &point) cons return base->contains(LinAl::Vector(point)); } -template -inline bool ExtrudedShape::check_intersection(const Ray &ray) const -{ - return get_intersections(ray, 0, 1); -} - template inline unsigned ExtrudedShape::get_max_ray_intersections() const { diff --git a/source/geometry/hyperbox.h b/source/geometry/hyperbox.h index 9a161f3..11c197e 100644 --- a/source/geometry/hyperbox.h +++ b/source/geometry/hyperbox.h @@ -33,7 +33,6 @@ public: virtual HyperBox get_axis_aligned_bounding_box() const { return *this; } virtual bool contains(const LinAl::Vector &) const; - virtual bool check_intersection(const Ray &) const; virtual unsigned get_max_ray_intersections() const { return 2; } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; }; @@ -77,12 +76,6 @@ inline bool HyperBox::contains(const LinAl::Vector &point) const return true; } -template -inline bool HyperBox::check_intersection(const Ray &ray) const -{ - return get_intersections(ray, 0, 1); -} - template inline unsigned HyperBox::get_intersections(const Ray &ray, SurfacePoint *points, unsigned size) const { diff --git a/source/geometry/hypersphere.h b/source/geometry/hypersphere.h index cbec2d9..abdd193 100644 --- a/source/geometry/hypersphere.h +++ b/source/geometry/hypersphere.h @@ -32,7 +32,6 @@ public: virtual HyperBox 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 { return 2; } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; }; @@ -66,16 +65,6 @@ inline bool HyperSphere::contains(const LinAl::Vector &point) const return inner_product(point, point)<=radius*radius; } -template -inline bool HyperSphere::check_intersection(const Ray &ray) const -{ - T x = inner_product(ray.get_direction(), ray.get_start()); - if(x>0) - return contains(ray.get_start()); - else - return contains(ray.get_start()-ray.get_direction()*x); -} - template inline unsigned HyperSphere::get_intersections(const Ray &ray, SurfacePoint *points, unsigned size) const { diff --git a/source/geometry/negation.h b/source/geometry/negation.h index 0fc0196..6746aed 100644 --- a/source/geometry/negation.h +++ b/source/geometry/negation.h @@ -25,7 +25,6 @@ public: virtual HyperBox 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 { return shape->get_max_ray_intersections(); } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; }; @@ -54,12 +53,6 @@ inline bool Negation::contains(const LinAl::Vector &point) const return !shape->contains(point); } -template -inline bool Negation::check_intersection(const Ray &ray) const -{ - return get_intersections(ray, 0, 1); -} - template inline unsigned Negation::get_intersections(const Ray &ray, SurfacePoint *points, unsigned size) const { diff --git a/source/geometry/shape.h b/source/geometry/shape.h index 3f52613..7b1d190 100644 --- a/source/geometry/shape.h +++ b/source/geometry/shape.h @@ -33,14 +33,20 @@ public: virtual HyperBox get_axis_aligned_bounding_box() const = 0; virtual bool contains(const LinAl::Vector &) const = 0; - virtual bool check_intersection(const Ray &) const = 0; + bool check_intersection(const Ray &) const; virtual unsigned get_max_ray_intersections() const = 0; virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const = 0; std::vector > get_intersections(const Ray &) const; }; template -std::vector > Shape::get_intersections(const Ray &ray) const +inline bool Shape::check_intersection(const Ray &ray) const +{ + return get_intersections(ray, 0, 1); +} + +template +inline std::vector > Shape::get_intersections(const Ray &ray) const { unsigned max_isect = get_max_ray_intersections(); std::vector > points(max_isect); diff --git a/source/geometry/transformedshape.h b/source/geometry/transformedshape.h index 5a5cf11..02587bf 100644 --- a/source/geometry/transformedshape.h +++ b/source/geometry/transformedshape.h @@ -35,7 +35,6 @@ public: private: Ray make_local_ray(const Ray &) const; public: - virtual bool check_intersection(const Ray &) const; virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; }; @@ -96,12 +95,6 @@ inline Ray TransformedShape::make_local_ray(const Ray &ray) co return Ray(inverse_trans.transform(ray.get_start()), local_dir, ray.get_limit()*distortion); } -template -inline bool TransformedShape::check_intersection(const Ray &ray) const -{ - return shape->check_intersection(make_local_ray(ray)); -} - template inline unsigned TransformedShape::get_intersections(const Ray &ray, SurfacePoint *points, unsigned size) const {