shared_ptr styles all over #4833

This commit is contained in:
Thomas Krijnen
2024-06-10 20:11:24 +02:00
parent f2074d95fd
commit c01263d721
13 changed files with 97 additions and 93 deletions
+18 -18
View File
@@ -66,7 +66,7 @@ void ColladaSerializer::ColladaExporter::ColladaGeometries::write(
const std::string &mesh_id, const std::string &/**<@todo 'default_material_name' unused, remove? */,
const std::vector<double>& positions, const std::vector<double>& normals,
const std::vector<int>& faces, const std::vector<int>& edges,
const std::vector<int>& material_ids, const std::vector<ifcopenshell::geometry::taxonomy::style>& /**<@todo 'materials' unused, remove? */,
const std::vector<int>& material_ids, const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& /**<@todo 'materials' unused, remove? */,
const std::vector<double>& uvs, const std::vector<std::string>& material_references)
{
openMesh(mesh_id);
@@ -302,24 +302,24 @@ void ColladaSerializer::ColladaExporter::ColladaScene::write() {
}
void ColladaSerializer::ColladaExporter::ColladaMaterials::ColladaEffects::write(
const ifcopenshell::geometry::taxonomy::style &material, const std::string &material_uri)
const ifcopenshell::geometry::taxonomy::style::ptr &material, const std::string &material_uri)
{
openEffect(material_uri + "-fx");
COLLADASW::EffectProfile effect(mSW);
effect.setShaderType(COLLADASW::EffectProfile::LAMBERT);
if (material.diffuse) {
const auto& diffuse = material.diffuse.ccomponents();
if (material->diffuse) {
const auto& diffuse = material->diffuse.ccomponents();
effect.setDiffuse(COLLADASW::ColorOrTexture(COLLADASW::Color(diffuse(0),diffuse(1),diffuse(2))));
}
if (material.specular) {
const auto& specular = material.specular.ccomponents();
if (material->specular) {
const auto& specular = material->specular.ccomponents();
effect.setSpecular(COLLADASW::ColorOrTexture(COLLADASW::Color(specular(0),specular(1),specular(2))));
}
if (material.specularity == material.specularity) {
effect.setShininess(material.specularity);
if (material->specularity == material->specularity) {
effect.setShininess(material->specularity);
}
if (material.transparency == material.transparency) {
const double transparency = material.transparency;
if (material->transparency == material->transparency) {
const double transparency = material->transparency;
if (transparency > 0) {
// The default opacity mode for Collada is A_ONE, which apparently indicates a
// transparency value of 1 to be fully opaque. Hence transparency is inverted.
@@ -334,12 +334,12 @@ void ColladaSerializer::ColladaExporter::ColladaMaterials::ColladaEffects::close
closeLibrary();
}
void ColladaSerializer::ColladaExporter::ColladaMaterials::add(const ifcopenshell::geometry::taxonomy::style& material) {
void ColladaSerializer::ColladaExporter::ColladaMaterials::add(const ifcopenshell::geometry::taxonomy::style::ptr& material) {
if (!contains(material)) {
std::string material_name = material.name;
std::string material_name = material->name;
if (material_name.empty()) {
material_name = "missing-material-" + material.name;
material_name = "missing-material-" + material->name;
}
collada_id(material_name);
@@ -350,19 +350,19 @@ void ColladaSerializer::ColladaExporter::ColladaMaterials::add(const ifcopenshel
}
}
std::string ColladaSerializer::ColladaExporter::ColladaMaterials::getMaterialUri(const ifcopenshell::geometry::taxonomy::style& material) {
std::vector<ifcopenshell::geometry::taxonomy::style>::iterator it = std::find(materials.begin(), materials.end(), material);
std::string ColladaSerializer::ColladaExporter::ColladaMaterials::getMaterialUri(const ifcopenshell::geometry::taxonomy::style::ptr& material) {
std::vector<ifcopenshell::geometry::taxonomy::style::ptr>::iterator it = std::find(materials.begin(), materials.end(), material);
ptrdiff_t index = std::distance(materials.begin(), it);
return material_uris.at(index);
}
bool ColladaSerializer::ColladaExporter::ColladaMaterials::contains(const ifcopenshell::geometry::taxonomy::style& material) {
bool ColladaSerializer::ColladaExporter::ColladaMaterials::contains(const ifcopenshell::geometry::taxonomy::style::ptr& material) {
return std::find(materials.begin(), materials.end(), material) != materials.end();
}
void ColladaSerializer::ColladaExporter::ColladaMaterials::write() {
effects.close();
BOOST_FOREACH(const ifcopenshell::geometry::taxonomy::style& material, materials) {
BOOST_FOREACH(const ifcopenshell::geometry::taxonomy::style::ptr& material, materials) {
std::string material_name = getMaterialUri(material);
openMaterial(material_name);
@@ -396,7 +396,7 @@ void ColladaSerializer::ColladaExporter::write(const IfcGeom::TriangulationEleme
collada_id(representation_id);
std::vector<std::string> material_references;
BOOST_FOREACH(const ifcopenshell::geometry::taxonomy::style& material, mesh.materials()) {
BOOST_FOREACH(const ifcopenshell::geometry::taxonomy::style::ptr& material, mesh.materials()) {
materials.add(material);
std::string material_name = materials.getMaterialUri(material);
+8 -8
View File
@@ -75,7 +75,7 @@ private:
const std::string &mesh_id, const std::string &default_material_name,
const std::vector<double>& positions, const std::vector<double>& normals,
const std::vector<int>& faces, const std::vector<int>& edges,
const std::vector<int>& material_ids, const std::vector<ifcopenshell::geometry::taxonomy::style>& materials,
const std::vector<int>& material_ids, const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& materials,
const std::vector<double>& uvs, const std::vector<std::string>& material_references);
void close();
ColladaSerializer *serializer;
@@ -118,11 +118,11 @@ private:
explicit ColladaEffects(COLLADASW::StreamWriter& stream)
: COLLADASW::LibraryEffects(&stream)
{}
void write(const ifcopenshell::geometry::taxonomy::style &material, const std::string &material_uri);
void write(const ifcopenshell::geometry::taxonomy::style::ptr& material, const std::string &material_uri);
void close();
ColladaSerializer *serializer;
};
std::vector<ifcopenshell::geometry::taxonomy::style> materials;
std::vector<ifcopenshell::geometry::taxonomy::style::ptr> materials;
std::vector<std::string> material_uris;
public:
explicit ColladaMaterials(COLLADASW::StreamWriter& stream, ColladaSerializer *_serializer)
@@ -130,9 +130,9 @@ private:
, serializer(_serializer)
, effects(stream)
{}
void add(const ifcopenshell::geometry::taxonomy::style& material);
std::string getMaterialUri(const ifcopenshell::geometry::taxonomy::style& material);
bool contains(const ifcopenshell::geometry::taxonomy::style& material);
void add(const ifcopenshell::geometry::taxonomy::style::ptr& material);
std::string getMaterialUri(const ifcopenshell::geometry::taxonomy::style::ptr& material);
bool contains(const ifcopenshell::geometry::taxonomy::style::ptr& material);
void write();
ColladaSerializer *serializer;
ColladaEffects effects;
@@ -165,14 +165,14 @@ private:
std::vector<int> faces;
std::vector<int> edges;
std::vector<int> material_ids;
std::vector<ifcopenshell::geometry::taxonomy::style> materials;
std::vector<ifcopenshell::geometry::taxonomy::style::ptr> materials;
std::vector<std::string> material_references;
std::vector<double> uvs;
std::vector<const IfcGeom::Element*> parents_;
DeferredObject(const std::string& unique_id, const std::string& representation_id, const std::string& type, const IfcGeom::Transformation& transformation,
const std::vector<double>& vertices, const std::vector<double>& normals, const std::vector<int>& faces,
const std::vector<int>& edges, const std::vector<int>& material_ids, const std::vector<ifcopenshell::geometry::taxonomy::style>& materials,
const std::vector<int>& edges, const std::vector<int>& material_ids, const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& materials,
const std::vector<std::string>& material_references, const std::vector<double>& uvs)
: unique_id(unique_id)
, representation_id(representation_id)
+8 -8
View File
@@ -86,29 +86,29 @@ void GltfSerializer::writeHeader() {
json_["materials"] = json::array();
}
int GltfSerializer::writeMaterial(const ifcopenshell::geometry::taxonomy::style& style) {
auto it = materials_.find(style.name);
int GltfSerializer::writeMaterial(const ifcopenshell::geometry::taxonomy::style::ptr style) {
auto it = materials_.find(style->name);
if (it != materials_.end()) {
return it->second;
}
int idx = json_["materials"].size();
materials_[style.name] = idx;
materials_[style->name] = idx;
std::array<double, 4> base;
base.fill(1.0);
if (style.diffuse) {
if (style->diffuse) {
for (int i = 0; i < 3; ++i) {
base[i] = style.diffuse.ccomponents()(i);
base[i] = style->diffuse.ccomponents()(i);
}
}
if (style.transparency == style.transparency) {
base[3] = 1. - style.transparency;
if (style->transparency == style->transparency) {
base[3] = 1. - style->transparency;
}
json_["materials"].push_back({ {"doubleSided", true}, {"pbrMetallicRoughness", {{"baseColorFactor", base}, {"metallicFactor", 0}}} });
if (style.transparency == style.transparency && style.transparency > 1.e-9) {
if (style->transparency == style->transparency && style->transparency > 1.e-9) {
json_["materials"].back()["alphaMode"] = "BLEND";
}
+1 -1
View File
@@ -39,7 +39,7 @@ private:
boost::optional<json> ecef_transform_, north_rotation_;
int bufferViewId;
int writeMaterial(const ifcopenshell::geometry::taxonomy::style& style);
int writeMaterial(const ifcopenshell::geometry::taxonomy::style::ptr style);
public:
GltfSerializer(const std::string& filename, const ifcopenshell::geometry::Settings& geometry_settings, const ifcopenshell::geometry::SerializerSettings& settings);
virtual ~GltfSerializer();
+7 -5
View File
@@ -233,7 +233,8 @@ namespace {
}
}
void HdfSerializer::read_surface_style(surface_style_serialization& s, ifcopenshell::geometry::taxonomy::style& gss) {
void HdfSerializer::read_surface_style(surface_style_serialization& s, const ifcopenshell::geometry::taxonomy::style::ptr& gss_) {
auto& gss = *gss_;
if (strlen(s.name) || s.id) {
if (s.diffuse[0] == s.diffuse[0]) {
gss.diffuse = ifcopenshell::geometry::taxonomy::colour(s.diffuse[0], s.diffuse[1], s.diffuse[2]);
@@ -355,7 +356,7 @@ IfcGeom::Element* HdfSerializer::read(IfcParse::IfcFile& f, const std::string& g
matrix->components() << Eigen::Map<Eigen::Matrix4d>(&part.matrix[0][0]);
auto style_ptr = ifcopenshell::geometry::taxonomy::make<ifcopenshell::geometry::taxonomy::style>();
read_surface_style(part.surface_style, *style_ptr);
read_surface_style(part.surface_style, style_ptr);
shapes.push_back(IfcGeom::ConversionResult(part.id, matrix, new ifcopenshell::geometry::OpenCascadeShape(shp), style_ptr));
}
@@ -416,7 +417,7 @@ IfcGeom::Element* HdfSerializer::read(IfcParse::IfcFile& f, const std::string& g
ds.read(surface_styles.data(), style_compound);
}
std::vector<ifcopenshell::geometry::taxonomy::style> surface_style_ptrs(surface_styles.size());
std::vector<ifcopenshell::geometry::taxonomy::style::ptr> surface_style_ptrs(surface_styles.size());
for (size_t i = 0; i < surface_styles.size(); ++i) {
read_surface_style(surface_styles[i], surface_style_ptrs[i]);
@@ -536,7 +537,8 @@ H5::Group HdfSerializer::createRepresentationGroup(const H5::Group& element_grou
return representation_group;
}
void HdfSerializer::write_style(surface_style_serialization& data, const ifcopenshell::geometry::taxonomy::style& s) {
void HdfSerializer::write_style(surface_style_serialization& data, const ifcopenshell::geometry::taxonomy::style::ptr& sptr) {
auto& s = *sptr;
data.name = s.name.c_str();
// @todo
data.original_name = s.name.c_str();
@@ -609,7 +611,7 @@ void HdfSerializer::write(const IfcGeom::BRepElement* o) {
parts[i].surface_style = { "", "", 0, {nan,nan,nan}, {nan,nan,nan}, nan, nan };
if (it->hasStyle()) {
auto& s = it->Style();
auto s = it->StylePtr();
write_style(parts[i].surface_style, s);
}
+2 -2
View File
@@ -89,8 +89,8 @@ private:
std::map<std::string, std::string> group_cache_;
H5::Group createRepresentationGroup(const H5::Group& element_group, const std::string& gid);
void read_surface_style(surface_style_serialization& sss, ifcopenshell::geometry::taxonomy::style& style_ptr);
void write_style(surface_style_serialization& data, const ifcopenshell::geometry::taxonomy::style& s);
void read_surface_style(surface_style_serialization& sss, const ifcopenshell::geometry::taxonomy::style::ptr& style_ptr);
void write_style(surface_style_serialization& data, const ifcopenshell::geometry::taxonomy::style::ptr& s);
public:
HdfSerializer(const std::string& hdf_filename, const ifcopenshell::geometry::Settings& geometry_settings, const ifcopenshell::geometry::SerializerSettings& settings, bool read_only=false);
+6 -5
View File
@@ -59,8 +59,9 @@ void WaveFrontOBJSerializer::writeHeader() {
mtl_stream.stream << "# File generated by IfcOpenShell " << IFCOPENSHELL_VERSION << "\n";
}
void WaveFrontOBJSerializer::writeMaterial(const ifcopenshell::geometry::taxonomy::style& style)
void WaveFrontOBJSerializer::writeMaterial(const ifcopenshell::geometry::taxonomy::style::ptr styleptr)
{
auto& style = *styleptr;
std::string material_name = style.name;
IfcUtil::sanitate_material_name(material_name);
mtl_stream.stream << "newmtl " << material_name << "\n";
@@ -127,8 +128,8 @@ void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement* o)
const int material_id = *(material_it++);
if (material_id != previous_material_id) {
const ifcopenshell::geometry::taxonomy::style& material = mesh.materials()[material_id];
std::string material_name = material.name;
const ifcopenshell::geometry::taxonomy::style::ptr material = mesh.materials()[material_id];
std::string material_name = material->name;
IfcUtil::sanitate_material_name(material_name);
obj_stream.stream << "usemtl " << material_name << "\n";
if (materials.find(material_name) == materials.end()) {
@@ -170,8 +171,8 @@ void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement* o)
const int material_id = *(material_it++);
if (material_id != previous_material_id) {
const ifcopenshell::geometry::taxonomy::style& material = mesh.materials()[material_id];
std::string material_name = material.name;
const ifcopenshell::geometry::taxonomy::style::ptr material = mesh.materials()[material_id];
std::string material_name = material->name;
IfcUtil::sanitate_material_name(material_name);
obj_stream.stream << "usemtl " << material_name << "\n";
if (materials.find(material_name) == materials.end()) {
+1 -1
View File
@@ -39,7 +39,7 @@ public:
virtual ~WaveFrontOBJSerializer() {}
bool ready();
void writeHeader();
void writeMaterial(const ifcopenshell::geometry::taxonomy::style& style);
void writeMaterial(const ifcopenshell::geometry::taxonomy::style::ptr style);
void write(const IfcGeom::TriangulationElement* o);
void write(const IfcGeom::BRepElement* /*o*/) {}
void finalize() {}