From 7fed55840f8482f8aa961f06f3f8f5b5af2d85c1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 16 Oct 2014 00:04:53 +0300 Subject: [PATCH] Add an empty flag to BoundingSphere Useful for distinguishing a bounding sphere that has nothing in it from one that contains a single point. --- source/geometry/boundingsphere.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/source/geometry/boundingsphere.h b/source/geometry/boundingsphere.h index 40826ca..dd20d96 100644 --- a/source/geometry/boundingsphere.h +++ b/source/geometry/boundingsphere.h @@ -13,9 +13,10 @@ class BoundingSphere private: LinAl::Vector center; T radius; + bool empty; public: - BoundingSphere(): radius(0) { } + BoundingSphere(); BoundingSphere(const LinAl::Vector &, T); template @@ -23,12 +24,20 @@ public: const LinAl::Vector get_center() const { return center; } T get_radius() const { return radius; } + bool is_empty() const { return empty; } }; +template +BoundingSphere::BoundingSphere(): + radius(0), + empty(true) +{ } + template BoundingSphere::BoundingSphere(const LinAl::Vector &c, T r): center(c), - radius(r) + radius(r), + empty(false) { } template @@ -37,6 +46,9 @@ BoundingSphere BoundingSphere::from_point_cloud(const Iter &begin, c { using std::sqrt; + if(begin==end) + return BoundingSphere(); + LinAl::Vector p1; T sqdist = 0; for(Iter i=begin; i!=end; ++i) @@ -83,6 +95,11 @@ BoundingSphere BoundingSphere::from_point_cloud(const Iter &begin, c template BoundingSphere operator|(const BoundingSphere &bs1, const BoundingSphere &bs2) { + if(bs1.is_empty()) + return bs2; + else if(bs2.is_empty()) + return bs1; + LinAl::Vector v = bs2.get_center()-bs1.get_center(); T d = v.norm(); if(d+bs2.get_radius()