diff --git a/src/serializers/USDSerializer.cpp b/src/serializers/USDSerializer.cpp index 870b15f696..4396595b61 100644 --- a/src/serializers/USDSerializer.cpp +++ b/src/serializers/USDSerializer.cpp @@ -30,6 +30,8 @@ #include "pxr/usd/usdShade/shader.h" #include "pxr/usd/usdShade/materialBindingAPI.h" +#include + USDSerializer::USDSerializer(const std::string& out_filename, const SerializerSettings& settings): WriteOnlyGeometrySerializer(settings), filename_(out_filename) @@ -102,6 +104,24 @@ std::vector USDSerializer::createMaterials(const std::vec return materials; } +pxr::GfVec3f USDSerializer::rotation_degrees_from_matrix(const std::vector& matrix) const { + const double epsilon = 1e-6; + double angleX, angleY, angleZ; + + angleX = atan2(-matrix[5], matrix[8]); + if (std::abs(std::abs(matrix[5]) - 1.0) < epsilon) { + angleZ = 0.0; + angleY = atan2(matrix[6], matrix[0]); + } else { + angleZ = atan2(matrix[1], matrix[4]); + angleY = atan2(matrix[2], matrix[8]); + } + + return pxr::GfVec3f(static_cast(angleX * 180.0 / M_PI), + static_cast(angleY * 180.0 / M_PI), + static_cast(angleZ * 180.0 / M_PI)); +} + void USDSerializer::writeHeader() { stage_->GetRootLayer()->SetComment("File generated by IfcOpenShell " + std::string(IFCOPENSHELL_VERSION)); } @@ -126,12 +146,13 @@ void USDSerializer::write(const IfcGeom::TriangulationElement* o) { 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.AddRotateXYZOp().Set(rotation_degrees_from_matrix(m)); pxr::VtVec3fArray points; for(std::size_t i = 0; i < verts.size(); i+=3) { - points.push_back(pxr::GfVec3f(static_cast(verts[i] * m[0] + verts[i+1] * m[3] + verts[i+2] * m[6]), - static_cast(verts[i] * m[1] + verts[i+1] * m[4] + verts[i+2] * m[7]), - static_cast(verts[i] * m[2] + verts[i+1] * m[5] + verts[i+2] * m[8]))); + points.push_back(pxr::GfVec3f(static_cast(verts[i]), + static_cast(verts[i+1]), + static_cast(verts[i+2]))); } usd_mesh.CreatePointsAttr().Set(points); diff --git a/src/serializers/USDSerializer.h b/src/serializers/USDSerializer.h index ce23ee2316..4f0c2a0692 100644 --- a/src/serializers/USDSerializer.h +++ b/src/serializers/USDSerializer.h @@ -66,6 +66,7 @@ private: std::map materials_; std::vector createMaterials(const std::vector&); + pxr::GfVec3f rotation_degrees_from_matrix(const std::vector&) const; public: USDSerializer(const std::string&, const SerializerSettings&); virtual ~USDSerializer();