Moved PairwiseDistances and ExplicitDistances into include/geometry/distances.h dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Thu, 14 May 2009 14:04:43 -0700
branchdev
changeset 136 beff535b53ff
parent 135 fc8ebbae62e2
child 137 069596c71902
child 150 eb629d8f00bf
Moved PairwiseDistances and ExplicitDistances into include/geometry/distances.h
examples/cohomology/rips-pairwise-cohomology.cpp
examples/consistency/rips-consistency-zigzag.cpp
examples/fitness/avida-rips-distance.cpp
examples/rips/rips-image-zigzag.cpp
examples/rips/rips-pairwise.cpp
examples/rips/rips-zigzag.cpp
include/geometry/distances.h
include/geometry/distances.hpp
include/topology/rips.h
include/topology/rips.hpp
--- a/examples/cohomology/rips-pairwise-cohomology.cpp	Thu May 14 13:09:06 2009 -0700
+++ b/examples/cohomology/rips-pairwise-cohomology.cpp	Thu May 14 14:04:43 2009 -0700
@@ -2,6 +2,7 @@
 #include <topology/rips.h>
 
 #include <geometry/l2distance.h>
+#include <geometry/distances.h>
 
 #include <utilities/containers.h>           // for BackInsertFunctor
 #include <utilities/timer.h>
--- a/examples/consistency/rips-consistency-zigzag.cpp	Thu May 14 13:09:06 2009 -0700
+++ b/examples/consistency/rips-consistency-zigzag.cpp	Thu May 14 14:04:43 2009 -0700
@@ -2,6 +2,7 @@
 #include <topology/zigzag-persistence.h>
 
 #include <geometry/l2distance.h>    // Point, PointContainer, L2DistanceType, read_points
+#include <geometry/distances.h>
 
 #include <utilities/types.h>
 #include <utilities/containers.h>
--- a/examples/fitness/avida-rips-distance.cpp	Thu May 14 13:09:06 2009 -0700
+++ b/examples/fitness/avida-rips-distance.cpp	Thu May 14 14:04:43 2009 -0700
@@ -7,6 +7,8 @@
 #include <topology/rips.h>
 #include <topology/static-persistence.h>
 
+#include <geometry/distances.h>     // for ExplicitDistances
+
 
 typedef         ExplicitDistances<AvidaPopulationDetail>            ExplicitDist;
 typedef         Rips<ExplicitDist>                                  RipsGen;
--- a/examples/rips/rips-image-zigzag.cpp	Thu May 14 13:09:06 2009 -0700
+++ b/examples/rips/rips-image-zigzag.cpp	Thu May 14 14:04:43 2009 -0700
@@ -8,6 +8,7 @@
 #include <utilities/timer.h>
 
 #include <geometry/l2distance.h>    // for L2Distance and read_points()
+#include <geometry/distances.h>
 
 #include <map>
 #include <cmath>
--- a/examples/rips/rips-pairwise.cpp	Thu May 14 13:09:06 2009 -0700
+++ b/examples/rips/rips-pairwise.cpp	Thu May 14 14:04:43 2009 -0700
@@ -5,6 +5,7 @@
 #include <topology/persistence-diagram.h>
 
 #include <geometry/l2distance.h>
+#include <geometry/distances.h>
 
 #include <utilities/containers.h>           // for BackInsertFunctor
 #include <utilities/timer.h>
--- a/examples/rips/rips-zigzag.cpp	Thu May 14 13:09:06 2009 -0700
+++ b/examples/rips/rips-zigzag.cpp	Thu May 14 14:04:43 2009 -0700
@@ -4,6 +4,7 @@
 #include <utilities/containers.h>
 
 #include <geometry/l2distance.h>    // Point, PointContainer, L2DistanceType, read_points
+#include <geometry/distances.h>
 
 #include <utilities/log.h>
 #include <utilities/memory.h>       // for report_memory()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/geometry/distances.h	Thu May 14 14:04:43 2009 -0700
@@ -0,0 +1,69 @@
+#ifndef __DISTANCES_H__
+#define __DISTANCES_H__
+
+/**
+ * Class: ExplicitDistances 
+ * Stores the pairwise distances of Distances_ instance passed at construction. 
+ * It's a protypical Distances template argument for the Rips complex.
+ */
+template<class Distances_>
+class ExplicitDistances
+{
+    public:
+        typedef             Distances_                                      Distances;
+        typedef             size_t                                          IndexType;
+        typedef             typename Distances::DistanceType                DistanceType;
+
+                            ExplicitDistances(IndexType size):
+                                size_(size), 
+                                distances_(size*(size + 1)/2 + size)        {}
+                            ExplicitDistances(const Distances& distances);
+
+        DistanceType        operator()(IndexType a, IndexType b) const;
+        DistanceType&       operator()(IndexType a, IndexType b);
+
+        size_t              size() const                                    { return size_; }
+        IndexType           begin() const                                   { return 0; }
+        IndexType           end() const                                     { return size(); }
+
+    private:
+        std::vector<DistanceType>                   distances_;
+        size_t                                      size_;
+};
+
+
+/**
+ * Class: PairwiseDistances
+ * Given a Container_ of points and a Distance_, it computes distances between elements 
+ * in the container (given as instances of Index_ defaulted to unsigned) using the Distance_ functor.
+ *
+ * Container_ is assumed to be an std::vector. That simplifies a number of things.
+ */
+template<class Container_, class Distance_, typename Index_ = unsigned>
+class PairwiseDistances
+{
+    public:
+        typedef             Container_                                      Container;
+        typedef             Distance_                                       Distance;
+        typedef             Index_                                          IndexType;
+        typedef             typename Distance::result_type                  DistanceType;
+
+
+                            PairwiseDistances(const Container& container, 
+                                              const Distance& distance = Distance()):
+                                container_(container), distance_(distance)  {}
+
+        DistanceType        operator()(IndexType a, IndexType b) const      { return distance_(container_[a], container_[b]); }
+
+        size_t              size() const                                    { return container_.size(); }
+        IndexType           begin() const                                   { return 0; }
+        IndexType           end() const                                     { return size(); }
+
+    private:
+        const Container&    container_;
+        Distance            distance_;
+};
+
+#include "distances.hpp"
+
+#endif // __DISTANCES_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/geometry/distances.hpp	Thu May 14 14:04:43 2009 -0700
@@ -0,0 +1,30 @@
+template<class Distances_>
+ExplicitDistances<Distances_>::
+ExplicitDistances(const Distances& distances): 
+    size_(distances.size()), distances_((distances.size() * (distances.size() + 1))/2)
+{
+    IndexType i = 0;
+    for (typename Distances::IndexType a = distances.begin(); a != distances.end(); ++a)
+        for (typename Distances::IndexType b = a; b != distances.end(); ++b)
+        {
+            distances_[i++] = distances(a,b);
+        }
+}
+
+template<class Distances_>
+typename ExplicitDistances<Distances_>::DistanceType
+ExplicitDistances<Distances_>::
+operator()(IndexType a, IndexType  b) const
+{
+    if (a > b) std::swap(a,b);
+    return distances_[a*size_ - ((a*(a-1))/2) + (b-a)];
+}
+
+template<class Distances_>
+typename ExplicitDistances<Distances_>::DistanceType&
+ExplicitDistances<Distances_>::
+operator()(IndexType a, IndexType  b)
+{
+    if (a > b) std::swap(a,b);
+    return distances_[a*size_ - ((a*(a-1))/2) + (b-a)];
+}
--- a/include/topology/rips.h	Thu May 14 13:09:06 2009 -0700
+++ b/include/topology/rips.h	Thu May 14 14:04:43 2009 -0700
@@ -171,65 +171,6 @@
 };
 
 
-/**
- * Class: ExplicitDistances 
- * Stores the pairwise distances of Distances_ instance passed at construction. 
- * It's a protypical Distances template argument for the Rips complex.
- */
-template<class Distances_>
-class ExplicitDistances
-{
-    public:
-        typedef             Distances_                                      Distances;
-        typedef             size_t                                          IndexType;
-        typedef             typename Distances::DistanceType                DistanceType;
-
-                            ExplicitDistances(const Distances& distances);
-
-        DistanceType        operator()(IndexType a, IndexType b) const;
-
-        size_t              size() const                                    { return size_; }
-        IndexType           begin() const                                   { return 0; }
-        IndexType           end() const                                     { return size(); }
-
-    private:
-        std::vector<DistanceType>                   distances_;
-        size_t                                      size_;
-};
-
-
-/**
- * Class: PairwiseDistances
- * Given a Container_ of points and a Distance_, it computes distances between elements 
- * in the container (given as instances of Index_ defaulted to unsigned) using the Distance_ functor.
- *
- * Container_ is assumed to be an std::vector. That simplifies a number of things.
- */
-template<class Container_, class Distance_, typename Index_ = unsigned>
-class PairwiseDistances
-{
-    public:
-        typedef             Container_                                      Container;
-        typedef             Distance_                                       Distance;
-        typedef             Index_                                          IndexType;
-        typedef             typename Distance::result_type                  DistanceType;
-
-
-                            PairwiseDistances(const Container& container, 
-                                              const Distance& distance = Distance()):
-                                container_(container), distance_(distance)  {}
-
-        DistanceType        operator()(IndexType a, IndexType b) const      { return distance_(container_[a], container_[b]); }
-
-        size_t              size() const                                    { return container_.size(); }
-        IndexType           begin() const                                   { return 0; }
-        IndexType           end() const                                     { return size(); }
-
-    private:
-        const Container&    container_;
-        Distance            distance_;
-};
-
 #include "rips.hpp"
 
 #endif // __RIPS_H__
--- a/include/topology/rips.hpp	Thu May 14 13:09:06 2009 -0700
+++ b/include/topology/rips.hpp	Thu May 14 14:04:43 2009 -0700
@@ -195,25 +195,3 @@
             mx = std::max(mx, distances_(*a,*b));
     return mx;
 }
-
-template<class Distances_>
-ExplicitDistances<Distances_>::
-ExplicitDistances(const Distances& distances): 
-    size_(distances.size()), distances_((distances.size() * (distances.size() + 1))/2)
-{
-    IndexType i = 0;
-    for (typename Distances::IndexType a = distances.begin(); a != distances.end(); ++a)
-        for (typename Distances::IndexType b = a; b != distances.end(); ++b)
-        {
-            distances_[i++] = distances(a,b);
-        }
-}
-
-template<class Distances_>
-typename ExplicitDistances<Distances_>::DistanceType
-ExplicitDistances<Distances_>::
-operator()(IndexType a, IndexType  b) const
-{
-    if (a > b) std::swap(a,b);
-    return distances_[a*size_ - ((a*(a-1))/2) + (b-a)];
-}