mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Add surface import from VTU files
Parse VTU files. If element results are detected, duplicate nodes and assign result values to nodes for display.
This commit is contained in:
@@ -45,7 +45,7 @@ void RicImportSurfacesFeature::onActionTriggered( bool isChecked )
|
||||
QStringList fileNames = RiuFileDialogTools::getOpenFileNames( Riu3DMainWindowTools::mainWindowWidget(),
|
||||
"Import Surfaces",
|
||||
defaultDir,
|
||||
"Surface files (*.ptl *.ts *.dat *.xyz);;All Files (*.*)" );
|
||||
"Surface files (*.ptl *.ts *.dat *.vtu *.xyz);;All Files (*.*)" );
|
||||
|
||||
if ( fileNames.isEmpty() ) return;
|
||||
|
||||
|
@@ -103,6 +103,7 @@ set(SOURCE_GROUP_HEADER_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifByteArrayArrowRandomAccessFile.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifArrowTools.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifReaderRegularGridModel.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifVtkSurfaceImporter.h
|
||||
)
|
||||
|
||||
set(SOURCE_GROUP_SOURCE_FILES
|
||||
@@ -203,6 +204,7 @@ set(SOURCE_GROUP_SOURCE_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifByteArrayArrowRandomAccessFile.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifArrowTools.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifReaderRegularGridModel.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifVtkSurfaceImporter.cpp
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
|
||||
|
@@ -207,7 +207,7 @@ void RifSurfaceImporter::readGocadFile( const QString& filename, RigGocadData* g
|
||||
if ( gocadData )
|
||||
{
|
||||
gocadData->setGeometryData( vertices, triangleIndices );
|
||||
gocadData->addPropertyData( propertyNames, propertyValues );
|
||||
gocadData->setPropertyData( propertyNames, propertyValues );
|
||||
}
|
||||
}
|
||||
|
||||
|
196
ApplicationLibCode/FileInterface/RifVtkSurfaceImporter.cpp
Normal file
196
ApplicationLibCode/FileInterface/RifVtkSurfaceImporter.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "RifVtkSurfaceImporter.h"
|
||||
|
||||
#include "../../Fwk/VizFwk/LibIo/cvfTinyXmlFused.hpp"
|
||||
#include "RigGocadData.h"
|
||||
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace RifVtkSurfaceImporter
|
||||
{
|
||||
using namespace cvf_tinyXML;
|
||||
|
||||
bool importFromFile( std::string filename, RigGocadData* gocadData )
|
||||
{
|
||||
TiXmlDocument doc;
|
||||
if ( !doc.LoadFile( filename.c_str() ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return importFromXMLDoc( doc, gocadData );
|
||||
}
|
||||
|
||||
bool importFromXMLDoc( const TiXmlDocument& doc, RigGocadData* gocadData )
|
||||
{
|
||||
auto* root = doc.FirstChildElement( "VTKFile" );
|
||||
if ( !root ) return false;
|
||||
|
||||
auto* grid = root->FirstChildElement( "UnstructuredGrid" );
|
||||
if ( !grid ) return false;
|
||||
|
||||
auto* piece = grid->FirstChildElement( "Piece" );
|
||||
if ( !piece ) return false;
|
||||
|
||||
// Read points
|
||||
std::vector<cvf::Vec3d> vertices;
|
||||
if ( !readPoints( piece, vertices ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read connectivity
|
||||
std::vector<unsigned> connectivity;
|
||||
if ( !readConnectivity( piece, connectivity ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Avoid shared nodes
|
||||
std::vector<cvf::Vec3d> nonSharedVertices;
|
||||
std::vector<unsigned> 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<unsigned int>( 3 * triangleIdx + 0 ) );
|
||||
nonSharedConnectivity.push_back( static_cast<unsigned int>( 3 * triangleIdx + 1 ) );
|
||||
nonSharedConnectivity.push_back( static_cast<unsigned int>( 3 * triangleIdx + 2 ) );
|
||||
}
|
||||
|
||||
// Set geometry data
|
||||
gocadData->setGeometryData( nonSharedVertices, nonSharedConnectivity );
|
||||
|
||||
// Read properties
|
||||
std::vector<std::string> propertyNamesOnFile;
|
||||
std::vector<std::vector<float>> propertyValuesOnFile;
|
||||
|
||||
readProperties( piece, propertyNamesOnFile, propertyValuesOnFile );
|
||||
|
||||
if ( propertyNamesOnFile.size() == propertyValuesOnFile.size() )
|
||||
{
|
||||
for ( size_t i = 0; i < propertyValuesOnFile.size(); i++ )
|
||||
{
|
||||
// These values are per element, so we need to duplicate them for each node
|
||||
auto values = propertyValuesOnFile[i];
|
||||
if ( values.size() * 3 == nonSharedVertices.size() )
|
||||
{
|
||||
std::vector<float> valuesForEachNode;
|
||||
for ( auto value : values )
|
||||
{
|
||||
valuesForEachNode.push_back( value );
|
||||
valuesForEachNode.push_back( value );
|
||||
valuesForEachNode.push_back( value );
|
||||
}
|
||||
|
||||
gocadData->addPropertyData( QString::fromStdString( propertyNamesOnFile[i] ), valuesForEachNode );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool readPoints( const TiXmlElement* piece, std::vector<cvf::Vec3d>& vertices )
|
||||
{
|
||||
auto* points = piece->FirstChildElement( "Points" );
|
||||
if ( !points ) return false;
|
||||
|
||||
auto* coords = points->FirstChildElement( "DataArray" );
|
||||
if ( !coords || strcmp( coords->Attribute( "Name" ), "Coordinates" ) != 0 ) return false;
|
||||
|
||||
std::string coordsText = coords->GetText();
|
||||
std::istringstream iss( coordsText );
|
||||
|
||||
double x, y, z;
|
||||
while ( iss >> x >> y >> z )
|
||||
{
|
||||
vertices.push_back( cvf::Vec3d( x, y, -z ) );
|
||||
}
|
||||
|
||||
return !vertices.empty();
|
||||
}
|
||||
|
||||
bool readConnectivity( const TiXmlElement* piece, std::vector<unsigned>& connectivity )
|
||||
{
|
||||
auto* cells = piece->FirstChildElement( "Cells" );
|
||||
if ( !cells ) return false;
|
||||
|
||||
auto* connectivityArray = cells->FirstChildElement( "DataArray" );
|
||||
while ( connectivityArray )
|
||||
{
|
||||
if ( strcmp( connectivityArray->Attribute( "Name" ), "connectivity" ) == 0 )
|
||||
{
|
||||
std::string connectivityText = connectivityArray->GetText();
|
||||
std::istringstream iss( connectivityText );
|
||||
|
||||
unsigned index;
|
||||
while ( iss >> index )
|
||||
{
|
||||
connectivity.push_back( index );
|
||||
}
|
||||
break;
|
||||
}
|
||||
connectivityArray = connectivityArray->NextSiblingElement( "DataArray" );
|
||||
}
|
||||
|
||||
return !connectivity.empty();
|
||||
}
|
||||
|
||||
void readProperties( const TiXmlElement* piece, std::vector<std::string>& propertyNames, std::vector<std::vector<float>>& propertyValues )
|
||||
{
|
||||
auto* cellData = piece->FirstChildElement( "CellData" );
|
||||
if ( !cellData ) return;
|
||||
|
||||
auto* dataArray = cellData->FirstChildElement( "DataArray" );
|
||||
while ( dataArray )
|
||||
{
|
||||
const char* name = dataArray->Attribute( "Name" );
|
||||
if ( name )
|
||||
{
|
||||
std::vector<float> values;
|
||||
std::string valuesText = dataArray->GetText();
|
||||
std::istringstream iss( valuesText );
|
||||
|
||||
float value;
|
||||
while ( iss >> value )
|
||||
{
|
||||
values.push_back( value );
|
||||
}
|
||||
|
||||
if ( !values.empty() )
|
||||
{
|
||||
propertyNames.push_back( name );
|
||||
propertyValues.push_back( values );
|
||||
}
|
||||
}
|
||||
dataArray = dataArray->NextSiblingElement( "DataArray" );
|
||||
}
|
||||
}
|
||||
}; // namespace RifVtkSurfaceImporter
|
49
ApplicationLibCode/FileInterface/RifVtkSurfaceImporter.h
Normal file
49
ApplicationLibCode/FileInterface/RifVtkSurfaceImporter.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
// for more details.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "cvfVector3.h"
|
||||
|
||||
class RigGocadData;
|
||||
|
||||
namespace cvf_tinyXML
|
||||
{
|
||||
class TiXmlDocument;
|
||||
class TiXmlElement;
|
||||
} // namespace cvf_tinyXML
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
namespace RifVtkSurfaceImporter
|
||||
{
|
||||
bool importFromFile( std::string filename, RigGocadData* gocadData );
|
||||
|
||||
bool importFromXMLDoc( const cvf_tinyXML::TiXmlDocument& doc, RigGocadData* gocadData );
|
||||
bool readPoints( const cvf_tinyXML::TiXmlElement* piece, std::vector<cvf::Vec3d>& vertices );
|
||||
bool readConnectivity( const cvf_tinyXML::TiXmlElement* piece, std::vector<unsigned>& connectivity );
|
||||
void readProperties( const cvf_tinyXML::TiXmlElement* piece,
|
||||
std::vector<std::string>& propertyNames,
|
||||
std::vector<std::vector<float>>& propertyValues );
|
||||
|
||||
}; // namespace RifVtkSurfaceImporter
|
@@ -20,11 +20,13 @@
|
||||
|
||||
#include "RiaPreferences.h"
|
||||
|
||||
#include "RimSurfaceCollection.h"
|
||||
|
||||
#include "RifSurfaceImporter.h"
|
||||
#include "RifVtkSurfaceImporter.h"
|
||||
|
||||
#include "RigGocadData.h"
|
||||
#include "RigSurface.h"
|
||||
#include "RimSurfaceCollection.h"
|
||||
|
||||
#include "cafPdmFieldScriptingCapability.h"
|
||||
#include "cafPdmObjectScriptingCapability.h"
|
||||
@@ -177,6 +179,13 @@ bool RimFileSurface::loadDataFromFile()
|
||||
|
||||
surface = m_gocadData->gocadGeometry();
|
||||
}
|
||||
else if ( filePath.endsWith( "vtu", Qt::CaseInsensitive ) )
|
||||
{
|
||||
m_gocadData = std::make_unique<RigGocadData>();
|
||||
RifVtkSurfaceImporter::importFromFile( filePath.toStdString(), m_gocadData.get() );
|
||||
|
||||
surface = m_gocadData->gocadGeometry();
|
||||
}
|
||||
else if ( filePath.endsWith( "dat", Qt::CaseInsensitive ) || filePath.endsWith( "xyz", Qt::CaseInsensitive ) )
|
||||
{
|
||||
double resamplingDistance = RiaPreferences::current()->surfaceImportResamplingDistance();
|
||||
|
@@ -76,8 +76,17 @@ void RigGocadData::setGeometryData( const std::vector<cvf::Vec3d>& nodeCoord, co
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RigGocadData::addPropertyData( const std::vector<QString>& propertyNames, std::vector<std::vector<float>>& propertyValues )
|
||||
void RigGocadData::setPropertyData( const std::vector<QString>& propertyNames, std::vector<std::vector<float>>& propertyValues )
|
||||
{
|
||||
m_propertyNames = propertyNames;
|
||||
m_propertyValues = propertyValues;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RigGocadData::addPropertyData( const QString& propertyName, std::vector<float>& propertyValues )
|
||||
{
|
||||
m_propertyNames.push_back( propertyName );
|
||||
m_propertyValues.push_back( propertyValues );
|
||||
}
|
||||
|
@@ -34,7 +34,8 @@ public:
|
||||
std::vector<float> propertyValues( const QString& property );
|
||||
|
||||
void setGeometryData( const std::vector<cvf::Vec3d>& nodeCoord, const std::vector<unsigned>& connectivities );
|
||||
void addPropertyData( const std::vector<QString>& propertyNames, std::vector<std::vector<float>>& propertyValues );
|
||||
void setPropertyData( const std::vector<QString>& propertyNames, std::vector<std::vector<float>>& propertyValues );
|
||||
void addPropertyData( const QString& propertyName, std::vector<float>& propertyValues );
|
||||
|
||||
private:
|
||||
std::vector<cvf::Vec3d> m_vertices;
|
||||
|
Reference in New Issue
Block a user