Guest User

VTK polydata iterator for CGAL AABB_tree

a guest
Aug 7th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <list>
  3. #include <CGAL/Simple_cartesian.h>
  4. #include <CGAL/AABB_tree.h>
  5. #include <CGAL/AABB_traits.h>
  6. #include <CGAL/AABB_triangle_primitive.h>
  7.  
  8. #include <vtkXMLPolyDataReader.h>
  9. #include <vtkPolyDataReader.h>
  10. #include <vtkPolyDataWriter.h>
  11. #include <vtkPolyData.h>
  12. #include <vtkCutter.h>
  13. #include <vtkPlane.h>
  14. #include <vtkIdList.h>
  15. #include <vtkCell.h>
  16.  
  17. typedef CGAL::Simple_cartesian<double> K;
  18. typedef K::FT FT;
  19. typedef K::Point_3 Point;
  20. typedef K::Vector_3 Vector;
  21. typedef K::Plane_3 Plane;
  22. typedef K::Segment_3 Segment;
  23. typedef K::Triangle_3 Triangle;
  24.  
  25. // defines the iterator over triangles needed by the tree:
  26. class Triangles_const_iterator
  27.     : public boost::iterator_facade<
  28.       Triangles_const_iterator
  29.     , K::Triangle_3 const           // Value
  30.     , boost::forward_traversal_tag  // CategoryOrTraversal
  31.     >
  32. {
  33. public:
  34.     Triangles_const_iterator()
  35.         : data(nullptr), cellId(-1) {}
  36.  
  37.     static Triangles_const_iterator begin(vtkPolyData* data)
  38.     {
  39.         return Triangles_const_iterator(data, 0);
  40.     }
  41.  
  42.     static Triangles_const_iterator end(vtkPolyData* data)
  43.     {
  44.         return Triangles_const_iterator(data, data->GetNumberOfCells());
  45.     }
  46.  
  47. private:
  48.     explicit Triangles_const_iterator(vtkPolyData* data, int index)
  49.         : data(data), cellId(index)
  50.     {
  51.     }
  52.  
  53.  
  54.     friend class boost::iterator_core_access;
  55.     void increment() {
  56.         ++cellId;
  57.     }
  58.  
  59.     K::Triangle_3 convertVTKtoCGAL() const
  60.     {
  61.         if (cellId < data->GetNumberOfCells()) {
  62.             vtkCell* cell = data->GetCell(cellId);
  63.  
  64.             if (cell->GetNumberOfPoints() == 3) {
  65.                 K::Point_3 p[3];
  66.                 for (int i = 0; i < 3; ++i) {
  67.                     double* pt = data->GetPoints()->GetPoint(cell->GetPointId(i));
  68.                     p[i] = K::Point_3(pt[0], pt[1], pt[2]);
  69.                 }
  70.  
  71.                 // Convert triangle to CGAL
  72.                 return K::Triangle_3(p[0], p[1], p[2]);
  73.             }
  74.         }
  75.         return K::Triangle_3();
  76.     }
  77.  
  78.     K::Triangle_3 dereference() const { return convertVTKtoCGAL(); }
  79.    
  80.     bool equal(Triangles_const_iterator const& other) const
  81.     {
  82.         return data == other.data && cellId == other.cellId;
  83.     }
  84.  
  85.     int cellId;
  86.     vtkPolyData* data;
  87. };
  88.  
  89. typedef CGAL::AABB_triangle_primitive<K, Triangles_const_iterator> Primitive;
  90. typedef CGAL::AABB_traits<K, Primitive> Traits;
  91. typedef CGAL::AABB_tree<Traits> Tree;
  92.  
  93.  
  94. int main()
  95. {
  96.     auto reader = vtkPolyDataReader::New();
  97.     reader->SetFileName("mesh.vtk");
  98.     reader->Update();
  99.  
  100.     auto polyData = reader->GetOutput();
  101.  
  102.     std::ifstream planeIn("plane.txt");
  103.  
  104.     double coords[9];
  105.     for (int i = 0; i < 9; ++i) {
  106.         planeIn >> coords[i];
  107.     }
  108.     Tree tree(Triangles_const_iterator::begin(polyData), Triangles_const_iterator::end(polyData));
  109.     tree.accelerate_distance_queries();
  110.  
  111.     Point points[] = { { coords[0], coords[1], coords[2] }, { coords[3], coords[4], coords[5] }, { coords[6], coords[7], coords[8] } };
  112.     Plane plane_query(points[0], points[1], points[2]);
  113.  
  114.     std::vector<Tree::Intersection_and_primitive_id<Plane>::Type> segments;
  115.     tree.all_intersections(plane_query, std::back_inserter(segments));
  116.  
  117.     std::cout << segments.size() << " intersections(s) with plane\n";
  118.  
  119.     return EXIT_SUCCESS;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment