Bläddra i källkod

simplify 501 tut; fix viewer bug; generalize 201

Alec Jacobson 4 år sedan
förälder
incheckning
6f0233183d
3 ändrade filer med 39 tillägg och 31 borttagningar
  1. 10 2
      include/igl/opengl/ViewerData.cpp
  2. 3 2
      tutorial/201_Normals/main.cpp
  3. 26 27
      tutorial/501_HarmonicParam/main.cpp

+ 10 - 2
include/igl/opengl/ViewerData.cpp

@@ -491,8 +491,16 @@ IGL_INLINE void igl::opengl::ViewerData::clear()
 
 IGL_INLINE void igl::opengl::ViewerData::compute_normals()
 {
-  igl::per_face_normals(V, F, F_normals);
-  igl::per_vertex_normals(V, F, F_normals, V_normals);
+  if(V.cols() == 2)
+  {
+    F_normals = Eigen::RowVector3d(0,0,1).replicate(F.rows(),1);
+    V_normals = Eigen::RowVector3d(0,0,1).replicate(V.rows(),1);
+  }else
+  {
+    assert(V.cols() == 3);
+    igl::per_face_normals(V, F, F_normals);
+    igl::per_vertex_normals(V, F, F_normals, V_normals);
+  }
   dirty |= MeshGL::DIRTY_NORMAL;
 }
 

+ 3 - 2
tutorial/201_Normals/main.cpp

@@ -1,4 +1,4 @@
-#include <igl/readOFF.h>
+#include <igl/read_triangle_mesh.h>
 #include <igl/opengl/glfw/Viewer.h>
 #include <igl/per_vertex_normals.h>
 #include <igl/per_face_normals.h>
@@ -36,7 +36,8 @@ bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifier
 int main(int argc, char *argv[])
 {
   // Load a mesh in OFF format
-  igl::readOFF(TUTORIAL_SHARED_PATH "/fandisk.off", V, F);
+  igl::read_triangle_mesh(
+    argc>1?argv[1]: TUTORIAL_SHARED_PATH "/fandisk.off",V,F);
 
   // Compute per-face normals
   igl::per_face_normals(V,F,N_faces);

+ 26 - 27
tutorial/501_HarmonicParam/main.cpp

@@ -1,39 +1,18 @@
 #include <igl/boundary_loop.h>
 #include <igl/harmonic.h>
 #include <igl/map_vertices_to_circle.h>
-#include <igl/readOFF.h>
+#include <igl/read_triangle_mesh.h>
 #include <igl/opengl/glfw/Viewer.h>
 
 #include "tutorial_shared_path.h"
 
-Eigen::MatrixXd V;
-Eigen::MatrixXi F;
-Eigen::MatrixXd V_uv;
-
-bool key_down(igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifier)
-{
-  if (key == '1')
-  {
-    // Plot the 3D mesh
-    viewer.data().set_mesh(V,F);
-    viewer.core().align_camera_center(V,F);
-  }
-  else if (key == '2')
-  {
-    // Plot the mesh in 2D using the UV coordinates as vertex coordinates
-    viewer.data().set_mesh(V_uv,F);
-    viewer.core().align_camera_center(V_uv,F);
-  }
-
-  viewer.data().compute_normals();
-
-  return false;
-}
 
 int main(int argc, char *argv[])
 {
+  Eigen::MatrixXd V, V_uv;
+  Eigen::MatrixXi F;
   // Load a mesh in OFF format
-  igl::readOFF(TUTORIAL_SHARED_PATH "/camelhead.off", V, F);
+  igl::read_triangle_mesh(TUTORIAL_SHARED_PATH "/camelhead.off", V, F);
 
   // Find the open boundary
   Eigen::VectorXi bnd;
@@ -53,14 +32,34 @@ int main(int argc, char *argv[])
   igl::opengl::glfw::Viewer viewer;
   viewer.data().set_mesh(V, F);
   viewer.data().set_uv(V_uv);
-  viewer.callback_key_down = &key_down;
+  // Attach callback to allow toggling between 3D and 2D view
+  viewer.callback_key_pressed = 
+    [&V,&V_uv,&F](igl::opengl::glfw::Viewer& viewer, unsigned int key, int /*mod*/)
+  {
+    if(key == '3' || key == '2')
+    {
+      // Plot the 3D mesh or 2D UV coordinates
+      viewer.data().set_vertices(key=='3'?V:V_uv);
+      viewer.data().compute_normals();
+      viewer.core().align_camera_center(key=='3'?V:V_uv,F);
+      // key press was used
+      return true;
+    }
+    // key press not used
+    return false;
+  };
 
   // Disable wireframe
   viewer.data().show_lines = false;
 
-  // Draw checkerboard texture
+  // Draw default checkerboard texture
   viewer.data().show_texture = true;
 
+  std::cout<<R"(
+3  Show 3D model
+2  Show 2D parametrization
+)";
+
   // Launch the viewer
   viewer.launch();
 }