Backspace everything regarding HDF5

This commit is contained in:
Thomas Krijnen
2026-05-08 16:20:26 +02:00
parent bd436765bf
commit 554c7174e3
66 changed files with 50 additions and 1928 deletions
-3
View File
@@ -28,9 +28,6 @@ endif()
if(WITH_MANIFOLD)
target_compile_definitions(IfcGeom PUBLIC IFOPSH_WITH_MANIFOLD)
endif()
if(HDF5_SUPPORT)
target_compile_definitions(IfcGeom PUBLIC WITH_HDF5)
endif()
if(UNIX)
find_package(Threads)
+3 -10
View File
@@ -362,7 +362,7 @@ void IfcGeom::Iterator::create_element_(ifcopenshell::geometry::Converter* kerne
logger::set_product(product);
IfcGeom::BRepElement* brep = static_cast<IfcGeom::BRepElement*>(decorate_with_cache_(GeometrySerializer::READ_BREP, (std::string)product.as<express::Entity>().get("GlobalId"), std::to_string(rep->item->instance.id()), [kernel, settings, product, place, rep]() {
IfcGeom::BRepElement* brep = static_cast<IfcGeom::BRepElement*>(create_processed_element_([kernel, settings, product, place, rep]() {
return kernel->create_brep_for_representation_and_product(rep->item, product, place);
}));
@@ -385,7 +385,7 @@ void IfcGeom::Iterator::create_element_(ifcopenshell::geometry::Converter* kerne
const express::Base product2 = p.first;
const auto& place2 = p.second;
IfcGeom::BRepElement* brep2 = static_cast<IfcGeom::BRepElement*>(decorate_with_cache_(GeometrySerializer::READ_BREP, (std::string)product2.as<express::Entity>().get("GlobalId"), std::to_string(rep->item->instance.id()), [kernel, settings, product2, place2, brep]() {
IfcGeom::BRepElement* brep2 = static_cast<IfcGeom::BRepElement*>(create_processed_element_([kernel, settings, product2, place2, brep]() {
return kernel->create_brep_for_processed_representation(product2, place2, brep);
}));
if (brep2) {
@@ -410,14 +410,7 @@ IfcGeom::Element* IfcGeom::Iterator::process_based_on_settings(ifcopenshell::geo
return nullptr;
}
} else if (settings.get<ifcopenshell::geometry::settings::IteratorOutput>().get() == ifcopenshell::geometry::settings::TRIANGULATED) {
// the part before the hyphen is the representation id
auto gid2 = elem->geometry().id();
auto hyphen = gid2.find("-");
if (hyphen != std::string::npos) {
gid2 = gid2.substr(0, hyphen);
}
return decorate_with_cache_(GeometrySerializer::READ_TRIANGULATION, elem->guid(), gid2, [elem, previous]() {
return create_processed_element_([elem, previous]() {
try {
if (!previous) {
return new TriangulationElement(*elem);
+2 -40
View File
@@ -66,7 +66,6 @@
#include "../ifcgeom/taxonomy.h"
#include "../ifcgeom/Converter.h"
#include "../ifcgeom/abstract_mapping.h"
#include "../ifcgeom/GeometrySerializer.h"
#include <boost/algorithm/string.hpp>
@@ -104,8 +103,6 @@ namespace IfcGeom {
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 };
@@ -153,44 +150,11 @@ namespace IfcGeom {
// 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;
Element* create_processed_element_(Fn f) {
return f();
}
express::Base create_shape_model_for_next_entity();
@@ -244,8 +208,6 @@ namespace IfcGeom {
~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());
@@ -64,11 +64,6 @@
#include <IntTools_FaceFace.hxx>
#include "clash_utils.h"
#ifdef WITH_HDF5
#include "H5Cpp.h"
#endif
namespace IfcGeom {
namespace {
@@ -1498,225 +1493,6 @@ namespace IfcGeom {
}
}
#ifdef WITH_HDF5
void write_h5() {
H5::H5File file("filename.h5", H5F_ACC_TRUNC);
H5::Group shapes = file.createGroup("/shapes");
std::set<std::string> processed_geometry_ids;
std::vector<int> element_shape_ids;
std::unordered_map<std::string, int> geometry_id_to_shape_id;
int geometry_index = 0;
std::vector<std::vector<float>> matrices;
std::vector<std::array<float, 4>> colours;
std::vector<std::string> names;
std::vector<std::string> global_ids;
const float tolerance = 0.01f; // Tolerance value for comparison
for (const auto& elem : triangulation_elements_) {
const auto geometry_id = elem->geometry().id();
const auto& placement = placements_[elem->product()];
{
decltype(matrices)::value_type m;
for (size_t i = 0; i < 16; ++i) {
m.push_back(static_cast<float>(placement->ccomponents().data()[i]));
}
matrices.push_back(m);
}
names.push_back(names_[elem->product()]);
global_ids.push_back(global_ids_[elem->product()]);
if (processed_geometry_ids.find(geometry_id) != processed_geometry_ids.end()) {
element_shape_ids.push_back(geometry_id_to_shape_id[geometry_id]);
continue;
}
processed_geometry_ids.insert(geometry_id);
H5::Group group = shapes.createGroup(std::to_string(geometry_index));
geometry_id_to_shape_id[geometry_id] = geometry_index;
element_shape_ids.push_back(geometry_index);
geometry_index++;
const auto& faces = local_faces_[geometry_id];
const auto& verts = local_verts_[geometry_id];
const auto& materials = local_materials_[geometry_id];
const auto& material_ids = local_material_ids_[geometry_id];
std::vector<float> verts_float(verts.size());
std::transform(verts.begin(), verts.end(), verts_float.begin(),
[](double val) { return static_cast<float>(val); });
// Write faces
size_t total_verts = verts.size() / 3;
hsize_t faces_dims[1] = {faces.size()};
H5::DataSpace faces_dataspace(1, faces_dims);
H5::DSetCreatPropList faces_propList;
faces_propList.setChunk(1, faces_dims);
faces_propList.setDeflate(9);
if (total_verts < (1 << 8)) {
H5::DataType dtype = H5::PredType::NATIVE_UINT8;
std::vector<uint8_t> faces_dtype(faces.begin(), faces.end());
H5::DataSet faces_dataset = group.createDataSet("faces", dtype, faces_dataspace, faces_propList);
faces_dataset.write(faces_dtype.data(), dtype);
} else if (total_verts < (1 << 16)) {
H5::DataType dtype = H5::PredType::NATIVE_UINT16;
std::vector<uint16_t> faces_dtype(faces.begin(), faces.end());
H5::DataSet faces_dataset = group.createDataSet("faces", dtype, faces_dataspace, faces_propList);
faces_dataset.write(faces_dtype.data(), dtype);
} else {
H5::DataType dtype = H5::PredType::NATIVE_UINT32;
H5::DataSet faces_dataset = group.createDataSet("faces", dtype, faces_dataspace, faces_propList);
faces_dataset.write(faces.data(), dtype);
}
// Write verts
H5::DataType dtype = H5::PredType::NATIVE_FLOAT;
hsize_t dims[1] = {verts.size()};
H5::DataSpace dataspace(1, dims);
H5::DSetCreatPropList propList;
propList.setChunk(1, dims);
propList.setDeflate(9);
H5::DataSet dataset = group.createDataSet("verts", dtype, dataspace, propList);
dataset.write(verts_float.data(), H5::PredType::NATIVE_FLOAT);
// Write materials
std::vector<uint8_t> material_keys;
for (const auto& material : materials) {
float alpha = 1.0;
if (material->has_transparency() && material->transparency > 0) {
alpha = 1.0 - material->transparency;
}
int i = 0;
bool is_existing_colour = false;
for (const auto& colour : colours) {
if (std::abs(colour[0] - static_cast<float>(material->diffuse.ccomponents()[0])) < tolerance
&& std::abs(colour[1] - static_cast<float>(material->diffuse.ccomponents()[1])) < tolerance
&& std::abs(colour[2] - static_cast<float>(material->diffuse.ccomponents()[2])) < tolerance
&& std::abs(colour[3] - alpha) < tolerance) {
is_existing_colour = true;
break;
}
i++;
}
if ( ! is_existing_colour) {
colours.push_back({
static_cast<float>(material->diffuse.ccomponents()[0]),
static_cast<float>(material->diffuse.ccomponents()[1]),
static_cast<float>(material->diffuse.ccomponents()[2]),
alpha});
}
material_keys.push_back(static_cast<decltype(material_keys)::value_type>(i));
}
size_t total_material_keys = material_keys.size();
if (total_material_keys) {
hsize_t dims[1] = {material_keys.size()};
H5::DataSpace dataspace(1, dims);
H5::DSetCreatPropList propList;
propList.setChunk(1, dims);
propList.setDeflate(9);
H5::DataType dtype = H5::PredType::NATIVE_UINT8;
H5::DataSet dataset = group.createDataSet("materials", dtype, dataspace, propList);
dataset.write(material_keys.data(), dtype);
}
if (total_material_keys > 1) {
hsize_t dims[1] = {material_ids.size()};
H5::DataSpace dataspace(1, dims);
H5::DSetCreatPropList propList;
propList.setChunk(1, dims);
propList.setDeflate(9);
H5::DataType dtype = H5::PredType::NATIVE_UINT8;
H5::DataSet dataset = group.createDataSet("material_ids", dtype, dataspace, propList);
std::vector<uint8_t> data_dtype(material_ids.begin(), material_ids.end());
dataset.write(data_dtype.data(), dtype);
}
}
// Write GlobalIds
std::vector<uint8_t> uuids_array;
for (const auto& id_str : global_ids) {
for (size_t i = 0; i < id_str.length(); i += 2) {
uuids_array.push_back(std::stoi(id_str.substr(i, 2), 0, 16));
}
}
hsize_t global_ids_dims[2] = {global_ids.size(), 16}; // 16 bytes per UUID
H5::DataSpace global_ids_dataspace(2, global_ids_dims);
H5::DataSet global_ids_dataset = file.createDataSet("element_global_ids", H5::PredType::NATIVE_UINT8, global_ids_dataspace);
global_ids_dataset.write(uuids_array.data(), H5::PredType::NATIVE_UINT8);
// Write names
H5::StrType strType(H5::PredType::C_S1, H5T_VARIABLE);
hsize_t names_dims[1] = {names.size()};
H5::DataSpace names_dataspace(1, names_dims);
H5::DataSet names_dataset = file.createDataSet("element_names", strType, names_dataspace);
std::vector<const char*> cstr_names;
for (const auto& name : names) {
cstr_names.push_back(name.c_str());
}
names_dataset.write(&cstr_names[0], strType);
// Write matrices
std::vector<float> flat_matrices;
for (const auto& matrix : matrices) {
flat_matrices.insert(flat_matrices.end(), matrix.begin(), matrix.end());
}
hsize_t dims[2] = {matrices.size(), matrices[0].size()};
H5::DataSpace dataspace(2, dims);
H5::DSetCreatPropList propList;
propList.setChunk(2, dims);
propList.setDeflate(9);
H5::DataSet dataset = file.createDataSet("element_matrices", H5::PredType::NATIVE_FLOAT, dataspace, propList);
dataset.write(flat_matrices.data(), H5::PredType::NATIVE_FLOAT);
// Write element_shape_ids
hsize_t element_shape_ids_dims[1] = {element_shape_ids.size()};
H5::DataSpace element_shape_ids_dataspace(1, element_shape_ids_dims);
H5::DSetCreatPropList element_shape_ids_propList;
element_shape_ids_propList.setChunk(1, element_shape_ids_dims);
element_shape_ids_propList.setDeflate(9);
if (geometry_index < (1 << 8)) {
H5::DataType dtype = H5::PredType::NATIVE_UINT8;
std::vector<uint8_t> element_shape_ids_dtype(element_shape_ids.begin(), element_shape_ids.end());
H5::DataSet element_shape_ids_dataset = file.createDataSet("element_shape_ids", dtype, element_shape_ids_dataspace, element_shape_ids_propList);
element_shape_ids_dataset.write(element_shape_ids_dtype.data(), dtype);
} else if (geometry_index < (1 << 16)) {
H5::DataType dtype = H5::PredType::NATIVE_UINT16;
std::vector<uint16_t> element_shape_ids_dtype(element_shape_ids.begin(), element_shape_ids.end());
H5::DataSet element_shape_ids_dataset = file.createDataSet("element_shape_ids", dtype, element_shape_ids_dataspace, element_shape_ids_propList);
element_shape_ids_dataset.write(element_shape_ids_dtype.data(), dtype);
} else if (geometry_index < (1UL << 32)) {
H5::DataType dtype = H5::PredType::NATIVE_UINT32;
std::vector<uint32_t> element_shape_ids_dtype(element_shape_ids.begin(), element_shape_ids.end());
H5::DataSet element_shape_ids_dataset = file.createDataSet("element_shape_ids", dtype, element_shape_ids_dataspace, element_shape_ids_propList);
element_shape_ids_dataset.write(element_shape_ids_dtype.data(), dtype);
}
// Write colours
if (colours.size()) {
std::vector<float> flat_colours;
for (const auto& colour : colours) {
flat_colours.insert(flat_colours.end(), colour.begin(), colour.end());
}
hsize_t colours_dims[2] = {colours.size(), colours[0].size()};
H5::DataSpace colours_dataspace(2, colours_dims);
H5::DSetCreatPropList colours_propList;
colours_propList.setChunk(2, colours_dims);
colours_propList.setDeflate(9);
H5::DataSet colours_dataset = file.createDataSet("materials", H5::PredType::NATIVE_FLOAT, colours_dataspace, colours_propList);
colours_dataset.write(flat_colours.data(), H5::PredType::NATIVE_FLOAT);
}
}
#endif
template <typename T>
void apply_matrix_to_flat_verts(const std::vector<T>& flat_list, const ifcopenshell::geometry::taxonomy::matrix4::ptr& matrix, std::vector<T>& result) {
Eigen::Vector3d vin;
@@ -164,12 +164,6 @@ namespace ifcopenshell {
return tree_.clash_clearance_many(set_a, set_b, clearance, check_all);
}
#ifdef WITH_HDF5
void write_h5() override {
tree_.write_h5();
}
#endif
private:
IfcGeom::opencascade_tree tree_;
};
-6
View File
@@ -227,12 +227,6 @@ const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& IfcGeom::tree::
return backend ? backend->styles() : empty_style_vector();
}
#ifdef WITH_HDF5
void IfcGeom::tree::write_h5() {
impl_->backend(default_clash_backend_id).write_h5();
}
#endif
std::string IfcGeom::tree::uint8_to_b64(const std::vector<uint8_t>& uuids_array) const {
std::string hex_str;
hex_str.reserve(uuids_array.size() * 2);
-4
View File
@@ -100,10 +100,6 @@ namespace IfcGeom {
void enable_face_styles(bool enable);
const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& styles() const;
#ifdef WITH_HDF5
void write_h5();
#endif
std::string uint8_to_b64(const std::vector<uint8_t>& uuids_array) const;
static bool is_manifold(const std::vector<int>& faces);
-6
View File
@@ -104,12 +104,6 @@ const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& ifcopenshell::g
unsupported_tree_operation(std::string(backend_id()), "styles()");
}
#ifdef WITH_HDF5
void ifcopenshell::geometry::trees::abstract_tree::write_h5() {
unsupported_tree_operation(std::string(backend_id()), "write_h5()");
}
#endif
void ifcopenshell::geometry::trees::tree_registry::bind(const tree_info& info, create_fn create, const plugin::module& module) {
entry entry;
entry.info_ = info;
-3
View File
@@ -70,9 +70,6 @@ namespace ifcopenshell {
virtual void enable_face_styles(bool enable);
virtual const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& styles() const;
#ifdef WITH_HDF5
virtual void write_h5();
#endif
};
class IFC_GEOM_API tree_registry {