|
| 1 | +#include <CGAL/Exact_predicates_inexact_constructions_kernel.h> |
| 2 | +#include <CGAL/Exact_predicates_exact_constructions_kernel.h> |
| 3 | +#include <CGAL/Exact_rational.h> |
| 4 | + |
| 5 | +#include <CGAL/AABB_tree.h> |
| 6 | +#include <CGAL/AABB_traits_2.h> |
| 7 | +#include <CGAL/AABB_triangle_primitive_2.h> |
| 8 | +#include <CGAL/IO/polygon_soup_io.h> |
| 9 | + |
| 10 | +#include <array> |
| 11 | +#include <iostream> |
| 12 | +#include <string> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +template<typename Kernel> |
| 16 | +void test(const std::vector<CGAL::Simple_cartesian<double>::Point_2> &points, const std::vector<std::array<std::size_t, 3> > &faces) { |
| 17 | + using Point_2 = typename Kernel::Point_2; |
| 18 | + using Triangle_2 = typename Kernel::Triangle_2; |
| 19 | + using Iterator = typename std::vector<Triangle_2>::const_iterator; |
| 20 | + using Primitive = CGAL::AABB_triangle_primitive_2<Kernel, Iterator>; |
| 21 | + using Tree_traits = CGAL::AABB_traits_2<Kernel, Primitive>; |
| 22 | + using Tree = CGAL::AABB_tree<Tree_traits>; |
| 23 | + |
| 24 | + std::vector<Triangle_2> triangles(faces.size()); |
| 25 | + for (std::size_t i = 0; i < faces.size(); ++i) { |
| 26 | + const auto& f = faces[i]; |
| 27 | + triangles[i] = Triangle_2(Point_2(points[f[0]].x(), points[f[0]].y()), Point_2(points[f[1]].x(), points[f[1]].y()), Point_2(points[f[2]].x(), points[f[2]].y())); |
| 28 | + } |
| 29 | + |
| 30 | + Tree tree(triangles.begin(), triangles.end()); |
| 31 | + |
| 32 | + // Without hint |
| 33 | + Point_2 query(-0.092372499264859229, -0.5067061545706153); |
| 34 | + Point_2 closest_point = tree.closest_point(query); |
| 35 | + std::cout << "Closest point to " << query << " is " << closest_point << std::endl; |
| 36 | + |
| 37 | + // With hint |
| 38 | + Point_2 hint(-0.077185400000000001, -0.42269299999999999); |
| 39 | + Point_2 closest_point_hint = tree.closest_point(query, hint); |
| 40 | + std::cout << "Closest point to " << query << " with hint " << hint << " is " << closest_point_hint << std::endl << std::endl; |
| 41 | + |
| 42 | + assert(closest_point == closest_point_hint); |
| 43 | +} |
| 44 | + |
| 45 | +int main(int argc, char** argv) |
| 46 | +{ |
| 47 | + std::cout.precision(17); |
| 48 | + |
| 49 | + // Read the input |
| 50 | + const std::string filename = (argc > 1) ? argv[1] : CGAL::data_file_path("meshes/camel.off"); |
| 51 | + std::cout << "Reading " << filename << "..." << std::endl; |
| 52 | + |
| 53 | + std::vector<CGAL::Simple_cartesian<double>::Point_3> points; |
| 54 | + std::vector<std::array<std::size_t, 3> > faces; |
| 55 | + if (!CGAL::IO::read_polygon_soup(filename, points, faces) || faces.empty()) |
| 56 | + { |
| 57 | + std::cerr << "Invalid input:" << filename << std::endl; |
| 58 | + return EXIT_FAILURE; |
| 59 | + } |
| 60 | + |
| 61 | + std::cout << "Input: " << points.size() << " points, " << faces.size() << " faces" << std::endl; |
| 62 | + |
| 63 | + // Project onto the XY plane |
| 64 | + std::vector<CGAL::Simple_cartesian<double>::Point_2> points_2(points.size()); |
| 65 | + for (std::size_t i = 0; i < points.size(); ++i) |
| 66 | + points_2[i] = CGAL::Simple_cartesian<double>::Point_2(points[i].x(), points[i].y()); |
| 67 | + |
| 68 | + std::cout << "Testing closest point with Simple_cartesian<double>:" << std::endl; |
| 69 | + test<CGAL::Simple_cartesian<double> >(points_2, faces); |
| 70 | + std::cout << "Testing closest point with Epick:" << std::endl; |
| 71 | + test<CGAL::Exact_predicates_inexact_constructions_kernel>(points_2, faces); |
| 72 | + std::cout << "Testing closest point with Epeck:" << std::endl; |
| 73 | + test<CGAL::Exact_predicates_exact_constructions_kernel>(points_2, faces); |
| 74 | + std::cout << "Testing closest point with Simple_cartesian<Exact_rational>:" << std::endl; |
| 75 | + test<CGAL::Simple_cartesian<CGAL::Exact_rational>>(points_2, faces); |
| 76 | + |
| 77 | + std::cout << "Done." << std::endl; |
| 78 | + |
| 79 | + return EXIT_SUCCESS; |
| 80 | +} |
0 commit comments