Browse Source

started to support the viewer. Working on adding callbacks management

Former-commit-id: f24ea0f8829ad19ab263535e4a7607613be7650c
Daniele Panozzo 9 years ago
parent
commit
5ea66d91e2
6 changed files with 109 additions and 17 deletions
  1. 4 15
      python/102_DrawMesh.py
  2. 44 0
      python/103_Events.py
  3. 2 2
      python/202_GaussianCurvature.py
  4. 24 0
      python/CMakeLists.txt
  5. 33 0
      python/py_igl_viewer.cpp
  6. 2 0
      python/python.cpp

+ 4 - 15
python/102_DrawMesh.py

@@ -1,22 +1,11 @@
-import sys, os
-import numpy as np
 import igl
 
-import matplotlib.pyplot as plt
-from mpl_toolkits.mplot3d import Axes3D
-import matplotlib.tri as mtri
-
 # Load a mesh in OFF format
 V = igl.eigen.MatrixXd()
 F = igl.eigen.MatrixXi()
 igl.readOFF("../tutorial/shared/beetle.off", V, F)
 
-# Convert the mesh to numpy matrices (without copying it)
-Vn = np.array(V, copy=False)
-Fn = np.array(F, copy=False)
-
-# Plot using matplotlib
-fig = plt.figure()
-ax = fig.add_subplot(1, 1, 1, projection='3d')
-ax.plot_trisurf(Vn[:,0], Vn[:,1], Vn[:,2], triangles=Fn, cmap=plt.cm.Spectral)
-plt.show()
+# Plot the mesh
+viewer = igl.viewer.Viewer();
+viewer.data.set_mesh(V, F);
+viewer.launch();

+ 44 - 0
python/103_Events.py

@@ -0,0 +1,44 @@
+import igl
+
+V1 = igl.eigen.MatrixXd()
+F1 = igl.eigen.MatrixXi()
+
+V2 = igl.eigen.MatrixXd()
+F2 = igl.eigen.MatrixXi()
+
+def key_down(viewer, key, modifier):
+    print("Key: ",key)
+
+    if key == 1:
+        # Clear should be called before drawing the mesh
+        viewer.data.clear();
+        # Draw_mesh creates or updates the vertices and faces of the displayed mesh.
+        # If a mesh is already displayed, draw_mesh returns an error if the given V and
+        # F have size different than the current ones
+        viewer.data.set_mesh(V1, F1);
+        viewer.core.align_camera_center(V1,F1);
+    elif key == 2:
+        viewer.data.clear();
+        viewer.data.set_mesh(V2, F2);
+        viewer.core.align_camera_center(V2,F2);
+
+  return False;
+
+
+#  Load two meshes
+igl.readOFF("../tutorial/shared/bumpy.off", V1, F1);
+igl.readOFF("../tutorial/shared/fertility.off", V2, F2);
+
+print("1 Switch to bump mesh")
+print("2 Switch to fertility mesh")
+
+viewer = igl.viewer.Viewer()
+
+# Register a keyboard callback that allows to switch between
+# the two loaded meshes
+
+viewer.callback_key_down = key_down;
+viewer.data.set_mesh(V1, F1);
+viewer.launch();
+
+}

+ 2 - 2
python/202_GaussianCurvature.py

@@ -1,8 +1,8 @@
 import igl
 
 # Load mesh
-V = igl.eigen.MatrixXd();
-F = igl.eigen.MatrixXi();
+V = igl.eigen.MatrixXd()
+F = igl.eigen.MatrixXi()
 igl.readOFF("../tutorial/shared/bumpy.off",V,F);
 
 # Compute Gaussian curvature

+ 24 - 0
python/CMakeLists.txt

@@ -65,16 +65,40 @@ include_directories(${PROJECT_SOURCE_DIR}/../external/nanogui/ext/eigen)
 ## include libigl
 include_directories(${PROJECT_SOURCE_DIR}/../include)
 
+# include nanogui and dependencies
+add_subdirectory("../external/nanogui/" "nanogui")
+include_directories("../external/nanogui/include")
+include_directories("../external/nanogui/ext/nanovg/src")
+list(APPEND SHARED_LIBRARIES "nanogui" "glfw")
+
+
 add_library(igl SHARED
   python.cpp
   py_vector.cpp
   py_igl.cpp
   py_doc.cpp
+  py_igl_viewer.cpp
 )
 
+# if(NOT APPLE)
+# 	find_package(GLEW REQUIRED)
+# endif(NOT APPLE)
+
+if(APPLE)
+	set(CMAKE_SHARED_LINKER_FLAGS "-framework OpenGL -framework Cocoa")
+endif (APPLE) #APPLE
+#
+# if(UNIX AND NOT APPLE)
+#   set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -lGL -lGLU -lrt -lX11 -lXxf86vm -lXrandr -lpthread -lXi  -lXcursor -lXinerama ")
+#   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx")
+# endif(UNIX AND NOT APPLE)
+
+
 set_target_properties(igl PROPERTIES PREFIX "")
 set_target_properties(igl PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
 
+target_link_libraries(igl ${SHARED_LIBRARIES})
+
 if (WIN32)
   if (MSVC)
     # Enforce size-based optimization and link time code generation on MSVC (~30% smaller binaries in experiments)

+ 33 - 0
python/py_igl_viewer.cpp

@@ -0,0 +1,33 @@
+#include <Eigen/Dense>
+#include <Eigen/Sparse>
+
+#include "python.h"
+#include <igl/viewer/Viewer.h>
+
+void python_export_igl_viewer(py::module &m)
+{
+  py::module me = m.def_submodule(
+    "viewer", "Mesh viewer");
+
+    py::class_<igl::viewer::ViewerData> viewerdata_class(me, "ViewerData");
+    viewerdata_class
+    .def(py::init<>())
+    .def("set_mesh", &igl::viewer::ViewerData::set_mesh)
+    ;
+
+    py::class_<igl::viewer::ViewerCore> viewercore_class(me, "ViewerCore");
+    viewercore_class
+    .def(py::init<>())
+    .def("align_camera_center", [](igl::viewer::ViewerCore& core, const Eigen::MatrixXd& V, const Eigen::MatrixXi& F){return core.align_camera_center(V,F);})
+    //.def("align_camera_center", &igl::viewer::ViewerCore::align_camera_center)
+    .def("init", &igl::viewer::ViewerCore::init)
+    ;
+
+    py::class_<igl::viewer::Viewer> viewer_class(me, "Viewer");
+    viewer_class
+    .def(py::init<>())
+    .def_readwrite("data", &igl::viewer::Viewer::data)
+    .def_readwrite("core", &igl::viewer::Viewer::core)
+    .def("launch", &igl::viewer::Viewer::launch, py::arg("resizable") = true, py::arg("fullscreen") = false)
+    ;
+}

+ 2 - 0
python/python.cpp

@@ -5,6 +5,7 @@
 
 extern void python_export_vector(py::module &);
 extern void python_export_igl(py::module &);
+extern void python_export_igl_viewer(py::module &);
 
 PYTHON_PLUGIN(igl) {
     py::init_threading();
@@ -12,6 +13,7 @@ PYTHON_PLUGIN(igl) {
 
     python_export_vector(m);
     python_export_igl(m);
+    python_export_igl_viewer(m);
 
     return m.ptr();
 }