summaryrefslogtreecommitdiff
path: root/src/Gudhi_stat/include/gudhi/Hausdorff_distances.h
blob: de39ad7dc9c4b74ee0c4985df7d7ef987a79f1d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*    This file is part of the Gudhi Library. The Gudhi library
 *    (Geometric Understanding in Higher Dimensions) is a generic C++
 *    library for computational topology.
 *
 *    Author(s):       Pawel Dlotko
 *
 *    Copyright (C) 2015  INRIA (France)
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef HAUSDORFF_DISTANCES_H
#define HAUSDORFF_DISTANCES_H

#include <cmath>
#include <limits>
#include <vector>
#include <cstdlib>
#include <iostream>


/**
 * This file contains various implementations of Hausrodff distances between collections of points. It contains various implementations that can be used for specific case.
**/ 

/**
 * The implementation below works for a case of a metric space given by a distance matrix, and a subspace of a metric space. Our task is to find a Hausdorff distance between the subspace and the whole space. 
 * The input is a distance matrix (lower triangular part) and a vector of bools indicating a subspace (elementss set to true belongs to the space, the elements set to false do not). 
**/ 
class Hausdorff_distance_between_subspace_and_the_whole_metric_space
{
public:
	Hausdorff_distance_between_subspace_and_the_whole_metric_space( const std::vector< std::vector<double> >& distance_matrix ):distance_matrix(distance_matrix){}	
	double operator()( const std::vector< bool >& is_subspace  )
	{
		double maximal_distance = -std::numeric_limits<double>::max();
		for ( size_t j = 0 ; j != this->distance_matrix.size() ; ++j )  
		{			
			double minimal_distance = std::numeric_limits<double>::max();
			for ( size_t i = 0 ; i != this->distance_matrix.size() ; ++i )
			{
				if ( !is_subspace[i] )continue;
				double distance = 0;
				if ( i < j )
				{
					distance = this->distance_matrix[i][j];
				}
				else
				{
					if ( i > j )distance = this->distance_matrix[j][i];
				}
				if ( distance > minimal_distance )minimal_distance = distance;
			}		
			if ( maximal_distance < minimal_distance )maximal_distance = minimal_distance;
		}
		return maximal_distance;	
	}//Hausdorff_distance_between_subspace_and_the_whole_metric_space
	
	double operator()( const std::vector< size_t >& subspace , const std::vector< size_t >& space = std::vector< size_t >()  )
	{
		bool dbg = false;
		if ( dbg )
		{
			std::cerr << "Calling double operator()( const std::vector< size_t >& subspace , const std::vector< size_t >& space = std::vector< size_t >()  ) method \n";
			std::cerr << "subspace.size() : " << subspace.size() << std::endl;
		}
		double maximal_distance = -std::numeric_limits<double>::max();
		for ( size_t j = 0 ; j != this->distance_matrix.size() ; ++j )
		{			
			double minimal_distance = std::numeric_limits<double>::max();
			for ( size_t i = 0 ; i != subspace.size() ; ++i )  
			{				
				double distance = 0;
				if ( subspace[i] < j )
				{
					distance = this->distance_matrix[ j ][ subspace[i] ];
				}
				else
				{
					if ( subspace[i] > j )distance = this->distance_matrix[ subspace[i] ][ j ];
				}
				if ( distance < minimal_distance )minimal_distance = distance;
			}
			if ( maximal_distance < minimal_distance )maximal_distance = minimal_distance;
		}
		if ( dbg )
		{
			std::cerr << "maximal_distance : " << maximal_distance << std::endl;
			getchar();
		}
		return maximal_distance;	
	}//Hausdorff_distance_between_subspace_and_the_whole_metric_space
private:
	const std::vector< std::vector<double> >& distance_matrix;
};

template <typename PointType , typename distanceFunction >
std::vector< std::vector<double> > compute_all_to_all_distance_matrix_between_points( const std::vector< PointType >& points )
{
	bool dbg = false;
	std::vector< std::vector<double> > result;
	result.reserve( points.size() );
	distanceFunction f;
	for ( size_t i = 0 ; i != points.size() ; ++i )
	{
		std::vector<double> this_row;
		this_row.reserve( i );
		for ( size_t j = 0  ; j != i ; ++j )
		{
			double distance = f( points[i] , points[j] );
			if ( dbg ){std::cerr << "The distance between point : " << i << " and the point : " << j << " is :" << distance << std::endl;}
			this_row.push_back( distance );
		}
		result.push_back( this_row );
	}
	return result;
}//compute_all_to_all_distance_matrix_between_points

template <typename T>
class identity
{
public:
	T& operator()( T& org )
	{
		return org;
	}
};



#endif