triangulate.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "triangulate.h"
  9. #include "triangle_header.h"
  10. template <
  11. typename DerivedV,
  12. typename DerivedE,
  13. typename DerivedH,
  14. typename DerivedV2,
  15. typename DerivedF2>
  16. IGL_INLINE void igl::triangle::triangulate(
  17. const Eigen::MatrixBase<DerivedV> & V,
  18. const Eigen::MatrixBase<DerivedE> & E,
  19. const Eigen::MatrixBase<DerivedH> & H,
  20. const std::string flags,
  21. Eigen::PlainObjectBase<DerivedV2> & V2,
  22. Eigen::PlainObjectBase<DerivedF2> & F2)
  23. {
  24. Eigen::VectorXi VM,EM,VM2,EM2;
  25. Eigen::MatrixXi E2;
  26. return triangulate(V,E,H,VM,EM,flags,V2,F2,VM2,E2,EM2);
  27. }
  28. template <
  29. typename DerivedV,
  30. typename DerivedE,
  31. typename DerivedH,
  32. typename DerivedVM,
  33. typename DerivedEM,
  34. typename DerivedV2,
  35. typename DerivedF2,
  36. typename DerivedVM2,
  37. typename DerivedE2,
  38. typename DerivedEM2>
  39. IGL_INLINE void igl::triangle::triangulate(
  40. const Eigen::MatrixBase<DerivedV> & V,
  41. const Eigen::MatrixBase<DerivedE> & E,
  42. const Eigen::MatrixBase<DerivedH> & H,
  43. const Eigen::MatrixBase<DerivedVM> & VM,
  44. const Eigen::MatrixBase<DerivedEM> & EM,
  45. const std::string flags,
  46. Eigen::PlainObjectBase<DerivedV2> & V2,
  47. Eigen::PlainObjectBase<DerivedF2> & F2,
  48. Eigen::PlainObjectBase<DerivedVM2> & VM2,
  49. Eigen::PlainObjectBase<DerivedE2> & E2,
  50. Eigen::PlainObjectBase<DerivedEM2> & EM2)
  51. {
  52. assert( (VM.size() == 0 || V.rows() == VM.size()) &&
  53. "Vertex markers must be empty or same size as V");
  54. assert( (EM.size() == 0 || E.rows() == EM.size()) &&
  55. "Segment markers must be empty or same size as E");
  56. assert(V.cols() == 2);
  57. assert(E.size() == 0 || E.cols() == 2);
  58. assert(H.size() == 0 || H.cols() == 2);
  59. // Prepare the flags
  60. std::string full_flags = flags + "pz" + (EM.size() || VM.size() ? "" : "B");
  61. typedef Eigen::Map< Eigen::Matrix<double ,Eigen::Dynamic ,Eigen::Dynamic,Eigen::RowMajor> > MapXdr;
  62. typedef Eigen::Map< Eigen::Matrix<int ,Eigen::Dynamic ,Eigen::Dynamic,Eigen::RowMajor> > MapXir;
  63. // Prepare the input struct
  64. triangulateio in;
  65. in.numberofpoints = V.rows();
  66. in.pointlist = (double*)calloc(V.size(),sizeof(double));
  67. {
  68. MapXdr inpl(in.pointlist,V.rows(),V.cols());
  69. inpl = V.template cast<double>();
  70. }
  71. in.numberofpointattributes = 0;
  72. in.pointmarkerlist = (int*)calloc(V.size(),sizeof(int)) ;
  73. for(unsigned i=0;i<V.rows();++i) in.pointmarkerlist[i] = VM.size()?VM(i):1;
  74. in.trianglelist = NULL;
  75. in.numberoftriangles = 0;
  76. in.numberofcorners = 0;
  77. in.numberoftriangleattributes = 0;
  78. in.triangleattributelist = NULL;
  79. in.numberofsegments = E.size()?E.rows():0;
  80. in.segmentlist = (int*)calloc(E.size(),sizeof(int));
  81. {
  82. MapXir insl(in.segmentlist,E.rows(),E.cols());
  83. insl = E.template cast<int>();
  84. }
  85. in.segmentmarkerlist = (int*)calloc(E.rows(),sizeof(int));
  86. for (unsigned i=0;i<E.rows();++i) in.segmentmarkerlist[i] = EM.size()?EM(i):1;
  87. in.numberofholes = H.size()?H.rows():0;
  88. in.holelist = (double*)calloc(H.size(),sizeof(double));
  89. {
  90. MapXdr inhl(in.holelist,H.rows(),H.cols());
  91. inhl = H.template cast<double>();
  92. }
  93. in.numberofregions = 0;
  94. // Prepare the output struct
  95. triangulateio out;
  96. out.pointlist = NULL;
  97. out.trianglelist = NULL;
  98. out.segmentlist = NULL;
  99. out.segmentmarkerlist = NULL;
  100. out.pointmarkerlist = NULL;
  101. // Call triangle
  102. ::triangulate(const_cast<char*>(full_flags.c_str()), &in, &out, 0);
  103. // Return the mesh
  104. V2 = MapXdr(out.pointlist,out.numberofpoints,2).cast<typename DerivedV2::Scalar>();
  105. F2 = MapXir(out.trianglelist,out.numberoftriangles,3).cast<typename DerivedF2::Scalar>();
  106. E2 = MapXir(out.segmentlist,out.numberofsegments,2).cast<typename DerivedE2::Scalar>();
  107. if(VM.size())
  108. {
  109. VM2 = MapXir(out.pointmarkerlist,out.numberofpoints,1).cast<typename DerivedVM2::Scalar>();
  110. }
  111. if(EM.size())
  112. {
  113. EM2 = MapXir(out.segmentmarkerlist,out.numberofsegments,1).cast<typename DerivedEM2::Scalar>();
  114. }
  115. // Cleanup in
  116. free(in.pointlist);
  117. free(in.pointmarkerlist);
  118. free(in.segmentlist);
  119. free(in.segmentmarkerlist);
  120. free(in.holelist);
  121. // Cleanup out
  122. free(out.pointlist);
  123. free(out.trianglelist);
  124. free(out.segmentlist);
  125. free(out.segmentmarkerlist);
  126. free(out.pointmarkerlist);
  127. }
  128. #ifdef IGL_STATIC_LIBRARY
  129. // Explicit template instantiation
  130. // generated by autoexplicit.sh
  131. template void igl::triangle::triangulate<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  132. template void igl::triangle::triangulate<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  133. template void igl::triangle::triangulate<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  134. #endif