Files
IfcOpenShell/src/serializers/HdfSerializer.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
4.5 KiB
C++
Raw Normal View History

2021-02-12 17:32:43 +01:00
/********************************************************************************
* *
* 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/>. *
* *
********************************************************************************/
#include "HdfSerializer.h"
#include "../ifcgeom_schema_agnostic/IfcGeomRenderStyles.h"
#include "../ifcparse/utils.h"
#include <boost/lexical_cast.hpp>
#include <iomanip>
2021-02-14 12:23:43 +01:00
2021-02-19 17:03:41 +01:00
HdfSerializer::HdfSerializer(const std::string& hdf_filename, const SerializerSettings& settings)
2021-02-12 17:32:43 +01:00
: GeometrySerializer(settings)
2021-02-19 17:03:41 +01:00
, hdf_filename(hdf_filename)
, DATASET_NAME_POSITIONS("Positions")
, DATASET_NAME_NORMALS("Normals")
, DATASET_NAME_INDICES("Indices")
2021-02-19 12:05:29 +01:00
, DATASET_NAME_OCCT("OCCT Text")
2021-02-12 17:32:43 +01:00
{
2021-02-15 21:17:35 +01:00
guids = {};
2021-02-12 17:32:43 +01:00
}
2021-02-15 21:17:35 +01:00
2021-02-12 17:32:43 +01:00
bool HdfSerializer::ready() {
2021-02-19 17:05:32 +01:00
//todo: check whether the file exists
2021-02-17 17:20:03 +01:00
return true;
2021-02-12 17:32:43 +01:00
}
void HdfSerializer::writeHeader() {
2021-02-19 17:03:41 +01:00
const H5std_string FILE_NAME(hdf_filename);
2021-02-17 17:20:03 +01:00
file = H5::H5File(FILE_NAME, H5F_ACC_TRUNC);
2021-02-12 17:32:43 +01:00
}
void HdfSerializer::write(const IfcGeom::BRepElement<real_t>* o) {
2021-02-15 21:17:35 +01:00
std::string guid = o->guid();
H5::Group elementGroup;
2021-02-15 21:17:35 +01:00
H5::Group meshGroup;
H5::DataSet positionsDataset;
H5::DataSet normalsDataset;
H5::DataSet indicesDataset;
H5::Group OCCTGroup;
2021-02-19 12:05:29 +01:00
H5::DataSet OCCTDataset;
const IfcGeom::Representation::BRep& brepmesh = o->geometry();
const IfcGeom::Representation::Serialization *serialization = new IfcGeom::Representation::Serialization(brepmesh);
std::string brep_data = serialization->brep_data();
const IfcGeom::TriangulationElement<real_t>*triangular_element = new IfcGeom::TriangulationElement<real_t>(*o);
const IfcGeom::Representation::Triangulation<real_t>& mesh = triangular_element->geometry();
const int vcount = (int)mesh.verts().size() / 3;
const int fcount = (int)mesh.faces().size() / 3;
2021-02-18 10:41:54 +01:00
const bool isyup = settings().get(SerializerSettings::USE_Y_UP);
std::string value = o->type();
if (fcount > 0) {
2021-02-15 21:17:35 +01:00
guids.insert(guid);
elementGroup = file.createGroup(guid);
2021-02-19 12:05:29 +01:00
2021-02-20 15:35:33 +01:00
meshGroup = elementGroup.createGroup("Triangle Mesh");
2021-02-19 12:05:29 +01:00
OCCTGroup = elementGroup.createGroup("OCCT Data");
H5::StrType str_type(0, H5T_VARIABLE);
H5:: DataSpace attrdspace(H5S_SCALAR);
H5::Attribute att = elementGroup.createAttribute("IFC entity type", str_type, attrdspace);
att.write(str_type, value);
2021-02-15 21:17:35 +01:00
const int RANK = 2;
hsize_t dimsf[2];
dimsf[0] = vcount;
2021-02-20 15:35:33 +01:00
dimsf[1] = 3;
2021-02-15 21:17:35 +01:00
H5::DataSpace dataspace(RANK, dimsf);
hsize_t dimsfaces[2];
dimsfaces[0] = fcount;
2021-02-20 15:35:33 +01:00
dimsfaces[1] = 3;
H5::DataSpace face_dataspace(RANK, dimsfaces);
2021-02-19 12:05:29 +01:00
const int RANK_OCCT = 1;
hsize_t dimsocct[2];
dimsocct[0] = 1;
dimsfaces[1] = 1;
H5::DataSpace occt_dataspace(RANK_OCCT, dimsocct);
OCCTDataset = OCCTGroup.createDataSet(DATASET_NAME_OCCT, str_type, occt_dataspace);
OCCTDataset.write(brep_data, str_type);
2021-02-17 10:38:27 +01:00
2021-02-20 15:35:33 +01:00
indicesDataset = meshGroup.createDataSet(DATASET_NAME_INDICES, H5::PredType::NATIVE_INT, face_dataspace);
positionsDataset = meshGroup.createDataSet(DATASET_NAME_POSITIONS, H5::PredType::NATIVE_DOUBLE, dataspace);
normalsDataset = meshGroup.createDataSet(DATASET_NAME_NORMALS, H5::PredType::NATIVE_DOUBLE, dataspace);
2021-02-18 10:41:54 +01:00
2021-02-20 15:35:33 +01:00
positionsDataset.write(mesh.verts().data(), H5::PredType::NATIVE_DOUBLE);
normalsDataset.write(mesh.normals().data(), H5::PredType::NATIVE_DOUBLE);
indicesDataset.write(mesh.faces().data(), H5::PredType::NATIVE_INT);
2021-02-18 10:41:54 +01:00
2021-02-14 12:23:43 +01:00
2021-02-12 17:32:43 +01:00
}
2021-02-12 17:32:43 +01:00
}