///////////////////////////////////////////////////////////////////////////////// // // 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 "RigTriangleMeshData.h" #include #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 = readPoints( piece ); if ( vertices.empty() ) return nullptr; // Read connectivity std::vector connectivity = 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 = 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; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RifVtkSurfaceImporter::readPoints( const pugi::xml_node& piece ) { auto points = piece.child( "Points" ); if ( !points ) return {}; auto coords = points.child( "DataArray" ); if ( !coords || std::string( coords.attribute( "Name" ).value() ) != "Coordinates" ) return {}; std::vector vertices; std::istringstream iss( coords.text().get() ); double x, y, z; while ( iss >> x >> y >> z ) { vertices.emplace_back( x, y, -z ); } return vertices; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RifVtkSurfaceImporter::readConnectivity( const pugi::xml_node& piece ) { auto cells = piece.child( "Cells" ); if ( !cells ) return {}; std::vector connectivity; auto connectivityArray = cells.child( "DataArray" ); while ( connectivityArray ) { if ( std::string( connectivityArray.attribute( "Name" ).value() ) == "connectivity" ) { std::string connectivityText = connectivityArray.text().get(); std::istringstream iss( connectivityText ); unsigned index; while ( iss >> index ) { connectivity.push_back( index ); } break; } connectivityArray = connectivityArray.next_sibling( "DataArray" ); } return connectivity; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::map> RifVtkSurfaceImporter::readProperties( const pugi::xml_node& piece ) { auto cellData = piece.child( "CellData" ); if ( !cellData ) return {}; std::map> properties; for ( const auto& dataArray : cellData.children( "DataArray" ) ) { const char* name = dataArray.attribute( "Name" ).value(); if ( name && *name ) { std::vector values; std::istringstream iss( dataArray.text().get() ); float value; while ( iss >> value ) { values.push_back( value ); } if ( !values.empty() ) { properties[name] = std::move( values ); } } } return properties; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RifVtkSurfaceImporter::parsePvdDatasets( const std::filesystem::path& filepath ) { pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file( filepath.string().c_str() ); if ( !result ) return {}; auto root = doc.child( "VTKFile" ); if ( !root ) return {}; auto collection = root.child( "Collection" ); if ( !collection ) return {}; std::filesystem::path baseDir = filepath.parent_path(); std::vector datasets; for ( const auto& datasetElem : collection.children( "DataSet" ) ) { const char* file = datasetElem.attribute( "file" ).value(); const char* timestepStr = datasetElem.attribute( "timestep" ).value(); if ( file && timestepStr ) { double timestep = std::stod( timestepStr ); std::filesystem::path fullPath = std::filesystem::absolute( baseDir / file ); datasets.push_back( { timestep, fullPath } ); } } return datasets; }