Browse Source

add refine functionality for triangle wrapper (#2402)

Alec Jacobson 1 year ago
parent
commit
0e02103df7

+ 96 - 0
include/igl/triangle/refine.cpp

@@ -0,0 +1,96 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2024 Alec Jacobson
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
+// obtain one at http://mozilla.org/MPL/2.0/.
+#include "refine.h"
+#include "triangle_header.h"
+
+template <
+  typename DerivedV,
+  typename DerivedF,
+  typename DerivedV2,
+  typename DerivedF2>
+IGL_INLINE void igl::triangle::refine(
+  const Eigen::MatrixBase<DerivedV> & V,
+  const Eigen::MatrixBase<DerivedF> & F,
+  const std::string flags,
+  Eigen::PlainObjectBase<DerivedV2> & V2,
+  Eigen::PlainObjectBase<DerivedF2> & F2)
+{
+  using namespace Eigen;
+  using namespace Eigen;
+  assert(V.cols() == 2);
+  assert(F.cols() == 3);
+  // Prepare the flags
+  std::string full_flags = flags + "rzB";
+
+  typedef Map< Matrix<double,Dynamic,Dynamic,RowMajor> > MapXdr;
+  typedef Map< Matrix<int,Dynamic,Dynamic,RowMajor> > MapXir;
+ 
+  // To-do: reduce duplicate code with triangulate.cpp
+  // Prepare the input struct
+  triangulateio in;
+  in.numberofpoints = V.rows();
+  in.pointlist = (double*)calloc(V.size(),sizeof(double));
+  {
+    MapXdr inpl(in.pointlist,V.rows(),V.cols());
+    inpl = V.template cast<double>();
+  }
+
+  in.numberofpointattributes = 0;
+  in.pointmarkerlist = (int*)calloc(V.size(),sizeof(int)) ;
+  for(unsigned i=0;i<V.rows();++i) in.pointmarkerlist[i] = 1;
+
+  in.numberoftriangles = F.rows();
+  in.trianglelist = (int*)calloc(F.size(),sizeof(int));
+  {
+    MapXir insl(in.trianglelist,F.rows(),F.cols());
+    insl = F.template cast<int>();
+  }
+  in.numberoftriangleattributes = 0;
+  in.triangleattributelist = NULL;
+
+  // Why?
+  in.numberofcorners = 3;
+
+  in.numberofsegments = 0;
+  in.segmentlist = NULL;
+  in.segmentmarkerlist = NULL;
+  in.numberofholes = 0;
+  in.holelist = NULL;
+  in.numberofregions = 0;
+ 
+  // Prepare the output struct
+  triangulateio out;
+  out.pointlist = NULL;
+  out.trianglelist = NULL;
+  out.segmentlist = NULL;
+  out.segmentmarkerlist = NULL;
+  out.pointmarkerlist = NULL;
+
+  // Call triangle
+  ::triangulate(const_cast<char*>(full_flags.c_str()), &in, &out, 0);
+
+  // Return the mesh
+  V2 = MapXdr(out.pointlist,out.numberofpoints,2).cast<typename DerivedV2::Scalar>();
+  F2 = MapXir(out.trianglelist,out.numberoftriangles,3).cast<typename DerivedF2::Scalar>();
+
+  // Cleanup in
+  free(in.pointlist);
+  free(in.pointmarkerlist);
+  free(in.trianglelist );
+  // Cleanup out
+  free(out.pointlist);
+  free(out.trianglelist);
+  free(out.segmentlist);
+  free(out.segmentmarkerlist);
+  free(out.pointmarkerlist);
+}
+
+#ifdef IGL_STATIC_LIBRARY
+// Explicit template instantiation
+template void igl::triangle::refine<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<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&, 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>>&);
+#endif

+ 44 - 0
include/igl/triangle/refine.h

@@ -0,0 +1,44 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2024 Alec Jacobson
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
+// obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef IGL_TRIANGLE_REFINE_H
+#define IGL_TRIANGLE_REFINE_H
+#include "../igl_inline.h"
+#include <string>
+#include <Eigen/Core>
+
+namespace igl
+{
+  namespace triangle
+  {
+    /// Refine an existing triangulation.
+    ///
+    /// @param[in] V #V by 2 list of 2D vertex positions
+    /// @param[in] F #F by 3 list of vertex ids forming triangles
+    /// @param[in] flags  string of options pass to triangle (see triangle documentation)
+    /// @param[out] V2  #V2 by 2  coordinates of the vertives of the generated triangulation
+    /// @param[out] F2  #F2 by 3  list of indices forming the faces of the generated triangulation
+    template <
+      typename DerivedV,
+      typename DerivedF,
+      typename DerivedV2,
+      typename DerivedF2>
+    IGL_INLINE void refine(
+      const Eigen::MatrixBase<DerivedV> & V,
+      const Eigen::MatrixBase<DerivedF> & F,
+      const std::string flags,
+      Eigen::PlainObjectBase<DerivedV2> & V2,
+      Eigen::PlainObjectBase<DerivedF2> & F2);
+  }
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#  include "refine.cpp"
+#endif
+
+#endif
+

+ 37 - 0
include/igl/triangle/triangle_header.h

@@ -0,0 +1,37 @@
+#ifndef IGL_TRIANGLE_TRIANGLE_HEADER_H
+#define IGL_TRIANGLE_TRIANGLE_HEADER_H
+
+#ifdef ANSI_DECLARATORS
+#  define IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS ANSI_DECLARATORS
+#  undef ANSI_DECLARATORS
+#endif
+#ifdef REAL
+#  define IGL_PREVIOUSLY_DEFINED_REAL REAL
+#  undef REAL
+#endif
+#ifdef VOID
+#  define IGL_PREVIOUSLY_DEFINED_VOID VOID
+#  undef VOID
+#endif
+#define ANSI_DECLARATORS
+#define REAL double
+#define VOID int
+
+#include <triangle.h>
+
+#undef ANSI_DECLARATORS
+#ifdef IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS
+#  define ANSI_DECLARATORS IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS
+#endif
+
+#undef REAL
+#ifdef IGL_PREVIOUSLY_DEFINED_REAL
+#  define REAL IGL_PREVIOUSLY_DEFINED_REAL
+#endif
+
+#undef VOID
+#ifdef IGL_PREVIOUSLY_DEFINED_VOID
+#  define VOID IGL_PREVIOUSLY_DEFINED_VOID
+#endif
+
+#endif

+ 1 - 31
include/igl/triangle/triangulate.cpp

@@ -6,38 +6,8 @@
 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "triangulate.h"
 #include "triangulate.h"
-#ifdef ANSI_DECLARATORS
-#  define IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS ANSI_DECLARATORS
-#  undef ANSI_DECLARATORS
-#endif
-#ifdef REAL
-#  define IGL_PREVIOUSLY_DEFINED_REAL REAL
-#  undef REAL
-#endif
-#ifdef VOID
-#  define IGL_PREVIOUSLY_DEFINED_VOID VOID
-#  undef VOID
-#endif
-#define ANSI_DECLARATORS
-#define REAL double
-#define VOID int
+#include "triangle_header.h"
 
 
-#include <triangle.h>
-
-#undef ANSI_DECLARATORS
-#ifdef IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS
-#  define ANSI_DECLARATORS IGL_PREVIOUSLY_DEFINED_ANSI_DECLARATORS
-#endif
-
-#undef REAL
-#ifdef IGL_PREVIOUSLY_DEFINED_REAL
-#  define REAL IGL_PREVIOUSLY_DEFINED_REAL
-#endif
-
-#undef VOID
-#ifdef IGL_PREVIOUSLY_DEFINED_VOID
-#  define VOID IGL_PREVIOUSLY_DEFINED_VOID
-#endif
 
 
 template <
 template <
  typename DerivedV,
  typename DerivedV,