How to get triangle index using tree.any_intersection

Viewed 58

Let's say we have a bunch of triangles 1,2,...,N, and a line segment. By making a tree, I want to get the intersection of the line segment with triangles and the index of the triangle that intersects the line segment (there is only one). How I call it from "intersection" below?

Thanks.

for (int i=0; i<NumTriangles; i++) {
    Point a(Triangles[i].vert1[0], Triangles[i].vert1[1], Triangles[i].vert1[2]);
    Point b(Triangles[i].vert2[0], Triangles[i].vert2[1], Triangles[i].vert2[2]);
    Point c(Triangles[i].vert3[0], Triangles[i].vert3[1], Triangles[i].vert3[2]);
    triangles.push_back(Triangle(a,b,c));
}
Tree tree(triangles.begin(),triangles.end());
Point a(0,0,0);
Point a(0,0,1);
Segment segment_query(a,b);
Segment_intersection intersection = tree.any_intersection(segment_query);
1 Answers

Each triangle you put in the three is called a primitive. This primitive has a unique identifier (also known as ID) and a geometry (datum, here a triangle).

The type of the primitive ID is defined by the template parameter of the class CGAL::AABB_primitive. If you are using the CGAL::AABB_triangle_primitive, which is just a simplified API around CGAL:AABB_primitive, then the ID is an iterator referring to the primitive within the range triangles.

The function any_intersection() returns an optional object which is a pair that contains both the geometry of the intersection (as first) and the intersected primitive (as second), from which you can get the ID with the id() function.

Related