filename bug

This commit is contained in:
D4ve-R
2023-05-15 11:52:36 +02:00
parent fca79ddb6c
commit b9f2dbb0b3
2 changed files with 132 additions and 113 deletions
+51 -30
View File
@@ -1,28 +1,45 @@
/********************************************************************************
* *
* 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/>. *
* *
********************************************************************************/
#ifdef WITH_USD #ifdef WITH_USD
#include "USDSerializer.h" #include "USDSerializer.h"
#include "pxr/base/tf/token.h"
#include "pxr/base/gf/vec3f.h" #include "pxr/base/gf/vec3f.h"
#include "pxr/base/gf/rotation.h"
#include "pxr/usd/usdGeom/xform.h" #include "pxr/usd/usdGeom/xform.h"
#include "pxr/usd/usdGeom/xformOp.h"
#include "pxr/usd/usdGeom/tokens.h"
#include "pxr/usd/usdGeom/scope.h" #include "pxr/usd/usdGeom/scope.h"
#include "pxr/usd/usdLux/distantLight.h" #include "pxr/usd/usdLux/distantLight.h"
#include "pxr/usd/usdShade/shader.h" #include "pxr/usd/usdShade/shader.h"
#include "pxr/usd/usdShade/tokens.h"
#include "pxr/usd/usdShade/materialBindingAPI.h" #include "pxr/usd/usdShade/materialBindingAPI.h"
#include "pxr/usd/usdShade/connectableAPI.h"
USDSerializer::USDSerializer(const std::string& out_filename, const SerializerSettings& settings): USDSerializer::USDSerializer(const std::string& out_filename, const SerializerSettings& settings):
WriteOnlyGeometrySerializer(settings), WriteOnlyGeometrySerializer(settings),
filename_(out_filename) filename_(out_filename)
{ {
// create a new stage // why does this not work with just the filename ???
stage_ = pxr::UsdStage::CreateNew(filename_ + ".usda"); stage_ = pxr::UsdStage::CreateNew(filename_ + ".usda");
pxr::UsdGeomXform::Define(stage_, pxr::SdfPath("/World")); if(!stage_)
throw std::runtime_error("Could not create USD stage");
auto world = pxr::UsdGeomXform::Define(stage_, pxr::SdfPath("/World"));
stage_->SetDefaultPrim(world.GetPrim());
pxr::UsdGeomScope::Define(stage_, pxr::SdfPath("/Looks")); pxr::UsdGeomScope::Define(stage_, pxr::SdfPath("/Looks"));
createLighting(); createLighting();
ready_ = true; ready_ = true;
@@ -33,26 +50,32 @@ USDSerializer::~USDSerializer() {
} }
void USDSerializer::createLighting() { void USDSerializer::createLighting() {
// create a distant light
const std::string& light_path = "/World/defaultLight"; const std::string& light_path = "/World/defaultLight";
pxr::UsdLuxDistantLight::Define(stage_, pxr::SdfPath(light_path)); pxr::UsdLuxDistantLight::Define(stage_, pxr::SdfPath(light_path));
// set the light's orientation
pxr::UsdGeomXform xform(stage_->GetPrimAtPath(pxr::SdfPath(light_path))); pxr::UsdGeomXform xform(stage_->GetPrimAtPath(pxr::SdfPath(light_path)));
pxr::GfVec3f light_direction(0.0f, 0.0f, -1.0f); pxr::GfVec3f light_direction(0.0f, 0.0f, -1.0f);
//xform.AddRotateOp(pxr::UsdGeomXformOp::PrecisionFloat, pxr::UsdGeomXformOp::TypeRotateXYZ).Set(pxr::GfRotation(pxr::GfVec3f(0.0f, 1.0f, 0.0f), light_direction).GetQuaternion());
// set the light's color
pxr::UsdLuxDistantLight light(stage_->GetPrimAtPath(pxr::SdfPath(light_path))); pxr::UsdLuxDistantLight light(stage_->GetPrimAtPath(pxr::SdfPath(light_path)));
light.CreateIntensityAttr().Set(1000.0f); light.CreateIntensityAttr().Set(1000.0f);
light.CreateColorAttr().Set(pxr::GfVec3f(1.0f, 1.0f, 1.0f)); light.CreateColorAttr().Set(pxr::GfVec3f(1.0f, 1.0f, 1.0f));
} }
std::vector<pxr::UsdShadeMaterial> USDSerializer::createMaterials(const pxr::UsdGeomMesh& mesh, std::vector<pxr::UsdShadeMaterial> USDSerializer::createMaterials(const std::vector<IfcGeom::Material>& styles)
const std::vector<IfcGeom::Material>& styles)
{ {
if(styles.empty())
throw std::runtime_error("No styles to create materials from");
std::vector<pxr::UsdShadeMaterial> materials {}; std::vector<pxr::UsdShadeMaterial> materials {};
for(auto style : styles) { for(auto style : styles) {
std::string path("/Looks/" + sanitize(style.original_name())); std::string material_path(style.original_name());
usd_utils::toPath(material_path);
if(materials_.find(material_path) != materials_.end()) {
materials.push_back(materials_[material_path]);
continue;
}
const std::string path("/Looks/" + material_path);
auto material = pxr::UsdShadeMaterial::Define(stage_, pxr::SdfPath(path)); auto material = pxr::UsdShadeMaterial::Define(stage_, pxr::SdfPath(path));
auto shader = pxr::UsdShadeShader::Define(stage_, pxr::SdfPath(path + "/Shader")); auto shader = pxr::UsdShadeShader::Define(stage_, pxr::SdfPath(path + "/Shader"));
shader.CreateIdAttr().Set(pxr::TfToken("UsdPreviewSurface")); shader.CreateIdAttr().Set(pxr::TfToken("UsdPreviewSurface"));
@@ -78,12 +101,10 @@ std::vector<pxr::UsdShadeMaterial> USDSerializer::createMaterials(const pxr::Usd
material.CreateSurfaceOutput().ConnectToSource(shader.ConnectableAPI(), pxr::TfToken("surface")); material.CreateSurfaceOutput().ConnectToSource(shader.ConnectableAPI(), pxr::TfToken("surface"));
materials.push_back(material); materials.push_back(material);
} materials_[material_path] = material;
return materials;
} }
bool USDSerializer::ready() { return materials;
return ready_;
} }
void USDSerializer::writeHeader() { void USDSerializer::writeHeader() {
@@ -92,21 +113,22 @@ void USDSerializer::writeHeader() {
void USDSerializer::write(const IfcGeom::TriangulationElement* o) { void USDSerializer::write(const IfcGeom::TriangulationElement* o) {
const IfcGeom::Representation::Triangulation& mesh = o->geometry(); const IfcGeom::Representation::Triangulation& mesh = o->geometry();
auto verts = mesh.verts(); const auto verts = mesh.verts();
auto faces = mesh.faces(); const auto faces = mesh.faces();
const auto material_ids = mesh.material_ids();
const std::vector<double>& m = o->transformation().matrix().data(); const std::vector<double>& m = o->transformation().matrix().data();
if ( mesh.material_ids().empty() || verts.empty() || faces.empty() ) if (material_ids.empty() || verts.empty() || faces.empty())
return; return;
std::string name = o->name(); std::string name = o->name();
if(name.empty()) { if(name.empty()) {
name = "Unnamed_" + std::to_string(unnamed_count_); name = "UnNamed";
unnamed_count_++;
} else { } else {
name = sanitize(name) + std::to_string(o->id()); name = usd_utils::toPath(name);
} }
pxr::UsdGeomMesh usd_mesh = pxr::UsdGeomMesh::Define(stage_, pxr::SdfPath("/World/" + name)); name += "_" + std::to_string(o->id());
auto usd_mesh = pxr::UsdGeomMesh::Define(stage_, pxr::SdfPath("/World/" + name));
usd_mesh.AddTranslateOp().Set(pxr::GfVec3d(m[9], m[10], m[11])); usd_mesh.AddTranslateOp().Set(pxr::GfVec3d(m[9], m[10], m[11]));
@@ -126,14 +148,13 @@ void USDSerializer::write(const IfcGeom::TriangulationElement* o) {
normals.push_back(pxr::GfVec3f(static_cast<float>(*(it++)), static_cast<float>(*(it++)), static_cast<float>(*(it++)))); normals.push_back(pxr::GfVec3f(static_cast<float>(*(it++)), static_cast<float>(*(it++)), static_cast<float>(*(it++))));
usd_mesh.CreateNormalsAttr().Set(normals); usd_mesh.CreateNormalsAttr().Set(normals);
auto materials = createMaterials(usd_mesh, mesh.materials()); auto materials = createMaterials(mesh.materials());
pxr::UsdShadeMaterialBindingAPI material_api(usd_mesh); pxr::UsdShadeMaterialBindingAPI material_api(usd_mesh);
if(materials.size() > 1) { if(materials.size() > 1) {
auto material_ids = mesh.material_ids();
std::vector<pxr::VtArray<int>> subsets(materials.size()); std::vector<pxr::VtArray<int>> subsets(materials.size());
for(int i = 0; i < material_ids.size(); ++i) { for(int i = 0; i < material_ids.size(); ++i)
subsets[material_ids[i]].push_back(i); subsets[material_ids[i]].push_back(i);
}
for(std::size_t i = 0; i < subsets.size(); ++i){ for(std::size_t i = 0; i < subsets.size(); ++i){
auto subset = material_api.CreateMaterialBindSubset(pxr::TfToken("subset_" + std::to_string(i)), subsets[i]); auto subset = material_api.CreateMaterialBindSubset(pxr::TfToken("subset_" + std::to_string(i)), subsets[i]);
pxr::UsdShadeMaterialBindingAPI(subset).Bind(materials[i]); pxr::UsdShadeMaterialBindingAPI(subset).Bind(materials[i]);
+10 -12
View File
@@ -41,6 +41,13 @@
#include <map> #include <map>
namespace usd_utils { namespace usd_utils {
inline std::string toPath(std::string& s) {
std::replace_if(s.begin(), s.end(),
[](char c) { return c < 48 || (c < 65 && c > 57) || (c < 97 && c > 90) || c > 122; },
'_');
return s;
}
template<typename T> template<typename T>
pxr::VtArray<T> toVtArray(const std::vector<T>& vec) { pxr::VtArray<T> toVtArray(const std::vector<T>& vec) {
auto array = pxr::VtArray<T>(vec.size()); auto array = pxr::VtArray<T>(vec.size());
@@ -52,26 +59,17 @@ namespace usd_utils {
class SERIALIZERS_API USDSerializer : public WriteOnlyGeometrySerializer { class SERIALIZERS_API USDSerializer : public WriteOnlyGeometrySerializer {
private: private:
std::string filename_;
bool ready_ = false; bool ready_ = false;
const std::string filename_;
pxr::UsdStageRefPtr stage_; pxr::UsdStageRefPtr stage_;
std::size_t unnamed_count_ = 0;
std::map<std::string, pxr::UsdGeomMesh> meshes_;
std::map<std::string, pxr::UsdShadeMaterial> materials_; std::map<std::string, pxr::UsdShadeMaterial> materials_;
inline std::string sanitize(std::string s) { std::vector<pxr::UsdShadeMaterial> createMaterials(const std::vector<IfcGeom::Material>&);
std::replace_if(s.begin(), s.end(),
[](char c) { return c > 122 || c < 48 || (c > 57 && c < 65) || (c > 90 && c < 97); },
'_');
return s;
}
std::vector<pxr::UsdShadeMaterial> createMaterials(const pxr::UsdGeomMesh&, const std::vector<IfcGeom::Material>&);
void createLighting(); void createLighting();
public: public:
USDSerializer(const std::string&, const SerializerSettings&); USDSerializer(const std::string&, const SerializerSettings&);
virtual ~USDSerializer(); virtual ~USDSerializer();
bool ready(); bool ready() { return ready_; }
void writeHeader(); void writeHeader();
void write(const IfcGeom::TriangulationElement*); void write(const IfcGeom::TriangulationElement*);
void write(const IfcGeom::BRepElement*) {} void write(const IfcGeom::BRepElement*) {}