From 3081a02b90ee80f85fc58d21194ae53f3e6959c8 Mon Sep 17 00:00:00 2001 From: Kristian Bendiksen Date: Tue, 22 Apr 2025 10:19:30 +0200 Subject: [PATCH] #12367 VTK surface import: handle NaN in vtu file. --- .../FileInterface/RifVtkSurfaceImporter.cpp | 27 ++++++++++++++++--- .../UnitTests/RifVtkSurfaceImporter-Test.cpp | 7 +++++ .../RifVtkSurfaceImporter/fracture-00000.vtu | 2 +- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ApplicationLibCode/FileInterface/RifVtkSurfaceImporter.cpp b/ApplicationLibCode/FileInterface/RifVtkSurfaceImporter.cpp index 3364a3ce1a..09fdc0507d 100644 --- a/ApplicationLibCode/FileInterface/RifVtkSurfaceImporter.cpp +++ b/ApplicationLibCode/FileInterface/RifVtkSurfaceImporter.cpp @@ -174,10 +174,31 @@ std::map> RifVtkSurfaceImporter::readProperties( std::vector values; std::istringstream iss( dataArray.text().get() ); - float value; - while ( iss >> value ) + std::string token; + while ( iss >> token ) { - values.push_back( value ); + if ( token == "nan" || token == "NaN" || token == "NAN" ) + { + values.push_back( std::numeric_limits::quiet_NaN() ); + } + else + { + try + { + float value = std::stof( token ); + values.push_back( value ); + } + catch ( const std::invalid_argument& ) + { + // Quit parsing when encountering non-numeric tokens + return {}; + } + catch ( const std::out_of_range& ) + { + // Quit parsing when seeing out-of-range errors + return {}; + } + } } if ( !values.empty() ) diff --git a/ApplicationLibCode/UnitTests/RifVtkSurfaceImporter-Test.cpp b/ApplicationLibCode/UnitTests/RifVtkSurfaceImporter-Test.cpp index 8c5ddd39a1..d79e35dcde 100644 --- a/ApplicationLibCode/UnitTests/RifVtkSurfaceImporter-Test.cpp +++ b/ApplicationLibCode/UnitTests/RifVtkSurfaceImporter-Test.cpp @@ -42,6 +42,13 @@ TEST( RifVtkSurfaceImporterTest, ImportFromFile ) [expectedProperty]( const QString& name ) { return name == expectedProperty; } ) ); EXPECT_EQ( triangleMeshData->propertyValues( expectedProperty ).size(), expectedVertexCount ); } + + // Check that we handled NaN values + auto values = triangleMeshData->propertyValues( "ReservoirPerm" ); + for ( size_t i = 24; i < 30; i++ ) + { + ASSERT_TRUE( std::isnan( triangleMeshData->propertyValues( "ReservoirPerm" )[i] ) ); + } } // Test parsing a PVD file diff --git a/ApplicationLibCode/UnitTests/TestData/RifVtkSurfaceImporter/fracture-00000.vtu b/ApplicationLibCode/UnitTests/TestData/RifVtkSurfaceImporter/fracture-00000.vtu index a0258a52d6..a800a2b263 100644 --- a/ApplicationLibCode/UnitTests/TestData/RifVtkSurfaceImporter/fracture-00000.vtu +++ b/ApplicationLibCode/UnitTests/TestData/RifVtkSurfaceImporter/fracture-00000.vtu @@ -8,7 +8,7 @@ 83769 83769 77369 77369 83769 83769 83769 83769 83769 83769 83769 70169 - 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 + 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-2 1.2e-13 1.2e-13 nan nan 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13 1.2e-13