Graphite  Version 3
An experimental 3D geometry processing program
simple_application.h
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef H_GEOGRAM_GFX_GUI_SIMPLE_APPLICATION_H
41 #define H_GEOGRAM_GFX_GUI_SIMPLE_APPLICATION_H
42 
51 #include <geogram_gfx/imgui_ext/imgui_ext.h>
52 #include <geogram_gfx/imgui_ext/icon_font.h>
53 
54 #include <map>
55 #include <functional>
56 
57 struct lua_State;
58 
59 namespace GEO {
60 
62  public:
63 
68  SimpleApplication(const std::string& name);
69 
73  ~SimpleApplication() override;
74 
78  void draw_gui() override;
79 
83  void draw_graphics() override;
84 
92  virtual bool save(const std::string& filename);
93 
101  virtual bool load(const std::string& filename);
102 
108  return text_editor_;
109  }
110 
115  text_editor_visible_ = true;
116  }
117 
122  text_editor_visible_ = false;
123  }
124 
128  void show_console() {
129  console_visible_ = true;
130  }
131 
135  void hide_console() {
136  console_visible_ = false;
137  }
138 
142  void home();
143 
147  void set_style(const std::string& style) override;
148 
156  double xmin, double ymin, double zmin,
157  double xmax, double ymax, double zmax
158  );
159 
167  double& xmin, double& ymin, double& zmin,
168  double& xmax, double& ymax, double& zmax
169  ) const;
170 
171  void zoom_in() {
172  zoom_ *= 1.1;
173  }
174 
175  void zoom_out() {
176  zoom_ /= 1.1;
177  }
178 
179  void set_clipping(bool x) {
180  clipping_ = x;
181  }
182 
183  void set_lighting(bool x) {
184  lighting_ = x;
185  }
186 
187  void set_background_color(const vec4f& color) {
188  background_color_ = color;
189  }
190 
191  virtual bool exec_command(const char* command);
192 
193  static SimpleApplication* instance() {
194  return dynamic_cast<SimpleApplication*>(
196  );
197  }
198 
205  vec3 project(const vec3& p);
206 
213  vec3 unproject(const vec3& p);
214 
220  vec2 unproject_2d(const vec2& p);
221 
222 
226  void drop_callback(int nb, const char** f) override;
227 
228  protected:
229 
237  const std::string& key, std::function<void()> cb,
238  const char* help = nullptr
239  );
240 
248  const std::string& key, bool* p_val,
249  const char* help = nullptr
250  );
251 
255  void char_callback(unsigned int c) override;
256 
260  void key_callback(int key, int scancode, int action, int mods) override;
261 
266  int button, int action, int mods, int source
267  ) override;
268 
272  void cursor_pos_callback(double x, double y, int source) override;
273 
277  void scroll_callback(double xoffset, double yoffset) override;
278 
282  virtual void draw_scene_begin();
283 
287  virtual void draw_scene();
288 
292  virtual void draw_scene_end();
293 
298 
302  virtual void draw_viewer_properties();
303 
308 
312  virtual void draw_object_properties();
313 
314 
318  virtual void draw_command_window();
319 
323  virtual void draw_console();
324 
325 
329  virtual void draw_menu_bar();
330 
331 
335  virtual void draw_load_menu();
336 
340  virtual void draw_save_menu();
341 
348  virtual void draw_fileops_menu();
349 
353  virtual void draw_about();
354 
358  virtual void draw_help();
359 
363  virtual void draw_windows_menu();
364 
369  virtual void draw_application_menus();
370 
375  virtual void draw_application_icons();
376 
380  void post_draw() override;
381 
389  virtual bool can_load(const std::string& filename);
390 
399  virtual std::string supported_read_file_extensions();
400 
412  virtual std::string supported_write_file_extensions();
413 
419  ImTextureID convert_to_ImTextureID(GLuint gl_texture_id_in);
420 
424  void GL_initialize() override;
425 
429  void GL_terminate() override;
430 
437  void browse(const std::string& path, bool subdirs=false);
438 
442  void geogram_initialize(int argc, char** argv) override;
443 
449  void set_default_filename(const std::string& filename) {
450  strcpy(filename_, filename.c_str());
451  }
452 
460  void init_colormap(const std::string& name, const char** xpm_data);
461 
468 
472  void ImGui_initialize() override;
473 
474  void set_2d() {
475  three_D_ = false;
476  }
477 
478  void set_3d() {
479  three_D_ = true;
480  }
481 
482  void set_default_layout();
483 
484  void resize(index_t w, index_t h, index_t fb_w, index_t fb_h) override;
485 
486  virtual const char* default_layout() const;
487  virtual const char* default_layout_android_vertical() const;
488  virtual const char* default_layout_android_horizontal() const;
489 
490  protected:
491  static void replay_latest_command();
492 
493  protected:
494  bool locked_; // avoid starting command when command is running
495  bool lighting_;
496  bool edit_light_;
497  bool clipping_;
498  GLUPclipMode clip_mode_;
499  bool edit_clip_;
500  bool fixed_clip_;
501  GLenum effect_;
502  vec4f background_color_;
503 
504  bool viewer_properties_visible_;
505  bool object_properties_visible_;
506  bool console_visible_;
507  bool text_editor_visible_;
508  bool use_text_editor_;
509 
510  Box roi_;
511  double roi_radius_;
512  vec3 object_translation_;
513  ArcBall object_rotation_;
514  ArcBall light_rotation_;
515  ArcBall clip_rotation_;
516  vec3 clip_translation_;
517  bool three_D_;
518  double zoom_;
519  double zoom_down_;
521  bool props_pinned_;
522 
523  enum MouseOp {
524  MOUSE_NOOP, MOUSE_ROTATE, MOUSE_TRANSLATE, MOUSE_ZOOM
525  } mouse_op_;
526 
527  enum MouseTarget {
528  MOUSE_NOTARGET, MOUSE_OBJECT, MOUSE_LIGHT, MOUSE_CLIP
529  } mouse_target_;
530 
531  vec2 mouse_down_xy_; // in [-1,1] x [-1,1]
532  vec2 mouse_xy_; // in [-1,1] x [-1,1]
533 
534  // Current transform, for picking
535  GLint viewport_[4];
536  mat4 modelview_transpose_;
537  mat4 project_transpose_;
538 
539  std::string path_;
540  std::string current_file_;
541  char filename_[geo_imgui_string_length]; // Buffer for file dialog.
542  GLuint geogram_logo_texture_;
543 
544  Console_var console_;
545  StatusBar_var status_bar_;
546  TextEditor text_editor_;
547 
548  std::map< std::string, std::function<void()> > key_funcs_;
549  std::map< std::string, std::string > key_funcs_help_;
550 
551  struct ColormapInfo {
552  ColormapInfo() : texture(0) {
553  }
554  GLuint texture;
555  std::string name;
556  };
557 
558  vector<ColormapInfo> colormaps_;
559  FullScreenEffectImpl_var full_screen_effect_;
560 
561  lua_State* lua_state_;
562  bool lua_error_occured_;
563  };
564 
565 }
566 
567 #endif
Base class for all applications.
Definition: application.h:66
static Application * instance()
Gets the instance.
Definition: application.h:84
Enables to interactively define a rotation.
Definition: arc_ball.h:66
Axis-aligned bounding box.
Definition: geometry.h:669
virtual void draw_fileops_menu()
Draws other file operation menu.
virtual void draw_console()
Draws the console.
void cursor_pos_callback(double x, double y, int source) override
Callback called whenever the mouse cursor is moved.
virtual void draw_scene_begin()
Setups OpenGL for scene drawing.
virtual void draw_application_menus()
Draws the application menus.
virtual void draw_load_menu()
Draws the load menu and browser.
void drop_callback(int nb, const char **f) override
Callback called whenever files are dropped in the window.
virtual bool save(const std::string &filename)
Saves the current content to a file.
virtual bool can_load(const std::string &filename)
Tests whether a file can be loaded.
virtual void draw_viewer_properties()
Draws the contents of viewer properties window.
virtual void draw_about()
Draws the about box in the file menu.
TextEditor & text_editor()
Gets the text editor.
virtual void draw_application_icons()
Draws the application icons on the menubar.
void char_callback(unsigned int c) override
Callback called whenever a key is pushed (high level version)
virtual void draw_help()
Draws help info (accelarators)
void set_style(const std::string &style) override
Sets the style of the application.
virtual void draw_scene_end()
Cleanups OpenGL after scene drawing.
void mouse_button_callback(int button, int action, int mods, int source) override
Callback called whenenver a mouse button changed.
void hide_text_editor()
Hides the text editor.
vec3 project(const vec3 &p)
Projects a point from model space to window coordinates.
vec3 unproject(const vec3 &p)
Unprojects a 3d point from window coordinates to model space.
void resize(index_t w, index_t h, index_t fb_w, index_t fb_h) override
Called whenever window size changes.
void init_colormap(const std::string &name, const char **xpm_data)
Initializes a new colormap from name and xpm data.
virtual void draw_menu_bar()
Draws the menu bar.
vec2 unproject_2d(const vec2 &p)
Unprojects a 2d point from window coordinates to model space.
virtual void draw_windows_menu()
Draws the windows menu.
virtual void draw_viewer_properties_window()
Draws the viewer properties window frame and contents.
void show_console()
Shows the console.
void show_text_editor()
Shows the text editor.
void GL_initialize() override
Initializes OpenGL and GLUP objects.
void key_callback(int key, int scancode, int action, int mods) override
Callback called whenever a key is pushed (low level version)
ImTextureID convert_to_ImTextureID(GLuint gl_texture_id_in)
Converts an OpenGL texture ID into an ImGUI texture ID.
SimpleApplication(const std::string &name)
SimpleApplication constructor.
void scroll_callback(double xoffset, double yoffset) override
Callback called whenenver the mouse wheel is moved.
void browse(const std::string &path, bool subdirs=false)
Recursively browses a directory and generates menu items.
void draw_gui() override
This function is called when the GUI should be redisplayed.
virtual void draw_command_window()
Draws the active command window if any.
void get_region_of_interest(double &xmin, double &ymin, double &zmin, double &xmax, double &ymax, double &zmax) const
Gets the region of interest.
virtual void draw_object_properties()
Draws the contents of the object properties window.
void draw_graphics() override
This function is called when the 3d content should be redisplayed.
void set_default_filename(const std::string &filename)
Sets the default filename used to save the current file.
virtual std::string supported_read_file_extensions()
Gets the list of supported file extensions for reading.
void post_draw() override
This function is called after all drawing operations.
void home()
Restores default viewing parameters.
void ImGui_initialize() override
Initializes the ImGui library.
~SimpleApplication() override
SimpleApplication destructor.
virtual bool load(const std::string &filename)
Loads a file.
void GL_terminate() override
Deallocates OpenGL and GLUP objects.
void hide_console()
Hides the console.
virtual void draw_scene()
Draws the scene.
virtual std::string supported_write_file_extensions()
Gets the list of supported file extensions for writing.
void add_key_func(const std::string &key, std::function< void()> cb, const char *help=nullptr)
Declares a function to be triggered when a key is pressed.
void init_colormaps()
Initializes all the default colormaps.
void geogram_initialize(int argc, char **argv) override
void add_key_toggle(const std::string &key, bool *p_val, const char *help=nullptr)
Declares a boolean to be toggled when a key is pressed.
virtual void draw_save_menu()
Draws the save menu.
void set_region_of_interest(double xmin, double ymin, double zmin, double xmax, double ymax, double zmax)
Sets the region of interest.
virtual void draw_object_properties_window()
Draw the object properties window frame and contents.
A minimalistic text editor.
Definition: text_editor.h:58
Vector with aligned memory allocation.
Definition: memory.h:635
A simple system to interface C++ functions with a ImGui interface. Used mainly by geobox.
Common include file, providing basic definitions. Should be included before anything else by all head...
Low-level base class for full screen effects.
Base class for all applications.
Controls a 3d rotation from user mouse input.
A console.
A simple text editor.
#define GEOGRAM_GFX_API
Linkage declaration for geogram symbols.
Definition: defs.h:55
Global Vorpaline namespace.
Matrix< 4, Numeric::float64 > mat4
Represents a 4x4 matrix.
Definition: geometry.h:133
vecng< 4, Numeric::float32 > vec4f
Represents points and vectors in 4d with single-precision coordinates.
Definition: geometry.h:92
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
vecng< 2, Numeric::float64 > vec2
Represents points and vectors in 2d.
Definition: geometry.h:59
Implementation of the status bar.