libigl v2.5.0
Loading...
Searching...
No Matches
Viewer.h
Go to the documentation of this file.
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_OPENGL_GLFW_VIEWER_H
9#define IGL_OPENGL_GLFW_VIEWER_H
10
11#ifndef IGL_OPENGL_4
12#define IGL_OPENGL_4
13#endif
14
15#include "../../igl_inline.h"
16#include "../MeshGL.h"
17#include "../ViewerCore.h"
18#include "../ViewerData.h"
19#include "ViewerPlugin.h"
20
21#include <Eigen/Core>
22#include <Eigen/Geometry>
23
24#include <vector>
25#include <string>
26#include <cstdint>
27
28#define IGL_MOD_SHIFT 0x0001
29#define IGL_MOD_CONTROL 0x0002
30#define IGL_MOD_ALT 0x0004
31#define IGL_MOD_SUPER 0x0008
32
33struct GLFWwindow;
34
35namespace igl
36{
37namespace opengl
38{
39namespace glfw
40{
41 // GLFW-based mesh viewer
42 class Viewer
43 {
44 public:
45 // UI Enumerations
46 enum class MouseButton {Left, Middle, Right};
48 IGL_INLINE int launch (bool fullscreen = false, const std::string &name = "libigl viewer", int width = 0, int height = 0);
49 IGL_INLINE int launch_init(bool fullscreen = false, const std::string &name = "libigl viewer", int width = 0, int height = 0);
50 IGL_INLINE bool launch_rendering(bool loop = true);
57 // Mesh IO
58 IGL_INLINE bool load_mesh_from_file(const std::string & mesh_file_name);
59 IGL_INLINE bool save_mesh_to_file(const std::string & mesh_file_name);
60 // Callbacks
61 IGL_INLINE bool key_pressed(unsigned int unicode_key,int modifier);
62 IGL_INLINE bool key_down(int key,int modifier);
63 IGL_INLINE bool key_up(int key,int modifier);
64 IGL_INLINE bool mouse_down(MouseButton button,int modifier);
65 IGL_INLINE bool mouse_up(MouseButton button,int modifier);
66 IGL_INLINE bool mouse_move(int mouse_x,int mouse_y);
67 IGL_INLINE bool mouse_scroll(float delta_y);
68 // Scene IO
70 IGL_INLINE bool load_scene(std::string fname);
72 IGL_INLINE bool save_scene(std::string fname);
73 // Draw everything
75 // Render given ViewerCore to a buffer. The width and height are determined
76 // by non-zeros dimensions of R or – if both are zero — are set to this
77 // core's viewport sizes. Other buffers are resized to fit if needed.
78 //
79 // Template:
80 // T image storage type, e.g., unsigned char (values ∈ [0,255]), double
81 // (values ∈ [0.0,1.0]).
82 // Inputs:
83 // data which ViewerData to draw
84 // update_matrices whether to update view, proj, and norm matrices in
85 // shaders
86 // Outputs:
87 // R width by height red pixel color values
88 // G width by height green pixel color values
89 // B width by height blue pixel color values
90 // A width by height alpha pixel color values
91 // D width by height depth pixel values. Depth values are _not_
92 // anti-aliased like RGBA.
93 //
94 template <typename T>
96 // can't be const because of writing in and out of `core.viewport`
97 /*const*/ igl::opengl::ViewerCore & core,
98 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & R,
99 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & G,
100 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B,
101 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
102 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & D);
103 // OpenGL context resize
104 IGL_INLINE void resize(int w,int h); // explicitly set window size
105 IGL_INLINE void post_resize(int w,int h); // external resize due to user interaction
106 // Helper functions
110
112 // Multi-mesh methods //
114
115 // Return the current mesh, or the mesh corresponding to a given unique identifier
116 //
117 // Inputs:
118 // mesh_id unique identifier associated to the desired mesh (current mesh if -1)
119 IGL_INLINE ViewerData& data(int mesh_id = -1);
120 IGL_INLINE const ViewerData& data(int mesh_id = -1) const;
121
122 // Append a new "slot" for a mesh (i.e., create empty entries at the end of
123 // the data_list and opengl_state_list.
124 //
125 // Inputs:
126 // visible If true, the new mesh is set to be visible on all existing viewports
127 // Returns the id of the last appended mesh
128 //
129 // Side Effects:
130 // selected_data_index is set this newly created, last entry (i.e.,
131 // #meshes-1)
132 IGL_INLINE int append_mesh(bool visible = true);
133
134 // Erase a mesh (i.e., its corresponding data and state entires in data_list
135 // and opengl_state_list)
136 //
137 // Inputs:
138 // index index of mesh to erase
139 // Returns whether erasure was successful <=> cannot erase last mesh
140 //
141 // Side Effects:
142 // If selected_data_index is greater than or equal to index then it is
143 // decremented
144 // Example:
145 // // Erase all mesh slots except first and clear remaining mesh
146 // viewer.selected_data_index = viewer.data_list.size()-1;
147 // while(viewer.erase_mesh(viewer.selected_data_index)){};
148 // viewer.data().clear();
149 //
150 IGL_INLINE bool erase_mesh(const size_t index);
151
152 // Retrieve mesh index from its unique identifier
153 // Returns 0 if not found
154 IGL_INLINE size_t mesh_index(const int id) const;
155
157 // Multi-viewport methods //
159
160 // Return the current viewport, or the viewport corresponding to a given unique identifier
161 //
162 // Inputs:
163 // core_id unique identifier corresponding to the desired viewport (current viewport if 0)
164 IGL_INLINE ViewerCore& core(unsigned core_id = 0);
165 IGL_INLINE const ViewerCore& core(unsigned core_id = 0) const;
166
167 // Append a new "slot" for a viewport (i.e., copy properties of the current viewport, only
168 // changing the viewport size/position)
169 //
170 // Inputs:
171 // viewport Vector specifying the viewport origin and size in screen coordinates.
172 // append_empty If true, existing meshes are hidden on the new viewport.
173 //
174 // Returns the unique id of the newly inserted viewport. There can be a maximum of 31
175 // viewports created in the same viewport. Erasing a viewport does not change the id of
176 // other existing viewports
177 IGL_INLINE int append_core(Eigen::Vector4f viewport, bool append_empty = false);
178
179 // Erase a viewport
180 //
181 // Inputs:
182 // index index of the viewport to erase
183 IGL_INLINE bool erase_core(const size_t index);
184
185 // Retrieve viewport index from its unique identifier
186 // Returns 0 if not found
187 IGL_INLINE size_t core_index(const int id) const;
188
189 // Change selected_core_index to the viewport containing the mouse
190 // (current_mouse_x, current_mouse_y)
192
193public:
195 // Member variables //
197
198 // Alec: I call this data_list instead of just data to avoid confusion with
199 // old "data" variable.
200 // Stores all the data that should be visualized
201 std::vector<ViewerData> data_list;
202
205 GLFWwindow* window;
206
207 // Stores all the viewing options
208 std::vector<ViewerCore> core_list;
211
212 // List of registered plugins
213 std::vector<ViewerPlugin*> plugins;
214 // Temporary data stored when the mouse button is pressed
215 Eigen::Quaternionf down_rotation;
221 Eigen::Vector3f down_translation;
222 bool down;
224 // Keep track of the global position of the scrollwheel
226 // C++-style functions
227 //
228 // Returns **true** if action should be cancelled.
229 std::function<bool(Viewer& viewer)> callback_init;
230 std::function<bool(Viewer& viewer)> callback_pre_draw;
231 std::function<bool(Viewer& viewer)> callback_post_draw;
232 std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_down;
233 std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
234 std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
235 std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
236 std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_pressed;
237 std::function<bool(Viewer& viewer, int w, int h)> callback_post_resize;
238 // THESE SHOULD BE DEPRECATED:
239 std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_down;
240 std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_up;
241 // Pointers to per-callback data
252
253 public:
254 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
255 };
256
257} // end namespace
258} // end namespace
259} // end namespace
260
261#ifndef IGL_STATIC_LIBRARY
262# include "Viewer.cpp"
263#endif
264
265#endif
Basic class of the 3D mesh viewer.
Definition ViewerCore.h:27
Object being drawn (i.e., mesh and its accessories) by the ViewerCore.
Definition ViewerData.h:39
Definition Viewer.h:43
int launch(bool fullscreen=false, const std::string &name="libigl viewer", int width=0, int height=0)
size_t core_index(const int id) const
std::function< bool(Viewer &viewer)> callback_init
Definition Viewer.h:229
MouseMode
Definition Viewer.h:47
void * callback_mouse_scroll_data
Definition Viewer.h:248
bool mouse_move(int mouse_x, int mouse_y)
void post_resize(int w, int h)
int append_mesh(bool visible=true)
std::function< bool(Viewer &viewer, unsigned int key, int modifiers)> callback_key_pressed
Definition Viewer.h:236
std::function< bool(Viewer &viewer, int button, int modifier)> callback_mouse_up
Definition Viewer.h:233
size_t selected_core_index
Definition Viewer.h:209
bool save_scene(std::string fname)
void * callback_init_data
Definition Viewer.h:242
int next_data_id
Definition Viewer.h:204
bool load_mesh_from_file(const std::string &mesh_file_name)
std::function< bool(Viewer &viewer, int w, int h)> callback_post_resize
Definition Viewer.h:237
size_t selected_data_index
Definition Viewer.h:203
std::function< bool(Viewer &viewer, float delta_y)> callback_mouse_scroll
Definition Viewer.h:235
std::function< bool(Viewer &viewer)> callback_pre_draw
Definition Viewer.h:230
bool erase_mesh(const size_t index)
int current_mouse_x
Definition Viewer.h:216
bool down
Definition Viewer.h:222
bool hack_never_moved
Definition Viewer.h:223
std::function< bool(Viewer &viewer, unsigned int key, int modifiers)> callback_key_down
Definition Viewer.h:239
void resize(int w, int h)
ViewerCore & core(unsigned core_id=0)
Eigen::Vector3f down_translation
Definition Viewer.h:221
std::function< bool(Viewer &viewer, int mouse_x, int mouse_y)> callback_mouse_move
Definition Viewer.h:234
const ViewerData & data(int mesh_id=-1) const
int launch_init(bool fullscreen=false, const std::string &name="libigl viewer", int width=0, int height=0)
std::function< bool(Viewer &viewer)> callback_post_draw
Definition Viewer.h:231
std::vector< ViewerPlugin * > plugins
Definition Viewer.h:213
void * callback_pre_draw_data
Definition Viewer.h:243
void * callback_post_draw_data
Definition Viewer.h:244
bool mouse_scroll(float delta_y)
int append_core(Eigen::Vector4f viewport, bool append_empty=false)
bool launch_rendering(bool loop=true)
float down_mouse_z
Definition Viewer.h:220
GLFWwindow * window
Definition Viewer.h:205
std::function< bool(Viewer &viewer, unsigned int key, int modifiers)> callback_key_up
Definition Viewer.h:240
Eigen::Quaternionf down_rotation
Definition Viewer.h:215
MouseButton
Definition Viewer.h:46
std::vector< ViewerCore > core_list
Definition Viewer.h:208
void * callback_key_down_data
Definition Viewer.h:250
bool mouse_up(MouseButton button, int modifier)
bool key_up(int key, int modifier)
bool key_pressed(unsigned int unicode_key, int modifier)
void * callback_key_pressed_data
Definition Viewer.h:249
size_t mesh_index(const int id) const
void * callback_mouse_move_data
Definition Viewer.h:247
bool mouse_down(MouseButton button, int modifier)
bool erase_core(const size_t index)
bool save_mesh_to_file(const std::string &mesh_file_name)
int down_mouse_x
Definition Viewer.h:218
const ViewerCore & core(unsigned core_id=0) const
void draw_buffer(igl::opengl::ViewerCore &core, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &R, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &G, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &B, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &A, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &D)
enum igl::opengl::glfw::Viewer::MouseMode mouse_mode
void * callback_mouse_down_data
Definition Viewer.h:245
void * callback_mouse_up_data
Definition Viewer.h:246
int current_mouse_y
Definition Viewer.h:217
float scroll_position
Definition Viewer.h:225
bool load_scene(std::string fname)
bool key_down(int key, int modifier)
ViewerData & data(int mesh_id=-1)
std::function< bool(Viewer &viewer, int button, int modifier)> callback_mouse_down
Definition Viewer.h:232
int next_core_id
Definition Viewer.h:210
int down_mouse_y
Definition Viewer.h:219
void * callback_key_up_data
Definition Viewer.h:251
std::vector< ViewerData > data_list
Definition Viewer.h:201
#define IGL_INLINE
Definition igl_inline.h:15
Definition AABB.h:17
void loop(const int n_verts, const Eigen::MatrixBase< DerivedF > &F, Eigen::SparseMatrix< SType > &S, Eigen::PlainObjectBase< DerivedNF > &NF)
Given the triangle mesh [V, F], where n_verts = V.rows(), computes newV and a sparse matrix S s....