# How to obtain the handle of a triangle that is intersected by a ray

3 messages
Open this post in threaded view
|
Report Content as Inappropriate

## How to obtain the handle of a triangle that is intersected by a ray

 I have an AABBtree structure and a ray that intersects it. How do I obtain the handle of the triangle that is interesected by the ray? The reason why I need that is that I need to compute the normal vector of the intersected triangle. I tried the following approach: ```Ray_intersection intersection = tree_3d_map_.first_intersection(ray); AABB_primitive_id primitive_id = intersection->second; KernelSCD::Vector_3 v = CGAL::Polygon_mesh_processing::compute_face_normal(primitive_id,points_3d);```But the last line does not compile. This is my code: ```// CGAL includes for AABB tree for intersection detection #include #include #include #include // CGAL includes for Delaunay triangulation #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int main (int argc, char *argv[]) { typedef CGAL::Simple_cartesian KernelSCD; typedef KernelSCD::Triangle_3 Triangle_3; typedef std::list::iterator IteratorTriangle; typedef CGAL::AABB_triangle_primitive Primitive; typedef CGAL::AABB_traits AABB_triangle_traits; typedef CGAL::AABB_tree Tree; typedef CGAL::Simple_cartesian K; typedef CGAL::Triangulation_vertex_base_with_info_2 Vb; typedef CGAL::Triangulation_data_structure_2 Tds; typedef CGAL::Delaunay_triangulation_2 Delaunay; typedef Delaunay::Point PointD; typedef Delaunay::Vertex_handle Vertex_handle; typedef KernelSCD::Point_3 Point_3; typedef KernelSCD::Vector_3 Vector_3; typedef KernelSCD::Ray_3 Ray_3; typedef boost::optional::Type> Ray_intersection; typedef Tree::Primitive_id AABB_primitive_id; Delaunay dt; std::vector< std::pair > points_2d; std::vector points_3d; Tree tree_3d_map_; std::list triangles_; std::vector verts = { 0, 0, 100, 0, 1, 101, 1, 0, 102, 1, 1, 103, 10, 0, 104, 0, 10, 105, 10, 10, 106}; // Filling Delaunay triangulation for (int i = 0; i+2 < verts.size(); i += 3) { points_2d.push_back(std::make_pair(PointD(verts.at(i), verts.at(i + 1)), i / 3)); points_3d.push_back(Point_3(verts.at(i), verts.at(i + 1), verts.at(i + 2))); } dt.insert(points_2d.begin(), points_2d.end()); // Filling AABB tree int v0, v1, v2; for (Delaunay::Finite_faces_iterator it = dt.finite_faces_begin(); it != dt.finite_faces_end(); it++){ v0 = it->vertex(0)->info(); v1 = it->vertex(1)->info(); v2 = it->vertex(2)->info(); triangles_.push_back(Triangle_3(points_3d.at(v0), points_3d.at(v1), points_3d.at(v2))); } tree_3d_map_.insert(triangles_.begin(), triangles_.end()); Point_3 p1(.4, .5, 0.1); Vector_3 v(0, 0, 1); Ray_3 ray(p1, v); // Find point where the ray intersects the mesh for the first time Ray_intersection intersection = tree_3d_map_.first_intersection(ray); if (intersection){ if (boost::get(&(intersection->first))){ const KernelSCD::Point_3* p = boost::get(&(intersection->first)); std::cout << "First intersection: " << p->x() << ", " << p->y() << ", " << p->z() << std::endl; AABB_primitive_id primitive_id = intersection->second; // Fails: // | | | | // v v v v KernelSCD::Vector_3 v = CGAL::Polygon_mesh_processing::compute_face_normal(primitive_id,points_3d); std::cout << "v: " << v.x() << ", " << v.y() << ", " << v.z() << std::endl; } } std::cout << "done" << std::endl; std::cin.get(); return 0; }```
Open this post in threaded view
|
Report Content as Inappropriate

## Re: How to obtain the handle of a triangle that is intersected by a ray

 Replace: KernelSCD::Vector_3 v = CGAL::Polygon_mesh_processing::compute_face_normal(primitive_id,points_3d); by KernelSCD::Vector_3 v = CGAL::normal(primitive_id->vertex(0), primitive_id->vertex(1), primitive_id->vertex(2) ); The function you were calling is meant to be used with a graph. Sebastien. On 03/21/2017 10:16 AM, adrian wrote: > I have an AABBtree structure and a ray that intersects it. > How do I obtain the handle of the triangle that is interesected by the ray? > > The reason why I need that is that I need to compute the normal vector of > the intersected triangle. > I tried the following approach: > > But the last line does not compile. > > > > This is my code: > > > > > > > > -- > View this message in context: http://cgal-discuss.949826.n4.nabble.com/How-to-obtain-the-handle-of-a-triangle-that-is-intersected-by-a-ray-tp4662591.html> Sent from the cgal-discuss mailing list archive at Nabble.com. > -- You are currently subscribed to cgal-discuss. To unsubscribe or access the archives, go to https://sympa.inria.fr/sympa/info/cgal-discuss