Guest User

Untitled

a guest
Mar 21st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. #define CGAL_MESH_2_OPTIMIZER_VERBOSE
  2. #define CGAL_MESHES_DEBUG_REFINEMENT_POINTS
  3. #define CGAL_MESH_2_DEBUG_BAD_FACES
  4. #define CGAL_MESHES_DEBUG_DOUBLE_MAP
  5. #define CGAL_MESH_2_DEBUG_CONFLICTS_ZONE
  6. #define CGAL_MESH_2_DEBUG_INSERTIONS
  7. #define CGAL_MESH_2_VERBOSE
  8.  
  9. #include <fstream>
  10.  
  11. #include "VoronoiGenerator.hpp"
  12.  
  13. #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
  14. #include <CGAL/Constrained_Delaunay_triangulation_2.h>
  15.  
  16. #include <CGAL/Delaunay_mesher_2.h>
  17. #include <CGAL/Delaunay_mesh_face_base_2.h>
  18. #include <CGAL/Delaunay_mesh_vertex_base_2.h>
  19. #include <CGAL/Delaunay_mesh_size_criteria_2.h>
  20.  
  21. #include <CGAL/Voronoi_diagram_2.h>
  22. #include <CGAL/Delaunay_triangulation_adaptation_traits_2.h>
  23. #include <CGAL/Delaunay_triangulation_adaptation_policies_2.h>
  24.  
  25. #include <CGAL/Boolean_set_operations_2.h>
  26.  
  27. #include <CGAL/Cartesian_converter.h>
  28.  
  29. using namespace dem;
  30.  
  31. typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
  32. typedef CGAL::Triangulation_vertex_base_2<K> Vb;
  33. typedef CGAL::Delaunay_mesh_face_base_2<K> Fb;
  34. typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
  35. typedef CGAL::Constrained_Delaunay_triangulation_2<K, Tds> CDT;
  36. typedef CGAL::Delaunay_mesh_size_criteria_2<CDT> Criteria;
  37. typedef CGAL::Delaunay_mesher_2<CDT, Criteria> Mesher;
  38.  
  39. typedef CGAL::Delaunay_triangulation_adaptation_traits_2<CDT> AT;
  40. typedef CGAL::Delaunay_triangulation_caching_degeneracy_removal_policy_2<CDT> AP;
  41. typedef CGAL::Voronoi_diagram_2<CDT,AT,AP> VD;
  42.  
  43. typedef CGAL::Exact_predicates_exact_constructions_kernel exactK;
  44. typedef K::Point_2 Point;
  45. typedef CGAL::Polygon_2<exactK> Polygon;
  46. typedef CGAL::Vector_2<K> Vector_2;
  47. typedef CGAL::Polygon_with_holes_2<exactK> Polygon_with_holes_2;
  48. typedef CGAL::Cartesian_converter<exactK,K> EK_to_IK;
  49.  
  50.  
  51. VoronoiGenerator::VoronoiGenerator(std::vector<Point> const& boundaryPtsIn)
  52. : boundaryPts(boundaryPtsIn)
  53. {
  54.  
  55. for(auto& pt : boundaryPts)
  56. bdryPoly.push_back( exactK::Point_2(pt.x(),pt.y()));
  57.  
  58. }
  59.  
  60. VoronoiGenerator::VoronoiGenerator(Polygon const& polygonIn)
  61. : bdryPoly(polygonIn)
  62. {
  63.  
  64. EK_to_IK to_inexact;
  65.  
  66. for (auto vi=bdryPoly.vertices_begin(); vi!=bdryPoly.vertices_end(); ++vi)
  67. boundaryPts.push_back(to_inexact(*vi));
  68.  
  69. }
  70.  
  71.  
  72. std::vector<Polygon> VoronoiGenerator::Generate()
  73. {
  74.  
  75. // The constrained Delaunay triangulation (dual of the Voronoi)
  76. CDT cdt;
  77.  
  78. cdt.insert_constraint(boundaryPts.begin(), boundaryPts.end(), true);
  79.  
  80. Mesher mesher(cdt);
  81. mesher.init(false);
  82. mesher.set_criteria(Criteria(0.25, 1.0));
  83. std::cout <<"\nNumber of faces: " << cdt.number_of_faces() << "\n" << std::endl;
  84.  
  85. std::cout << "\nFirst refinement step:" << std::endl;
  86. mesher.try_one_step_refine_mesh();
  87. std::cout <<"\nNumber of faces: " << cdt.number_of_faces() << "\n" << std::endl;
  88.  
  89. std::cout << "\nSecond refinement step:" << std::endl;
  90. std::cout << "Is refinement done: " << mesher.is_refinement_done() << std::endl;
  91. mesher.try_one_step_refine_mesh();
  92. std::cout <<"\nNumber of faces: " << cdt.number_of_faces() << "\n" << std::endl;
  93. /*
  94. std::cout << "\nThird refinement step:" << std::endl;
  95. std::cout << "Is refinement done: " << mesher.is_refinement_done() << std::endl;
  96. mesher.try_one_step_refine_mesh();
  97. std::cout <<"\nNumber of faces: " << cdt.number_of_faces() << "\n" << std::endl;
  98.  
  99. mesher.refine_mesh();
  100. */
  101. // CGAL::refine_Delaunay_mesh_2(cdt, Criteria(0.125, 0.05));
  102.  
  103.  
  104. std::vector<Polygon> allCells;
  105. std::vector<exactK::Point_2> cellVerts;
  106.  
  107.  
  108. for (auto face=cdt.finite_faces_begin(); face!=cdt.finite_faces_end(); ++face)
  109. {
  110.  
  111. std::cout << "Area: " << cdt.triangle(face).area() << std::endl;
  112.  
  113. cellVerts.clear();
  114.  
  115. cellVerts.push_back(exactK::Point_2(cdt.triangle(face)[0].x(),
  116. cdt.triangle(face)[0].y()));
  117. cellVerts.push_back(exactK::Point_2(cdt.triangle(face)[1].x(),
  118. cdt.triangle(face)[1].y()));
  119. cellVerts.push_back(exactK::Point_2(cdt.triangle(face)[2].x(),
  120. cdt.triangle(face)[2].y()));
  121.  
  122. Polygon voroCell(cellVerts.begin(), cellVerts.end());
  123. allCells.push_back(voroCell);
  124.  
  125. }
  126.  
  127.  
  128. /*
  129. // Construct the Voronoi diagram from the Delaunay triangulation
  130. VD voro(cdt);
  131.  
  132. int faceNum = 0;
  133. std::vector<Polygon> allCells;
  134.  
  135. for(auto it = voro.bounded_faces_begin(); it != voro.bounded_faces_end(); ++it, ++faceNum)
  136. {
  137.  
  138. auto edgeIterStart = it->ccb();
  139. auto edgeIter = edgeIterStart;
  140.  
  141. //Polygon_2 voroCell;
  142. std::vector<exactK::Point_2> cellVerts;
  143.  
  144. do {
  145.  
  146. Point newPt;
  147.  
  148. if(edgeIter->is_segment()){
  149.  
  150. std::cout << "a" << std::endl;
  151. // std::cout << "Is halfedge:" << it->is_halfedge_on_ccb(*e
  152. std::cout << "Valid: " << edgeIter->is_unbounded() << std::endl;
  153. std::cout << "Target: " << edgeIter->target()->point() << std::endl;
  154. std::cout << "Source: " << edgeIter->has_source() << std::endl;;
  155. newPt = edgeIter->source()->point();
  156. cellVerts.push_back(exactK::Point_2(newPt.x(), newPt.y()));
  157.  
  158. newPt = edgeIter->target()->point();
  159. cellVerts.push_back(exactK::Point_2(newPt.x(), newPt.y()));
  160.  
  161. }else if (edgeIter->is_ray()){
  162.  
  163. std::cout << "b" << std::endl;
  164.  
  165. Point p1 = edgeIter->face()->dual()->point();
  166. Point p2 = edgeIter->twin()->face()->dual()->point();
  167. Point halfPt = p1 + 0.5*(p2-p1);
  168.  
  169. if(edgeIter->has_target()){
  170.  
  171. std::cout << "c" << std::endl;
  172. Point tgtPt = edgeIter->target()->point();
  173. newPt = tgtPt + bigNum*(halfPt - tgtPt);
  174.  
  175. cellVerts.push_back(exactK::Point_2(newPt.x(), newPt.y()));
  176. cellVerts.push_back(exactK::Point_2(tgtPt.x(), tgtPt.y()));
  177.  
  178. }else if(edgeIter->has_source()){
  179.  
  180. std::cout << "d" << std::endl;
  181. Point srcPt = edgeIter->source()->point();
  182. newPt = srcPt + bigNum*(halfPt - srcPt);
  183.  
  184. cellVerts.push_back(exactK::Point_2(srcPt.x(), srcPt.y()));
  185. cellVerts.push_back(exactK::Point_2(newPt.x(), newPt.y()));
  186.  
  187. }
  188. }
  189.  
  190. } while ( ++edgeIter != edgeIterStart );
  191.  
  192.  
  193. cellVerts.erase( std::unique(cellVerts.begin(), cellVerts.end()), cellVerts.end());
  194.  
  195. if(*(cellVerts.end()-1) == cellVerts.at(0))
  196. cellVerts.pop_back();
  197.  
  198. // Copy the vertices into a polygon
  199. Polygon voroCell(cellVerts.begin(), cellVerts.end());
  200.  
  201. // Get the intersection of the
  202. std::vector<Polygon_with_holes_2> intersection;
  203. CGAL::intersection (voroCell, bdryPoly, std::back_inserter(intersection));
  204.  
  205. assert(intersection.size()==1);
  206. allCells.push_back(intersection.at(0).outer_boundary());
  207.  
  208. allCells.push_back(voroCell);
  209.  
  210.  
  211. } // end of loop over Voronoi cells
  212. */
  213. return allCells;
  214.  
  215. }
  216.  
  217.  
  218. void dem::VoronoiToFile(std::vector<Polygon>& polys, std::string filename)
  219. {
  220.  
  221. std::ofstream polysOut(filename);
  222.  
  223. if (polysOut.is_open())
  224. {
  225.  
  226. for (auto p : polys)
  227. {
  228.  
  229. polysOut << p.size() << std::endl;
  230.  
  231. for (auto it=p.vertices_begin(); it!=p.vertices_end(); ++it)
  232. polysOut << it->x() << " " << it->y() << std::endl;
  233.  
  234. }
  235.  
  236. }
  237.  
  238. }
Add Comment
Please, Sign In to add comment