Files
IfcOpenShell/src/serializers/HdfSerializer.cpp
T

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

159 lines
5.3 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"
2021-02-14 12:23:43 +01:00
2021-02-12 17:32:43 +01:00
#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-15 21:23:08 +01:00
const H5std_string FILE_NAME("output_file.h5");
2021-02-14 12:23:43 +01:00
2021-02-12 17:32:43 +01:00
HdfSerializer::HdfSerializer(const std::string& obj_filename, const std::string& mtl_filename, const SerializerSettings& settings)
: GeometrySerializer(settings)
, mtl_filename(mtl_filename)
, obj_stream(IfcUtil::path::from_utf8(obj_filename).c_str())
, mtl_stream(IfcUtil::path::from_utf8(mtl_filename).c_str())
, vcount_total(1)
2021-02-15 21:17:35 +01:00
, file(FILE_NAME, H5F_ACC_TRUNC)
2021-02-12 17:32:43 +01:00
{
obj_stream << std::setprecision(settings.precision);
mtl_stream << std::setprecision(settings.precision);
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() {
return obj_stream.is_open() && mtl_stream.is_open();
}
void HdfSerializer::writeHeader() {
obj_stream << "# File generated by IfcOpenShell " << IFCOPENSHELL_VERSION << "\n";
#ifdef WIN32
const char dir_separator = '\\';
#else
const char dir_separator = '/';
#endif
std::string mtl_basename = mtl_filename;
std::string::size_type slash = mtl_basename.find_last_of(dir_separator);
if (slash != std::string::npos) {
2021-02-15 21:17:35 +01:00
mtl_basename = mtl_basename.substr(slash + 1);
2021-02-12 17:32:43 +01:00
}
obj_stream << "mtllib " << mtl_basename << "\n";
mtl_stream << "# File generated by IfcOpenShell " << IFCOPENSHELL_VERSION << "\n";
}
void HdfSerializer::writeMaterial(const IfcGeom::Material& style)
{
2021-02-15 21:17:35 +01:00
std::string material_name = (settings().get(SerializerSettings::USE_MATERIAL_NAMES)
? style.original_name() : style.name());
IfcUtil::sanitate_material_name(material_name);
mtl_stream << "newmtl " << material_name << "\n";
2021-02-12 17:32:43 +01:00
if (style.hasDiffuse()) {
const double* diffuse = style.diffuse();
mtl_stream << "Kd " << diffuse[0] << " " << diffuse[1] << " " << diffuse[2] << "\n";
}
if (style.hasSpecular()) {
const double* specular = style.specular();
mtl_stream << "Ks " << specular[0] << " " << specular[1] << " " << specular[2] << "\n";
}
if (style.hasSpecularity()) {
mtl_stream << "Ns " << style.specularity() << "\n";
}
if (style.hasTransparency()) {
const double transparency = 1.0 - style.transparency();
if (transparency < 1) {
2021-02-15 21:17:35 +01:00
mtl_stream << "d " << transparency << "\n";
2021-02-12 17:32:43 +01:00
}
}
}
2021-02-14 12:23:43 +01:00
2021-02-15 21:17:35 +01:00
void HdfSerializer::write(const IfcGeom::TriangulationElement<real_t>* o){
std::string guid = o->guid();
//Logger::Status(guid);
//Logger::Status(o->context());
//Logger::Status("\n");
H5::Group elementGroup;
H5::Group meshGroup;
H5::DataSet dataset;
const IfcGeom::Representation::Triangulation<real_t>& mesh = o->geometry();
const int vcountt = (int)mesh.verts().size() / 3;
if (std::find(guids.begin(), guids.end(), guid) != guids.end())
2021-02-14 12:23:43 +01:00
{
2021-02-15 21:17:35 +01:00
elementGroup = file.openGroup(guid);
meshGroup = elementGroup.openGroup("Triangle Mesh");
dataset = meshGroup.openDataSet("Positions");
} else {
guids.insert(guid);
elementGroup = file.createGroup(guid);
meshGroup = elementGroup.createGroup("Triangle Mesh");
H5std_string DATASET_NAME_POSITIONS("Positions");
H5std_string DATASET_NAME_NORMALS("Normals");
H5std_string DATASET_NAME_INDICES("Indices");
const int RANK = 2;
hsize_t dimsf[2];
dimsf[0] = vcountt;
dimsf[1] = 3;
H5::DataSpace dataspace(RANK, dimsf);
H5::IntType datatype(H5::PredType::NATIVE_INT);
datatype.setOrder(H5T_ORDER_LE);
dataset = meshGroup.createDataSet(DATASET_NAME_POSITIONS, datatype, dataspace);
for (std::vector<real_t>::const_iterator it = mesh.verts().begin(); it != mesh.verts().end(); ) {
const real_t x = *(it++);
const real_t y = *(it++);
const real_t z = *(it++);
data_container.push_back(x);
data_container.push_back(y);
data_container.push_back(z);
2021-02-14 12:23:43 +01:00
2021-02-15 21:17:35 +01:00
}
2021-02-14 12:23:43 +01:00
2021-02-15 21:17:35 +01:00
//data_container.clear();
//data_container.push_back(3.4);
dataset.write(data_container.data(), H5::PredType::NATIVE_DOUBLE);
data_container.clear();
2021-02-14 12:23:43 +01:00
2021-02-12 17:32:43 +01:00
}
}