triangulate.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  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. #ifdef ANSI_DECLARATORS
  10. # define IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS ANSI_DECLARATORS
  11. # undef ANSI_DECLARATORS
  12. #endif
  13. #ifdef REAL
  14. # define IGL_PREVIOUSLY_DEFINED_REAL REAL
  15. # undef REAL
  16. #endif
  17. #ifdef VOID
  18. # define IGL_PREVIOUSLY_DEFINED_VOID VOID
  19. # undef VOID
  20. #endif
  21. #define ANSI_DECLARATORS
  22. #define REAL double
  23. #define VOID int
  24. extern "C"
  25. {
  26. #include <triangle.h>
  27. }
  28. #undef ANSI_DECLARATORS
  29. #ifdef IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS
  30. # define ANSI_DECLARATORS IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS
  31. #endif
  32. #undef REAL
  33. #ifdef IGL_PREVIOUSLY_DEFINED_REAL
  34. # define REAL IGL_PREVIOUSLY_DEFINED_REAL
  35. #endif
  36. #undef VOID
  37. #ifdef IGL_PREVIOUSLY_DEFINED_VOID
  38. # define VOID IGL_PREVIOUSLY_DEFINED_VOID
  39. #endif
  40. IGL_INLINE void igl::triangle::triangulate(
  41. const Eigen::MatrixXd& V,
  42. const Eigen::MatrixXi& E,
  43. const Eigen::MatrixXd& H,
  44. const std::string flags,
  45. Eigen::MatrixXd& V2,
  46. Eigen::MatrixXi& F2)
  47. {
  48. using namespace std;
  49. using namespace Eigen;
  50. // Prepare the flags
  51. string full_flags = flags + "pzB";
  52. // Prepare the input struct
  53. triangulateio in;
  54. assert(V.cols() == 2);
  55. in.numberofpoints = V.rows();
  56. in.pointlist = (double*)calloc(V.rows()*2,sizeof(double));
  57. for (unsigned i=0;i<V.rows();++i)
  58. for (unsigned j=0;j<2;++j)
  59. in.pointlist[i*2+j] = V(i,j);
  60. in.numberofpointattributes = 0;
  61. in.pointmarkerlist = (int*)calloc(V.rows(),sizeof(int));
  62. for (unsigned i=0;i<V.rows();++i)
  63. in.pointmarkerlist[i] = 1;
  64. in.trianglelist = NULL;
  65. in.numberoftriangles = 0;
  66. in.numberofcorners = 0;
  67. in.numberoftriangleattributes = 0;
  68. in.triangleattributelist = NULL;
  69. in.numberofsegments = E.rows();
  70. in.segmentlist = (int*)calloc(E.rows()*2,sizeof(int));
  71. for (unsigned i=0;i<E.rows();++i)
  72. for (unsigned j=0;j<2;++j)
  73. in.segmentlist[i*2+j] = E(i,j);
  74. in.segmentmarkerlist = (int*)calloc(E.rows(),sizeof(int));
  75. for (unsigned i=0;i<E.rows();++i)
  76. in.segmentmarkerlist[i] = 1;
  77. in.numberofholes = H.rows();
  78. in.holelist = (double*)calloc(H.rows()*2,sizeof(double));
  79. for (unsigned i=0;i<H.rows();++i)
  80. for (unsigned j=0;j<2;++j)
  81. in.holelist[i*2+j] = H(i,j);
  82. in.numberofregions = 0;
  83. // Prepare the output struct
  84. triangulateio out;
  85. out.pointlist = NULL;
  86. out.trianglelist = NULL;
  87. out.segmentlist = NULL;
  88. // Call triangle
  89. ::triangulate(const_cast<char*>(full_flags.c_str()), &in, &out, 0);
  90. // Return the mesh
  91. V2.resize(out.numberofpoints,2);
  92. for (unsigned i=0;i<V2.rows();++i)
  93. for (unsigned j=0;j<2;++j)
  94. V2(i,j) = out.pointlist[i*2+j];
  95. F2.resize(out.numberoftriangles,3);
  96. for (unsigned i=0;i<F2.rows();++i)
  97. for (unsigned j=0;j<3;++j)
  98. F2(i,j) = out.trianglelist[i*3+j];
  99. // Cleanup in
  100. free(in.pointlist);
  101. free(in.pointmarkerlist);
  102. free(in.segmentlist);
  103. free(in.segmentmarkerlist);
  104. free(in.holelist);
  105. // Cleanup out
  106. free(out.pointlist);
  107. free(out.trianglelist);
  108. free(out.segmentlist);
  109. }
  110. // Allows use of markers
  111. IGL_INLINE void igl::triangle::triangulate(
  112. const Eigen::MatrixXd& V,
  113. const Eigen::MatrixXi& E,
  114. const Eigen::MatrixXd& H,
  115. const Eigen::VectorXi& VM,
  116. const Eigen::VectorXi& EM,
  117. const std::string flags,
  118. Eigen::MatrixXd& V2,
  119. Eigen::MatrixXi& F2,
  120. Eigen::VectorXi& M2)
  121. {
  122. using namespace std;
  123. using namespace Eigen;
  124. // Prepare the flags
  125. string full_flags = flags + "pz";
  126. // Prepare the input struct
  127. triangulateio in;
  128. assert(V.cols() == 2);
  129. in.numberofpoints = V.rows();
  130. in.pointlist = (double*)calloc(V.rows()*2,sizeof(double));
  131. for (unsigned i=0;i<V.rows();++i)
  132. for (unsigned j=0;j<2;++j)
  133. in.pointlist[i*2+j] = V(i,j);
  134. in.numberofpointattributes = 0;
  135. in.pointmarkerlist = (int*)calloc(VM.rows(),sizeof(int));
  136. for (unsigned i=0;i<VM.rows();++i) {
  137. in.pointmarkerlist[i] = VM(i);
  138. }
  139. in.trianglelist = NULL;
  140. in.numberoftriangles = 0;
  141. in.numberofcorners = 0;
  142. in.numberoftriangleattributes = 0;
  143. in.triangleattributelist = NULL;
  144. in.numberofsegments = E.rows();
  145. in.segmentlist = (int*)calloc(E.rows()*2,sizeof(int));
  146. for (unsigned i=0;i<E.rows();++i)
  147. for (unsigned j=0;j<2;++j)
  148. in.segmentlist[i*2+j] = E(i,j);
  149. in.segmentmarkerlist = (int*)calloc(E.rows(),sizeof(int));
  150. for (unsigned i=0;i<E.rows();++i)
  151. in.segmentmarkerlist[i] = EM(i);
  152. in.numberofholes = H.rows();
  153. in.holelist = (double*)calloc(H.rows()*2,sizeof(double));
  154. for (unsigned i=0;i<H.rows();++i)
  155. for (unsigned j=0;j<2;++j)
  156. in.holelist[i*2+j] = H(i,j);
  157. in.numberofregions = 0;
  158. // Prepare the output struct
  159. triangulateio out;
  160. out.pointlist = NULL;
  161. out.trianglelist = NULL;
  162. out.segmentlist = NULL;
  163. out.segmentmarkerlist = NULL;
  164. out.pointmarkerlist = NULL;
  165. // Call triangle
  166. ::triangulate(const_cast<char*>(full_flags.c_str()), &in, &out, 0);
  167. // Return the mesh
  168. V2.resize(out.numberofpoints,2);
  169. for (unsigned i=0;i<V2.rows();++i)
  170. for (unsigned j=0;j<2;++j)
  171. V2(i,j) = out.pointlist[i*2+j];
  172. F2.resize(out.numberoftriangles,3);
  173. for (unsigned i=0;i<F2.rows();++i)
  174. for (unsigned j=0;j<3;++j)
  175. F2(i,j) = out.trianglelist[i*3+j];
  176. M2.resize(out.numberofpoints);
  177. for (unsigned int i = 0; i < out.numberofpoints; ++i) {
  178. M2(i) = out.pointmarkerlist[i];
  179. }
  180. // Cleanup in
  181. free(in.pointlist);
  182. free(in.pointmarkerlist);
  183. free(in.segmentlist);
  184. free(in.segmentmarkerlist);
  185. free(in.holelist);
  186. // Cleanup out
  187. free(out.pointlist);
  188. free(out.trianglelist);
  189. free(out.segmentlist);
  190. free(out.segmentmarkerlist);
  191. free(out.pointmarkerlist);
  192. }
  193. #ifdef IGL_STATIC_LIBRARY
  194. // Explicit template specialization
  195. #endif