Viewer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_VIEWER_H
  9. #define IGL_VIEWER_H
  10. #include <AntTweakBar.h>
  11. #include <vector>
  12. #include <string>
  13. #include <cstdint>
  14. #define IGL_MOD_SHIFT 0x0001
  15. #define IGL_MOD_CONTROL 0x0002
  16. #define IGL_MOD_ALT 0x0004
  17. #define IGL_MOD_SUPER 0x0008
  18. #ifdef ENABLE_XML_SERIALIZATION
  19. #include <igl/xml/XMLSerializer.h>
  20. #include <igl/xml/XMLSerialization.h>
  21. #endif
  22. #include <Eigen/Core>
  23. #include <Eigen/Geometry>
  24. #include <igl/viewer/OpenGL_shader.h>
  25. #include <igl/viewer/ViewerData.h>
  26. #include <igl/viewer/OpenGL_state.h>
  27. #include <igl/viewer/ViewerPlugin.h>
  28. #include <igl/viewer/ViewerCore.h>
  29. namespace igl
  30. {
  31. // GLFW-based mesh viewer
  32. class Viewer
  33. {
  34. public:
  35. int launch(std::string filename = "");
  36. void init();
  37. // Stores all the viewing options
  38. igl::ViewerCore core;
  39. // Stores all the data that should be visualized
  40. igl::ViewerData data;
  41. // Stores the vbos indices and opengl related settings
  42. igl::OpenGL_state opengl;
  43. // List of registered plugins
  44. std::vector<ViewerPlugin*> plugins;
  45. void init_plugins();
  46. void shutdown_plugins();
  47. // Temporary data stored when the mouse button is pressed
  48. Eigen::Vector4f down_rotation;
  49. int current_mouse_x;
  50. int current_mouse_y;
  51. int down_mouse_x;
  52. int down_mouse_y;
  53. float down_mouse_z;
  54. Eigen::Vector3f down_translation;
  55. bool down;
  56. bool hack_never_moved;
  57. // Anttweak bar
  58. TwBar* bar;
  59. // Keep track of the global position of the scrollwheel
  60. float scroll_position;
  61. // Wrappers for ViewerCore functions
  62. void align_camera_center();
  63. // Wrappers for ViewerData functions
  64. void clear();
  65. void compute_normals();
  66. void set_face_based(bool newvalue);
  67. void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  68. void set_vertices(const Eigen::MatrixXd& V);
  69. void set_normals(const Eigen::MatrixXd& N);
  70. void set_colors(const Eigen::MatrixXd &C);
  71. void set_uv(const Eigen::MatrixXd& UV);
  72. void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  73. void set_texture(
  74. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  75. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  76. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B);
  77. // Sets points given a list of point vertices. In constrast to `set_points`
  78. // this will (purposefully) clober existing points.
  79. //
  80. // Inputs:
  81. // P #P by 3 list of vertex positions
  82. // C #P|1 by 3 color(s)
  83. void set_points(
  84. const Eigen::MatrixXd& P,
  85. const Eigen::MatrixXd& C);
  86. void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  87. // Sets edges given a list of edge vertices and edge indices. In
  88. // constrast
  89. // to `add_edges` this will (purposefully) clober existing edges.
  90. //
  91. // Inputs:
  92. // P #P by 3 list of vertex positions
  93. // E #E by 2 list of edge indices into P
  94. // C #E|1 by 3 color(s)
  95. void set_edges(
  96. const Eigen::MatrixXd& P,
  97. const Eigen::MatrixXi& E,
  98. const Eigen::MatrixXd& C);
  99. void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  100. void add_label (const Eigen::VectorXd& P, const std::string& str);
  101. // UI Enumerations
  102. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  103. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  104. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  105. Viewer();
  106. ~Viewer();
  107. // Mesh IO
  108. bool load_mesh_from_file(const char* mesh_file_name);
  109. bool save_mesh_to_file(const char* mesh_file_name);
  110. // Callbacks
  111. bool key_down(unsigned char key, int modifier);
  112. bool key_up(unsigned char key, int modifier);
  113. bool mouse_down(MouseButton button, int modifier);
  114. bool mouse_up(MouseButton button, int modifier);
  115. bool mouse_move(int mouse_x, int mouse_y);
  116. bool mouse_scroll(float delta_y);
  117. // Scene IO
  118. bool load_scene();
  119. bool save_scene();
  120. // Draw everything
  121. void draw();
  122. // OpenGL context resize
  123. void resize(int w, int h);
  124. // C-style callbacks
  125. bool (*callback_pre_draw)(Viewer& viewer);
  126. bool (*callback_post_draw)(Viewer& viewer);
  127. bool (*callback_mouse_down)(Viewer& viewer, int button, int modifier);
  128. bool (*callback_mouse_up)(Viewer& viewer, int button, int modifier);
  129. bool (*callback_mouse_move)(Viewer& viewer, int mouse_x, int mouse_y);
  130. bool (*callback_mouse_scroll)(Viewer& viewer, float delta_y);
  131. bool (*callback_key_down)(Viewer& viewer, unsigned char key, int modifiers);
  132. bool (*callback_key_up)(Viewer& viewer, unsigned char key, int modifiers);
  133. // Pointers to per-callback data
  134. void* callback_pre_draw_data;
  135. void* callback_post_draw_data;
  136. void* callback_mouse_down_data;
  137. void* callback_mouse_up_data;
  138. void* callback_mouse_move_data;
  139. void* callback_mouse_scroll_data;
  140. void* callback_key_down_data;
  141. void* callback_key_up_data;
  142. /********* AntTweakBar callbacks *********/
  143. static void TW_CALL snap_to_canonical_quaternion_cb(void *clientData);
  144. static void TW_CALL save_scene_cb(void *clientData);
  145. static void TW_CALL load_scene_cb(void *clientData);
  146. static void TW_CALL open_dialog_mesh(void *clientData);
  147. static void TW_CALL align_camera_center_cb(void *clientData);
  148. static void TW_CALL set_face_based_cb(const void *param, void *clientData);
  149. static void TW_CALL get_face_based_cb(void *param, void *clientData);
  150. static void TW_CALL set_invert_normals_cb(const void *param, void *clientData);
  151. static void TW_CALL get_invert_normals_cb(void *param, void *clientData);
  152. public:
  153. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  154. };
  155. } // end namespace
  156. #ifndef IGL_STATIC_LIBRARY
  157. # include "Viewer.cpp"
  158. #endif
  159. #endif