ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
visualization.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
11 #include "pybind11/functional.h"
12 
13 // We cannot give out a shared_ptr to objects like Window which reference
14 // Filament objects, because we cannot guarantee that the Python script is
15 // not holding on to a reference when we cleanup Filament. The CloudViewer
16 // library will clear its shared_ptrs expecting the dependent object(s) to clean
17 // up, but they won't because Python still has a shared_ptr, leading to a crash
18 // when the variable goes of scope on the Python side.
19 // The following would crash gui.Window's holder is std::shared_ptr:
20 // import cloudViewer.visualization.gui as gui
21 // def main():
22 // gui.Application.instance.initialize()
23 // w = gui.Application.instance.create_window("Crash", 640, 480)
24 // gui.Application.instance.run()
25 // if __name__ == "__main__":
26 // main()
27 // However, if remove the 'w = ' part, it would not crash.
28 template <typename T>
30 public:
31  UnownedPointer() : ptr_(nullptr) {}
32  explicit UnownedPointer(T *p) : ptr_(p) {}
33  ~UnownedPointer() {} // don't delete!
34 
35  T *get() { return ptr_; }
36  T &operator*() { return *ptr_; }
37  T *operator->() { return ptr_; }
38  void reset() { ptr_ = nullptr; } // don't delete!
39 
40 private:
41  T *ptr_;
42 };
44 
45 namespace cloudViewer {
46 namespace visualization {
47 
48 template <typename T>
49 std::shared_ptr<T> TakeOwnership(UnownedPointer<T> x) {
50  return std::shared_ptr<T>(x.get());
51 }
52 
53 void pybind_visualization(py::module &m);
54 
55 void pybind_renderoption(py::module &m);
56 void pybind_viewcontrol(py::module &m);
57 void pybind_visualizer(py::module &m);
58 void pybind_visualization_utility(py::module &m);
59 
60 void pybind_renderoption_method(py::module &m);
61 void pybind_viewcontrol_method(py::module &m);
62 void pybind_visualizer_method(py::module &m);
63 void pybind_visualization_utility_methods(py::module &m);
64 
65 void pybind_o3dvisualizer(py::module &m);
66 
67 } // namespace visualization
68 } // namespace cloudViewer
UnownedPointer(T *p)
Definition: visualization.h:32
void pybind_visualization_utility(py::module &m)
Definition: utility.cpp:26
std::shared_ptr< T > TakeOwnership(UnownedPointer< T > x)
Definition: visualization.h:49
void pybind_o3dvisualizer(py::module &m)
void pybind_visualization_utility_methods(py::module &m)
Definition: utility.cpp:119
void pybind_visualizer_method(py::module &m)
Definition: visualizer.cpp:331
void pybind_visualization(py::module &m)
void pybind_visualizer(py::module &m)
Definition: visualizer.cpp:42
void pybind_viewcontrol(py::module &m)
Definition: viewcontrol.cpp:33
void pybind_viewcontrol_method(py::module &m)
void pybind_renderoption(py::module &m)
void pybind_renderoption_method(py::module &m)
Generic file read and write utility for python interface.
PYBIND11_DECLARE_HOLDER_TYPE(T, UnownedPointer< T >)