ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Draw.cpp
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 
9 
10 #include <Logging.h>
11 
12 #include <sstream>
13 
16 
17 namespace cloudViewer {
18 namespace visualization {
19 
20 DrawObject::DrawObject(const std::string &n,
21  std::shared_ptr<ccHObject> g,
22  bool vis /*= true*/) {
23  this->name = n;
24  this->geometry = g;
25  this->is_visible = vis;
26 }
27 
28 DrawObject::DrawObject(const std::string &n,
29  std::shared_ptr<t::geometry::Geometry> tg,
30  bool vis /*= true*/) {
31  this->name = n;
32  this->tgeometry = tg;
33  this->is_visible = vis;
34 }
35 
36 DrawObject::DrawObject(const std::string &n,
37  std::shared_ptr<rendering::TriangleMeshModel> m,
38  bool vis /*= true*/) {
39  this->name = n;
40  this->model = m;
41  this->is_visible = vis;
42 }
43 
44 // ----------------------------------------------------------------------------
45 void Draw(const std::vector<std::shared_ptr<ccHObject>> &geometries,
46  const std::string &window_name /*= "CloudViewer"*/,
47  int width /*= 1024*/,
48  int height /*= 768*/,
49  const std::vector<DrawAction> &actions /*= {}*/) {
50  std::vector<DrawObject> objs;
51  objs.reserve(geometries.size());
52  for (size_t i = 0; i < geometries.size(); ++i) {
53  std::stringstream name;
54  name << "Object " << (i + 1);
55  objs.emplace_back(name.str(), geometries[i]);
56  }
57  Draw(objs, window_name, width, height, actions);
58 }
59 
60 void Draw(
61  const std::vector<std::shared_ptr<t::geometry::Geometry>> &tgeometries,
62  const std::string &window_name /*= "CloudViewer"*/,
63  int width /*= 1024*/,
64  int height /*= 768*/,
65  const std::vector<DrawAction> &actions /*= {}*/) {
66  std::vector<DrawObject> objs;
67  objs.reserve(tgeometries.size());
68  for (size_t i = 0; i < tgeometries.size(); ++i) {
69  std::stringstream name;
70  name << "Object " << (i + 1);
71  objs.emplace_back(name.str(), tgeometries[i]);
72  }
73  Draw(objs, window_name, width, height, actions);
74 }
75 
76 void Draw(const std::vector<std::shared_ptr<rendering::TriangleMeshModel>>
77  &models,
78  const std::string &window_name /*= "CloudViewer"*/,
79  int width /*= 1024*/,
80  int height /*= 768*/,
81  const std::vector<DrawAction> &actions /*= {}*/) {
82  std::vector<DrawObject> objs;
83  objs.reserve(models.size());
84  for (size_t i = 0; i < models.size(); ++i) {
85  std::stringstream name;
86  name << "Object " << (i + 1);
87  objs.emplace_back(name.str(), models[i]);
88  }
89  Draw(objs, window_name, width, height, actions);
90 }
91 
92 void Draw(const std::vector<DrawObject> &objects,
93  const std::string &window_name /*= "CloudViewer"*/,
94  int width /*= 1024*/,
95  int height /*= 768*/,
96  const std::vector<DrawAction> &actions /*= {}*/) {
98  auto draw = std::make_shared<visualizer::O3DVisualizer>(window_name, width,
99  height);
100  for (auto &o : objects) {
101  if (o.geometry) {
102  draw->AddGeometry(o.name, o.geometry);
103  } else if (o.tgeometry) {
104  draw->AddGeometry(o.name, o.tgeometry);
105  } else if (o.model) {
106  draw->AddGeometry(o.name, o.model);
107  } else {
108  utility::LogWarning("Invalid object passed to Draw");
109  }
110  draw->ShowGeometry(o.name, o.is_visible);
111  }
112 
113  for (auto &act : actions) {
114  draw->AddAction(act.name, act.callback);
115  }
116 
117  draw->ResetCameraToDefault();
118 
120  draw.reset(); // so we don't hold onto the pointer after Run() cleans up
122 }
123 
124 } // namespace visualization
125 } // namespace cloudViewer
int width
std::string name
int height
void Run()
Does not return until the UI is completely finished.
void AddWindow(std::shared_ptr< Window > window)
Must be called on the same thread that calls Run()
#define LogWarning(...)
Definition: Logging.h:72
void Draw(const std::vector< std::shared_ptr< ccHObject >> &geometries, const std::string &window_name, int width, int height, const std::vector< DrawAction > &actions)
Definition: Draw.cpp:45
Generic file read and write utility for python interface.
std::shared_ptr< ccHObject > geometry
Definition: Draw.h:22
DrawObject(const std::string &n, std::shared_ptr< ccHObject > g, bool vis=true)
Definition: Draw.cpp:20
std::shared_ptr< t::geometry::Geometry > tgeometry
Definition: Draw.h:23
std::shared_ptr< rendering::TriangleMeshModel > model
Definition: Draw.h:24