1 // ----------------------------------------------------------------------------
2 // -                        Open3D: www.open3d.org                            -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 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 #include "DrawGeometry.h"
28 
29 #include <Visualization/Visualizer/Visualizer.h>
30 #include <Visualization/Visualizer/VisualizerWithCustomAnimation.h>
31 #include <Visualization/Visualizer/VisualizerWithKeyCallback.h>
32 #include <Visualization/Visualizer/VisualizerWithEditing.h>
33 #include <Visualization/Visualizer/ViewControlWithCustomAnimation.h>
34 #include <Visualization/Visualizer/ViewControlWithEditing.h>
35 
36 namespace three{
37 
DrawGeometries(const std::vector<std::shared_ptr<const Geometry>> & geometry_ptrs,const std::string & window_name,int width,int height,int left,int top)38 bool DrawGeometries(
39         const std::vector<std::shared_ptr<const Geometry>> &geometry_ptrs,
40         const std::string &window_name/* = "Open3D"*/,
41         int width/* = 640*/, int height/* = 480*/,
42         int left/* = 50*/, int top/* = 50*/)
43 {
44     Visualizer visualizer;
45     if (visualizer.CreateWindow(window_name, width, height, left, top) ==
46             false) {
47         PrintWarning("[DrawGeometries] Failed creating OpenGL window.\n");
48         return false;
49     }
50     for (const auto &geometry_ptr : geometry_ptrs) {
51         if (visualizer.AddGeometry(geometry_ptr) == false) {
52             PrintWarning("[DrawGeometries] Failed adding geometry.\n");
53             PrintWarning("[DrawGeometries] Possibly due to bad geometry or wrong geometry type.\n");
54             return false;
55         }
56     }
57     visualizer.Run();
58     visualizer.DestroyWindow();
59     return true;
60 }
61 
DrawGeometriesWithCustomAnimation(const std::vector<std::shared_ptr<const Geometry>> & geometry_ptrs,const std::string & window_name,int width,int height,int left,int top,const std::string & json_filename)62 bool DrawGeometriesWithCustomAnimation(
63         const std::vector<std::shared_ptr<const Geometry>> &geometry_ptrs,
64         const std::string &window_name/* = "Open3D"*/,
65         int width/* = 640*/, int height/* = 480*/,
66         int left/* = 50*/, int top/* = 50*/,
67         const std::string &json_filename/* = ""*/)
68 {
69     VisualizerWithCustomAnimation visualizer;
70     if (visualizer.CreateWindow(window_name, width, height, left, top) ==
71             false) {
72         PrintWarning("[DrawGeometriesWithCustomAnimation] Failed creating OpenGL window.\n");
73         return false;
74     }
75     for (const auto &geometry_ptr : geometry_ptrs) {
76         if (visualizer.AddGeometry(geometry_ptr) == false) {
77             PrintWarning("[DrawGeometriesWithCustomAnimation] Failed adding geometry.\n");
78             PrintWarning("[DrawGeometriesWithCustomAnimation] Possibly due to bad geometry or wrong geometry type.\n");
79             return false;
80         }
81     }
82     auto &view_control =
83             (ViewControlWithCustomAnimation &)visualizer.GetViewControl();
84     if (json_filename.empty() == false) {
85         if (view_control.LoadTrajectoryFromJsonFile(json_filename) == false) {
86             PrintWarning("[DrawGeometriesWithCustomAnimation] Failed loading json file.\n");
87             PrintWarning("[DrawGeometriesWithCustomAnimation] Possibly due to bad file or file does not contain trajectory.\n");
88             return false;
89         }
90         visualizer.UpdateWindowTitle();
91     }
92     visualizer.Run();
93     visualizer.DestroyWindow();
94     return true;
95 }
96 
DrawGeometriesWithAnimationCallback(const std::vector<std::shared_ptr<const Geometry>> & geometry_ptrs,std::function<bool (Visualizer *)> callback_func,const std::string & window_name,int width,int height,int left,int top)97 bool DrawGeometriesWithAnimationCallback(
98         const std::vector<std::shared_ptr<const Geometry>> &geometry_ptrs,
99         std::function<bool(Visualizer *)> callback_func,
100         const std::string &window_name/* = "Open3D"*/,
101         int width/* = 640*/, int height/* = 480*/,
102         int left/* = 50*/, int top/* = 50*/)
103 {
104     Visualizer visualizer;
105     if (visualizer.CreateWindow(window_name, width, height, left, top) ==
106             false) {
107         PrintWarning("[DrawGeometriesWithAnimationCallback] Failed creating OpenGL window.\n");
108         return false;
109     }
110     for (const auto &geometry_ptr : geometry_ptrs) {
111         if (visualizer.AddGeometry(geometry_ptr) == false) {
112             PrintWarning("[DrawGeometriesWithAnimationCallback] Failed adding geometry.\n");
113             PrintWarning("[DrawGeometriesWithAnimationCallback] Possibly due to bad geometry or wrong geometry type.\n");
114             return false;
115         }
116     }
117     visualizer.RegisterAnimationCallback(callback_func);
118     visualizer.Run();
119     visualizer.DestroyWindow();
120     return true;
121 }
122 
DrawGeometriesWithKeyCallbacks(const std::vector<std::shared_ptr<const Geometry>> & geometry_ptrs,const std::map<int,std::function<bool (Visualizer *)>> & key_to_callback,const std::string & window_name,int width,int height,int left,int top)123 bool DrawGeometriesWithKeyCallbacks(
124         const std::vector<std::shared_ptr<const Geometry>> &geometry_ptrs,
125         const std::map<int, std::function<bool(Visualizer *)>> &key_to_callback,
126         const std::string &window_name/* = "Open3D"*/,
127         int width/* = 640*/, int height/* = 480*/,
128         int left/* = 50*/, int top/* = 50*/)
129 {
130     VisualizerWithKeyCallback visualizer;
131     if (visualizer.CreateWindow(window_name, width, height, left, top) ==
132             false) {
133         PrintWarning("[DrawGeometriesWithKeyCallbacks] Failed creating OpenGL window.\n");
134         return false;
135     }
136     for (const auto &geometry_ptr : geometry_ptrs) {
137         if (visualizer.AddGeometry(geometry_ptr) == false) {
138             PrintWarning("[DrawGeometriesWithKeyCallbacks] Failed adding geometry.\n");
139             PrintWarning("[DrawGeometriesWithKeyCallbacks] Possibly due to bad geometry or wrong geometry type.\n");
140             return false;
141         }
142     }
143     for (auto key_func_pair : key_to_callback) {
144         visualizer.RegisterKeyCallback(key_func_pair.first,
145                 key_func_pair.second);
146     }
147     visualizer.Run();
148     visualizer.DestroyWindow();
149     return true;
150 }
151 
DrawGeometriesWithEditing(const std::vector<std::shared_ptr<const Geometry>> & geometry_ptrs,const std::string & window_name,int width,int height,int left,int top)152 bool DrawGeometriesWithEditing(
153         const std::vector<std::shared_ptr<const Geometry>> &geometry_ptrs,
154         const std::string &window_name/* = "Open3D"*/,
155         int width/* = 640*/, int height/* = 480*/,
156         int left/* = 50*/, int top/* = 50*/)
157 {
158     VisualizerWithEditing visualizer;
159     if (visualizer.CreateWindow(window_name, width, height, left, top) ==
160             false) {
161         PrintWarning("[DrawGeometriesWithEditing] Failed creating OpenGL window.\n");
162         return false;
163     }
164     for (const auto &geometry_ptr : geometry_ptrs) {
165         if (visualizer.AddGeometry(geometry_ptr) == false) {
166             PrintWarning("[DrawGeometriesWithEditing] Failed adding geometry.\n");
167             PrintWarning("[DrawGeometriesWithEditing] Possibly due to bad geometry or wrong geometry type.\n");
168             return false;
169         }
170     }
171     visualizer.Run();
172     visualizer.DestroyWindow();
173     return true;
174 }
175 
176 }    // namespace three
177