triangulate.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifdef IGL_STATIC_LIBRARY
  111. // Explicit template specialization
  112. #endif