Files

192 lines
6.6 KiB
C++
Raw Permalink Normal View History

2020-05-03 11:29:33 +02:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2020- 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.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimFileSurface.h"
#include "RiaPreferences.h"
#include "RimSurfaceCollection.h"
2020-08-24 14:12:51 +02:00
#include "RifSurfaceImporter.h"
#include "RifVtkSurfaceImporter.h"
2025-05-21 18:09:22 +02:00
#include "Surface/RigSurface.h"
#include "Surface/RigTriangleMeshData.h"
2020-05-03 11:29:33 +02:00
2020-08-26 12:31:23 +02:00
#include "cafPdmFieldScriptingCapability.h"
#include "cafPdmObjectScriptingCapability.h"
2020-05-03 11:29:33 +02:00
#include <QFileInfo>
2024-09-02 11:35:00 +00:00
#include <memory>
2020-05-03 11:29:33 +02:00
CAF_PDM_SOURCE_INIT( RimFileSurface, "Surface", "FileSurface" );
2020-05-03 11:29:33 +02:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimFileSurface::RimFileSurface()
{
CAF_PDM_InitScriptableObject( "Surface", ":/ReservoirSurface16x16.png" );
2020-05-03 11:29:33 +02:00
CAF_PDM_InitFieldNoDefault( &m_surfaceDefinitionFilePath, "SurfaceFilePath", "File" );
2020-05-03 11:29:33 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimFileSurface::~RimFileSurface()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFileSurface::setFilePath( const QString& filePath )
2020-05-03 11:29:33 +02:00
{
m_surfaceDefinitionFilePath = filePath;
clearCachedNativeData();
2020-05-03 11:29:33 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimFileSurface::filePath() const
2020-05-03 11:29:33 +02:00
{
return m_surfaceDefinitionFilePath().path();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimFileSurface::onLoadData()
2020-05-03 11:29:33 +02:00
{
return updateSurfaceData();
2020-05-03 11:29:33 +02:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurface* RimFileSurface::createCopy()
{
2024-06-14 17:18:28 +02:00
auto newSurface = copyObject<RimFileSurface>();
if ( !newSurface->onLoadData() )
{
delete newSurface;
return nullptr;
}
return newSurface;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-02-26 10:48:40 +01:00
void RimFileSurface::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue )
2020-05-03 11:29:33 +02:00
{
RimSurface::fieldChangedByUi( changedField, oldValue, newValue );
if ( changedField == &m_surfaceDefinitionFilePath )
{
clearCachedNativeData();
updateSurfaceData();
2020-05-03 11:29:33 +02:00
auto surfColl = firstAncestorOrThisOfTypeAsserted<RimSurfaceCollection>();
2020-11-06 09:46:38 +00:00
surfColl->updateViews( { this } );
2020-05-03 11:29:33 +02:00
}
}
//--------------------------------------------------------------------------------------------------
/// Regenerate the surface geometry, using the offset specified.
/// If the surface data hasn't been loaded from file yet, load it.
2020-05-03 11:29:33 +02:00
/// Returns false for fatal failure
//--------------------------------------------------------------------------------------------------
bool RimFileSurface::updateSurfaceData()
2020-05-03 11:29:33 +02:00
{
bool result = true;
2025-05-28 14:19:57 +02:00
if ( !m_triangleMeshData || m_triangleMeshData->geometry().first.empty() )
{
result = loadDataFromFile();
}
2020-05-03 11:29:33 +02:00
2025-05-28 14:19:57 +02:00
if ( !m_triangleMeshData ) return false;
auto [vertices, tringleIndices] = m_triangleMeshData->geometry();
auto surface = new RigSurface;
if ( !vertices.empty() && !tringleIndices.empty() )
{
RimSurface::applyDepthOffsetIfNeeded( &vertices );
surface->setTriangleData( tringleIndices, vertices );
}
if ( m_triangleMeshData )
{
auto propertyNames = m_triangleMeshData->propertyNames();
for ( const auto& name : propertyNames )
{
auto values = m_triangleMeshData->propertyValues( name );
2025-03-17 09:58:34 +01:00
surface->addVertexResult( name, values );
}
}
setSurfaceData( surface );
return result;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFileSurface::clearCachedNativeData()
{
m_triangleMeshData.reset();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimFileSurface::loadDataFromFile()
{
m_triangleMeshData = std::make_unique<RigTriangleMeshData>();
2020-05-03 11:29:33 +02:00
QString path = filePath();
if ( path.endsWith( "ptl", Qt::CaseInsensitive ) )
2020-05-03 11:29:33 +02:00
{
auto surface = RifSurfaceImporter::readPetrelFile( path );
m_triangleMeshData->setGeometryData( surface.first, surface.second );
2020-05-03 11:29:33 +02:00
}
else if ( path.endsWith( "ts", Qt::CaseInsensitive ) )
2020-05-03 11:29:33 +02:00
{
RifSurfaceImporter::readGocadFile( path, m_triangleMeshData.get() );
2020-05-03 11:29:33 +02:00
}
else if ( path.endsWith( "vtu", Qt::CaseInsensitive ) )
{
m_triangleMeshData = RifVtkSurfaceImporter::importFromFile( path.toStdString() );
}
else if ( path.endsWith( "dat", Qt::CaseInsensitive ) || path.endsWith( "xyz", Qt::CaseInsensitive ) )
{
double resamplingDistance = RiaPreferences::current()->surfaceImportResamplingDistance();
auto surface = RifSurfaceImporter::readOpenWorksXyzFile( path, resamplingDistance );
m_triangleMeshData->setGeometryData( surface.first, surface.second );
}
2020-05-03 11:29:33 +02:00
return !( m_triangleMeshData->geometry().first.empty() );
2020-05-03 11:29:33 +02:00
}