-
-
Notifications
You must be signed in to change notification settings - Fork 902
Expand file tree
/
Copy pathIterator.h
More file actions
333 lines (268 loc) · 11.9 KB
/
Iterator.h
File metadata and controls
333 lines (268 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
/********************************************************************************
* *
* Geometrical data in an IFC file consists of shapes (IfcShapeRepresentation) *
* and instances (SUBTYPE OF IfcBuildingElement e.g. IfcWindow). *
* *
* IfcGeom::Representation::Triangulation is a class that represents a *
* triangulated IfcShapeRepresentation. *
* Triangulation.verts is a 1 dimensional vector of float defining the *
* cartesian coordinates of the vertices of the triangulated shape in the *
* format of [x1,y1,z1,..,xn,yn,zn] *
* Triangulation.faces is a 1 dimensional vector of int containing the *
* indices of the triangles referencing positions in Triangulation.verts *
* Triangulation.edges is a 1 dimensional vector of int in {0,1} that dictates*
* the visibility of the edges that span the faces in Triangulation.faces *
* *
* IfcGeom::Element represents the actual IfcBuildingElements. *
* IfcGeomObject.name is the GUID of the element *
* IfcGeomObject.type is the datatype of the element e.g. IfcWindow *
* IfcGeomObject.mesh is a pointer to an IfcMesh *
* IfcGeomObject.transformation.matrix is a 4x3 matrix that defines the *
* orientation and translation of the mesh in relation to the world origin *
* *
* IfcGeom::Iterator::initialize() *
* finds the most suitable representation contexts. Returns true iff *
* at least a single representation will process successfully *
* *
* IfcGeom::Iterator::get() *
* returns a pointer to the current IfcGeom::Element *
* *
* IfcGeom::Iterator::next() *
* returns true iff a following entity is available for a successive call to *
* IfcGeom::Iterator::get() *
* *
* IfcGeom::Iterator::progress() *
* returns an int in [0..100] that indicates the overall progress *
* *
********************************************************************************/
#ifndef IFCGEOMITERATOR_H
#define IFCGEOMITERATOR_H
#include "../ifcparse/IfcFile.h"
#include "../ifcgeom/IfcGeomElement.h"
#include "../ifcgeom/ConversionResult.h"
#include "../ifcgeom/IfcGeomFilter.h"
#include "../ifcgeom/taxonomy.h"
#include "../ifcgeom/Converter.h"
#include "../ifcgeom/abstract_mapping.h"
#include "../ifcgeom/GeometrySerializer.h"
#include <boost/algorithm/string.hpp>
#include <map>
#include <set>
#include <vector>
#include <limits>
#include <algorithm>
#include <future>
#include <thread>
#include <chrono>
#include <atomic>
namespace IfcGeom {
struct IFC_GEOM_API geometry_conversion_result {
int index;
// For NoParallelMapping==true
ifcopenshell::geometry::taxonomy::ptr item;
std::vector<std::pair<IfcUtil::IfcBaseEntity*, ifcopenshell::geometry::taxonomy::matrix4::ptr>> products;
// For NoParallelMapping==false
IfcUtil::IfcBaseEntity* representation;
aggregate_of_instance::ptr products_2;
std::vector<IfcGeom::BRepElement*> breps;
std::vector<IfcGeom::Element*> elements;
};
class IFC_GEOM_API Iterator {
private:
GeometrySerializer* cache_ = nullptr;
std::atomic<bool> finished_{ false };
std::atomic<bool> terminating_{ false };
std::atomic<bool> had_error_processing_elements_ { false };
std::atomic<int> progress_{ 0 };
std::vector<geometry_conversion_result> tasks_;
std::vector<geometry_conversion_result>::iterator task_iterator_;
std::list<IfcGeom::Element*> all_processed_elements_;
std::list<IfcGeom::BRepElement*> all_processed_native_elements_;
std::list<IfcGeom::Element*>::const_iterator task_result_iterator_;
std::list<IfcGeom::BRepElement*>::const_iterator native_task_result_iterator_;
std::mutex element_ready_mutex_;
bool task_result_ptr_initialized = false;
bool task_result_ptr_exhausted = false;
size_t async_elements_returned_ = 0;
ifcopenshell::geometry::Settings settings_;
IfcParse::IfcFile* ifc_file;
std::vector<filter_t> filters_;
int num_threads_;
std::string geometry_library_;
// When single-threaded
ifcopenshell::geometry::Converter* converter_;
// When multi-threaded
std::vector<ifcopenshell::geometry::Converter*> kernel_pool;
// The object is fetched beforehand to be sure that get() returns a valid element
TriangulationElement* current_triangulation;
BRepElement* current_shape_model;
SerializedElement* current_serialization;
double lowest_precision_encountered;
bool any_precision_encountered;
int done;
int total;
ifcopenshell::geometry::taxonomy::point3 bounds_min_;
ifcopenshell::geometry::taxonomy::point3 bounds_max_;
// Should not be destructed because, destructor is blocking
std::future<void> init_future_;
std::mutex caching_mutex_;
std::array<std::chrono::high_resolution_clock::time_point, 4> time_points;
template <typename Fn>
Element* decorate_with_cache_(GeometrySerializer::read_type rt, const std::string& product_guid, const std::string& representation_id, Fn f) {
bool read_from_cache = false;
Element* element = nullptr;
#ifdef WITH_HDF5
if (cache_) {
std::lock_guard<std::mutex> lk(caching_mutex_);
auto from_cache = cache_->read(*ifc_file, product_guid, representation_id, rt);
if (from_cache) {
read_from_cache = true;
element = from_cache;
}
}
#endif
if (!read_from_cache) {
element = f();
}
#ifdef WITH_HDF5
if (cache_ && !read_from_cache && element) {
std::lock_guard<std::mutex> lk(caching_mutex_);
if (rt == GeometrySerializer::READ_TRIANGULATION) {
cache_->write((IfcGeom::TriangulationElement*)element);
} else {
cache_->write((IfcGeom::BRepElement*)element);
}
}
#endif
return element;
}
const IfcUtil::IfcBaseClass* create_shape_model_for_next_entity();
void create_element_(
ifcopenshell::geometry::Converter* kernel,
ifcopenshell::geometry::Settings settings,
geometry_conversion_result* rep);
IfcGeom::Element* process_based_on_settings(
ifcopenshell::geometry::Settings settings,
IfcGeom::BRepElement* elem,
IfcGeom::TriangulationElement* previous = nullptr);
bool wait_for_element();
void log_timepoints() const;
void validate_iterator_state() const;
ifcopenshell::geometry::taxonomy::direction3::ptr remove_offset_();
public:
Iterator(std::unique_ptr<ifcopenshell::geometry::kernels::AbstractKernel>&& geometry_library, const ifcopenshell::geometry::Settings& settings, IfcParse::IfcFile* file, const std::vector<IfcGeom::filter_t>& filters, int num_threads)
: settings_(settings)
, ifc_file(file)
, filters_(filters)
, num_threads_(num_threads)
, geometry_library_(geometry_library->geometry_library())
// @todo verify whether settings are correctly passed on
, converter_(new ifcopenshell::geometry::Converter(std::move(geometry_library), ifc_file, settings_))
{
}
Iterator(std::unique_ptr<ifcopenshell::geometry::kernels::AbstractKernel>&& geometry_library, const ifcopenshell::geometry::Settings& settings, IfcParse::IfcFile* file)
: settings_(settings)
, ifc_file(file)
, num_threads_(1)
, geometry_library_(geometry_library->geometry_library())
, converter_(new ifcopenshell::geometry::Converter(std::move(geometry_library), ifc_file, settings_))
{
}
Iterator(std::unique_ptr<ifcopenshell::geometry::kernels::AbstractKernel>&& geometry_library, const ifcopenshell::geometry::Settings& settings, IfcParse::IfcFile* file, int num_threads)
: settings_(settings)
, ifc_file(file)
, num_threads_(num_threads)
, geometry_library_(geometry_library->geometry_library())
, converter_(new ifcopenshell::geometry::Converter(std::move(geometry_library), ifc_file, settings_))
{
}
~Iterator();
void set_cache(GeometrySerializer* cache) { cache_ = cache; }
std::vector<ifcopenshell::geometry::taxonomy::item::ptr> get_task_items() const {
std::vector<ifcopenshell::geometry::taxonomy::item::ptr> items;
items.reserve(tasks_.size());
for (const auto& task : tasks_) {
items.push_back(task.item);
}
return items;
}
aggregate_of_aggregate_of_instance::ptr get_task_products() const {
aggregate_of_aggregate_of_instance::ptr products = aggregate_of_aggregate_of_instance::ptr(new aggregate_of_aggregate_of_instance);
for (const auto& task : tasks_) {
if (task.products_2) {
products->push(task.products_2);
} else {
for (auto& product : task.products) {
aggregate_of_instance::ptr p(new aggregate_of_instance);
p->push(product.first);
products->push(p);
}
}
}
return products;
}
const std::string& unit_name() const { return converter_->mapping()->get_length_unit_name(); }
double unit_magnitude() const { return converter_->mapping()->get_length_unit(); }
// Check if error occurred during iterator initialization or iteration over elements.
bool had_error_processing_elements() const { return had_error_processing_elements_; }
boost::optional<bool> initialization_outcome_;
/**
* @return Returns true if the iterator is initialized with any elements, false otherwise.
*
* @note
* - A true return value does not guarantee successful initialization of all elements.
* Some elements may have failed to initialize. Check had_error_processing_elements()
* to see whether there were errors during the initialization.
*
* - For non-concurrent iterators, a false return may occur if initialization of the first
* element fails, even if subsequent elements could be initialized successfully.
*/
bool initialize();
size_t processed_ = 0;
void process_finished_rep(geometry_conversion_result* rep);
void process_concurrently();
/// Computes model's bounding box (bounds_min and bounds_max).
/// @note Can take several minutes for large files.
void compute_bounds(bool with_geometry);
int progress() const {
return progress_;
}
std::string getLog() const { return Logger::GetLog(); }
IfcParse::IfcFile* file() const { return ifc_file; }
const std::vector<IfcGeom::filter_t>& filters() const { return filters_; }
std::vector<IfcGeom::filter_t>& filters() { return filters_; }
const ifcopenshell::geometry::taxonomy::point3& bounds_min() const { return bounds_min_; }
const ifcopenshell::geometry::taxonomy::point3& bounds_max() const { return bounds_max_; }
/// Moves to the next shape representation, create its geometry, and returns the associated product.
/// Use get() to retrieve the created geometry.
const IfcUtil::IfcBaseClass* next();
/// Gets the representation of the current geometrical entity.
Element* get();
/// Gets the native (Open Cascade or CGAL) representation of the current geometrical entity.
BRepElement* get_native()
{
return *native_task_result_iterator_;
}
const Element* get_object(int id);
const IfcUtil::IfcBaseClass* create();
};
}
#endif