knn.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <test_common.h>
  2. #include <igl/list_to_matrix.h>
  3. #include <igl/knn.h>
  4. #include <igl/octree.h>
  5. TEST_CASE("knn", "[igl]")
  6. {
  7. auto run_knn = [](int k, const Eigen::MatrixXd & P, const Eigen::MatrixXd& V, typename Eigen::MatrixXi& I) {
  8. std::vector<std::vector<int> > point_indices;
  9. Eigen::Matrix<int,Eigen::Dynamic,8> CH;
  10. Eigen::Matrix<double,Eigen::Dynamic,3> CN;
  11. Eigen::Matrix<double,Eigen::Dynamic,1> W;
  12. Eigen::Matrix<double,Eigen::Dynamic,1> A;
  13. igl::octree(V,point_indices,CH,CN,W);
  14. igl::knn(P,V,k,point_indices,CH,CN,W,I);
  15. };
  16. Eigen::Matrix<double,Eigen::Dynamic,3> V;
  17. Eigen::Matrix<double,Eigen::Dynamic,3> P;
  18. Eigen::MatrixXi I;
  19. Eigen::MatrixXi answer;
  20. {
  21. //Test some simple points off a unit cube
  22. V.resize(8,3);
  23. V <<
  24. 0,0,1,
  25. 0,1,0,
  26. 0,1,1,
  27. 0,0,0,
  28. 1,0,0,
  29. 1,1,0,
  30. 1,1,1,
  31. 1,0,1;
  32. P.resize(3,3);
  33. P <<
  34. 0 ,0 ,0.6,
  35. 0.3 ,0.1 ,0.2,
  36. .7,.6,0;
  37. answer.resize(3,3);
  38. answer << 0,3,2,
  39. 3,4,0,
  40. 5,4,1;
  41. run_knn(3,P,V,I);
  42. REQUIRE (I == answer);
  43. }
  44. {
  45. //Test whether the kdtree works when passed things of different size
  46. V.resize(2,3);
  47. V << 0,0,0,
  48. 1,1,1;
  49. P << 0,0,0,
  50. -1,-1,-1,
  51. 2,2,2;
  52. run_knn(10,P,V,I);
  53. answer.resize(3,2);
  54. answer <<
  55. 0,1,
  56. 0,1,
  57. 1,0;
  58. REQUIRE (I == answer);
  59. }
  60. }