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. #endif
  12. #ifdef REAL
  13. # define IGL_PREVIOUSLY_DEFINED_REAL REAL
  14. #endif
  15. #ifdef VOID
  16. # define IGL_PREVIOUSLY_DEFINED_VOID VOID
  17. #endif
  18. #define ANSI_DECLARATORS
  19. #define REAL double
  20. #define VOID int
  21. extern "C"
  22. {
  23. #include <triangle.h>
  24. }
  25. #ifdef IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS
  26. # define ANSI_DECLARATORS IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS
  27. #else
  28. # undef ANSI_DECLARATORS
  29. #endif
  30. #ifdef IGL_PREVIOUSLY_DEFINED_REAL
  31. # define REAL IGL_PREVIOUSLY_DEFINED_REAL
  32. #else
  33. # undef REAL
  34. #endif
  35. #ifdef IGL_PREVIOUSLY_DEFINED_VOID
  36. # define VOID IGL_PREVIOUSLY_DEFINED_VOID
  37. #else
  38. # undef VOID
  39. #endif
  40. IGL_INLINE void igl::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 + "pzBV";
  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. // Cleanup in
  91. free(in.pointlist);
  92. free(in.pointmarkerlist);
  93. free(in.segmentlist);
  94. free(in.segmentmarkerlist);
  95. free(in.holelist);
  96. // Cleanup out
  97. free(out.pointlist);
  98. free(out.trianglelist);
  99. free(out.segmentlist);
  100. // Return the mesh
  101. V2.resize(out.numberofpoints,2);
  102. for (unsigned i=0;i<V2.rows();++i)
  103. for (unsigned j=0;j<2;++j)
  104. V2(i,j) = out.pointlist[i*2+j];
  105. F2.resize(out.numberoftriangles,3);
  106. for (unsigned i=0;i<F2.rows();++i)
  107. for (unsigned j=0;j<3;++j)
  108. F2(i,j) = out.trianglelist[i*3+j];
  109. }
  110. #ifdef IGL_STATIC_LIBRARY
  111. // Explicit template instanciation
  112. #endif