Răsfoiți Sursa

- added wrapper for triangle library
- added tutorial example for triangle


Former-commit-id: a7029dfc2a82727ee1289962701cc1a1e5846403

Daniele Panozzo 11 ani în urmă
părinte
comite
7460ef1034

+ 101 - 0
include/igl/triangle/triangle_wrapper.cpp

@@ -0,0 +1,101 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
+//
+// 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 "triangle_wrapper.h"
+#define REAL double
+#define VOID int
+
+extern "C"
+{
+#include <triangle.h>
+}
+
+IGL_INLINE void igl::triangle_wrapper(
+  const Eigen::MatrixXd& V,
+  const Eigen::MatrixXi& E,
+  const Eigen::MatrixXd& H,
+  Eigen::MatrixXd& V2,
+  Eigen::MatrixXi& F2,
+  const std::string flags)
+{
+  using namespace std;
+  using namespace Eigen;
+
+  // Prepare the flags
+  string full_flags = flags + "pzBV";
+
+  // Prepare the input struct
+  triangulateio in;
+
+  assert(V.cols() == 2);
+
+  in.numberofpoints = V.rows();
+  in.pointlist = (double*)calloc(V.rows()*2,sizeof(double));
+  for (unsigned i=0;i<V.rows();++i)
+    for (unsigned j=0;j<2;++j)
+      in.pointlist[i*2+j] = V(i,j);
+
+  in.numberofpointattributes = 0;
+  in.pointmarkerlist = (int*)calloc(V.rows(),sizeof(int));
+   for (unsigned i=0;i<V.rows();++i)
+     in.pointmarkerlist[i] = 1;
+
+  in.trianglelist = NULL;
+  in.numberoftriangles = 0;
+  in.numberofcorners = 0;
+  in.numberoftriangleattributes = 0;
+  in.triangleattributelist = NULL;
+
+  in.numberofsegments = E.rows();
+  in.segmentlist = (int*)calloc(E.rows()*2,sizeof(int));
+  for (unsigned i=0;i<E.rows();++i)
+    for (unsigned j=0;j<2;++j)
+      in.segmentlist[i*2+j] = E(i,j);
+  in.segmentmarkerlist = (int*)calloc(E.rows(),sizeof(int));
+  for (unsigned i=0;i<E.rows();++i)
+    in.segmentmarkerlist[i] = 1;
+
+  in.numberofholes = H.rows();
+  in.holelist = (double*)calloc(H.rows()*2,sizeof(double));
+  for (unsigned i=0;i<H.rows();++i)
+    for (unsigned j=0;j<2;++j)
+      in.holelist[i*2+j] = H(i,j);
+  in.numberofregions = 0;
+
+  // Prepare the output struct
+  triangulateio out;
+
+  out.pointlist = NULL;
+  out.trianglelist = NULL;
+  out.segmentlist = NULL;
+
+  // Call triangle
+  triangulate(const_cast<char*>(full_flags.c_str()), &in, &out, 0);
+
+  // Cleanup in
+  free(in.pointlist);
+  free(in.pointmarkerlist);
+  free(in.segmentlist);
+  free(in.segmentmarkerlist);
+  free(in.holelist);
+
+  // Cleanup out
+  V2.resize(out.numberofpoints,2);
+  for (unsigned i=0;i<V2.rows();++i)
+    for (unsigned j=0;j<2;++j)
+      V2(i,j) = out.pointlist[i*2+j];
+  
+  F2.resize(out.numberoftriangles,3);
+  for (unsigned i=0;i<F2.rows();++i)
+    for (unsigned j=0;j<3;++j)
+      F2(i,j) = out.trianglelist[i*3+j];
+  
+}
+
+#ifndef IGL_HEADER_ONLY
+// Explicit template instanciation
+#endif

+ 42 - 0
include/igl/triangle/triangle_wrapper.h

@@ -0,0 +1,42 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
+//
+// 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_WRAPPER_H
+#define IGL_TRIANGLE_WRAPPER_H
+#include <igl/igl_inline.h>
+#include <string>
+#include <Eigen/Core>
+
+namespace igl
+{
+  // Triangulate the interior of a polygon using the triangle library.
+  //
+  // Inputs:
+  //   V #V by 2 list of 2D vertex positions
+  //   E #E by 2 list of vertex ids forming unoriented edges of the boundary of the polygon
+  //   H #H by 2 coordinates of points contained inside holes of the polygon
+  // Outputs:
+  //   V2  #V2 by 2  coordinates of the vertives of the generated triangulation
+  //   F2  #F2 by 3  list of indices forming the faces of the generated triangulation
+  //
+  // TODO: expose the option to prevent Steiner points on the boundary
+  //
+  IGL_INLINE void triangle_wrapper(
+    const Eigen::MatrixXd& V,
+    const Eigen::MatrixXi& E,
+    const Eigen::MatrixXd& H,
+    Eigen::MatrixXd& V2,
+    Eigen::MatrixXi& F2,
+    const std::string flags = std::string("q"));
+
+}
+
+#ifdef IGL_HEADER_ONLY
+#  include "triangle_wrapper.cpp"
+#endif
+
+#endif

+ 14 - 0
tutorial/604_Triangle/CMakeLists.txt

@@ -0,0 +1,14 @@
+cmake_minimum_required(VERSION 2.6)
+project(601_Serialization)
+
+include("../CMakeLists.shared")
+find_package(TRIANGLE REQUIRED)
+
+include_directories( ${TRIANGLE_INCLUDE_DIR})
+
+set(SOURCES
+${PROJECT_SOURCE_DIR}/main.cpp
+)
+
+add_executable(${CMAKE_PROJECT_NAME} ${SOURCES} ${SHARED_SOURCES} ${TRIANGLE_SOURCES})
+target_link_libraries(${CMAKE_PROJECT_NAME} ${SHARED_LIBRARIES})

+ 37 - 0
tutorial/604_Triangle/main.cpp

@@ -0,0 +1,37 @@
+#include <igl/viewer/Viewer.h>
+#include <igl/triangle/triangle_wrapper.h>
+// Input polygon
+Eigen::MatrixXd V;
+Eigen::MatrixXi E;
+Eigen::MatrixXd H;
+
+// Triangulated interior
+Eigen::MatrixXd V2;
+Eigen::MatrixXi F2;
+
+int main(int argc, char *argv[])
+{
+  using namespace Eigen;
+  using namespace std;
+
+  // Create the boundary of a square
+  V.resize(8,2);
+  E.resize(8,2);
+  H.resize(1,2);
+
+  V << -1,-1, 1,-1, 1,1, -1, 1,
+       -2,-2, 2,-2, 2,2, -2, 2;
+
+  E << 0,1, 1,2, 2,3, 3,0,
+       4,5, 5,6, 6,7, 7,4;
+
+  H << 0,0;
+
+  // Triangulate the interior
+  igl::triangle_wrapper(V,E,H,V2,F2,"a0.005q");
+
+  // Plot the generated mesh
+  igl::Viewer viewer;
+  viewer.set_mesh(V2,F2);
+  viewer.launch();
+}

+ 27 - 0
tutorial/cmake/FindTRIANGLE.cmake

@@ -0,0 +1,27 @@
+# - Try to find the TRIANGLE library
+# Once done this will define
+#
+#  TRIANGLE_FOUND - system has TRIANGLE
+#  TRIANGLE_INCLUDE_DIR - the TRIANGLE include directory
+#  TRIANGLE_SOURCES - the TRIANGLE source files
+
+FIND_PATH(TRIANGLE_INCLUDE_DIR triangle.h
+   /usr/include
+   /usr/local/include
+   ${PROJECT_SOURCE_DIR}/../libigl/external/triangle/
+   ${PROJECT_SOURCE_DIR}/../../external/triangle/
+   NO_DEFAULT_PATH
+)
+
+set(TRIANGLE_SOURCES ${TRIANGLE_INCLUDE_DIR}/triangle.c)
+
+if(TRIANGLE_INCLUDE_DIR)
+   message(STATUS "Found TRIANGLE: ${TRIANGLE_INCLUDE_DIR}")
+else(TRIANGLE_INCLUDE_DIR)
+   message(FATAL_ERROR "could NOT find TRIANGLE")
+endif(TRIANGLE_INCLUDE_DIR)
+
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DTRILIBRARY -DANSI_DECLARATORS")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTRILIBRARY -DANSI_DECLARATORS")
+
+MARK_AS_ADVANCED(TRIANGLE_INCLUDE_DIR TRIANGLE_LIBRARIES TRIANGLE_SOURCES)