Files
IfcOpenShell/src/ifcgeom_schema_agnostic/SurfaceStyle.cpp
T

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

132 lines
5.7 KiB
C++
Raw Normal View History

2017-12-20 17:09:42 +01:00
#include "../ifcgeom_schema_agnostic/IfcGeomRenderStyles.h"
2017-12-20 16:38:55 +01:00
2018-10-30 14:32:28 +01:00
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
2017-12-20 16:38:55 +01:00
#include <map>
2020-12-20 14:38:54 +01:00
#include <mutex>
2017-12-20 16:38:55 +01:00
2018-10-30 14:32:28 +01:00
namespace pt = boost::property_tree;
2021-10-08 15:38:46 +02:00
static std::map<std::string, std::shared_ptr<IfcGeom::SurfaceStyle>> default_materials;
static std::shared_ptr<IfcGeom::SurfaceStyle> default_material;
2017-12-20 16:38:55 +01:00
static bool default_materials_initialized = false;
void InitDefaultMaterials() {
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcSite", std::make_shared<IfcGeom::SurfaceStyle>("IfcSite")));
default_materials["IfcSite"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.75, 0.8, 0.65));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcSlab", std::make_shared<IfcGeom::SurfaceStyle>("IfcSlab")));
default_materials["IfcSlab"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.4, 0.4, 0.4));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcWallStandardCase", std::make_shared<IfcGeom::SurfaceStyle>("IfcWallStandardCase")));
default_materials["IfcWallStandardCase"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.9, 0.9, 0.9));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcWall", std::make_shared<IfcGeom::SurfaceStyle>("IfcWall")));
default_materials["IfcWall"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.9, 0.9, 0.9));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcWindow", std::make_shared<IfcGeom::SurfaceStyle>("IfcWindow")));
default_materials["IfcWindow"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.75, 0.8, 0.75));
default_materials["IfcWindow"]->Transparency().reset(0.3);
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcDoor", std::make_shared<IfcGeom::SurfaceStyle>("IfcDoor")));
default_materials["IfcDoor"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.55, 0.3, 0.15));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcBeam", std::make_shared<IfcGeom::SurfaceStyle>("IfcBeam")));
default_materials["IfcBeam"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.75, 0.7, 0.7));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcRailing", std::make_shared<IfcGeom::SurfaceStyle>("IfcRailing")));
default_materials["IfcRailing"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.65, 0.6, 0.6));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcMember", std::make_shared<IfcGeom::SurfaceStyle>("IfcMember")));
default_materials["IfcMember"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.65, 0.6, 0.6));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcPlate", std::make_shared<IfcGeom::SurfaceStyle>("IfcPlate")));
default_materials["IfcPlate"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.8, 0.8, 0.8));
2017-12-20 16:38:55 +01:00
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair("IfcSpace", std::make_shared<IfcGeom::SurfaceStyle>("IfcSpace")));
2023-03-08 23:04:15 +08:00
default_materials["IfcSpace"]->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.65, 0.75, 0.8));
default_materials["IfcSpace"]->Transparency().reset(0.8);
2021-10-08 15:38:46 +02:00
default_material = std::make_shared<IfcGeom::SurfaceStyle>("DefaultMaterial");
default_material->Diffuse().reset(IfcGeom::SurfaceStyle::ColorComponent(0.7, 0.7, 0.7));
2017-12-20 16:38:55 +01:00
default_materials_initialized = true;
}
2018-10-30 14:32:28 +01:00
boost::optional<IfcGeom::SurfaceStyle::ColorComponent> read_colour_component(const boost::optional<pt::ptree&> list) {
if (!list) {
return boost::none;
}
double rgb[3];
int i = 0;
for (pt::ptree::value_type &colour : list.get()) {
if (3 <= i) {
throw std::runtime_error("rgb array over 3 elements large");
}
rgb[i] = colour.second.get_value<double>();
i++;
}
if (i != 3) {
throw std::runtime_error("rgb array less than 3 elements large (was " + std::to_string(i) + ")");
}
return IfcGeom::SurfaceStyle::ColorComponent(rgb[0], rgb[1], rgb[2]);
}
void IfcGeom::set_default_style_file(const std::string& json_file) {
if (!default_materials_initialized) InitDefaultMaterials();
default_materials.clear();
2019-03-08 11:40:16 +01:00
// @todo this will probably need to be updated for UTF-8 paths on Windows
2018-10-30 14:32:28 +01:00
pt::ptree root;
pt::read_json(json_file, root);
for (pt::ptree::value_type &material_pair : root) {
std::string name = material_pair.first;
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair(name, std::make_shared<IfcGeom::SurfaceStyle>(name)));
2018-10-30 14:32:28 +01:00
pt::ptree material = material_pair.second;
boost::optional<pt::ptree&> diffuse = material.get_child_optional("diffuse");
2021-10-08 15:38:46 +02:00
default_materials[name]->Diffuse() = read_colour_component(diffuse);
2018-10-30 14:32:28 +01:00
boost::optional<pt::ptree&> specular = material.get_child_optional("specular");
2021-10-08 15:38:46 +02:00
default_materials[name]->Specular() = read_colour_component(specular);
2018-10-30 14:32:28 +01:00
if (material.get_child_optional("specular-roughness")) {
2021-10-08 15:38:46 +02:00
default_materials[name]->Specularity().reset(1.0 / material.get<double>("specular-roughness"));
2018-10-30 14:32:28 +01:00
}
if (material.get_child_optional("transparency")) {
2021-10-08 15:38:46 +02:00
default_materials[name]->Transparency() = material.get<double>("transparency");
2018-10-30 14:32:28 +01:00
}
}
// Is "*" present? If yes, remove it and make it the default style.
2021-10-08 15:38:46 +02:00
auto it = default_materials.find("*");
2018-10-30 14:32:28 +01:00
if (it != default_materials.end()) {
2021-10-08 15:38:46 +02:00
default_material = it->second;
2018-10-30 14:32:28 +01:00
default_materials.erase(it);
}
}
2021-10-08 15:38:46 +02:00
std::shared_ptr<const IfcGeom::SurfaceStyle> IfcGeom::get_default_style(const std::string& s) {
2020-12-20 14:38:54 +01:00
static std::mutex m;
std::lock_guard<std::mutex> lk(m);
2017-12-20 16:38:55 +01:00
if (!default_materials_initialized) InitDefaultMaterials();
2021-10-08 15:38:46 +02:00
auto it = default_materials.find(s);
2017-12-20 16:38:55 +01:00
if (it == default_materials.end()) {
2021-10-08 15:38:46 +02:00
default_materials.insert(std::make_pair(s, default_material));
2017-12-20 16:38:55 +01:00
it = default_materials.find(s);
}
2021-10-08 15:38:46 +02:00
return it->second;
2017-12-20 16:38:55 +01:00
}
IfcGeom::SurfaceStyle& IfcGeom::update_default_style(const std::string& s) {
if (!default_materials_initialized) InitDefaultMaterials();
2021-10-08 15:38:46 +02:00
auto it = default_materials.find(s);
if (it == default_materials.end()) {
throw std::runtime_error("No style registered for " + s);
}
2021-10-08 15:38:46 +02:00
return *it->second;
}