From: Mikko Rasa Date: Wed, 15 Oct 2014 21:04:53 +0000 (+0300) Subject: Add an empty flag to BoundingSphere X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7fed55840f8482f8aa961f06f3f8f5b5af2d85c1;p=libs%2Fmath.git Add an empty flag to BoundingSphere Useful for distinguishing a bounding sphere that has nothing in it from one that contains a single point. --- 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()