From c1fa59b9385ea872ebdeb42eefbf87da83799d3f Mon Sep 17 00:00:00 2001 From: Hugues Delorme Date: Tue, 30 Apr 2024 17:31:19 +0200 Subject: [PATCH] IO_Assimp: use OccHandle and makeOccHandle() --- src/io_assimp/io_assimp_reader.cpp | 32 +++++++++++++++--------------- src/io_assimp/io_assimp_reader.h | 15 +++++++------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/io_assimp/io_assimp_reader.cpp b/src/io_assimp/io_assimp_reader.cpp index 92a9ae82..7b61b7f8 100644 --- a/src/io_assimp/io_assimp_reader.cpp +++ b/src/io_assimp/io_assimp_reader.cpp @@ -94,7 +94,7 @@ bool deep_aiNodeTransformationHasScaling(const aiNode* node) || scaling.x < 0. || scaling.y < 0. || scaling.z < 0. ) { - //std::cout << "[TRACE] hasScaling: " << scaling.x << " " << scaling.y << " " < createOccTexture(const aiTexture* texture) { const auto textureWidth = texture->mWidth; const auto textureHeight = texture->mHeight; const auto textureSize = textureHeight == 0 ? textureWidth : 4 * textureWidth * textureHeight; - Handle(NCollection_Buffer) buff = new NCollection_Buffer( + auto buff = makeOccHandle( NCollection_BaseAllocator::CommonBaseAllocator(), textureSize ); @@ -151,14 +151,14 @@ Handle(Image_Texture) createOccTexture(const aiTexture* texture) // Create an OpenCascade Poly_Triangulation object from assimp mesh // The input 'mesh' is assumed to contain only triangles -Handle(Poly_Triangulation) createOccTriangulation(const aiMesh* mesh) +OccHandle createOccTriangulation(const aiMesh* mesh) { assert(mesh != nullptr); assert(mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE); const unsigned textureIndex = 0; const bool hasUvNodes = mesh->HasTextureCoords(textureIndex) && mesh->mNumUVComponents[textureIndex] == 2; - Handle(Poly_Triangulation) triangulation = new Poly_Triangulation(mesh->mNumVertices, mesh->mNumFaces, hasUvNodes); + auto triangulation = makeOccHandle(mesh->mNumVertices, mesh->mNumFaces, hasUvNodes); for (unsigned i = 0; i < mesh->mNumVertices; ++i) { const aiVector3D& vertex = mesh->mVertices[i]; @@ -340,7 +340,7 @@ TDF_LabelSequence AssimpReader::transfer(DocumentPtr doc, TaskProgress* progress // Add materials in target document auto materialTool = doc->xcaf().visMaterialTool(); - for (const Handle_XCAFDoc_VisMaterial& material : m_vecMaterial) { + for (const OccHandle& material : m_vecMaterial) { const TDF_Label label = materialTool->AddMaterial(material, material->RawName()->String()); m_mapMaterialLabel.insert({ material, label }); } @@ -370,7 +370,7 @@ void AssimpReader::applyProperties(const PropertyGroup* group) } } -Handle(Image_Texture) AssimpReader::findOccTexture( +OccHandle AssimpReader::findOccTexture( const std::string& strFilepath, const FilePath& modelFilepath ) { @@ -378,14 +378,14 @@ Handle(Image_Texture) AssimpReader::findOccTexture( { // Note: aiScene::GetEmbeddedTextureAndIndex() isn't available for version < 5.1 const aiTexture* texture = m_scene->GetEmbeddedTexture(strFilepath.c_str()); - Handle(Image_Texture) occTexture = Cpp::findValue(texture, m_mapEmbeddedTexture); + OccHandle occTexture = Cpp::findValue(texture, m_mapEmbeddedTexture); if (occTexture) return occTexture; } // Texture might have already been loaded from file { - Handle(Image_Texture) texture = CppUtils::findValue(strFilepath, m_mapFileTexture); + OccHandle texture = CppUtils::findValue(strFilepath, m_mapFileTexture); if (texture) return texture; } @@ -410,7 +410,7 @@ Handle(Image_Texture) AssimpReader::findOccTexture( // Could find an existing filepath for the texture if (ptrTextureFilepath) { - Handle(Image_Texture) texture = new Image_Texture(filepathTo(*ptrTextureFilepath)); + auto texture = makeOccHandle(filepathTo(*ptrTextureFilepath)); // Cache texture m_mapFileTexture.insert({ strFilepath, texture }); return texture; @@ -425,11 +425,11 @@ Handle(Image_Texture) AssimpReader::findOccTexture( return {}; } -Handle(XCAFDoc_VisMaterial) AssimpReader::createOccVisMaterial( +OccHandle AssimpReader::createOccVisMaterial( const aiMaterial* material, const FilePath& modelFilepath ) { - Handle(XCAFDoc_VisMaterial) mat = new XCAFDoc_VisMaterial; + auto mat = makeOccHandle(); //mat->SetAlphaMode(Graphic3d_AlphaMode_Opaque); @@ -448,7 +448,7 @@ Handle(XCAFDoc_VisMaterial) AssimpReader::createOccVisMaterial( aiString matName; material->Get(AI_MATKEY_NAME, matName); std::string_view vMatName{ matName.C_Str(), matName.length }; - mat->SetRawName(string_conv(vMatName)); + mat->SetRawName(string_conv>(vMatName)); } // Backface culling diff --git a/src/io_assimp/io_assimp_reader.h b/src/io_assimp/io_assimp_reader.h index 6593b7d9..41845f64 100644 --- a/src/io_assimp/io_assimp_reader.h +++ b/src/io_assimp/io_assimp_reader.h @@ -8,6 +8,7 @@ #include "../base/document_ptr.h" #include "../base/io_reader.h" +#include "../base/occ_handle.h" #include "../base/tkernel_utils.h" #include @@ -42,11 +43,11 @@ class AssimpReader : public Reader { // Create OpenCascade texture object // Parameter 'strFilepath' is the filepath to the texture as specified by the assimp material // Parameter 'modelFilepath' is the filepath to the 3D model being imported with Reader::readFile() - Handle(Image_Texture) findOccTexture(const std::string& strFilepath, const FilePath& modelFilepath); + OccHandle findOccTexture(const std::string& strFilepath, const FilePath& modelFilepath); // Create XCAFDoc_VisMaterial from assimp material // Parameter 'modelFilepath' is the filepath to the 3D model being imported with Reader::readFile() - Handle(XCAFDoc_VisMaterial) createOccVisMaterial(const aiMaterial* material, const FilePath& modelFilepath); + OccHandle createOccVisMaterial(const aiMaterial* material, const FilePath& modelFilepath); void transferSceneNode( const aiNode* node, @@ -64,12 +65,12 @@ class AssimpReader : public Reader { Assimp::Importer m_importer; const aiScene* m_scene = nullptr; - std::vector m_vecTriangulation; - std::vector m_vecMaterial; - std::unordered_map m_mapMaterialLabel; + std::vector> m_vecTriangulation; + std::vector> m_vecMaterial; + std::unordered_map, TDF_Label> m_mapMaterialLabel; std::unordered_map m_mapNodeData; - std::unordered_map m_mapEmbeddedTexture; - std::unordered_map m_mapFileTexture; + std::unordered_map> m_mapEmbeddedTexture; + std::unordered_map> m_mapFileTexture; }; } // namespace IO