Open3D (C++ API)  0.15.1
SceneWidget.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <map>
30 
34 
35 namespace open3d {
36 
37 namespace camera {
38 class PinholeCameraIntrinsic;
39 } // namespace camera
40 
41 namespace geometry {
42 class AxisAlignedBoundingBox;
43 class Geometry3D;
44 } // namespace geometry
45 
46 namespace t {
47 namespace geometry {
48 class Geometry;
49 } // namespace geometry
50 } // namespace t
51 
52 namespace visualization {
53 namespace rendering {
54 class Camera;
55 class CameraManipulator;
56 class MatrixInteractorLogic;
57 class Open3DScene;
58 class View;
59 } // namespace rendering
60 } // namespace visualization
61 
62 namespace visualization {
63 namespace gui {
64 
65 class Label3D;
66 class Color;
67 
68 class SceneWidget : public Widget {
69  using Super = Widget;
70 
71 public:
73  public:
74  virtual ~MouseInteractor() = default;
75 
77  virtual void Mouse(const MouseEvent& e) = 0;
78  virtual void Key(const KeyEvent& e) = 0;
79  virtual bool Tick(const TickEvent& e) { return false; }
80  };
81 
82 public:
83  explicit SceneWidget();
84  ~SceneWidget() override;
85 
86  void SetFrame(const Rect& f) override;
87 
88  enum Controls {
91  FLY,
96  };
98 
99  void SetupCamera(float verticalFoV,
100  const geometry::AxisAlignedBoundingBox& scene_bounds,
101  const Eigen::Vector3f& center_of_rotation);
102  void SetupCamera(const camera::PinholeCameraIntrinsic& intrinsic,
103  const Eigen::Matrix4d& extrinsic,
104  const geometry::AxisAlignedBoundingBox& scene_bounds);
105  void SetupCamera(const Eigen::Matrix3d& intrinsic,
106  const Eigen::Matrix4d& extrinsic,
107  int intrinsic_width_px,
108  int intrinsic_height_px,
109  const geometry::AxisAlignedBoundingBox& scene_bounds);
110  void LookAt(const Eigen::Vector3f& center,
111  const Eigen::Vector3f& eye,
112  const Eigen::Vector3f& up);
113 
114  void SetOnCameraChanged(
115  std::function<void(visualization::rendering::Camera*)>
116  on_cam_changed);
117 
118  Eigen::Vector3f GetCenterOfRotation() const;
119  void SetCenterOfRotation(const Eigen::Vector3f& center);
120 
125  std::function<void(const Eigen::Vector3f&)> on_dir_changed);
126 
127  void SetScene(std::shared_ptr<rendering::Open3DScene> scene);
128  std::shared_ptr<rendering::Open3DScene> GetScene() const;
129 
130  rendering::View* GetRenderView() const; // is nullptr if no scene
131 
134  void EnableSceneCaching(bool enable);
135 
138  void ForceRedraw();
139  enum class Quality { FAST, BEST };
140  void SetRenderQuality(Quality level);
141  Quality GetRenderQuality() const;
142 
143  enum class CameraPreset {
144  PLUS_X, // at (X, 0, 0), looking (-1, 0, 0)
145  PLUS_Y, // at (0, Y, 0), looking (0, -1, 0)
146  PLUS_Z // at (0, 0, Z), looking (0, 0, 1) [default OpenGL camera]
147  };
148  void GoToCameraPreset(CameraPreset preset);
149 
151  std::string name;
152  const geometry::Geometry3D* geometry = nullptr;
154 
155  PickableGeometry(const std::string& n, const geometry::Geometry3D* g)
156  : name(n), geometry(g) {}
157 
158  PickableGeometry(const std::string& n, const t::geometry::Geometry* t)
159  : name(n), tgeometry(t) {}
160 
164  PickableGeometry(const std::string& n,
165  const geometry::Geometry3D* g,
166  const t::geometry::Geometry* t)
167  : name(n), geometry(g), tgeometry(t) {}
168  };
169 
170  void SetSunInteractorEnabled(bool enable);
171 
172  void SetPickableGeometry(const std::vector<PickableGeometry>& geometry);
173  void SetPickablePointSize(int px);
174  void SetOnPointsPicked(
175  std::function<void(
176  const std::map<
177  std::string,
178  std::vector<std::pair<size_t, Eigen::Vector3d>>>&,
179  int)> on_picked);
180  void SetOnStartedPolygonPicking(std::function<void()> on_poly_pick);
181  enum class PolygonPickAction { CANCEL = 0, SELECT };
182  void DoPolygonPick(PolygonPickAction action);
183 
184  // 3D Labels
185  std::shared_ptr<Label3D> AddLabel(const Eigen::Vector3f& pos,
186  const char* text);
187  void RemoveLabel(std::shared_ptr<Label3D> label);
188  void ClearLabels();
189 
190  Widget::DrawResult Draw(const DrawContext& context) override;
191 
192  Widget::EventResult Mouse(const MouseEvent& e) override;
193  Widget::EventResult Key(const KeyEvent& e) override;
194  Widget::DrawResult Tick(const TickEvent& e) override;
195 
196 private:
197  visualization::rendering::Camera* GetCamera() const;
198 
199 private:
200  struct Impl;
201  std::unique_ptr<Impl> impl_;
202 };
203 
204 } // namespace gui
205 } // namespace visualization
206 } // namespace open3d
ImGuiContext * context
Definition: Window.cpp:95
Contains the pinhole camera intrinsic parameters.
Definition: PinholeCameraIntrinsic.h:51
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:155
The base geometry class for 3D geometries.
Definition: Geometry3D.h:47
The base geometry class.
Definition: Geometry.h:38
virtual rendering::MatrixInteractorLogic & GetMatrixInteractor()=0
virtual bool Tick(const TickEvent &e)
Definition: SceneWidget.h:79
Definition: SceneWidget.h:68
void GoToCameraPreset(CameraPreset preset)
Definition: SceneWidget.cpp:1049
void LookAt(const Eigen::Vector3f &center, const Eigen::Vector3f &eye, const Eigen::Vector3f &up)
Definition: SceneWidget.cpp:891
~SceneWidget() override
Definition: SceneWidget.cpp:820
void RemoveLabel(std::shared_ptr< Label3D > label)
Definition: SceneWidget.cpp:1092
PolygonPickAction
Definition: SceneWidget.h:181
void SetOnCameraChanged(std::function< void(visualization::rendering::Camera *)> on_cam_changed)
Definition: SceneWidget.cpp:907
void SetScene(std::shared_ptr< rendering::Open3DScene > scene)
Definition: SceneWidget.cpp:945
void SetFrame(const Rect &f) override
Definition: SceneWidget.cpp:824
void SetupCamera(float verticalFoV, const geometry::AxisAlignedBoundingBox &scene_bounds, const Eigen::Vector3f &center_of_rotation)
Definition: SceneWidget.cpp:840
void SetSunInteractorEnabled(bool enable)
Definition: SceneWidget.cpp:923
void ForceRedraw()
Definition: SceneWidget.cpp:1017
void SetOnStartedPolygonPicking(std::function< void()> on_poly_pick)
Definition: SceneWidget.cpp:959
Widget::EventResult Key(const KeyEvent &e) override
Definition: SceneWidget.cpp:1220
void SetViewControls(Controls mode)
Definition: SceneWidget.cpp:987
void SetOnSunDirectionChanged(std::function< void(const Eigen::Vector3f &)> on_dir_changed)
Definition: SceneWidget.cpp:912
void SetRenderQuality(Quality level)
Definition: SceneWidget.cpp:1025
void DoPolygonPick(PolygonPickAction action)
Definition: SceneWidget.cpp:964
rendering::View * GetRenderView() const
Definition: SceneWidget.cpp:979
void SetOnPointsPicked(std::function< void(const std::map< std::string, std::vector< std::pair< size_t, Eigen::Vector3d >>> &, int)> on_picked)
Definition: SceneWidget.cpp:936
Quality
Definition: SceneWidget.h:139
Quality GetRenderQuality() const
Definition: SceneWidget.cpp:1045
void SetCenterOfRotation(const Eigen::Vector3f &center)
Definition: SceneWidget.cpp:903
void SetPickablePointSize(int px)
Definition: SceneWidget.cpp:932
void SetPickableGeometry(const std::vector< PickableGeometry > &geometry)
Definition: SceneWidget.cpp:927
CameraPreset
Definition: SceneWidget.h:143
Eigen::Vector3f GetCenterOfRotation() const
Definition: SceneWidget.cpp:899
Widget::DrawResult Tick(const TickEvent &e) override
Definition: SceneWidget.cpp:1229
Widget::DrawResult Draw(const DrawContext &context) override
Definition: SceneWidget.cpp:1101
Controls
Definition: SceneWidget.h:88
@ ROTATE_CAMERA_SPHERE
Definition: SceneWidget.h:90
@ ROTATE_IBL
Definition: SceneWidget.h:93
@ ROTATE_CAMERA
Definition: SceneWidget.h:89
@ FLY
Definition: SceneWidget.h:91
@ ROTATE_SUN
Definition: SceneWidget.h:92
@ ROTATE_MODEL
Definition: SceneWidget.h:94
@ PICK_POINTS
Definition: SceneWidget.h:95
std::shared_ptr< rendering::Open3DScene > GetScene() const
Definition: SceneWidget.cpp:975
SceneWidget()
Definition: SceneWidget.cpp:815
Widget::EventResult Mouse(const MouseEvent &e) override
Definition: SceneWidget.cpp:1188
void EnableSceneCaching(bool enable)
Definition: SceneWidget.cpp:1009
void ClearLabels()
Definition: SceneWidget.cpp:1099
std::shared_ptr< Label3D > AddLabel(const Eigen::Vector3f &pos, const char *text)
Definition: SceneWidget.cpp:1085
Definition: Widget.h:68
Widget()
Definition: Widget.cpp:53
EventResult
Definition: Widget.h:122
DrawResult
Definition: Widget.h:114
Definition: MatrixInteractorLogic.h:39
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t k4a_color_control_mode_t default_mode mode
Definition: K4aPlugin.cpp:697
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Events.h:222
PickableGeometry(const std::string &n, const t::geometry::Geometry *t)
Definition: SceneWidget.h:158
PickableGeometry(const std::string &n, const geometry::Geometry3D *g, const t::geometry::Geometry *t)
Definition: SceneWidget.h:164
const t::geometry::Geometry * tgeometry
Definition: SceneWidget.h:153
PickableGeometry(const std::string &n, const geometry::Geometry3D *g)
Definition: SceneWidget.h:155
std::string name
Definition: SceneWidget.h:151
const geometry::Geometry3D * geometry
Definition: SceneWidget.h:152