Skip to content
Browse files

Added unit tests.

  • Loading branch information...
1 parent 4e9b422 commit 8ff9b4e82f21aba70bd23ba901bb42e7e2805895 @stephanemagnenat stephanemagnenat committed
View
2 CMakeLists.txt
@@ -20,6 +20,6 @@ add_library(nabo
install(TARGETS nabo ARCHIVE DESTINATION lib)
install(FILES nabo/nabo.h DESTINATION include/nabo)
-add_subdirectory(benchmarks)
+enable_testing()
add_subdirectory(tests)
View
0 benchmarks/CMakeLists.txt
No changes.
View
4 nabo/kdtree_cpu.cpp
@@ -203,7 +203,7 @@ namespace Nabo
{
// enqueue offside ?
if (offset2 < bestDist && nodes[childLeft(n)].dim != -2)
- queue.push(SearchElement(childLeft(n), dist));
+ queue.push(SearchElement(childLeft(n), offset2));
// continue onside
if (nodes[childRight(n)].dim != -2)
n = childRight(n);
@@ -214,7 +214,7 @@ namespace Nabo
{
// enqueue offside ?
if (offset2 < bestDist && nodes[childRight(n)].dim != -2)
- queue.push(SearchElement(childRight(n), dist));
+ queue.push(SearchElement(childRight(n), offset2));
// continue onside
if (nodes[childLeft(n)].dim != -2)
n = childLeft(n);
View
39 tests/CMakeLists.txt
@@ -1,3 +1,38 @@
include_directories(..)
-add_executable(knn knn.cpp)
-target_link_libraries(knn nabo)
+
+add_executable(knnvalidate knnvalidate.cpp)
+target_link_libraries(knnvalidate nabo)
+
+add_test(validation-2D ${EXECUTABLE_OUTPUT_PATH}/knnvalidate ${CMAKE_CURRENT_SOURCE_DIR}/data/scan.2d.txt 10 1000)
+add_test(validation-3D ${EXECUTABLE_OUTPUT_PATH}/knnvalidate ${CMAKE_CURRENT_SOURCE_DIR}/data/scan.3d.txt 10 1000)
+
+# find_package(Boost)
+# if (Boost_FOUND)
+# find_path(ANN_INCLUDE_DIR ANN.h
+# /usr/local/include/ANN
+# /usr/include/ANN
+# )
+#
+# include_directories(.. ${Boost_INCLUDE_DIRS} ${ANN_INCLUDE_DIR})
+#
+# add_executable(knnbench knnbench.cpp
+# # hiiiikkk^2 this is even uglier, let's talk about this irl ok?
+# ~/test/ann_1.1.2/src/ANN.cpp
+# ~/test/ann_1.1.2/src/bd_fix_rad_search.cpp
+# ~/test/ann_1.1.2/src/bd_pr_search.cpp
+# ~/test/ann_1.1.2/src/bd_search.cpp
+# ~/test/ann_1.1.2/src/bd_tree.cpp
+# ~/test/ann_1.1.2/src/brute.cpp
+# ~/test/ann_1.1.2/src/kd_dump.cpp
+# ~/test/ann_1.1.2/src/kd_fix_rad_search.cpp
+# ~/test/ann_1.1.2/src/kd_pr_search.cpp
+# ~/test/ann_1.1.2/src/kd_search.cpp
+# ~/test/ann_1.1.2/src/kd_split.cpp
+# ~/test/ann_1.1.2/src/kd_tree.cpp
+# ~/test/ann_1.1.2/src/kd_util.cpp
+# ~/test/ann_1.1.2/src/perf.cpp)
+#
+# target_link_libraries(knnbench nabo)
+# else (Boost_FOUND)
+# message("Disabling benchmark because boost has not been found")
+# endif (Boost_FOUND)
View
361 tests/data/scan.2d.txt
@@ -0,0 +1,361 @@
+ 2.8600000e-01 8.8600000e-01
+ 2.9377500e-01 8.9096600e-01
+ 3.0169000e-01 8.9886300e-01
+ 3.0953300e-01 8.9869200e-01
+ 3.1737500e-01 8.9845200e-01
+ 3.2521400e-01 8.9814400e-01
+ 3.3252700e-01 8.8778200e-01
+ 3.4082200e-01 8.9632500e-01
+ 3.4919900e-01 9.0379300e-01
+ 3.5755500e-01 9.0918900e-01
+ 3.6566000e-01 9.1052200e-01
+ 3.7341100e-01 9.0780100e-01
+ 3.8133000e-01 9.0700400e-01
+ 3.8912800e-01 9.0514400e-01
+ 3.9702300e-01 9.0421000e-01
+ 4.0556200e-01 9.0816400e-01
+ 4.1264800e-01 9.0114400e-01
+ 4.2168900e-01 9.0791700e-01
+ 4.2945000e-01 9.0571000e-01
+ 4.3685400e-01 9.0146500e-01
+ 4.4506200e-01 9.0208400e-01
+ 4.5238100e-01 8.9771200e-01
+ 4.6001800e-01 8.9524400e-01
+ 4.6961800e-01 9.0251100e-01
+ 4.7727900e-01 8.9989600e-01
+ 4.8534100e-01 8.9916900e-01
+ 4.9228000e-01 8.9349700e-01
+ 5.0263700e-01 9.0235900e-01
+ 5.1002000e-01 8.9849400e-01
+ 5.1760200e-01 8.9553700e-01
+ 5.2773700e-01 9.0217500e-01
+ 5.3533300e-01 8.9906700e-01
+ 5.4289400e-01 8.9589600e-01
+ 5.5325800e-01 9.0224900e-01
+ 5.6112200e-01 8.9988300e-01
+ 5.6836300e-01 8.9554000e-01
+ 5.7925700e-01 9.0255300e-01
+ 5.8680500e-01 8.9901100e-01
+ 5.9463900e-01 8.9635200e-01
+ 6.0244900e-01 8.9362400e-01
+ 6.1297100e-01 8.9834600e-01
+ 6.2079800e-01 8.9545900e-01
+ 6.2860000e-01 8.9250300e-01
+ 6.3967400e-01 8.9785300e-01
+ 6.5049200e-01 9.0215000e-01
+ 6.5835100e-01 8.9893500e-01
+ 6.6657200e-01 8.9657200e-01
+ 6.7438200e-01 8.9321700e-01
+ 6.8582200e-01 8.9801500e-01
+ 6.9364400e-01 8.9449200e-01
+ 7.0481500e-01 8.9815100e-01
+ 7.1306700e-01 8.9536500e-01
+ 7.1998700e-01 8.8980600e-01
+ 7.3175200e-01 8.9404000e-01
+ 7.3953700e-01 8.9011600e-01
+ 7.5098100e-01 8.9322000e-01
+ 7.6298300e-01 8.9707500e-01
+ 7.7079300e-01 8.9287800e-01
+ 7.7953600e-01 8.9036300e-01
+ 7.9024200e-01 8.9124400e-01
+ 8.0400000e-01 8.9720200e-01
+ 8.1079500e-01 8.9092500e-01
+ 8.2318500e-01 8.9402600e-01
+ 8.3148900e-01 8.9015600e-01
+ 8.4347500e-01 8.9214700e-01
+ 8.5661200e-01 8.9568200e-01
+ 8.6713000e-01 8.9486200e-01
+ 8.7602100e-01 8.9142400e-01
+ 8.8936900e-01 8.9453200e-01
+ 8.9998400e-01 8.9335300e-01
+ 9.0775700e-01 8.8796100e-01
+ 9.2245000e-01 8.9227100e-01
+ 9.3315200e-01 8.9072800e-01
+ 9.4565900e-01 8.9147700e-01
+ 9.5401500e-01 8.8648500e-01
+ 9.6537800e-01 8.8538200e-01
+ 9.7985100e-01 8.8808800e-01
+ 9.9379900e-01 8.8982600e-01
+ 1.0053130e+00 8.8827800e-01
+ 1.0193980e+00 8.8968300e-01
+ 1.0329190e+00 8.9014400e-01
+ 1.0400090e+00 8.8283100e-01
+ 1.0575250e+00 8.8753800e-01
+ 1.0672290e+00 8.8301900e-01
+ 1.0856110e+00 8.8805800e-01
+ 1.0940060e+00 8.8178400e-01
+ 1.1037160e+00 8.7689300e-01
+ 1.1230390e+00 8.8205500e-01
+ 1.1376510e+00 8.8191100e-01
+ 1.1509220e+00 8.8015100e-01
+ 1.1642270e+00 8.7822700e-01
+ 1.1811290e+00 8.7964100e-01
+ 1.1930880e+00 8.7596400e-01
+ 1.2065000e+00 8.7352200e-01
+ 1.2199390e+00 8.7091200e-01
+ 1.2385620e+00 8.7286300e-01
+ 1.2580340e+00 8.7522300e-01
+ 1.2708770e+00 8.7134500e-01
+ 1.2912730e+00 8.7387100e-01
+ 1.3049440e+00 8.7026000e-01
+ 1.3247560e+00 8.7162000e-01
+ 1.3377240e+00 8.6697500e-01
+ 1.3576840e+00 8.6783300e-01
+ 1.3769560e+00 8.6778500e-01
+ 1.3947310e+00 8.6623600e-01
+ 1.2872120e+00 7.6825700e-01
+ 1.2667240e+00 7.3902900e-01
+ 1.2546480e+00 7.1676100e-01
+ 1.2406400e+00 6.9358700e-01
+ 1.2214190e+00 6.6722800e-01
+ 1.2059080e+00 6.4412600e-01
+ 1.1917150e+00 6.2248100e-01
+ 1.1763860e+00 6.0057300e-01
+ 1.1615800e+00 5.7953400e-01
+ 1.1540240e+00 5.6370100e-01
+ 1.1454160e+00 5.4750800e-01
+ 1.1357440e+00 5.3097900e-01
+ 1.1215870e+00 5.1204900e-01
+ 1.1285950e+00 5.0628200e-01
+ 1.1191950e+00 4.9079000e-01
+ 1.1338390e+00 4.8950000e-01
+ 1.1311150e+00 4.7814300e-01
+ 1.1413780e+00 4.7414400e-01
+ 1.1525140e+00 4.7047900e-01
+ 1.1574690e+00 4.6336800e-01
+ 1.1597060e+00 4.5482200e-01
+ 1.1716610e+00 4.5126700e-01
+ 1.1818290e+00 4.4664400e-01
+ 1.1937820e+00 4.4275500e-01
+ 1.1976110e+00 4.3481600e-01
+ 1.2013710e+00 4.2684400e-01
+ 1.2114310e+00 4.2174300e-01
+ 1.2223840e+00 4.1690500e-01
+ 1.2351570e+00 4.1270500e-01
+ 1.2460870e+00 4.0753300e-01
+ 1.2486820e+00 3.9875600e-01
+ 1.2595430e+00 3.9333700e-01
+ 1.2694510e+00 3.8739200e-01
+ 1.2811970e+00 3.8202000e-01
+ 1.2919860e+00 3.7612300e-01
+ 1.3008680e+00 3.6938200e-01
+ 1.3210200e+00 3.6652000e-01
+ 1.3241790e+00 3.5747400e-01
+ 1.3509680e+00 3.5633300e-01
+ 2.1367560e+00 6.0134700e-01
+ 2.1791280e+00 5.9690100e-01
+ 2.1842650e+00 5.8035800e-01
+ 2.1806280e+00 5.6121400e-01
+ 2.1768020e+00 5.4217900e-01
+ 2.1814610e+00 5.2565800e-01
+ 2.1772830e+00 5.0676800e-01
+ 2.1816330e+00 4.9024400e-01
+ 2.1771070e+00 4.7150600e-01
+ 2.1723980e+00 4.5288400e-01
+ 2.1772520e+00 4.3663000e-01
+ 2.1712280e+00 4.1794500e-01
+ 2.1757810e+00 4.0168500e-01
+ 2.1703950e+00 3.8338500e-01
+ 2.1746510e+00 3.6711700e-01
+ 2.1777830e+00 3.5062100e-01
+ 2.1728920e+00 3.3271000e-01
+ 2.1737510e+00 3.1590100e-01
+ 2.1685340e+00 2.9816400e-01
+ 2.1710640e+00 2.8172500e-01
+ 2.1734510e+00 2.6526400e-01
+ 2.1667710e+00 2.4760800e-01
+ 2.1688600e+00 2.3118600e-01
+ 2.1717990e+00 2.1486000e-01
+ 2.1656470e+00 1.9755900e-01
+ 2.1682940e+00 1.8124400e-01
+ 2.1598420e+00 1.6394000e-01
+ 2.1621990e+00 1.4766000e-01
+ 2.1614210e+00 1.3114200e-01
+ 2.1634920e+00 1.1483200e-01
+ 2.1554350e+00 9.7973000e-02
+ 2.1652100e+00 8.2048000e-02
+ 2.1568600e+00 6.5332000e-02
+ 2.1563590e+00 4.8977000e-02
+ 2.1667130e+00 3.2828000e-02
+ 2.1579290e+00 1.6336000e-02
+ 2.1680000e+00 0.0000000e+00
+ 2.1579290e+00 -1.6336000e-02
+ 2.1587150e+00 -3.2688000e-02
+ 2.1583580e+00 -4.9029000e-02
+ 2.1588590e+00 -6.5402000e-02
+ 2.1572180e+00 -8.1699000e-02
+ 2.1554350e+00 -9.7973000e-02
+ 2.1565050e+00 -1.1440500e-01
+ 2.1624180e+00 -1.3121200e-01
+ 2.1512320e+00 -1.4679700e-01
+ 2.1598420e+00 -1.6394000e-01
+ 2.1583400e+00 -1.8028600e-01
+ 2.1557010e+00 -1.9651400e-01
+ 2.1529220e+00 -2.1270900e-01
+ 2.1509940e+00 -2.2899300e-01
+ 2.1489250e+00 -2.4525900e-01
+ 2.1556260e+00 -2.6275900e-01
+ 2.1542510e+00 -2.7921200e-01
+ 2.1497680e+00 -2.9519200e-01
+ 2.1490940e+00 -3.1177500e-01
+ 2.1453170e+00 -3.2784800e-01
+ 2.1423850e+00 -3.4406100e-01
+ 2.1461840e+00 -3.6158300e-01
+ 2.1439370e+00 -3.7800200e-01
+ 2.1483930e+00 -3.9586400e-01
+ 2.1458440e+00 -4.1231700e-01
+ 2.1421750e+00 -4.2853200e-01
+ 2.1461440e+00 -4.4658100e-01
+ 2.1421760e+00 -4.6279700e-01
+ 1.8756990e+00 -4.1112400e-01
+ 1.6923880e+00 -3.7684100e-01
+ 1.6659190e+00 -3.8268500e-01
+ 1.6471470e+00 -3.9030300e-01
+ 1.6283480e+00 -3.9762200e-01
+ 1.6028320e+00 -4.0259600e-01
+ 1.5782870e+00 -4.0745600e-01
+ 1.5585140e+00 -4.1346500e-01
+ 1.5415810e+00 -4.2011100e-01
+ 1.5217930e+00 -4.2551800e-01
+ 1.5029500e+00 -4.3094500e-01
+ 1.4859880e+00 -4.3676000e-01
+ 1.4662070e+00 -4.4126100e-01
+ 1.4529760e+00 -4.4796000e-01
+ 1.4415790e+00 -4.5519500e-01
+ 1.4199460e+00 -4.5814400e-01
+ 1.4094380e+00 -4.6534300e-01
+ 1.3896850e+00 -4.6848700e-01
+ 1.3717990e+00 -4.7211900e-01
+ 1.3685510e+00 -4.8198300e-01
+ 1.3688540e+00 -4.9348500e-01
+ 1.3590680e+00 -5.0038000e-01
+ 1.3763230e+00 -5.2005700e-01
+ 1.3789340e+00 -5.3305900e-01
+ 1.3876640e+00 -5.4927000e-01
+ 1.3899570e+00 -5.6249400e-01
+ 1.3921030e+00 -5.7580100e-01
+ 1.3940990e+00 -5.8918700e-01
+ 1.4020980e+00 -6.0599200e-01
+ 1.4037640e+00 -6.1958700e-01
+ 1.4061480e+00 -6.3374900e-01
+ 1.4187610e+00 -6.5400000e-01
+ 1.4121490e+00 -6.6335300e-01
+ 1.4208900e+00 -6.8191000e-01
+ 1.4097800e+00 -6.8865300e-01
+ 1.5869060e+00 -8.1289600e-01
+ 1.5527740e+00 -8.0702400e-01
+ 1.5297480e+00 -8.0770000e-01
+ 1.5076430e+00 -8.0858800e-01
+ 1.4798140e+00 -8.0523800e-01
+ 1.4595560e+00 -8.0656200e-01
+ 1.4328130e+00 -8.0300700e-01
+ 1.4176210e+00 -8.0717700e-01
+ 1.3838360e+00 -7.9762500e-01
+ 1.3575410e+00 -7.9289900e-01
+ 1.3433940e+00 -7.9680300e-01
+ 1.3165660e+00 -7.9078100e-01
+ 1.2985940e+00 -7.9112500e-01
+ 1.2783470e+00 -7.8934900e-01
+ 1.2652040e+00 -7.9294400e-01
+ 1.2451290e+00 -7.9064500e-01
+ 1.2312990e+00 -7.9320000e-01
+ 1.2114140e+00 -7.9037800e-01
+ 1.1924060e+00 -7.8792700e-01
+ 1.1720150e+00 -7.8388000e-01
+ 1.1591950e+00 -7.8622900e-01
+ 1.1397670e+00 -7.8233400e-01
+ 1.1270570e+00 -7.8429800e-01
+ 1.1071240e+00 -7.7921700e-01
+ 1.0895030e+00 -7.7593300e-01
+ 1.0769950e+00 -7.7730800e-01
+ 1.0694740e+00 -7.8347400e-01
+ 1.0506920e+00 -7.7815600e-01
+ 1.0397040e+00 -7.8048400e-01
+ 1.0266700e+00 -7.8050300e-01
+ 1.0116460e+00 -7.7816000e-01
+ 9.9537000e-01 -7.7414100e-01
+ 9.8858700e-01 -7.8030200e-01
+ 9.7512500e-01 -7.7891400e-01
+ 9.6239700e-01 -7.7810600e-01
+ 9.4908600e-01 -7.7637500e-01
+ 9.3200200e-01 -7.6987500e-01
+ 9.2525900e-01 -7.7548300e-01
+ 9.0965700e-01 -7.7015200e-01
+ 9.0229000e-01 -7.7478200e-01
+ 8.8811700e-01 -7.7067500e-01
+ 8.7528100e-01 -7.6796600e-01
+ 8.6374200e-01 -7.6669000e-01
+ 8.5405600e-01 -7.6768300e-01
+ 8.4204500e-01 -7.6533000e-01
+ 8.3592600e-01 -7.7096700e-01
+ 8.2344100e-01 -7.6754600e-01
+ 8.1219100e-01 -7.6561300e-01
+ 8.0157600e-01 -7.6437300e-01
+ 7.9378200e-01 -7.6717500e-01
+ 7.8216600e-01 -7.6402900e-01
+ 7.7118200e-01 -7.6158200e-01
+ 7.5974800e-01 -7.5815500e-01
+ 7.5050100e-01 -7.5799700e-01
+ 7.3923400e-01 -7.5430700e-01
+ 7.3364900e-01 -7.5995700e-01
+ 7.2200000e-01 -7.5517400e-01
+ 7.1342400e-01 -7.5546900e-01
+ 7.0245100e-01 -7.5129800e-01
+ 6.9492500e-01 -7.5314600e-01
+ 6.8411200e-01 -7.4874000e-01
+ 6.7433100e-01 -7.4597600e-01
+ 6.6826000e-01 -7.5022800e-01
+ 6.5768300e-01 -7.4548000e-01
+ 6.5116300e-01 -7.4869500e-01
+ 6.4203300e-01 -7.4643800e-01
+ 6.3550500e-01 -7.4951700e-01
+ 6.2521900e-01 -7.4434800e-01
+ 6.1830400e-01 -7.4636700e-01
+ 6.1177800e-01 -7.4923800e-01
+ 6.0522700e-01 -7.5205200e-01
+ 5.9214700e-01 -7.3910400e-01
+ 5.8568500e-01 -7.4174700e-01
+ 5.7700200e-01 -7.3875200e-01
+ 5.7090300e-01 -7.4219600e-01
+ 5.6441500e-01 -7.4465400e-01
+ 5.5448600e-01 -7.3765900e-01
+ 5.4803800e-01 -7.3997400e-01
+ 5.4059400e-01 -7.3939600e-01
+ 5.3635300e-01 -7.4822700e-01
+ 5.2703300e-01 -7.4182400e-01
+ 5.2085100e-01 -7.4485300e-01
+ 5.1463500e-01 -7.4783000e-01
+ 5.0554400e-01 -7.4116800e-01
+ 4.9906800e-01 -7.4305500e-01
+ 4.9257500e-01 -7.4488600e-01
+ 4.8322000e-01 -7.3603600e-01
+ 4.7679000e-01 -7.3772900e-01
+ 4.7107000e-01 -7.4227600e-01
+ 4.6271800e-01 -7.3608400e-01
+ 4.5583800e-01 -7.3564900e-01
+ 4.5006100e-01 -7.4003200e-01
+ 4.4359700e-01 -7.4143600e-01
+ 4.3532700e-01 -7.3396400e-01
+ 4.2891600e-01 -7.3523900e-01
+ 4.2140100e-01 -7.3055800e-01
+ 4.1554200e-01 -7.3466700e-01
+ 4.0896000e-01 -7.3478300e-01
+ 4.0160500e-01 -7.2990200e-01
+ 3.9670900e-01 -7.4077300e-01
+ 3.8940600e-01 -7.3576900e-01
+ 3.8311200e-01 -7.3763500e-01
+ 3.7728000e-01 -7.4341700e-01
+ 3.7056300e-01 -7.4219800e-01
+ 3.6366500e-01 -7.3893000e-01
+ 3.5692600e-01 -7.3659300e-01
+ 3.5084400e-01 -7.4116900e-01
+ 3.4445200e-01 -7.4270300e-01
+ 3.3782900e-01 -7.4119000e-01
+ 3.3148100e-01 -7.4361100e-01
+ 3.2509500e-01 -7.4597600e-01
+ 3.1832200e-01 -7.4029500e-01
+ 3.1182600e-01 -7.3954900e-01
+ 3.0537100e-01 -7.3974600e-01
+ 2.9893200e-01 -7.4088700e-01
+ 2.9244000e-01 -7.3797200e-01
+ 2.8600000e-01 -7.4200000e-01
View
81,360 tests/data/scan.3d.large.txt
81,360 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
2,498 tests/data/scan.3d.txt
2,498 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
9 tests/data/sources.txt
@@ -0,0 +1,9 @@
+scan.2d.txt
+scan.3d.txt
+ François Pomerleau, ETHZ, Switzerland
+
+scan.3d.large.txt
+ Andreas Nüchter and Kai Lingemann, University of Osnabrück, Germany
+ http://kos.informatik.uni-osnabrueck.de/3Dscans/dagstuhl/scan000.3d
+
+
View
132 tests/knn.cpp
@@ -1,132 +0,0 @@
-#include "nabo/nabo.h"
-#include <iostream>
-#include <fstream>
-#include <stdexcept>
-
-using namespace std;
-using namespace Nabo;
-
-template<typename T>
-typename NearestNeighborSearch<T>::Matrix load(const char *fileName)
-{
- typedef typename NearestNeighborSearch<T>::Matrix Matrix;
-
- ifstream ifs(fileName);
- if (!ifs.good())
- throw runtime_error(string("Cannot open file ") + fileName);
-
- vector<T> data;
- int dim(0);
- bool firstLine(true);
-
- while (!ifs.eof())
- {
- char line[1024];
- ifs.getline(line, sizeof(line));
- line[sizeof(line)-1] = 0;
-
- char *token = strtok(line, " \t,;");
- while (token)
- {
- if (firstLine)
- ++dim;
- data.push_back(atof(token));
- //cout << atof(token) << " ";
- token = strtok(NULL, " \t,;"); // FIXME: non reentrant, use strtok_r
- }
- firstLine = false;
- }
-
- return Matrix::Map(&data[0], dim, data.size() / dim);
-}
-
-template<typename T>
-void dumpCoordinateForSVG(const typename NearestNeighborSearch<T>::Vector coord, const float zoom = 1, const float ptSize = 1, const char* style = "stroke=\"black\" fill=\"red\"")
-{
- if (coord.size() == 2)
- cout
- << "<circle cx=\"" << zoom*coord(0)
- << "\" cy=\"" << zoom*coord(1)
- << "\" r=\"" << ptSize
- << "\" stroke-width=\"" << 0.2 * ptSize
- << "\" " << style << "/>" << endl;
- else
- assert(false);
-}
-
-int main(int argc, char* argv[])
-{
- typedef Nabo::NearestNeighborSearch<float>::Matrix Matrix;
- typedef Nabo::NearestNeighborSearch<float>::Vector Vector;
- typedef Nabo::NearestNeighborSearch<float>::Index Index;
- typedef Nabo::NearestNeighborSearch<float>::Indexes Indexes;
- typedef Nabo::BruteForceSearch<float> BFSF;
- typedef Nabo::KDTree<float> KDTF;
-
- if (argc != 2)
- {
- cerr << "Usage " << argv[0] << " DATA" << endl;
- return 1;
- }
-
- Matrix d(load<float>(argv[1]));
- BFSF bfs(d);
- KDTF kdt(d);
- const Index K(10);
-
- // uncomment to compare KDTree with brute force search
- assert(K < d.size());
- const int itCount(100);
- for (int i = 0; i < itCount; ++i)
- {
- //Vector q(bfs.minBound.size());
- //for (int i = 0; i < q.size(); ++i)
- // q(i) = bfs.minBound(i) + float(rand()) * (bfs.maxBound(i) - bfs.minBound(i)) / float(RAND_MAX);
- Vector q(d.col(rand() % d.cols()));
- q.cwise() += 0.01;
- //cerr << "bfs:\n";
- Indexes indexes_bf(bfs.knn(q, K, false));
- //cerr << "kdt:\n";
- Indexes indexes_kdtree(kdt.knn(q, K, false));
- //cout << indexes_bf.size() << " " << indexes_kdtree.size() << " " << K << endl;
- assert(indexes_bf.size() == indexes_kdtree.size());
- assert(indexes_bf.size() == K);
- //cerr << "\nquery:\n" << q << "\n\n";
- for (size_t j = 0; j < K; ++j)
- {
- Vector pbf(d.col(indexes_bf[j]));
- Vector pkdtree(d.col(indexes_kdtree[j]));
- //cerr << "index " << j << ": " << indexes_bf[j] << ", " << indexes_kdtree[j] << "\n";
- //cerr << "point " << j << ": " << "\nbf:\n" << pbf << "\nkdtree:\n" << pkdtree << "\n\n";
- assert(dist2(pbf, pkdtree) < numeric_limits<float>::epsilon());
- }
- }
- cerr << "stats kdtree: "
- << kdt.getStatistics().totalVisitCount << " on "
- << itCount * d.cols() << " ("
- << double(100 * kdt.getStatistics().totalVisitCount) / double(itCount * d.cols()) << " %"
- << ")" << endl;
-
- /*
- // uncomment to randomly get a point and find its minimum
- cout << "<?xml version=\"1.0\" standalone=\"no\"?>" << endl;
- cout << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">" << endl;
- cout << "<svg width=\"100%\" height=\"100%\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">" << endl;
- srand(time(0));
- for (int i = 0; i < d.cols(); ++i)
- dumpCoordinateForSVG<float>(d.col(i), 100, 1);
- Vector q(bfs.minBound.size());
- for (int i = 0; i < q.size(); ++i)
- q(i) = bfs.minBound(i) + float(rand()) * (bfs.maxBound(i) - bfs.minBound(i)) / float(RAND_MAX);
- Indexes indexes_bf(bfs.knn(q, K, false));
- for (size_t i = 0; i < K; ++i)
- dumpCoordinateForSVG<float>(d.col(indexes_bf[i]), 100, 1, "stroke=\"black\" fill=\"green\"");
- dumpCoordinateForSVG<float>(q, 100, 1, "stroke=\"black\" fill=\"blue\"");
- cout << "</svg>" << endl;
- */
-
- //cout << "Average KDTree visit count: " << double(totKDTreeVisitCount) * 100. / double(itCount * d.cols()) << " %" << endl;
-
-
- return 0;
-}
View
112 tests/knnvalidate.cpp
@@ -0,0 +1,112 @@
+#include "nabo/nabo.h"
+#include <iostream>
+#include <fstream>
+#include <stdexcept>
+
+using namespace std;
+using namespace Nabo;
+
+template<typename T>
+typename NearestNeighborSearch<T>::Matrix load(const char *fileName)
+{
+ typedef typename NearestNeighborSearch<T>::Matrix Matrix;
+
+ ifstream ifs(fileName);
+ if (!ifs.good())
+ {
+ cerr << "Cannot open file "<< fileName << endl;
+ exit(1);
+ }
+
+ vector<T> data;
+ int dim(0);
+ bool firstLine(true);
+
+ while (!ifs.eof())
+ {
+ char line[1024];
+ ifs.getline(line, sizeof(line));
+ line[sizeof(line)-1] = 0;
+
+ char *token = strtok(line, " \t,;");
+ while (token)
+ {
+ if (firstLine)
+ ++dim;
+ data.push_back(atof(token));
+ token = strtok(NULL, " \t,;"); // FIXME: non reentrant, use strtok_r
+ }
+ firstLine = false;
+ }
+
+ return Matrix::Map(&data[0], dim, data.size() / dim);
+}
+
+int main(int argc, char* argv[])
+{
+ typedef Nabo::NearestNeighborSearch<float>::Matrix Matrix;
+ typedef Nabo::NearestNeighborSearch<float>::Vector Vector;
+ typedef Nabo::NearestNeighborSearch<float>::Index Index;
+ typedef Nabo::NearestNeighborSearch<float>::Indexes Indexes;
+ typedef Nabo::BruteForceSearch<float> BFSF;
+ typedef Nabo::KDTree<float> KDTF;
+
+ if (argc != 4)
+ {
+ cerr << "Usage " << argv[0] << " DATA K IT_COUNT" << endl;
+ return 1;
+ }
+
+ const Matrix d(load<float>(argv[1]));
+ const Index K(atoi(argv[2]));
+ const int itCount(atoi(argv[3]));
+ BFSF bfs(d);
+ KDTF kdt(d);
+
+ // compare KDTree with brute force search
+ if (K >= d.size())
+ {
+ cerr << "Requested more nearest neighbor than points in the data set" << endl;
+ return 2;
+ }
+ for (int i = 0; i < itCount; ++i)
+ {
+ //Vector q(bfs.minBound.size());
+ //for (int i = 0; i < q.size(); ++i)
+ // q(i) = bfs.minBound(i) + float(rand()) * (bfs.maxBound(i) - bfs.minBound(i)) / float(RAND_MAX);
+ Vector q(d.col(rand() % d.cols()));
+ q.cwise() += 0.01;
+ Indexes indexes_bf(bfs.knn(q, K, false));
+ Indexes indexes_kdtree(kdt.knn(q, K, false));
+ if (indexes_bf.size() != indexes_kdtree.size())
+ {
+ cerr << "Different number of points found between search methods" << endl;
+ return 3;
+ }
+ if (indexes_bf.size() != K)
+ {
+ cerr << "Different number of points found between brute force and request" << endl;
+ return 3;
+ }
+ for (size_t j = 0; j < K; ++j)
+ {
+ Vector pbf(d.col(indexes_bf[j]));
+ Vector pkdtree(d.col(indexes_kdtree[j]));
+ if (dist2(pbf, pkdtree) >= numeric_limits<float>::epsilon())
+ {
+ cerr << "Point " << j << " of " << K << " is different between bf and kdtree:\n";
+ cerr << "* query:\n";
+ cerr << q << "\n";
+ cerr << "* indexes " << indexes_bf[j] << " (bf) vs " << indexes_kdtree[j] << " (kdtree)\n";
+ cerr << "* coordinates:\n";
+ cerr << "bf:\n";
+ cerr << pbf << "\n";
+ cerr << "kdtree:\n";
+ cerr << pkdtree << endl;
+ return 4;
+ }
+ }
+ }
+
+ return 0;
+}

0 comments on commit 8ff9b4e

Please sign in to comment.
Something went wrong with that request. Please try again.