Skip to content

Commit e5b9cbe

Browse files
author
Marius Muja
committed
Merge branch 'master' of https://github.com/mariusmuja/flann
2 parents 209190e + b32acec commit e5b9cbe

File tree

8 files changed

+94
-17
lines changed

8 files changed

+94
-17
lines changed

CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ option(BUILD_PYTHON_BINDINGS "Build Python bindings" ON)
5454
option(BUILD_MATLAB_BINDINGS "Build Matlab bindings" ON)
5555
option(BUILD_CUDA_LIB "Build CUDA library" OFF)
5656
option(BUILD_EXAMPLES "Build examples" ON)
57+
option(BUILD_TESTS "Build tests" ON)
58+
option(BUILD_DOC "Build documentation" ON)
5759
option(USE_OPENMP "Use OpenMP multi-threading" ON)
5860
option(USE_MPI "Use MPI" OFF)
5961

@@ -158,8 +160,12 @@ add_subdirectory( src )
158160
if (BUILD_EXAMPLES)
159161
add_subdirectory( examples )
160162
endif(BUILD_EXAMPLES)
161-
add_subdirectory( test )
162-
add_subdirectory( doc )
163+
if (BUILD_TESTS)
164+
add_subdirectory( test )
165+
endif (BUILD_TESTS)
166+
if (BUILD_DOC)
167+
add_subdirectory( doc )
168+
endif (BUILD_DOC)
163169

164170

165171
# CPACK options
@@ -202,6 +208,9 @@ include(CPack)
202208
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}")
203209
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
204210
message(STATUS "Building C bindings: ${BUILD_C_BINDINGS}")
211+
message(STATUS "Building examples: ${BUILD_EXAMPLES}")
212+
message(STATUS "Building tests: ${BUILD_TESTS}")
213+
message(STATUS "Building documentation: ${BUILD_DOC}")
205214
message(STATUS "Building python bindings: ${BUILD_PYTHON_BINDINGS}")
206215
message(STATUS "Building matlab bindings: ${BUILD_MATLAB_BINDINGS}")
207216
message(STATUS "Building CUDA library: ${BUILD_CUDA_LIB}")

cmake/flann_utils.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
macro(GET_OS_INFO)
22
string(REGEX MATCH "Linux" OS_IS_LINUX ${CMAKE_SYSTEM_NAME})
3-
set(FLANN_LIB_INSTALL_DIR "lib")
3+
set(FLANN_LIB_INSTALL_DIR "lib${LIB_SUFFIX}")
44
set(FLANN_INCLUDE_INSTALL_DIR
55
"include/${PROJECT_NAME_LOWER}-${FLANN_MAJOR_VERSION}.${FLANN_MINOR_VERSION}")
66
endmacro(GET_OS_INFO)

doc/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
find_package(LATEX)
2+
3+
if (NOT DOCDIR)
4+
set(DOCDIR share/doc/flann)
5+
endif ()
26

37
if (EXISTS ${PDFLATEX_COMPILER} AND EXISTS ${BIBTEX_COMPILER})
48
include(${PROJECT_SOURCE_DIR}/cmake/UseLATEX.cmake)
@@ -14,6 +18,6 @@ endif()
1418

1519
install(
1620
FILES manual.pdf
17-
DESTINATION share/doc/flann
21+
DESTINATION ${DOCDIR}
1822
OPTIONAL
1923
)

doc/manual.tex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,6 @@ \subsubsection{flann\_find\_nearest\_neighbors\_index()}
10111011
int* indices,
10121012
float* dists,
10131013
int nn,
1014-
int checks,
10151014
struct FLANNParameters* flann_params);
10161015
\end{Verbatim}
10171016
This function searches for the nearest neighbors of the
@@ -1024,8 +1023,7 @@ \subsubsection{flann\_find\_nearest\_neighbors\_index()}
10241023
row-major format). The memory for the \texttt{result} matrix must be allocated
10251024
before the \texttt{flann\_find\_nearest\_neighbors\_index()} function is
10261025
called. The distances to the nearest neighbors found are stored in the \texttt{dists}
1027-
matrix. The \texttt{checks} parameter specifies how many tree traversals should
1028-
be performed during the search.
1026+
matrix.
10291027

10301028

10311029

src/cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ endif()
1515
set_property(TARGET flann_cpp_s PROPERTY COMPILE_DEFINITIONS FLANN_STATIC FLANN_USE_CUDA)
1616

1717
if (BUILD_CUDA_LIB)
18-
SET(CUDA_NVCC_FLAGS -DFLANN_USE_CUDA)
18+
SET(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-DFLANN_USE_CUDA")
1919
if(CMAKE_COMPILER_IS_GNUCC)
2020
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-Xcompiler;-fPIC;-arch=sm_13" )
2121
if (NVCC_COMPILER_BINDIR)

src/cpp/flann/algorithms/center_chooser.h

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,63 @@
1313
namespace flann
1414
{
1515

16+
template <typename Distance, typename ElementType>
17+
struct squareDistance
18+
{
19+
typedef typename Distance::ResultType ResultType;
20+
ResultType operator()( ResultType dist ) { return dist*dist; }
21+
};
22+
23+
24+
template <typename ElementType>
25+
struct squareDistance<L2_Simple<ElementType>, ElementType>
26+
{
27+
typedef typename L2_Simple<ElementType>::ResultType ResultType;
28+
ResultType operator()( ResultType dist ) { return dist; }
29+
};
30+
31+
template <typename ElementType>
32+
struct squareDistance<L2_3D<ElementType>, ElementType>
33+
{
34+
typedef typename L2_3D<ElementType>::ResultType ResultType;
35+
ResultType operator()( ResultType dist ) { return dist; }
36+
};
37+
38+
template <typename ElementType>
39+
struct squareDistance<L2<ElementType>, ElementType>
40+
{
41+
typedef typename L2<ElementType>::ResultType ResultType;
42+
ResultType operator()( ResultType dist ) { return dist; }
43+
};
44+
45+
46+
template <typename ElementType>
47+
struct squareDistance<HellingerDistance<ElementType>, ElementType>
48+
{
49+
typedef typename HellingerDistance<ElementType>::ResultType ResultType;
50+
ResultType operator()( ResultType dist ) { return dist; }
51+
};
52+
53+
54+
template <typename ElementType>
55+
struct squareDistance<ChiSquareDistance<ElementType>, ElementType>
56+
{
57+
typedef typename ChiSquareDistance<ElementType>::ResultType ResultType;
58+
ResultType operator()( ResultType dist ) { return dist; }
59+
};
60+
61+
62+
template <typename Distance>
63+
typename Distance::ResultType ensureSquareDistance( typename Distance::ResultType dist )
64+
{
65+
typedef typename Distance::ElementType ElementType;
66+
67+
squareDistance<Distance, ElementType> dummy;
68+
return dummy( dist );
69+
}
70+
71+
72+
1673
template <typename Distance>
1774
class CenterChooser
1875
{
@@ -176,8 +233,11 @@ class KMeansppCenterChooser : public CenterChooser<Distance>
176233
assert(index >=0 && index < n);
177234
centers[0] = indices[index];
178235

236+
// Computing distance^2 will have the advantage of even higher probability further to pick new centers
237+
// far from previous centers (and this complies to "k-means++: the advantages of careful seeding" article)
179238
for (int i = 0; i < n; i++) {
180239
closestDistSq[i] = distance_(points_[indices[i]], points_[indices[index]], cols_);
240+
closestDistSq[i] = ensureSquareDistance<Distance>( closestDistSq[i] );
181241
currentPot += closestDistSq[i];
182242
}
183243

@@ -203,7 +263,10 @@ class KMeansppCenterChooser : public CenterChooser<Distance>
203263

204264
// Compute the new potential
205265
double newPot = 0;
206-
for (int i = 0; i < n; i++) newPot += std::min( distance_(points_[indices[i]], points_[indices[index]], cols_), closestDistSq[i] );
266+
for (int i = 0; i < n; i++) {
267+
DistanceType dist = distance_(points_[indices[i]], points_[indices[index]], cols_);
268+
newPot += std::min( ensureSquareDistance<Distance>(dist), closestDistSq[i] );
269+
}
207270

208271
// Store the best result
209272
if ((bestNewPot < 0)||(newPot < bestNewPot)) {
@@ -215,7 +278,10 @@ class KMeansppCenterChooser : public CenterChooser<Distance>
215278
// Add the appropriate center
216279
centers[centerCount] = indices[bestNewIndex];
217280
currentPot = bestNewPot;
218-
for (int i = 0; i < n; i++) closestDistSq[i] = std::min( distance_(points_[indices[i]], points_[indices[bestNewIndex]], cols_), closestDistSq[i] );
281+
for (int i = 0; i < n; i++) {
282+
DistanceType dist = distance_(points_[indices[i]], points_[indices[bestNewIndex]], cols_);
283+
closestDistSq[i] = std::min( ensureSquareDistance<Distance>(dist), closestDistSq[i] );
284+
}
219285
}
220286

221287
centers_length = centerCount;

src/cpp/flann/algorithms/nn_index.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ class NNIndex : public IndexBase
708708
return id;
709709
}
710710
size_t point_index = size_t(-1);
711-
if (ids_[id]==id) {
711+
if (id < ids_.size() && ids_[id]==id) {
712712
return id;
713713
}
714714
else {

src/cpp/flann/util/cuda/result_set.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct SingleResultSet
5454
DistanceType bestDist;
5555
const DistanceType epsError;
5656

57-
__device__
57+
__device__ __host__
5858
SingleResultSet( DistanceType eps ) : bestIndex(-1),bestDist(INFINITY), epsError(eps){ }
5959

6060
__device__
@@ -129,7 +129,7 @@ struct KnnResultSet
129129
const DistanceType epsError;
130130

131131

132-
__device__
132+
__device__ __host__
133133
KnnResultSet(int knn, bool sortResults, DistanceType eps) : foundNeighbors(0),largestHeapDist(INFINITY),k(knn), sorted(sortResults), epsError(eps){ }
134134

135135
// __host__ __device__
@@ -229,7 +229,7 @@ struct CountingRadiusResultSet
229229
DistanceType radius_sq_;
230230
int max_neighbors_;
231231

232-
__device__
232+
__device__ __host__
233233
CountingRadiusResultSet(DistanceType radius, int max_neighbors) : count_(0),radius_sq_(radius), max_neighbors_(max_neighbors){ }
234234

235235
__device__
@@ -279,7 +279,7 @@ struct RadiusKnnResultSet
279279
// int count_;
280280

281281

282-
__device__
282+
__device__ __host__
283283
RadiusKnnResultSet(DistanceType radius, int knn, int* segment_starts, bool sortResults) : foundNeighbors(0),largestHeapDist(radius),k(knn), sorted(sortResults), radius_sq_(radius),segment_starts_(segment_starts) { }
284284

285285
// __host__ __device__
@@ -381,7 +381,7 @@ struct KnnRadiusResultSet
381381
const DistanceType radius_sq;
382382

383383

384-
__device__
384+
__device__ __host__
385385
KnnRadiusResultSet(int knn, bool sortResults, DistanceType eps, DistanceType radius) : foundNeighbors(0),largestHeapDist(radius),k(knn), sorted(sortResults), epsError(eps),radius_sq(radius){ }
386386

387387
// __host__ __device__
@@ -484,7 +484,7 @@ struct RadiusResultSet
484484
int count_;
485485
bool sorted_;
486486

487-
__device__
487+
__device__ __host__
488488
RadiusResultSet(DistanceType radius, int* segment_starts, bool sorted) : radius_sq_(radius), segment_starts_(segment_starts), count_(0), sorted_(sorted){ }
489489

490490
__device__

0 commit comments

Comments
 (0)