///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2025- Equinor ASA // // ResInsight is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // ResInsight 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 GNU General Public License at // for more details. // ///////////////////////////////////////////////////////////////////////////////// #include "RifVtkSurfaceImporter.h" #include "RifVtkImportUtil.h" #include "Surface/RigTriangleMeshData.h" #include #include #include #include //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::unique_ptr RifVtkSurfaceImporter::importFromFile( const std::filesystem::path& filepath ) { pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file( filepath.string().c_str() ); if ( !result ) return nullptr; return importFromXmlDoc( doc ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::unique_ptr RifVtkSurfaceImporter::importFromXmlDoc( const pugi::xml_document& doc ) { auto root = doc.child( "VTKFile" ); if ( !root ) return nullptr; auto grid = root.child( "UnstructuredGrid" ); if ( !grid ) return nullptr; auto piece = grid.child( "Piece" ); if ( !piece ) return nullptr; // Read points std::vector vertices = RifVtkImportUtil::readPoints( piece ); if ( vertices.empty() ) return nullptr; // Read connectivity std::vector connectivity = RifVtkImportUtil::readConnectivity( piece ); if ( connectivity.empty() ) return nullptr; // Avoid shared nodes std::vector nonSharedVertices; std::vector nonSharedConnectivity; const auto numTriangles = connectivity.size() / 3; for ( size_t triangleIdx = 0; triangleIdx < numTriangles; triangleIdx++ ) { nonSharedVertices.push_back( vertices[connectivity[3 * triangleIdx + 0]] ); nonSharedVertices.push_back( vertices[connectivity[3 * triangleIdx + 1]] ); nonSharedVertices.push_back( vertices[connectivity[3 * triangleIdx + 2]] ); nonSharedConnectivity.push_back( static_cast( 3 * triangleIdx + 0 ) ); nonSharedConnectivity.push_back( static_cast( 3 * triangleIdx + 1 ) ); nonSharedConnectivity.push_back( static_cast( 3 * triangleIdx + 2 ) ); } // Set geometry data auto triangleMeshData = std::make_unique(); triangleMeshData->setGeometryData( nonSharedVertices, nonSharedConnectivity ); // Read properties const std::map> properties = RifVtkImportUtil::readProperties( piece ); for ( const auto& [name, values] : properties ) { // These values are per element, so we need to duplicate them for each node if ( values.size() * 3 == nonSharedVertices.size() ) { std::vector valuesForEachNode; for ( auto value : values ) { valuesForEachNode.push_back( value ); valuesForEachNode.push_back( value ); valuesForEachNode.push_back( value ); } triangleMeshData->addPropertyData( QString::fromStdString( name ), valuesForEachNode ); } } return triangleMeshData; }