Adapt USD serializer for v0.8 and define nominmax for windows.h compatibility

This commit is contained in:
Thomas Krijnen
2024-06-10 20:10:53 +02:00
parent 83a929604c
commit f2074d95fd
3 changed files with 33 additions and 20 deletions
+5 -1
View File
@@ -26,6 +26,10 @@
* * * *
********************************************************************************/ ********************************************************************************/
// windows stuff: defines max as a macro when including windows.h
// error C2589: '(': illegal token on right side of '::'
#define NOMINMAX
#include "../serializers/ColladaSerializer.h" #include "../serializers/ColladaSerializer.h"
#include "../serializers/GltfSerializer.h" #include "../serializers/GltfSerializer.h"
#include "../serializers/HdfSerializer.h" #include "../serializers/HdfSerializer.h"
@@ -841,7 +845,7 @@ int main(int argc, char** argv) {
#endif #endif
#ifdef WITH_USD #ifdef WITH_USD
} else if (output_extension == USD || output_extension == USDA || output_extension == USDC) { } else if (output_extension == USD || output_extension == USDA || output_extension == USDC) {
serializer = boost::make_shared<USDSerializer>(IfcUtil::path::to_utf8(output_filename), settings); serializer = boost::make_shared<USDSerializer>(IfcUtil::path::to_utf8(output_filename), geometry_settings, serializer_settings);
#endif #endif
#ifdef IFOPSH_WITH_OPENCASCADE #ifdef IFOPSH_WITH_OPENCASCADE
} else if (output_extension == STP) { } else if (output_extension == STP) {
+21 -15
View File
@@ -19,6 +19,10 @@
#ifdef WITH_USD #ifdef WITH_USD
// windows stuff: defines max as a macro when including windows.h
// error C2589: '(': illegal token on right side of '::'
#define NOMINMAX
#include "USDSerializer.h" #include "USDSerializer.h"
#include "pxr/base/gf/vec3f.h" #include "pxr/base/gf/vec3f.h"
@@ -32,8 +36,8 @@
#include <math.h> #include <math.h>
USDSerializer::USDSerializer(const std::string& out_filename, const SerializerSettings& settings): USDSerializer::USDSerializer(const std::string& out_filename, const ifcopenshell::geometry::Settings& geometry_settings, const ifcopenshell::geometry::SerializerSettings& settings):
WriteOnlyGeometrySerializer(settings), WriteOnlyGeometrySerializer(geometry_settings, settings),
filename_(out_filename) filename_(out_filename)
{ {
std::size_t found = filename_.find_last_of("/\\"); std::size_t found = filename_.find_last_of("/\\");
@@ -43,8 +47,9 @@ USDSerializer::USDSerializer(const std::string& out_filename, const SerializerSe
if(!stage_) if(!stage_)
throw std::runtime_error("Could not create USD stage"); throw std::runtime_error("Could not create USD stage");
if(!settings.get(SerializerSettings::USE_Y_UP)) if (!settings.get<ifcopenshell::geometry::settings::UseYUp>().get()) {
pxr::UsdGeomSetStageUpAxis(stage_, pxr::UsdGeomTokens->z); pxr::UsdGeomSetStageUpAxis(stage_, pxr::UsdGeomTokens->z);
}
pxr::UsdGeomSetStageMetersPerUnit(stage_, 1.0f); pxr::UsdGeomSetStageMetersPerUnit(stage_, 1.0f);
@@ -61,15 +66,16 @@ USDSerializer::~USDSerializer() {
} }
std::vector<pxr::UsdShadeMaterial> USDSerializer::createMaterials(const std::vector<IfcGeom::Material>& styles) std::vector<pxr::UsdShadeMaterial> USDSerializer::createMaterials(const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>& styles)
{ {
if(styles.empty()) if(styles.empty())
throw std::runtime_error("No styles to create materials from"); 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 styleptr : styles) {
std::string material_path(style.original_name()); auto& style = *styleptr;
std::string material_path(style.name);
usd_utils::toPath(material_path); usd_utils::toPath(material_path);
if(materials_.find(material_path) != materials_.end()) { if(materials_.find(material_path) != materials_.end()) {
@@ -83,18 +89,18 @@ std::vector<pxr::UsdShadeMaterial> USDSerializer::createMaterials(const std::vec
shader.CreateIdAttr().Set(pxr::TfToken("UsdPreviewSurface")); shader.CreateIdAttr().Set(pxr::TfToken("UsdPreviewSurface"));
float rgba[4] { 0.18f, 0.18f, 0.18f, 1.0f }; float rgba[4] { 0.18f, 0.18f, 0.18f, 1.0f };
if (style.hasDiffuse()) if (style.diffuse)
for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
rgba[i] = static_cast<float>(style.diffuse()[i]); rgba[i] = static_cast<float>(style.diffuse.ccomponents()(i));
shader.CreateInput(pxr::TfToken("diffuseColor"), pxr::SdfValueTypeNames->Color3f).Set(pxr::GfVec3f(rgba[0], rgba[1], rgba[2])); shader.CreateInput(pxr::TfToken("diffuseColor"), pxr::SdfValueTypeNames->Color3f).Set(pxr::GfVec3f(rgba[0], rgba[1], rgba[2]));
if(style.hasTransparency()) if (style.has_transparency())
rgba[3] -= style.transparency(); rgba[3] -= style.transparency;
shader.CreateInput(pxr::TfToken("opacity"), pxr::SdfValueTypeNames->Float).Set(rgba[3]); shader.CreateInput(pxr::TfToken("opacity"), pxr::SdfValueTypeNames->Float).Set(rgba[3]);
if(style.hasSpecular()) { if(style.specular) {
for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
rgba[i] = static_cast<float>(style.specular()[i]); rgba[i] = static_cast<float>(style.specular.ccomponents()(i));
shader.CreateInput(pxr::TfToken("useSpecularWorkflow"), pxr::SdfValueTypeNames->Int).Set(1); shader.CreateInput(pxr::TfToken("useSpecularWorkflow"), pxr::SdfValueTypeNames->Int).Set(1);
} else { } else {
shader.CreateInput(pxr::TfToken("useSpecularWorkflow"), pxr::SdfValueTypeNames->Int).Set(0); shader.CreateInput(pxr::TfToken("useSpecularWorkflow"), pxr::SdfValueTypeNames->Int).Set(0);
@@ -109,7 +115,7 @@ std::vector<pxr::UsdShadeMaterial> USDSerializer::createMaterials(const std::vec
return materials; return materials;
} }
pxr::GfVec3f USDSerializer::rotation_degrees_from_matrix(const std::vector<double>& matrix) const { pxr::GfVec3f USDSerializer::rotation_degrees_from_matrix(const double* matrix) const {
const double epsilon = 1e-6; const double epsilon = 1e-6;
double angleX, angleY, angleZ; double angleX, angleY, angleZ;
@@ -137,7 +143,7 @@ void USDSerializer::write(const IfcGeom::TriangulationElement* o) {
const auto verts = mesh.verts(); const auto verts = mesh.verts();
const auto faces = mesh.faces(); const auto faces = mesh.faces();
const auto material_ids = mesh.material_ids(); const auto material_ids = mesh.material_ids();
const std::vector<double>& m = o->transformation().matrix().data(); auto m = o->transformation().data()->ccomponents().data();
if (material_ids.empty() || verts.empty() || faces.empty()) if (material_ids.empty() || verts.empty() || faces.empty())
return; return;
+7 -4
View File
@@ -22,8 +22,11 @@
#ifdef WITH_USD #ifdef WITH_USD
#define __TBB_NO_IMPLICIT_LINKAGE 1
#define __TBB_LIB_NAME bier
#include "../serializers/serializers_api.h" #include "../serializers/serializers_api.h"
#include "../ifcgeom_schema_agnostic/GeometrySerializer.h" #include "../ifcgeom/GeometrySerializer.h"
#include "../ifcparse/utils.h" #include "../ifcparse/utils.h"
// undefine opencascade Handle macro, because it conflicts with USD // undefine opencascade Handle macro, because it conflicts with USD
@@ -67,10 +70,10 @@ private:
std::map<std::string, pxr::UsdShadeMaterial> materials_; std::map<std::string, pxr::UsdShadeMaterial> materials_;
std::map<std::string, std::string> meshes_; std::map<std::string, std::string> meshes_;
std::vector<pxr::UsdShadeMaterial> createMaterials(const std::vector<IfcGeom::Material>&); std::vector<pxr::UsdShadeMaterial> createMaterials(const std::vector<ifcopenshell::geometry::taxonomy::style::ptr>&);
pxr::GfVec3f rotation_degrees_from_matrix(const std::vector<double>&) const; pxr::GfVec3f rotation_degrees_from_matrix(const double*) const;
public: public:
USDSerializer(const std::string&, const SerializerSettings&); USDSerializer(const std::string&, const ifcopenshell::geometry::Settings&, const ifcopenshell::geometry::SerializerSettings&);
virtual ~USDSerializer(); virtual ~USDSerializer();
bool ready() { return ready_; } bool ready() { return ready_; }
void writeHeader(); void writeHeader();