# HG changeset patch # User Dmitriy Morozov <dmitriy@mrzv.org> # Date 1242335083 25200 # Node ID beff535b53ff7f063d0ebb81cc8f992132edc94e # Parent fc8ebbae62e2fa57718116ff07057ef8c8ba61b0 Moved PairwiseDistances and ExplicitDistances into include/geometry/distances.h diff -r fc8ebbae62e2 -r beff535b53ff examples/cohomology/rips-pairwise-cohomology.cpp --- 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> diff -r fc8ebbae62e2 -r beff535b53ff examples/consistency/rips-consistency-zigzag.cpp --- 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> diff -r fc8ebbae62e2 -r beff535b53ff examples/fitness/avida-rips-distance.cpp --- 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; diff -r fc8ebbae62e2 -r beff535b53ff examples/rips/rips-image-zigzag.cpp --- 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> diff -r fc8ebbae62e2 -r beff535b53ff examples/rips/rips-pairwise.cpp --- 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> diff -r fc8ebbae62e2 -r beff535b53ff examples/rips/rips-zigzag.cpp --- 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() diff -r fc8ebbae62e2 -r beff535b53ff include/geometry/distances.h --- /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__ diff -r fc8ebbae62e2 -r beff535b53ff include/geometry/distances.hpp --- /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)]; +} diff -r fc8ebbae62e2 -r beff535b53ff include/topology/rips.h --- 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__ diff -r fc8ebbae62e2 -r beff535b53ff include/topology/rips.hpp --- 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)]; -}