Skip to content

Commit b8cbb72

Browse files
committed
adding do_intersect_circle_iso_rectangle_2 to AABB_traits_2
1 parent b8d043f commit b8cbb72

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

AABB_tree/include/CGAL/AABB_traits_2.h

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,15 @@ class AABB_traits_2
395395
public:
396396
CGAL::Comparison_result operator()(const Point& p, const Bounding_box& bb, const Point& bound, Tag_true) const
397397
{
398-
return GeomTraits().do_intersect_2_object()
398+
return do_intersect_circle_iso_rectangle_2
399399
(GeomTraits().construct_circle_2_object()
400-
(p, GeomTraits().compute_squared_distance_2_object()(p, bound)), bb,true)?
400+
(p, GeomTraits().compute_squared_distance_2_object()(p, bound)), bb)?
401401
CGAL::SMALLER : CGAL::LARGER;
402402
}
403403

404404
CGAL::Comparison_result operator()(const Point& p, const Bounding_box& bb, const Point& bound, Tag_false) const
405405
{
406-
return GeomTraits().do_intersect_2_object()
406+
return do_intersect_circle_iso_rectangle_2
407407
(GeomTraits().construct_circle_2_object()
408408
(p, GeomTraits().compute_squared_distance_2_object()(p, bound)), bb)?
409409
CGAL::SMALLER : CGAL::LARGER;
@@ -433,6 +433,45 @@ class AABB_traits_2
433433
CGAL::SMALLER :
434434
CGAL::LARGER;
435435
}
436+
437+
typename GeomTraits::Boolean do_intersect_circle_iso_rectangle_2(const typename GeomTraits::Circle_2& circle,
438+
const typename GeomTraits::Iso_rectangle_2& rec) const
439+
{
440+
typedef typename GeomTraits::FT FT;
441+
typedef typename GeomTraits::Point_2 Point;
442+
443+
Point center = circle.center();
444+
445+
// Check that the minimum distance to the box is smaller than the radius, otherwise there is
446+
// no intersection. `distance` stays at 0 if the center is inside or on `rec`.
447+
FT distance = FT(0);
448+
if (center.x() < rec.xmin())
449+
{
450+
FT d = rec.xmin() - center.x();
451+
distance += d * d;
452+
}
453+
else if (center.x() > rec.xmax())
454+
{
455+
FT d = center.x() - rec.xmax();
456+
distance += d * d;
457+
}
458+
459+
if (center.y() < rec.ymin())
460+
{
461+
FT d = rec.ymin() - center.y();
462+
distance += d * d;
463+
}
464+
else if (center.y() > rec.ymax())
465+
{
466+
FT d = center.y() - rec.ymax();
467+
distance += d * d;
468+
}
469+
470+
if (distance <= circle.squared_radius())
471+
return true;
472+
473+
return false;
474+
}
436475
};
437476

438477
Closest_point closest_point_object() const {return Closest_point(*this);}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)