Skip to content

Commit 137f750

Browse files
author
Nathanael Perraudin
committed
add flann library
1 parent ecc2a4d commit 137f750

File tree

119 files changed

+29839
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+29839
-0
lines changed

3rdparty/flann/flann_build_index.m

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
function [index, params, speedup] = flann_build_index(dataset, build_params)
2+
%FLANN_BUILD_INDEX Builds an index for fast approximate nearest neighbors search
3+
%
4+
% [index, params, speedup] = flann_build_index(dataset, build_params) - Constructs the
5+
% index from the provided 'dataset' and (optionally) computes the optimal parameters.
6+
%
7+
% Url: http://lts2research.epfl.ch/gsp/doc/3rdparty/flann/flann_build_index.php
8+
9+
% Copyright (C) 2013-2014 Nathanael Perraudin, Johan Paratte, David I Shuman.
10+
% This file is part of GSPbox version 0.4.0
11+
%
12+
% This program is free software: you can redistribute it and/or modify
13+
% it under the terms of the GNU General Public License as published by
14+
% the Free Software Foundation, either version 3 of the License, or
15+
% (at your option) any later version.
16+
%
17+
% This program is distributed in the hope that it will be useful,
18+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
% GNU General Public License for more details.
21+
%
22+
% You should have received a copy of the GNU General Public License
23+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
25+
% If you use this toolbox please kindly cite
26+
% N. Perraudin, J. Paratte, D. Shuman, V. Kalofolias, P. Vandergheynst,
27+
% and D. K. Hammond. GSPBOX: A toolbox for signal processing on graphs.
28+
% ArXiv e-prints, Aug. 2014.
29+
% http://arxiv.org/abs/1408.5781
30+
31+
% Marius Muja, January 2008
32+
33+
algos = struct( 'linear', 0, 'kdtree', 1, 'kmeans', 2, 'composite', 3, 'kdtree_single', 4, 'hierarchical', 5, 'lsh', 6, 'saved', 254, 'autotuned', 255 );
34+
center_algos = struct('random', 0, 'gonzales', 1, 'kmeanspp', 2 );
35+
log_levels = struct('none', 0, 'fatal', 1, 'error', 2, 'warning', 3, 'info', 4);
36+
function value = id2value(map, id)
37+
fields = fieldnames(map);
38+
for i = 1:length(fields),
39+
val = cell2mat(fields(i));
40+
if map.(val) == id
41+
value = val;
42+
break;
43+
end
44+
end
45+
end
46+
function id = value2id(map,value)
47+
id = map.(value);
48+
end
49+
50+
default_params = struct('algorithm', 'kdtree' ,'checks', 32, 'eps', 0.0, 'sorted', 1, 'max_neighbors', -1, 'cores', 1, 'trees', 4, 'branching', 32, 'iterations', 5, 'centers_init', 'random', 'cb_index', 0.4, 'target_precision', 0.9,'build_weight', 0.01, 'memory_weight', 0, 'sample_fraction', 0.1, 'log_level', 'warning', 'random_seed', 0);
51+
52+
if ~isstruct(build_params)
53+
error('The "build_params" argument must be a structure');
54+
end
55+
56+
params = default_params;
57+
fn = fieldnames(build_params);
58+
for i = [1:length(fn)],
59+
name = cell2mat(fn(i));
60+
params.(name) = build_params.(name);
61+
end
62+
if ~isnumeric(params.algorithm),
63+
params.algorithm = value2id(algos,params.algorithm);
64+
end
65+
if ~isnumeric(params.centers_init),
66+
params.centers_init = value2id(center_algos,params.centers_init);
67+
end
68+
if ~isnumeric(params.log_level),
69+
params.log_level = value2id(log_levels,params.log_level);
70+
end
71+
72+
[index, params, speedup] = nearest_neighbors('build_index',dataset, params);
73+
74+
if isnumeric(params.algorithm),
75+
params.algorithm = id2value(algos,params.algorithm);
76+
end
77+
if isnumeric(params.centers_init),
78+
params.centers_init = id2value(center_algos,params.centers_init);
79+
end
80+
end
81+

3rdparty/flann/flann_free_index.m

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
%Copyright 2008-2009 Marius Muja ([email protected]). All rights reserved.
2+
%Copyright 2008-2009 David G. Lowe ([email protected]). All rights reserved.
3+
%
4+
%THE BSD LICENSE
5+
%
6+
%Redistribution and use in source and binary forms, with or without
7+
%modification, are permitted provided that the following conditions
8+
%are met:
9+
%
10+
%1. Redistributions of source code must retain the above copyright
11+
% notice, this list of conditions and the following disclaimer.
12+
%2. Redistributions in binary form must reproduce the above copyright
13+
% notice, this list of conditions and the following disclaimer in the
14+
% documentation and/or other materials provided with the distribution.
15+
%
16+
%THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
17+
%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
%
27+
% Url: http://lts2research.epfl.ch/gsp/doc/3rdparty/flann/flann_free_index.php
28+
29+
% Copyright (C) 2013-2014 Nathanael Perraudin, Johan Paratte, David I Shuman.
30+
% This file is part of GSPbox version 0.4.0
31+
%
32+
% This program is free software: you can redistribute it and/or modify
33+
% it under the terms of the GNU General Public License as published by
34+
% the Free Software Foundation, either version 3 of the License, or
35+
% (at your option) any later version.
36+
%
37+
% This program is distributed in the hope that it will be useful,
38+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
39+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40+
% GNU General Public License for more details.
41+
%
42+
% You should have received a copy of the GNU General Public License
43+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
44+
45+
% If you use this toolbox please kindly cite
46+
% N. Perraudin, J. Paratte, D. Shuman, V. Kalofolias, P. Vandergheynst,
47+
% and D. K. Hammond. GSPBOX: A toolbox for signal processing on graphs.
48+
% ArXiv e-prints, Aug. 2014.
49+
% http://arxiv.org/abs/1408.5781
50+
51+
function flann_free_index(index_id)
52+
%FLANN_FREE_INDEX Deletes the nearest-neighbors index
53+
%
54+
% Deletes an index constructed using flann_build_index.
55+
56+
% Marius Muja, January 2008
57+
58+
nearest_neighbors('free_index',index_id);
59+
end
60+

3rdparty/flann/flann_load_index.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
%Copyright 2008-2009 Marius Muja ([email protected]). All rights reserved.
2+
%Copyright 2008-2009 David G. Lowe ([email protected]). All rights reserved.
3+
%
4+
%THE BSD LICENSE
5+
%
6+
%Redistribution and use in source and binary forms, with or without
7+
%modification, are permitted provided that the following conditions
8+
%are met:
9+
%
10+
%1. Redistributions of source code must retain the above copyright
11+
% notice, this list of conditions and the following disclaimer.
12+
%2. Redistributions in binary form must reproduce the above copyright
13+
% notice, this list of conditions and the following disclaimer in the
14+
% documentation and/or other materials provided with the distribution.
15+
%
16+
%THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
17+
%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
%
27+
% Url: http://lts2research.epfl.ch/gsp/doc/3rdparty/flann/flann_load_index.php
28+
29+
% Copyright (C) 2013-2014 Nathanael Perraudin, Johan Paratte, David I Shuman.
30+
% This file is part of GSPbox version 0.4.0
31+
%
32+
% This program is free software: you can redistribute it and/or modify
33+
% it under the terms of the GNU General Public License as published by
34+
% the Free Software Foundation, either version 3 of the License, or
35+
% (at your option) any later version.
36+
%
37+
% This program is distributed in the hope that it will be useful,
38+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
39+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40+
% GNU General Public License for more details.
41+
%
42+
% You should have received a copy of the GNU General Public License
43+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
44+
45+
% If you use this toolbox please kindly cite
46+
% N. Perraudin, J. Paratte, D. Shuman, V. Kalofolias, P. Vandergheynst,
47+
% and D. K. Hammond. GSPBOX: A toolbox for signal processing on graphs.
48+
% ArXiv e-prints, Aug. 2014.
49+
% http://arxiv.org/abs/1408.5781
50+
51+
function index = flann_load_index(filename, dataset)
52+
%FLANN_LOAD_INDEX Loads an index from disk
53+
%
54+
% Marius Muja, March 2009
55+
56+
index = nearest_neighbors('load_index', filename, dataset);
57+
end
58+

3rdparty/flann/flann_save_index.m

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
%Copyright 2008-2009 Marius Muja ([email protected]). All rights reserved.
2+
%Copyright 2008-2009 David G. Lowe ([email protected]). All rights reserved.
3+
%
4+
%THE BSD LICENSE
5+
%
6+
%Redistribution and use in source and binary forms, with or without
7+
%modification, are permitted provided that the following conditions
8+
%are met:
9+
%
10+
%1. Redistributions of source code must retain the above copyright
11+
% notice, this list of conditions and the following disclaimer.
12+
%2. Redistributions in binary form must reproduce the above copyright
13+
% notice, this list of conditions and the following disclaimer in the
14+
% documentation and/or other materials provided with the distribution.
15+
%
16+
%THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
17+
%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
%
27+
% Url: http://lts2research.epfl.ch/gsp/doc/3rdparty/flann/flann_save_index.php
28+
29+
% Copyright (C) 2013-2014 Nathanael Perraudin, Johan Paratte, David I Shuman.
30+
% This file is part of GSPbox version 0.4.0
31+
%
32+
% This program is free software: you can redistribute it and/or modify
33+
% it under the terms of the GNU General Public License as published by
34+
% the Free Software Foundation, either version 3 of the License, or
35+
% (at your option) any later version.
36+
%
37+
% This program is distributed in the hope that it will be useful,
38+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
39+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40+
% GNU General Public License for more details.
41+
%
42+
% You should have received a copy of the GNU General Public License
43+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
44+
45+
% If you use this toolbox please kindly cite
46+
% N. Perraudin, J. Paratte, D. Shuman, V. Kalofolias, P. Vandergheynst,
47+
% and D. K. Hammond. GSPBOX: A toolbox for signal processing on graphs.
48+
% ArXiv e-prints, Aug. 2014.
49+
% http://arxiv.org/abs/1408.5781
50+
51+
function flann_save_index(index_id, filename)
52+
%FLANN_SAVE_INDEX Saves an index to disk
53+
%
54+
55+
% Marius Muja, March 2010
56+
57+
nearest_neighbors('save_index',index_id, filename);
58+
end
59+

3rdparty/flann/flann_search.m

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
%Copyright 2008-2009 Marius Muja ([email protected]). All rights reserved.
2+
%Copyright 2008-2009 David G. Lowe ([email protected]). All rights reserved.
3+
%
4+
%THE BSD LICENSE
5+
%
6+
%Redistribution and use in source and binary forms, with or without
7+
%modification, are permitted provided that the following conditions
8+
%are met:
9+
%
10+
%1. Redistributions of source code must retain the above copyright
11+
% notice, this list of conditions and the following disclaimer.
12+
%2. Redistributions in binary form must reproduce the above copyright
13+
% notice, this list of conditions and the following disclaimer in the
14+
% documentation and/or other materials provided with the distribution.
15+
%
16+
%THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
17+
%IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18+
%OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19+
%IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
%INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21+
%NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22+
%DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
%THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
%(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25+
%THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
%
27+
% Url: http://lts2research.epfl.ch/gsp/doc/3rdparty/flann/flann_search.php
28+
29+
% Copyright (C) 2013-2014 Nathanael Perraudin, Johan Paratte, David I Shuman.
30+
% This file is part of GSPbox version 0.4.0
31+
%
32+
% This program is free software: you can redistribute it and/or modify
33+
% it under the terms of the GNU General Public License as published by
34+
% the Free Software Foundation, either version 3 of the License, or
35+
% (at your option) any later version.
36+
%
37+
% This program is distributed in the hope that it will be useful,
38+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
39+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40+
% GNU General Public License for more details.
41+
%
42+
% You should have received a copy of the GNU General Public License
43+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
44+
45+
% If you use this toolbox please kindly cite
46+
% N. Perraudin, J. Paratte, D. Shuman, V. Kalofolias, P. Vandergheynst,
47+
% and D. K. Hammond. GSPBOX: A toolbox for signal processing on graphs.
48+
% ArXiv e-prints, Aug. 2014.
49+
% http://arxiv.org/abs/1408.5781
50+
51+
function [indices, dists] = flann_search(data, testset, n, search_params)
52+
%NN_SEARCH Fast approximate nearest neighbors search
53+
%
54+
% Performs a fast approximate nearest neighbor search using an
55+
% index constructed using flann_build_index or directly a
56+
% dataset.
57+
58+
% Marius Muja, January 2008
59+
60+
61+
algos = struct( 'linear', 0, 'kdtree', 1, 'kmeans', 2, 'composite', 3, 'saved', 254, 'autotuned', 255 );
62+
center_algos = struct('random', 0, 'gonzales', 1, 'kmeanspp', 2 );
63+
log_levels = struct('none', 0, 'fatal', 1, 'error', 2, 'warning', 3, 'info', 4);
64+
function value = id2value(map, id)
65+
fields = fieldnames(map);
66+
for i = 1:length(fields),
67+
val = cell2mat(fields(i));
68+
if map.(val) == id
69+
value = val;
70+
break;
71+
end
72+
end
73+
end
74+
function id = value2id(map,value)
75+
id = map.(value);
76+
end
77+
78+
default_params = struct('algorithm', 'kdtree' ,'checks', 32, 'eps', 0.0, 'sorted', 1, 'max_neighbors', -1, 'cores', 1, 'trees', 4, 'branching', 32, 'iterations', 5, 'centers_init', 'random', 'cb_index', 0.4, 'target_precision', 0.9,'build_weight', 0.01, 'memory_weight', 0, 'sample_fraction', 0.1, 'log_level', 'warning', 'random_seed', 0);
79+
80+
81+
if ~isstruct(search_params)
82+
error('The "search_params" argument must be a structure');
83+
end
84+
85+
params = default_params;
86+
fn = fieldnames(search_params);
87+
for i = [1:length(fn)],
88+
name = cell2mat(fn(i));
89+
params.(name) = search_params.(name);
90+
end
91+
if ~isnumeric(params.algorithm),
92+
params.algorithm = value2id(algos,params.algorithm);
93+
end
94+
if ~isnumeric(params.centers_init),
95+
params.centers_init = value2id(center_algos,params.centers_init);
96+
end
97+
if ~isnumeric(params.log_level),
98+
params.log_level = value2id(log_levels,params.log_level);
99+
end
100+
101+
if (size(data,1)==1 && size(data,2)==1)
102+
% we already have an index
103+
[indices,dists] = nearest_neighbors('index_find_nn', data, testset, n, params);
104+
else
105+
% create the index and search
106+
[indices,dists] = nearest_neighbors('find_nn', data, testset, n, params);
107+
end
108+
end
109+

0 commit comments

Comments
 (0)