]> git.tdb.fi Git - libs/math.git/commitdiff
Add an empty flag to BoundingSphere
authorMikko Rasa <tdb@tdb.fi>
Wed, 15 Oct 2014 21:04:53 +0000 (00:04 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 15 Oct 2014 21:04:53 +0000 (00:04 +0300)
Useful for distinguishing a bounding sphere that has nothing in it from
one that contains a single point.

source/geometry/boundingsphere.h

index 40826ca806915e529442c824bc290ce7140da819..dd20d96b6a4b701206f9e27b56a16cfc1402c158 100644 (file)
@@ -13,9 +13,10 @@ class BoundingSphere
 private:
        LinAl::Vector<T, N> center;
        T radius;
+       bool empty;
 
 public:
-       BoundingSphere(): radius(0) { }
+       BoundingSphere();
        BoundingSphere(const LinAl::Vector<T, N> &, T);
 
        template<typename Iter>
@@ -23,12 +24,20 @@ public:
 
        const LinAl::Vector<T, N> get_center() const { return center; }
        T get_radius() const { return radius; }
+       bool is_empty() const { return empty; }
 };
 
+template<typename T, unsigned N>
+BoundingSphere<T, N>::BoundingSphere():
+       radius(0),
+       empty(true)
+{ }
+
 template<typename T, unsigned N>
 BoundingSphere<T, N>::BoundingSphere(const LinAl::Vector<T, N> &c, T r):
        center(c),
-       radius(r)
+       radius(r),
+       empty(false)
 { }
 
 template<typename T, unsigned N>
@@ -37,6 +46,9 @@ BoundingSphere<T, N> BoundingSphere<T, N>::from_point_cloud(const Iter &begin, c
 {
        using std::sqrt;
 
+       if(begin==end)
+               return BoundingSphere<T, N>();
+
        LinAl::Vector<T, N> p1;
        T sqdist = 0;
        for(Iter i=begin; i!=end; ++i)
@@ -83,6 +95,11 @@ BoundingSphere<T, N> BoundingSphere<T, N>::from_point_cloud(const Iter &begin, c
 template<typename T, unsigned N>
 BoundingSphere<T, N> operator|(const BoundingSphere<T, N> &bs1, const BoundingSphere<T, N> &bs2)
 {
+       if(bs1.is_empty())
+               return bs2;
+       else if(bs2.is_empty())
+               return bs1;
+
        LinAl::Vector<T, N> v = bs2.get_center()-bs1.get_center();
        T d = v.norm();
        if(d+bs2.get_radius()<bs1.get_radius())