mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-10-02 04:59:52 +00:00
Create first groups and datasets
This commit is contained in:
@@ -30,7 +30,7 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
|
|
||||||
const H5std_string FILE_NAME("output_file.hdf5");
|
const H5std_string FILE_NAME("output_file2.hdf5");
|
||||||
|
|
||||||
|
|
||||||
HdfSerializer::HdfSerializer(const std::string& obj_filename, const std::string& mtl_filename, const SerializerSettings& settings)
|
HdfSerializer::HdfSerializer(const std::string& obj_filename, const std::string& mtl_filename, const SerializerSettings& settings)
|
||||||
@@ -39,20 +39,20 @@ HdfSerializer::HdfSerializer(const std::string& obj_filename, const std::string&
|
|||||||
, obj_stream(IfcUtil::path::from_utf8(obj_filename).c_str())
|
, obj_stream(IfcUtil::path::from_utf8(obj_filename).c_str())
|
||||||
, mtl_stream(IfcUtil::path::from_utf8(mtl_filename).c_str())
|
, mtl_stream(IfcUtil::path::from_utf8(mtl_filename).c_str())
|
||||||
, vcount_total(1)
|
, vcount_total(1)
|
||||||
|
, file(FILE_NAME, H5F_ACC_TRUNC)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
obj_stream << std::setprecision(settings.precision);
|
obj_stream << std::setprecision(settings.precision);
|
||||||
mtl_stream << std::setprecision(settings.precision);
|
mtl_stream << std::setprecision(settings.precision);
|
||||||
|
guids = {};
|
||||||
file = H5::H5File(FILE_NAME, H5F_ACC_TRUNC);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool HdfSerializer::ready() {
|
bool HdfSerializer::ready() {
|
||||||
return obj_stream.is_open() && mtl_stream.is_open();
|
return obj_stream.is_open() && mtl_stream.is_open();
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ void HdfSerializer::writeHeader() {
|
|||||||
std::string mtl_basename = mtl_filename;
|
std::string mtl_basename = mtl_filename;
|
||||||
std::string::size_type slash = mtl_basename.find_last_of(dir_separator);
|
std::string::size_type slash = mtl_basename.find_last_of(dir_separator);
|
||||||
if (slash != std::string::npos) {
|
if (slash != std::string::npos) {
|
||||||
mtl_basename = mtl_basename.substr(slash+1);
|
mtl_basename = mtl_basename.substr(slash + 1);
|
||||||
}
|
}
|
||||||
obj_stream << "mtllib " << mtl_basename << "\n";
|
obj_stream << "mtllib " << mtl_basename << "\n";
|
||||||
mtl_stream << "# File generated by IfcOpenShell " << IFCOPENSHELL_VERSION << "\n";
|
mtl_stream << "# File generated by IfcOpenShell " << IFCOPENSHELL_VERSION << "\n";
|
||||||
@@ -75,10 +75,10 @@ void HdfSerializer::writeHeader() {
|
|||||||
|
|
||||||
void HdfSerializer::writeMaterial(const IfcGeom::Material& style)
|
void HdfSerializer::writeMaterial(const IfcGeom::Material& style)
|
||||||
{
|
{
|
||||||
std::string material_name = (settings().get(SerializerSettings::USE_MATERIAL_NAMES)
|
std::string material_name = (settings().get(SerializerSettings::USE_MATERIAL_NAMES)
|
||||||
? style.original_name() : style.name());
|
? style.original_name() : style.name());
|
||||||
IfcUtil::sanitate_material_name(material_name);
|
IfcUtil::sanitate_material_name(material_name);
|
||||||
mtl_stream << "newmtl " << material_name << "\n";
|
mtl_stream << "newmtl " << material_name << "\n";
|
||||||
|
|
||||||
if (style.hasDiffuse()) {
|
if (style.hasDiffuse()) {
|
||||||
const double* diffuse = style.diffuse();
|
const double* diffuse = style.diffuse();
|
||||||
@@ -94,144 +94,84 @@ void HdfSerializer::writeMaterial(const IfcGeom::Material& style)
|
|||||||
if (style.hasTransparency()) {
|
if (style.hasTransparency()) {
|
||||||
const double transparency = 1.0 - style.transparency();
|
const double transparency = 1.0 - style.transparency();
|
||||||
if (transparency < 1) {
|
if (transparency < 1) {
|
||||||
mtl_stream << "d " << transparency << "\n";
|
mtl_stream << "d " << transparency << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HdfSerializer::write(const IfcGeom::TriangulationElement<real_t>* o)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
const H5std_string DATASET_NAME("IntArray");
|
|
||||||
const int NX = 5; // dataset dimensions
|
|
||||||
const int NY = 6;
|
|
||||||
const int RANK = 2;
|
|
||||||
|
|
||||||
int i, j;
|
|
||||||
int data[NX][NY]; // buffer for data to write
|
void HdfSerializer::write(const IfcGeom::TriangulationElement<real_t>* o){
|
||||||
for (j = 0; j < NX; j++)
|
|
||||||
|
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())
|
||||||
{
|
{
|
||||||
for (i = 0; i < NY; i++)
|
elementGroup = file.openGroup(guid);
|
||||||
data[j][i] = i + j;
|
meshGroup = elementGroup.openGroup("Triangle Mesh");
|
||||||
}
|
dataset = meshGroup.openDataSet("Positions");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
guids.insert(guid);
|
||||||
|
elementGroup = file.createGroup(guid);
|
||||||
|
meshGroup = elementGroup.createGroup("Triangle Mesh");
|
||||||
|
|
||||||
H5::Group group = file.createGroup("/Data");
|
H5std_string DATASET_NAME_POSITIONS("Positions");
|
||||||
|
H5std_string DATASET_NAME_NORMALS("Normals");
|
||||||
|
H5std_string DATASET_NAME_INDICES("Indices");
|
||||||
|
|
||||||
hsize_t dimsf[2]; // dataset dimensions
|
const int RANK = 2;
|
||||||
dimsf[0] = NX;
|
hsize_t dimsf[2];
|
||||||
dimsf[1] = NY;
|
dimsf[0] = vcountt;
|
||||||
H5::DataSpace dataspace(RANK, dimsf);
|
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);
|
||||||
|
|
||||||
H5::IntType datatype(H5::PredType::NATIVE_INT);
|
for (std::vector<real_t>::const_iterator it = mesh.verts().begin(); it != mesh.verts().end(); ) {
|
||||||
datatype.setOrder(H5T_ORDER_LE);
|
const real_t x = *(it++);
|
||||||
H5::DataSet dataset = group.createDataSet(DATASET_NAME, datatype, dataspace);
|
const real_t y = *(it++);
|
||||||
dataset.write(data, H5::PredType::NATIVE_INT);
|
const real_t z = *(it++);
|
||||||
|
data_container.push_back(x);
|
||||||
|
data_container.push_back(y);
|
||||||
|
data_container.push_back(z);
|
||||||
|
|
||||||
|
|
||||||
obj_stream << "g " << object_id(o) << "\n";
|
|
||||||
obj_stream << "s 1" << "\n";
|
|
||||||
const bool isyup = settings().get(SerializerSettings::USE_Y_UP);
|
|
||||||
|
|
||||||
const IfcGeom::Representation::Triangulation<real_t>& mesh = o->geometry();
|
|
||||||
|
|
||||||
const int vcount = (int)mesh.verts().size() / 3;
|
|
||||||
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++);
|
|
||||||
|
|
||||||
if (isyup) {
|
|
||||||
obj_stream << "v " << x << " " << z << " " << -y << "\n";
|
|
||||||
} else {
|
|
||||||
obj_stream << "v " << x << " " << y << " " << z << "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for ( std::vector<real_t>::const_iterator it = mesh.normals().begin(); it != mesh.normals().end(); ) {
|
|
||||||
const real_t x = *(it++);
|
|
||||||
const real_t y = *(it++);
|
|
||||||
const real_t z = *(it++);
|
|
||||||
obj_stream << "vn " << x << " " << y << " " << z << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
for (std::vector<real_t>::const_iterator it = mesh.uvs().begin(); it != mesh.uvs().end();) {
|
|
||||||
const real_t u = *it++;
|
|
||||||
const real_t v = *it++;
|
|
||||||
obj_stream << "vt " << u << " " << v << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
int previous_material_id = -2;
|
|
||||||
std::vector<int>::const_iterator material_it = mesh.material_ids().begin();
|
|
||||||
|
|
||||||
const bool has_uvs = !mesh.uvs().empty();
|
|
||||||
const bool has_normals = !mesh.normals().empty();
|
|
||||||
for ( std::vector<int>::const_iterator it = mesh.faces().begin(); it != mesh.faces().end(); ) {
|
|
||||||
|
|
||||||
const int material_id = *(material_it++);
|
|
||||||
if (material_id != previous_material_id) {
|
|
||||||
const IfcGeom::Material& material = mesh.materials()[material_id];
|
|
||||||
std::string material_name = (settings().get(SerializerSettings::USE_MATERIAL_NAMES)
|
|
||||||
? material.original_name() : material.name());
|
|
||||||
IfcUtil::sanitate_material_name(material_name);
|
|
||||||
obj_stream << "usemtl " << material_name << "\n";
|
|
||||||
if (materials.find(material_name) == materials.end()) {
|
|
||||||
writeMaterial(material);
|
|
||||||
materials.insert(material_name);
|
|
||||||
}
|
|
||||||
previous_material_id = material_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const int v1 = *(it++)+vcount_total;
|
//data_container.clear();
|
||||||
const int v2 = *(it++)+vcount_total;
|
//data_container.push_back(3.4);
|
||||||
const int v3 = *(it++)+vcount_total;
|
|
||||||
|
dataset.write(data_container.data(), H5::PredType::NATIVE_DOUBLE);
|
||||||
|
data_container.clear();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (has_normals && has_uvs) {
|
|
||||||
obj_stream << "f " << v1 << "/" << v1 << "/" << v1 << " "
|
|
||||||
<< v2 << "/" << v2 << "/" << v2 << " "
|
|
||||||
<< v3 << "/" << v3 << "/" << v3 << "\n";
|
|
||||||
} else if (has_normals) {
|
|
||||||
obj_stream << "f " << v1 << "//" << v1 << " "
|
|
||||||
<< v2 << "//" << v2 << " "
|
|
||||||
<< v3 << "//" << v3 << "\n";
|
|
||||||
} else {
|
|
||||||
obj_stream << "f " << v1 << " " << v2 << " " << v3 << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<int> faces_set (mesh.faces().begin(), mesh.faces().end());
|
|
||||||
const std::vector<int>& edges = mesh.edges();
|
|
||||||
|
|
||||||
for ( std::vector<int>::const_iterator it = edges.begin(); it != edges.end(); ) {
|
|
||||||
const int i1 = *(it++);
|
|
||||||
const int i2 = *(it++);
|
|
||||||
|
|
||||||
if (faces_set.find(i1) != faces_set.end() || faces_set.find(i2) != faces_set.end()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int material_id = *(material_it++);
|
|
||||||
|
|
||||||
if (material_id != previous_material_id) {
|
|
||||||
const IfcGeom::Material& material = mesh.materials()[material_id];
|
|
||||||
std::string material_name = (settings().get(SerializerSettings::USE_MATERIAL_NAMES)
|
|
||||||
? material.original_name() : material.name());
|
|
||||||
IfcUtil::sanitate_material_name(material_name);
|
|
||||||
obj_stream << "usemtl " << material_name << "\n";
|
|
||||||
if (materials.find(material_name) == materials.end()) {
|
|
||||||
writeMaterial(material);
|
|
||||||
materials.insert(material_name);
|
|
||||||
}
|
|
||||||
previous_material_id = material_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int v1 = i1 + vcount_total;
|
|
||||||
const int v2 = i2 + vcount_total;
|
|
||||||
|
|
||||||
obj_stream << "l " << v1 << " " << v2 << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
vcount_total += vcount;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ private:
|
|||||||
unsigned int vcount_total;
|
unsigned int vcount_total;
|
||||||
std::set<std::string> materials;
|
std::set<std::string> materials;
|
||||||
H5::H5File file;
|
H5::H5File file;
|
||||||
|
std::set<std::string> guids;
|
||||||
|
std::vector<double> data_container;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in New Issue
Block a user