refine.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2024 Alec Jacobson
  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 "refine.h"
  9. #include "triangle_header.h"
  10. template <
  11. typename DerivedV,
  12. typename DerivedE,
  13. typename DerivedF,
  14. typename DerivedV2,
  15. typename DerivedF2>
  16. IGL_INLINE void igl::triangle::refine(
  17. const Eigen::MatrixBase<DerivedV> & V,
  18. const Eigen::MatrixBase<DerivedE> & E,
  19. const Eigen::MatrixBase<DerivedF> & F,
  20. const std::string flags,
  21. Eigen::PlainObjectBase<DerivedV2> & V2,
  22. Eigen::PlainObjectBase<DerivedF2> & F2)
  23. {
  24. assert(V.cols() == 2);
  25. assert(F.cols() == 3);
  26. // Prepare the flags
  27. std::string full_flags = flags + "rzB" + (E.size()?"p":"");
  28. typedef Eigen::Map< Eigen::Matrix<double ,Eigen::Dynamic ,Eigen::Dynamic,Eigen::RowMajor> > MapXdr;
  29. typedef Eigen::Map< Eigen::Matrix<int ,Eigen::Dynamic ,Eigen::Dynamic,Eigen::RowMajor> > MapXir;
  30. // To-do: reduce duplicate code with triangulate.cpp
  31. // Prepare the input struct
  32. triangulateio in;
  33. in.numberofpoints = V.rows();
  34. in.pointlist = (double*)calloc(V.size(),sizeof(double));
  35. {
  36. MapXdr inpl(in.pointlist,V.rows(),V.cols());
  37. inpl = V.template cast<double>();
  38. }
  39. in.numberofpointattributes = 0;
  40. in.pointmarkerlist = (int*)calloc(V.size(),sizeof(int)) ;
  41. for(unsigned i=0;i<V.rows();++i) in.pointmarkerlist[i] = 1;
  42. in.numberoftriangles = F.rows();
  43. in.trianglelist = (int*)calloc(F.size(),sizeof(int));
  44. {
  45. MapXir insl(in.trianglelist,F.rows(),F.cols());
  46. insl = F.template cast<int>();
  47. }
  48. in.numberoftriangleattributes = 0;
  49. in.triangleattributelist = NULL;
  50. // Why?
  51. in.numberofcorners = 3;
  52. //in.numberofsegments = 0;
  53. //in.segmentlist = NULL;
  54. //in.segmentmarkerlist = NULL;
  55. in.numberofsegments = E.size()?E.rows():0;
  56. in.segmentlist = (int*)calloc(E.size(),sizeof(int));
  57. {
  58. MapXir insl(in.segmentlist,E.rows(),E.cols());
  59. insl = E.template cast<int>();
  60. }
  61. // Empty edge markers (to-do)
  62. Eigen::VectorXi EM;
  63. in.segmentmarkerlist = (int*)calloc(E.rows(),sizeof(int));
  64. for (unsigned i=0;i<E.rows();++i) in.segmentmarkerlist[i] = EM.size()?EM(i):1;
  65. in.numberofholes = 0;
  66. in.holelist = NULL;
  67. in.numberofregions = 0;
  68. // Prepare the output struct
  69. triangulateio out;
  70. out.pointlist = NULL;
  71. out.trianglelist = NULL;
  72. out.segmentlist = NULL;
  73. out.segmentmarkerlist = NULL;
  74. out.pointmarkerlist = NULL;
  75. // Call triangle
  76. ::triangulate(const_cast<char*>(full_flags.c_str()), &in, &out, 0);
  77. // Return the mesh
  78. V2 = MapXdr(out.pointlist,out.numberofpoints,2).cast<typename DerivedV2::Scalar>();
  79. F2 = MapXir(out.trianglelist,out.numberoftriangles,3).cast<typename DerivedF2::Scalar>();
  80. // Cleanup in
  81. free(in.pointlist);
  82. free(in.pointmarkerlist);
  83. free(in.trianglelist );
  84. // Cleanup out
  85. free(out.pointlist);
  86. free(out.trianglelist);
  87. free(out.segmentlist);
  88. free(out.segmentmarkerlist);
  89. free(out.pointmarkerlist);
  90. }
  91. #ifdef IGL_STATIC_LIBRARY
  92. // Explicit template instantiation
  93. template void igl::triangle::refine<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>>&);
  94. #endif