소스 검색

key usage in viewer

Former-commit-id: 301a437dd1acaaf8b6cdacded50f99827855a490
Alec Jacobson 9 년 전
부모
커밋
6d578a7133

+ 9 - 7
include/igl/snap_to_fixed_up.cpp

@@ -7,19 +7,21 @@
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "snap_to_fixed_up.h"
 
+template <typename Qtype>
 IGL_INLINE void igl::snap_to_fixed_up(
-  const Eigen::Quaterniond & q,
-  Eigen::Quaterniond & s)
+  const Eigen::Quaternion<Qtype> & q,
+  Eigen::Quaternion<Qtype> & s)
 {
   using namespace Eigen;
-  const Vector3d up = q.matrix() * Vector3d(0,1,0);
-  Vector3d proj_up(0,up(1),up(2));
+  typedef Eigen::Matrix<Qtype,3,1> Vector3Q;
+  const Vector3Q up = q.matrix() * Vector3Q(0,1,0);
+  Vector3Q proj_up(0,up(1),up(2));
   if(proj_up.norm() == 0)
   {
-    proj_up = Vector3d(0,1,0);
+    proj_up = Vector3Q(0,1,0);
   }
   proj_up.normalize();
-  Quaterniond dq;
-  dq = Quaterniond::FromTwoVectors(up,proj_up);
+  Quaternion<Qtype> dq;
+  dq = Quaternion<Qtype>::FromTwoVectors(up,proj_up);
   s = dq * q;
 }

+ 3 - 2
include/igl/snap_to_fixed_up.h

@@ -24,9 +24,10 @@ namespace igl
   //   s the resulting rotation (as quaternion)
   //
   // See also: two_axis_valuator_fixed_up
+  template <typename Qtype>
   IGL_INLINE void snap_to_fixed_up(
-    const Eigen::Quaterniond & q,
-    Eigen::Quaterniond & s);
+    const Eigen::Quaternion<Qtype> & q,
+    Eigen::Quaternion<Qtype> & s);
 }
 
 #ifndef IGL_STATIC_LIBRARY

+ 63 - 0
include/igl/viewer/Viewer.cpp

@@ -458,6 +458,69 @@ namespace viewer
     for (unsigned int i = 0; i<plugins.size(); ++i)
       if (plugins[i]->key_pressed(unicode_key, modifiers))
         return true;
+    const std::string usage(R"(igl::viewer::Viewer:
+A,a  Toggle animation (tight draw loop)
+I,i  Toggle invert normals
+L,l  Toggle wireframe
+O,o  Toggle orthographic/perspective projection
+T,t  Toggle filled faces
+Z    Snap to canonical view
+[,]  Toggle between rotation control types (e.g. trackball, two-axis valuator
+     with fixed up)
+)");
+    switch(unicode_key)
+    {
+      case 'A':
+      case 'a':
+      {
+        core.is_animating = !core.is_animating;
+        return true;
+      }
+      case 'I':
+      case 'i':
+      {
+        data.dirty |= ViewerData::DIRTY_NORMAL;
+        core.invert_normals = !core.invert_normals;
+        return true;
+      }
+      case 'L':
+      case 'l':
+      {
+        core.show_lines = !core.show_lines;
+        return true;
+      }
+      case 'O':
+      case 'o':
+      {
+        core.orthographic = !core.orthographic;
+        return true;
+      }
+      case 'T':
+      case 't':
+      {
+        core.show_faces = !core.show_faces;
+        return true;
+      }
+      case 'Z':
+      {
+        snap_to_canonical_quaternion();
+        return true;
+      }
+      case '[':
+      case ']':
+      {
+        if(core.rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
+        {
+          core.set_rotation_type(
+            ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
+        }else
+        {
+          core.set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
+        }
+        return true;
+      }
+      default: break;//do nothing
+    }
     return false;
   }
 

+ 2 - 0
include/igl/viewer/Viewer.h

@@ -127,6 +127,8 @@ namespace viewer
     IGL_INLINE void open_dialog_save_mesh();
 
     // C++-style functions
+    //
+    // Returns **true** if action should be cancelled.
     std::function<bool(Viewer& viewer)> callback_init;
     std::function<bool(Viewer& viewer)> callback_pre_draw;
     std::function<bool(Viewer& viewer)> callback_post_draw;

+ 16 - 1
include/igl/viewer/ViewerCore.cpp

@@ -8,6 +8,7 @@
 
 #include "ViewerCore.h"
 #include <igl/quat_to_mat.h>
+#include <igl/snap_to_fixed_up.h>
 #include <igl/look_at.h>
 #include <igl/frustum.h>
 #include <igl/ortho.h>
@@ -457,6 +458,20 @@ IGL_INLINE void igl::viewer::ViewerCore::draw_buffer(ViewerData& data,
   glDeleteFramebuffers(1, &frameBuffer);
 }
 
+IGL_INLINE void igl::viewer::ViewerCore::set_rotation_type(
+  const igl::viewer::ViewerCore::RotationType & value)
+{
+  using namespace Eigen;
+  using namespace std;
+  const RotationType old_rotation_type = rotation_type;
+  rotation_type = value;
+  if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
+    old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
+  {
+    snap_to_fixed_up(Quaternionf(trackball_angle),trackball_angle);
+  }
+}
+
 
 IGL_INLINE igl::viewer::ViewerCore::ViewerCore()
 {
@@ -473,7 +488,7 @@ IGL_INLINE igl::viewer::ViewerCore::ViewerCore()
 
   // Default trackball
   trackball_angle = Eigen::Quaternionf::Identity();
-  rotation_type = ViewerCore::ROTATION_TYPE_TRACKBALL;
+  set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
 
   // Defalut model viewing parameters
   model_zoom = 1.0f;

+ 19 - 14
include/igl/viewer/ViewerCore.h

@@ -74,13 +74,23 @@ public:
 
   // Draw everything
   IGL_INLINE void draw(ViewerData& data, OpenGL_state& opengl, bool update_matrices = true);
-  IGL_INLINE void draw_buffer(ViewerData& data,
-                              OpenGL_state& opengl,
-                              bool update_matrices,
-                              Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
-                              Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
-                              Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
-                              Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
+  IGL_INLINE void draw_buffer(
+    ViewerData& data,
+    OpenGL_state& opengl,
+    bool update_matrices,
+    Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
+    Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
+    Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
+    Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
+
+  // Trackball angle (quaternion)
+  enum RotationType
+  {
+    ROTATION_TYPE_TRACKBALL = 0,
+    ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
+    NUM_ROTATION_TYPES = 2
+  };
+  IGL_INLINE void set_rotation_type(const RotationType & value);
 
   // ------------------- Properties
 
@@ -100,13 +110,8 @@ public:
   Eigen::Vector3f light_position;
   float lighting_factor;
 
-  // Trackball angle (quaternion)
-  enum RotationType
-  {
-    ROTATION_TYPE_TRACKBALL = 0,
-    ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
-    NUM_ROTATION_TYPES = 2
-  } rotation_type;
+  RotationType rotation_type;
+
   Eigen::Quaternionf trackball_angle;
 
   // Model viewing parameters

+ 19 - 3
include/igl/viewer/ViewerData.h

@@ -60,12 +60,28 @@ public:
   // Inputs:
   //   C  #V|#F|1 by 3 list of colors
   IGL_INLINE void set_colors(const Eigen::MatrixXd &C);
+  // Set per-vertex UV coordinates
+  //
+  // Inputs:
+  //   UV  #V by 2 list of UV coordinates (indexed by F)
   IGL_INLINE void set_uv(const Eigen::MatrixXd& UV);
+  // Set per-corner UV coordinates
+  //
+  // Inputs:
+  //   UV_V  #UV by 2 list of UV coordinates
+  //   UV_F  #F by 3 list of UV indices into UV_V
   IGL_INLINE void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
+  // Set the texture associated with the mesh.
+  //
+  // Inputs:
+  //   R  width by height image matrix of red channel
+  //   G  width by height image matrix of green channel
+  //   B  width by height image matrix of blue channel
+  //
   IGL_INLINE void set_texture(
-                    const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
-                    const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
-                    const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B);
+    const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
+    const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
+    const Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B);
 
   // Sets points given a list of point vertices. In constrast to `set_points`
   // this will (purposefully) clober existing points.