MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
scene.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <iostream>
11#include <algorithm>
12
13#include "util/exception.h"
14#include "util/timer.h"
15#include "util/file_system.h"
16#include "mve/scene.h"
17#include "mve/bundle_io.h"
18
20
21void
22Scene::load_scene (std::string const& base_path)
23{
24 if (base_path.empty())
25 throw util::Exception("Invalid file name given");
26 this->basedir = base_path;
27 this->init_views();
28}
29
30/* ---------------------------------------------------------------- */
31
32void
33Scene::save_scene (void)
34{
35 this->save_bundle();
36 this->save_views();
37}
38
39/* ---------------------------------------------------------------- */
40
41void
42Scene::save_bundle (void)
43{
44 if (this->bundle == nullptr || !this->bundle_dirty)
45 return;
46 std::string filename = util::fs::join_path(this->basedir, "synth_0.out");
47 save_mve_bundle(this->bundle, filename);
48 this->bundle_dirty = false;
49}
50
51/* ---------------------------------------------------------------- */
52
53void
54Scene::save_views (void)
55{
56 std::cout << "Saving views to MVE files..." << std::flush;
57 for (std::size_t i = 0; i < this->views.size(); ++i)
58 if (this->views[i] != nullptr && this->views[i]->is_dirty())
59 this->views[i]->save_view();
60 std::cout << " done." << std::endl;
61}
62
63/* ---------------------------------------------------------------- */
64
65bool
66Scene::is_dirty (void) const
67{
68 if (this->bundle_dirty)
69 return true;
70
71 for (std::size_t i = 0; i < this->views.size(); ++i)
72 if (this->views[i] != nullptr && this->views[i]->is_dirty())
73 return true;
74
75 return false;
76}
77
78/* ---------------------------------------------------------------- */
79
80void
81Scene::cache_cleanup (void)
82{
83 if (this->bundle.use_count() == 1)
84 this->bundle.reset();
85
86 std::size_t released = 0;
87 std::size_t affected_views = 0;
88 std::size_t total_views = 0;
89 for (std::size_t i = 0; i < this->views.size(); ++i)
90 {
91 if (this->views[i] == nullptr)
92 continue;
93
94 total_views += 1;
95 std::size_t num = this->views[i]->cache_cleanup();
96 if (num)
97 {
98 released += num;
99 affected_views += 1;
100 }
101 }
102
103 std::cout << "Cleanup: Released " << released << " embeddings in "
104 << affected_views << " of " << total_views << " views." << std::endl;
105}
106
107/* ---------------------------------------------------------------- */
108
109std::size_t
110Scene::get_total_mem_usage (void)
111{
112 std::size_t ret = 0;
113 ret += this->get_view_mem_usage();
114 ret += this->get_bundle_mem_usage();
115 return ret;
116}
117
118/* ---------------------------------------------------------------- */
119
120std::size_t
121Scene::get_view_mem_usage (void)
122{
123 std::size_t ret = 0;
124 for (std::size_t i = 0; i < this->views.size(); ++i)
125 if (this->views[i] != nullptr)
126 ret += this->views[i]->get_byte_size();
127 return ret;
128}
129
130/* ---------------------------------------------------------------- */
131
132std::size_t
133Scene::get_bundle_mem_usage (void)
134{
135 return (this->bundle != nullptr ? this->bundle->get_byte_size() : 0);
136}
137
138/* ---------------------------------------------------------------- */
139
140void
141Scene::init_views (void)
142{
143 util::WallTimer timer;
144
145 /* Iterate over all mve files and create view. */
146 std::string views_path = util::fs::join_path(this->basedir,
148 util::fs::Directory views_dir;
149 try
150 {
151 views_dir.scan(views_path);
152 }
153 catch (util::Exception& e)
154 {
155 throw util::Exception(views_path + ": ", e.what());
156 }
157 std::sort(views_dir.begin(), views_dir.end());
158
159 /* Give some feedback... */
160 std::cout << "Initializing scene with " << views_dir.size()
161 << " views..." << std::endl;
162
163 /* Load views in a temp list. */
164 ViewList temp_list;
165 int max_id = 0;
166 for (std::size_t i = 0; i < views_dir.size(); ++i)
167 {
168 if (views_dir[i].name.size() < 4)
169 continue;
170 if (util::string::right(views_dir[i].name, 4) != ".mve")
171 continue;
172 View::Ptr view = View::create();
173 view->load_view(views_dir[i].get_absolute_name());
174 temp_list.push_back(view);
175 max_id = std::max(max_id, view->get_id());
176 }
177
178 if (max_id > 5000 && max_id > 2 * (int)temp_list.size())
179 throw util::Exception("Spurious view IDs");
180
181 /* Transfer temp list to real list. */
182 this->views.clear();
183 if (!temp_list.empty())
184 this->views.resize(max_id + 1);
185 for (std::size_t i = 0; i < temp_list.size(); ++i)
186 {
187 std::size_t id = temp_list[i]->get_id();
188
189 if (this->views[id] != nullptr)
190 {
191 std::cout << "Warning loading MVE file "
192 << this->views[id]->get_directory() << std::endl
193 << " View with ID " << id << " already present"
194 << ", skipping file."
195 << std::endl;
196 continue;
197 }
198
199 this->views[id] = temp_list[i];
200 }
201
202 std::cout << "Initialized " << temp_list.size()
203 << " views (max ID is " << max_id << "), took "
204 << timer.get_elapsed() << "ms." << std::endl;
205}
206
207/* ---------------------------------------------------------------- */
208
209Bundle::ConstPtr
210Scene::get_bundle (void)
211{
212 if (this->bundle == nullptr)
213 {
214 std::string filename = util::fs::join_path(this->basedir,
216 this->bundle = load_mve_bundle(filename);
217 this->bundle_dirty = false;
218 }
219 return this->bundle;
220}
221
Universal, simple exception class.
Definition exception.h:24
virtual const char * what(void) const
Definition exception.h:43
Cross-platform high-resolution real-time timer.
Definition timer.h:30
std::size_t get_elapsed(void) const
Returns the milli seconds since last reset.
Definition timer.h:94
Directory abstraction to scan directory contents.
void scan(std::string const &path)
#define MVE_NAMESPACE_BEGIN
Definition defines.h:13
#define MVE_NAMESPACE_END
Definition defines.h:14
void save_mve_bundle(Bundle::ConstPtr bundle, std::string const &filename)
TODO: For now refers to save_photosynther_bundle().
Definition bundle_io.cc:40
Bundle::Ptr load_mve_bundle(std::string const &filename)
TODO: For now refers to load_photosynther_bundle().
Definition bundle_io.cc:34
std::string join_path(std::string const &path1, std::string const &path2)
Concatenate and canonicalize two paths.
std::string right(std::string const &str, std::size_t chars)
Returns the rightmost 'chars' characters of 'str'.
Definition strings.h:452
#define MVE_SCENE_VIEWS_DIR
Definition scene.h:21
#define MVE_SCENE_BUNDLE_FILE
Definition scene.h:22