Open3D (C++ API)  0.19.0
Application.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cstdint>
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
18 
19 namespace open3d {
20 
21 namespace geometry {
22 class Image;
23 }
24 
25 namespace visualization {
26 
27 namespace rendering {
28 class Renderer;
29 class View;
30 class Scene;
31 } // namespace rendering
32 
33 namespace gui {
34 
35 struct Theme;
36 class Dialog;
37 class Window;
38 class WindowSystem;
39 
40 class Application {
41 public:
42  static Application &GetInstance();
43 
44  virtual ~Application();
45 
48  void Initialize();
49 
51  void Initialize(const char *resource_path);
52 
54  static constexpr FontId DEFAULT_FONT_ID = 0;
57  FontId AddFont(const FontDescription &fd);
60  void SetFont(FontId id, const FontDescription &fd);
61 
63  void Run();
65  void Quit();
66 
74  void RunInThread(std::function<void()> f);
80  void PostToMainThread(Window *window, std::function<void()> f);
81 
82  std::shared_ptr<Menu> GetMenubar() const;
83  void SetMenubar(std::shared_ptr<Menu> menubar);
84 
86  void AddWindow(std::shared_ptr<Window> window);
90  void RemoveWindow(Window *window);
91 
100  void ShowMessageBox(const char *title, const char *message);
101 
102  WindowSystem &GetWindowSystem() const;
103 
104  // (std::string not good in interfaces for ABI reasons)
105  const char *GetResourcePath() const;
106 
111  const Theme &GetTheme() const;
112 
115  double Now() const;
116 
118  bool IsRunning() const;
119 
121  void OnMenuItemSelected(Menu::ItemId itemId);
122 
127  void OnTerminate();
128 
129  class EnvUnlocker {
130  public:
132  virtual ~EnvUnlocker() {}
133  virtual void unlock() {}
134  virtual void relock() {}
135  };
146  bool RunOneTick(EnvUnlocker &unlocker, bool cleanup_if_no_windows = true);
147 
151  void SetWindowSystem(std::shared_ptr<WindowSystem> ws);
152 
157  bool UsingNativeWindows() const;
158 
161  void VerifyIsInitialized();
162 
163  const std::vector<FontDescription> &GetFontDescriptions() const;
164 
168  std::shared_ptr<geometry::Image> RenderToImage(
169  rendering::Renderer &renderer,
170  rendering::View *view,
171  rendering::Scene *scene,
172  int width,
173  int height);
174 
175  // Same as RenderToImage(), but returns the depth values in a float image.
176  std::shared_ptr<geometry::Image> RenderToDepthImage(
177  rendering::Renderer &renderer,
178  rendering::View *view,
179  rendering::Scene *scene,
180  int width,
181  int height,
182  bool z_in_view_space = false);
183 
184 private:
185  Application();
186 
187  enum class RunStatus { CONTINUE, DONE };
188  RunStatus ProcessQueuedEvents(EnvUnlocker &unlocker);
189 
190 private:
191  struct Impl;
192  std::unique_ptr<Impl> impl_;
193 };
194 
195 // ---------------------------------------------------------------------------
196 // Shared dialog factories (usable from any visualizer)
197 // ---------------------------------------------------------------------------
198 
201 std::shared_ptr<Dialog> CreateAboutDialog(Window *window);
202 
206 std::shared_ptr<Dialog> CreateControlsHelpDialog(Window *window);
207 
208 } // namespace gui
209 } // namespace visualization
210 } // namespace open3d
virtual void unlock()
Definition: Application.h:133
virtual ~EnvUnlocker()
Definition: Application.h:132
virtual void relock()
Definition: Application.h:134
Definition: Application.h:40
void VerifyIsInitialized()
Definition: Application.cpp:369
FontId AddFont(const FontDescription &fd)
Definition: Application.cpp:404
std::shared_ptr< Menu > GetMenubar() const
Definition: Application.cpp:483
bool UsingNativeWindows() const
Definition: Application.cpp:386
double Now() const
Definition: Application.cpp:474
void SetWindowSystem(std::shared_ptr< WindowSystem > ws)
Definition: Application.cpp:396
void PostToMainThread(Window *window, std::function< void()> f)
Definition: Application.cpp:719
const Theme & GetTheme() const
Definition: Application.cpp:728
void OnTerminate()
Definition: Application.cpp:536
WindowSystem & GetWindowSystem() const
Definition: Application.cpp:392
virtual ~Application()
Definition: Application.cpp:322
void ShowMessageBox(const char *title, const char *message)
Definition: Application.cpp:241
void RunInThread(std::function< void()> f)
Definition: Application.cpp:713
void Run()
Does not return until the UI is completely finished.
Definition: Application.cpp:579
void SetMenubar(std::shared_ptr< Menu > menubar)
Definition: Application.cpp:487
bool RunOneTick(EnvUnlocker &unlocker, bool cleanup_if_no_windows=true)
Definition: Application.cpp:584
void AddWindow(std::shared_ptr< Window > window)
Must be called on the same thread that calls Run()
Definition: Application.cpp:506
std::shared_ptr< geometry::Image > RenderToDepthImage(rendering::Renderer &renderer, rendering::View *view, rendering::Scene *scene, int width, int height, bool z_in_view_space=false)
Definition: Application.cpp:754
std::shared_ptr< geometry::Image > RenderToImage(rendering::Renderer &renderer, rendering::View *view, rendering::Scene *scene, int width, int height)
Definition: Application.cpp:730
void SetFont(FontId id, const FontDescription &fd)
Definition: Application.cpp:411
void RemoveWindow(Window *window)
Definition: Application.cpp:512
static constexpr FontId DEFAULT_FONT_ID
Identifier for font used by default for all UI elements.
Definition: Application.h:54
void OnMenuItemSelected(Menu::ItemId itemId)
Delivers the itemId to the active window. Used internally.
Definition: Application.cpp:562
bool IsRunning() const
Returns true once the GUI run loop has started processing frames.
Definition: Application.cpp:481
const char * GetResourcePath() const
Definition: Application.cpp:724
void Initialize()
Definition: Application.cpp:324
static Application & GetInstance()
Definition: Application.cpp:236
const std::vector< FontDescription > & GetFontDescriptions() const
Definition: Application.cpp:470
void Quit()
Closes all the windows, which exits as a result.
Definition: Application.cpp:530
int ItemId
Definition: MenuBase.h:28
Definition: Window.h:30
Definition: WindowSystem.h:27
int width
Definition: FilePCD.cpp:52
int height
Definition: FilePCD.cpp:53
std::shared_ptr< Dialog > CreateAboutDialog(Window *window)
Definition: Application.cpp:783
std::shared_ptr< Dialog > CreateControlsHelpDialog(Window *window)
Definition: Application.cpp:832
unsigned int FontId
Definition: Gui.h:68
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Theme.h:20