Просмотр исходного кода

Restructuring Python bindings, moved submodules and dependencies to own folders, added dependency checking for some tutorials, minor refactorings

Former-commit-id: 2214125878970ad106dc6cdad9fcae3636f60eb4
Sebastian Koch 9 лет назад
Родитель
Сommit
ad00bd397f

+ 23 - 7
python/CMakeLists.txt

@@ -47,7 +47,7 @@ endif()
 
 include_directories(${PYTHON_INCLUDE_DIR} include)
 
-## include pybing
+## include pybind
 include_directories(${PROJECT_SOURCE_DIR}/../external/pybind11/include)
 
 ## include libigl
@@ -57,7 +57,7 @@ option(LIBIGL_WITH_NANOGUI          "Use Nanogui menu"   OFF)
 option(LIBIGL_WITH_CGAL             "Use CGAL"           OFF)
 option(LIBIGL_WITH_BOOLEAN          "Use Cork boolean"   OFF)
 option(LIBIGL_WITH_COMISO           "Use CoMiso"         ON)
-option(LIBIGL_WITH_EMBREE           "Use Embree"         OFF)
+option(LIBIGL_WITH_EMBREE           "Use Embree"         ON)
 option(LIBIGL_WITH_LIM              "Use LIM"            ON)
 option(LIBIGL_WITH_MATLAB           "Use Matlab"         OFF)
 option(LIBIGL_WITH_MOSEK            "Use MOSEK"          OFF)
@@ -84,19 +84,34 @@ add_definitions(${LIBIGL_DEFINITIONS})
 ## Optional modules
 if (LIBIGL_WITH_VIEWER)
   add_definitions(-DPY_VIEWER)
-  list(APPEND SHARED_SOURCES "py_igl_viewer.cpp")
+  list(APPEND SHARED_SOURCES "modules/py_igl_viewer.cpp")
 endif ()
 
 if (LIBIGL_WITH_COMISO)
   add_definitions(-DPY_COMISO)
-  list(APPEND SHARED_SOURCES "copyleft/py_igl_comiso.cpp")
+  list(APPEND SHARED_SOURCES "modules/copyleft/py_igl_comiso.cpp")
+endif ()
+
+if (LIBIGL_WITH_TETGEN)
+  add_definitions(-DPY_TETGEN)
+  list(APPEND SHARED_SOURCES "modules/copyleft/py_igl_tetgen.cpp")
+endif ()
+
+if (LIBIGL_WITH_EMBREE)
+  add_definitions(-DPY_EMBREE)
+  list(APPEND SHARED_SOURCES "modules/py_igl_embree.cpp")
+endif ()
+
+if (LIBIGL_WITH_TRIANGLE)
+  add_definitions(-DPY_TRIANGLE)
+  list(APPEND SHARED_SOURCES "modules/py_igl_triangle.cpp")
 endif ()
 
 
 ## Prepare the python library
 add_library(pyigl SHARED
   python_shared.cpp
-  py_vector.cpp
+  modules/py_vector.cpp
   py_igl.cpp
   py_doc.cpp
   ${SHARED_SOURCES}
@@ -142,10 +157,10 @@ elseif (UNIX)
   #Enable flag if undefined symbols appear on pyigl module import to get notified about the missing symbols at link time
   option(CHECK_UNDEFINED        "Check for undefined symbols"    OFF)
 
-  # Strip unnecessary sections of the binary on Linux/Mac OS
+  # Strip unnecessary sections of the binary on Linux/Mac OS 
   if(APPLE)
     set_target_properties(pyigl PROPERTIES MACOSX_RPATH ".")
-
+    
     if (NOT CHECK_UNDEFINED)
       set_target_properties(pyigl PROPERTIES LINK_FLAGS "-undefined dynamic_lookup -dead_strip")
     endif()
@@ -165,3 +180,4 @@ elseif (UNIX)
     endif()
   endif()
 endif()
+

+ 3 - 3
python/copyleft/py_igl_comiso.cpp → python/modules/copyleft/py_igl_comiso.cpp

@@ -3,7 +3,7 @@
 #include <Eigen/Sparse>
 
 
-#include "../python_shared.h"
+#include "../../python_shared.h"
 
 #include <igl/copyleft/comiso/nrosy.h>
 #include <igl/copyleft/comiso/miq.h>
@@ -13,7 +13,7 @@ void python_export_igl_comiso(py::module &me) {
   py::module m = me.def_submodule(
     "comiso", "Wrappers for libigl functions that use comiso");
 
-  #include "../py_igl/copyleft/comiso/py_nrosy.cpp"
-  #include "../py_igl/copyleft/comiso/py_miq.cpp"
+  #include "../../py_igl/copyleft/comiso/py_nrosy.cpp"
+  #include "../../py_igl/copyleft/comiso/py_miq.cpp"
 
 }

+ 18 - 0
python/modules/copyleft/py_igl_tetgen.cpp

@@ -0,0 +1,18 @@
+//#include <Eigen/Geometry>
+//#include <Eigen/Dense>
+//#include <Eigen/Sparse>
+
+
+#include "../../python_shared.h"
+
+#include <igl/copyleft/tetgen/tetrahedralize.h>
+
+
+void python_export_igl_tetgen(py::module &me) {
+
+  py::module m = me.def_submodule(
+    "tetgen", "Wrappers for libigl functions that use tetgen");
+
+  #include "../../py_igl/copyleft/tetgen/py_tetrahedralize.cpp"
+
+}

+ 18 - 0
python/modules/py_igl_embree.cpp

@@ -0,0 +1,18 @@
+//#include <Eigen/Geometry>
+//#include <Eigen/Dense>
+//#include <Eigen/Sparse>
+
+
+#include "../python_shared.h"
+
+#include <igl/embree/ambient_occlusion.h>
+
+
+void python_export_igl_embree(py::module &me) {
+
+  py::module m = me.def_submodule(
+    "embree", "Wrappers for libigl functions that use embree");
+
+  #include "../py_igl/embree/py_ambient_occlusion.cpp"
+
+}

+ 18 - 0
python/modules/py_igl_triangle.cpp

@@ -0,0 +1,18 @@
+//#include <Eigen/Geometry>
+//#include <Eigen/Dense>
+//#include <Eigen/Sparse>
+
+
+#include "../python_shared.h"
+
+#include <igl/triangle/triangulate.h>
+
+
+void python_export_igl_triangle(py::module &me) {
+
+  py::module m = me.def_submodule(
+    "triangle", "Wrappers for libigl functions that use triangle");
+
+  #include "../py_igl/triangle/py_triangulate.cpp"
+
+}

+ 1 - 1
python/py_igl_viewer.cpp → python/modules/py_igl_viewer.cpp

@@ -1,7 +1,7 @@
 #include <Eigen/Dense>
 #include <Eigen/Sparse>
 
-#include "python_shared.h"
+#include "../python_shared.h"
 #define ENABLE_SERIALIZATION
 #include <igl/viewer/Viewer.h>
 #include <igl/viewer/ViewerCore.h>

+ 1 - 1
python/py_vector.cpp → python/modules/py_vector.cpp

@@ -3,7 +3,7 @@
 #include <Eigen/Sparse>
 
 
-#include "python_shared.h"
+#include "../python_shared.h"
 
 /// Creates Python bindings for a dynamic Eigen matrix
 template <typename Type>

+ 0 - 6
python/py_igl.cpp

@@ -66,9 +66,6 @@
 #include <igl/signed_distance.h>
 #include <igl/quad_planarity.h>
 #include <igl/planarize_quad_mesh.h>
-#include <igl/triangle/triangulate.h>
-#include <igl/copyleft/tetgen/tetrahedralize.h>
-#include <igl/embree/ambient_occlusion.h>
 #include <igl/unproject_onto_mesh.h>
 //#include <igl/.h>
 
@@ -139,9 +136,6 @@ void python_export_igl(py::module &m)
 #include "py_igl/py_signed_distance.cpp"
 #include "py_igl/py_quad_planarity.cpp"
 #include "py_igl/py_planarize_quad_mesh.cpp"
-#include "py_igl/py_triangle_triangulate.cpp"
-#include "py_igl/py_copyleft_tetgen_tetrahedralize.cpp"
-#include "py_igl/py_embree_ambient_occlusion.cpp"
 #include "py_igl/py_unproject_onto_mesh.cpp"
 //#include "py_igl/py_.cpp"
 

+ 1 - 1
python/py_igl/py_copyleft_tetgen_tetrahedralize.cpp → python/py_igl/copyleft/tetgen/py_tetrahedralize.cpp

@@ -1,5 +1,5 @@
 
-m.def("copyleft_tetgen_tetrahedralize", []
+m.def("tetrahedralize", []
 (
   const Eigen::MatrixXd& V,
   const Eigen::MatrixXi& F,

+ 1 - 1
python/py_igl/py_embree_ambient_occlusion.cpp → python/py_igl/embree/py_ambient_occlusion.cpp

@@ -1,6 +1,6 @@
 
 
-m.def("embree_ambient_occlusion", []
+m.def("ambient_occlusion", []
 (
   const Eigen::MatrixXd& V,
   const Eigen::MatrixXi& F,

+ 1 - 1
python/py_igl/py_triangle_triangulate.cpp → python/py_igl/triangle/py_triangulate.cpp

@@ -1,6 +1,6 @@
 
 
-m.def("triangle_triangulate", []
+m.def("triangulate", []
 (
   const Eigen::MatrixXd& V,
   const Eigen::MatrixXi& E,

+ 24 - 0
python/python_shared.cpp

@@ -14,6 +14,18 @@ extern void python_export_igl_viewer(py::module &);
 extern void python_export_igl_comiso(py::module &);
 #endif
 
+#ifdef PY_TETGEN
+extern void python_export_igl_tetgen(py::module &);
+#endif
+
+#ifdef PY_EMBREE
+extern void python_export_igl_embree(py::module &);
+#endif
+
+#ifdef PY_TRIANGLE
+extern void python_export_igl_triangle(py::module &);
+#endif
+
 PYBIND11_PLUGIN(pyigl) {
     py::module m("pyigl", R"pyigldoc(
         Python wrappers for libigl
@@ -39,5 +51,17 @@ PYBIND11_PLUGIN(pyigl) {
     python_export_igl_comiso(m);
     #endif
 
+    #ifdef PY_TETGEN
+    python_export_igl_tetgen(m);
+    #endif
+
+    #ifdef PY_EMBREE
+    python_export_igl_embree(m);
+    #endif
+
+    #ifdef PY_TRIANGLE
+    python_export_igl_triangle(m);
+    #endif
+
     return m.ptr();
 }

+ 3 - 2
python/tutorial/101_FileIO.py

@@ -1,10 +1,11 @@
 from __future__ import print_function
-# Add the igl library to the modules search path
 import sys, os
-sys.path.insert(0, os.getcwd() + "/../")
 
+# Add the igl library to the modules search path
+sys.path.insert(0, os.getcwd() + "/../")
 import pyigl as igl
 
+
 # Load a mesh in OFF format
 V = igl.eigen.MatrixXd()
 F = igl.eigen.MatrixXi()

+ 11 - 6
python/tutorial/102_DrawMesh.py

@@ -1,15 +1,20 @@
-# Add the igl library to the modules search path
 import sys, os
-sys.path.insert(0, os.getcwd() + "/../")
 
+# Add the igl library to the modules search path
+sys.path.insert(0, os.getcwd() + "/../")
 import pyigl as igl
 
+from shared import TUTORIAL_SHARED_PATH, check_dependencies
+
+dependencies = ["viewer"]
+check_dependencies(dependencies)
+
 # Load a mesh in OFF format
 V = igl.eigen.MatrixXd()
 F = igl.eigen.MatrixXi()
-igl.readOFF("../../tutorial/shared/beetle.off", V, F)
+igl.readOFF(TUTORIAL_SHARED_PATH + "beetle.off", V, F)
 
 # Plot the mesh
-viewer = igl.viewer.Viewer();
-viewer.data.set_mesh(V, F);
-viewer.launch();
+viewer = igl.viewer.Viewer()
+viewer.data.set_mesh(V, F)
+viewer.launch()

+ 10 - 4
python/tutorial/507_PolyVectorField.py

@@ -1,11 +1,17 @@
-# Add the igl library to the modules search path
 import sys, os
-sys.path.insert(0, os.getcwd() + "/../")
-
-import pyigl as igl
 import random
 from math import cos,sin,pi
 
+# Add the igl library to the modules search path
+sys.path.insert(0, os.getcwd() + "/../")
+import pyigl as igl
+
+from shared import TUTORIAL_SHARED_PATH, check_dependencies
+
+dependencies = ["embree", "viewer"]
+check_dependencies(dependencies)
+
+
 # Input mesh
 V = igl.eigen.MatrixXd()
 F = igl.eigen.MatrixXi()

+ 9 - 5
python/tutorial/509_Planarization.py

@@ -1,13 +1,17 @@
-# Add the igl library to the modules search path
 import sys, os
+import random
+from math import cos, sin, pi
 
+# Add the igl library to the modules search path
 sys.path.insert(0, os.getcwd() + "/../")
-
 import pyigl as igl
-import random
-from math import cos, sin, pi
 
-TUTORIAL_SHARED_PATH = "../../tutorial/shared/"
+from shared import TUTORIAL_SHARED_PATH, check_dependencies
+
+dependencies = ["viewer"]
+check_dependencies(dependencies)
+
+
 
 viewer = igl.viewer.Viewer()
 

+ 8 - 3
python/tutorial/604_Triangle.py

@@ -1,9 +1,14 @@
-# Add the igl library to the modules search path
 import sys, os
-sys.path.insert(0, os.getcwd() + "/../")
 
+# Add the igl library to the modules search path
+sys.path.insert(0, os.getcwd() + "/../")
 import pyigl as igl
 
+from shared import check_dependencies
+
+dependencies = ["triangle", "viewer"]
+check_dependencies(dependencies)
+
 # Input polygon
 V = igl.eigen.MatrixXd([[-1, -1], [1, -1], [1, 1], [-1, 1], [-2, -2], [2, -2], [2, 2], [-2, 2]])
 E = igl.eigen.MatrixXi([[0, 1], [1, 2], [2, 3], [3, 0], [4, 5], [5, 6], [6,7], [7,4]])
@@ -13,7 +18,7 @@ H = igl.eigen.MatrixXd([[0, 0]])
 V2 = igl.eigen.MatrixXd()
 F2 = igl.eigen.MatrixXi()
 
-igl.triangle_triangulate(V, E, H, "a0.005q", V2, F2)
+igl.triangle.triangulate(V, E, H, "a0.005q", V2, F2)
 
 # Plot the mesh
 viewer = igl.viewer.Viewer()

+ 7 - 4
python/tutorial/605_Tetgen.py

@@ -1,10 +1,13 @@
-# Add the igl library to the modules search path
 import sys, os
-sys.path.insert(0, os.getcwd() + "/../")
 
+# Add the igl library to the modules search path
+sys.path.insert(0, os.getcwd() + "/../")
 import pyigl as igl
 
-TUTORIAL_SHARED_PATH = "../../tutorial/shared/"
+from shared import TUTORIAL_SHARED_PATH, check_dependencies
+
+dependencies = ["tetgen", "viewer"]
+check_dependencies(dependencies)
 
 
 # Input polygon
@@ -60,7 +63,7 @@ def key_down(viewer, key, modifier):
 igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
 
 # Tetrahedralize the interior
-igl.copyleft_tetgen_tetrahedralize(V, F, "pq1.414Y", TV, TT, TF)
+igl.tetgen.tetrahedralize(V, F, "pq1.414Y", TV, TT, TF)
 
 # Compute barycenters
 igl.barycenter(TV, TT, B)

+ 6 - 5
python/tutorial/606_AmbientOcclusion.py

@@ -1,13 +1,14 @@
-# Add the igl library to the modules search path
 import sys, os
-
 import math
 
+# Add the igl library to the modules search path
 sys.path.insert(0, os.getcwd() + "/../")
-
 import pyigl as igl
 
-TUTORIAL_SHARED_PATH = "../../tutorial/shared/"
+from shared import TUTORIAL_SHARED_PATH, check_dependencies
+
+dependencies = ["embree", "viewer"]
+check_dependencies(dependencies)
 
 
 # Mesh + AO values + Normals
@@ -53,7 +54,7 @@ igl.readOFF(TUTORIAL_SHARED_PATH + "fertility.off", V, F)
 igl.per_vertex_normals(V, F, N)
 
 # Compute ambient occlusion factor using embree
-igl.embree_ambient_occlusion(V, F, V, N, 500, AO)
+igl.embree.ambient_occlusion(V, F, V, N, 500, AO)
 AO = 1.0 - AO
 
 # Plot the generated mesh

+ 7 - 3
python/tutorial/607_Picking.py

@@ -1,10 +1,14 @@
-# Add the igl library to the modules search path
 import sys, os
-sys.path.insert(0, os.getcwd() + "/../")
 
+# Add the igl library to the modules search path
+sys.path.insert(0, os.getcwd() + "/../")
 import pyigl as igl
 
-TUTORIAL_SHARED_PATH = "../../tutorial/shared/"
+
+from shared import TUTORIAL_SHARED_PATH, check_dependencies
+
+dependencies = ["viewer"]
+check_dependencies(dependencies)
 
 
 # Mesh with per-face color

+ 8 - 6
python/tutorial/704_SignedDistance.py

@@ -1,15 +1,17 @@
-from __future__ import print_function
-
-# Add the igl library to the modules search path
 import sys, os
+import math
 
+# Add the igl library to the modules search path
 sys.path.insert(0, os.getcwd() + "/../")
-
 import pyigl as igl
+
 from iglhelpers import e2p
-import math
+from shared import TUTORIAL_SHARED_PATH, check_dependencies
+
+dependencies = ["viewer"]
+check_dependencies(dependencies)
+
 
-TUTORIAL_SHARED_PATH = "../../tutorial/shared/"
 
 global V, F, T, tree, FN, VN, EN, E, EMAP, max_distance, slice_z, overlay
 

+ 16 - 0
python/tutorial/shared.py

@@ -0,0 +1,16 @@
+import pyigl as igl
+import sys
+
+
+TUTORIAL_SHARED_PATH = "../../tutorial/shared/"
+
+def check_dependencies(deps):
+    available = [hasattr(igl, m) for m in deps]
+    all_available = True
+    for i, d in enumerate(available):
+        if not d:
+            all_available = False
+            print("The libigl python bindings were compiled without %s support. Please recompile with the CMAKE flag LIBIGL_WITH_%s." %(deps[i], deps[i].upper()))
+
+    if not all_available:
+        sys.exit(-1)