Files
ResInsight/ApplicationLibCode/ProjectDataModel/Surfaces/RimSurfaceInViewCollection.cpp
T

642 lines
23 KiB
C++
Raw Normal View History

/////////////////////////////////////////////////////////////////////////////////
//
// 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 "RimSurfaceInViewCollection.h"
#include "Surface/RigSurface.h"
2023-08-07 16:35:59 +02:00
#include "Rim3dView.h"
2021-09-13 10:16:09 +02:00
#include "RimEnsembleSurface.h"
2020-01-08 11:56:48 +01:00
#include "RimGridView.h"
#include "RimIntersectionResultDefinition.h"
#include "RimIntersectionResultsDefinitionCollection.h"
#include "RimOilField.h"
#include "RimProject.h"
#include "RimSurface.h"
#include "RimSurfaceCollection.h"
#include "RimSurfaceInView.h"
#include "RimSurfaceResultDefinition.h"
2020-01-08 11:56:48 +01:00
#include "RivSurfacePartMgr.h"
#include "cafPdmUiTreeOrdering.h"
2020-01-08 11:56:48 +01:00
#include "cvfModelBasicList.h"
#include <algorithm>
CAF_PDM_SOURCE_INIT( RimSurfaceInViewCollection, "SurfaceInViewCollection" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurfaceInViewCollection::RimSurfaceInViewCollection()
{
CAF_PDM_InitObject( "Surfaces", ":/ReservoirSurfaces16x16.png" );
CAF_PDM_InitFieldNoDefault( &m_collectionName, "CollectionName", "Name" );
2020-08-26 12:31:23 +02:00
m_collectionName.registerGetMethod( this, &RimSurfaceInViewCollection::name );
m_collectionName.uiCapability()->setUiReadOnly( true );
CAF_PDM_InitFieldNoDefault( &m_collectionsInView, "SurfacesInViewFieldCollections", "SurfacesInViewFieldCollections" );
2023-08-07 16:35:59 +02:00
CAF_PDM_InitFieldNoDefault( &m_surfacesInView, "SurfacesInViewField", "Surfaces" );
CAF_PDM_InitFieldNoDefault( &m_surfaceCollection, "SurfaceCollectionRef", "SurfaceCollection" );
2020-08-26 12:31:23 +02:00
m_surfaceCollection.uiCapability()->setUiHidden( true );
CAF_PDM_InitField( &m_surfaceColorMode, "SurfaceColorMode", caf::AppEnum<SurfaceColorMode>( SurfaceColorMode::BOTH ), "Color Mode" );
caf::AppEnum<SurfaceColorMode>::setEnumSubset( &m_surfaceColorMode,
{ SurfaceColorMode::BOTH, SurfaceColorMode::SURFACE_COLOR, SurfaceColorMode::RESULT_COLORS } );
nameField()->uiCapability()->setUiHidden( true );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurfaceInViewCollection::~RimSurfaceInViewCollection()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmFieldHandle* RimSurfaceInViewCollection::userDescriptionField()
{
2020-08-26 12:31:23 +02:00
return &m_collectionName;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-02-26 10:48:40 +01:00
void RimSurfaceInViewCollection::defineUiTreeOrdering( caf::PdmUiTreeOrdering& uiTreeOrdering, QString uiConfigName /*= ""*/ )
{
auto gridView = firstAncestorOfType<RimGridView>();
auto surfViewColl = firstAncestorOfType<RimSurfaceInViewCollection>();
if ( gridView && !surfViewColl )
{
auto uiTree = gridView->separateSurfaceResultsCollection()->uiTreeOrdering();
uiTreeOrdering.appendChild( uiTree );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimSurfaceInViewCollection::name() const
{
2020-08-26 12:31:23 +02:00
if ( m_surfaceCollection ) return m_surfaceCollection->collectionName();
return "";
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurfaceInViewCollection::SurfaceColorMode RimSurfaceInViewCollection::surfaceColorMode() const
{
return m_surfaceColorMode();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurfaceCollection* RimSurfaceInViewCollection::surfaceCollection() const
{
2020-08-26 12:31:23 +02:00
return m_surfaceCollection;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceInViewCollection::setSurfaceCollection( RimSurfaceCollection* surfcoll )
{
2020-08-26 12:31:23 +02:00
m_surfaceCollection = surfcoll;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceInViewCollection::updateAllViewItems()
{
syncCollectionsWithView();
syncSurfacesWithView();
updateConnectedEditors();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceInViewCollection::syncCollectionsWithView()
{
// check that we have surface in view collections for all sub-collections
std::vector<RimSurfaceInViewCollection*> colls = m_collectionsInView.childrenByType();
for ( auto surfcoll : colls )
{
if ( !surfcoll->surfaceCollection() )
{
m_collectionsInView.removeChild( surfcoll );
delete surfcoll;
}
}
// Create new collection entries and reorder
std::vector<RimSurfaceInViewCollection*> orderedColls;
2020-08-26 12:31:23 +02:00
if ( m_surfaceCollection )
{
// pick up the collections and the order from the surface collection
2020-08-26 12:31:23 +02:00
std::vector<RimSurfaceCollection*> surfcolls = m_surfaceCollection->subCollections();
for ( auto surfcoll : surfcolls )
{
// check if this is a collection we need to create
RimSurfaceInViewCollection* viewSurfColl = getCollectionInViewForCollection( surfcoll );
if ( viewSurfColl == nullptr )
{
RimSurfaceInViewCollection* newColl = new RimSurfaceInViewCollection();
newColl->setSurfaceCollection( surfcoll );
orderedColls.push_back( newColl );
}
else
{
orderedColls.push_back( viewSurfColl );
}
}
// make sure our view surfaces have the same order as the source surface collection
m_collectionsInView.clearWithoutDelete();
for ( auto viewColl : orderedColls )
{
m_collectionsInView.push_back( viewColl );
viewColl->updateAllViewItems();
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceInViewCollection::syncSurfacesWithView()
{
// Delete surfaceInView without any real Surface connection
std::vector<RimSurfaceInView*> surfsInView = m_surfacesInView.childrenByType();
for ( auto surf : surfsInView )
{
if ( !surf->surface() )
{
m_surfacesInView.removeChild( surf );
delete surf;
}
}
2021-09-13 10:16:09 +02:00
// Create new surface entries and reorder
std::vector<RimSurfaceInView*> orderedSurfs;
2020-08-26 12:31:23 +02:00
if ( m_surfaceCollection )
{
// pick up the surfaces and the order from the surface collection
2020-08-26 12:31:23 +02:00
std::vector<RimSurface*> surfs = m_surfaceCollection->surfaces();
for ( auto surf : surfs )
{
// check if this is a surface we need to create
RimSurfaceInView* viewSurf = getSurfaceInViewForSurface( surf );
if ( viewSurf == nullptr )
{
RimSurfaceInView* newSurfInView = new RimSurfaceInView();
newSurfInView->setSurface( surf );
2021-09-13 10:16:09 +02:00
if ( m_surfaceCollection->collectionName() == RimEnsembleSurface::ensembleSourceFileCollectionName() )
{
newSurfInView->setActive( false );
}
newSurfInView->setMeshLinesEnabled( surf->isMeshLinesEnabledDefault() );
orderedSurfs.push_back( newSurfInView );
}
else
{
orderedSurfs.push_back( viewSurf );
}
}
// make sure our view surfaces have the same order as the source surface collection
m_surfacesInView.clearWithoutDelete();
for ( auto viewSurf : orderedSurfs )
{
m_surfacesInView.push_back( viewSurf );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceInViewCollection::updateFromSurfaceCollection()
{
updateAllViewItems();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimSurfaceInViewCollection::setSurfaceVisible( RimSurface* surface, bool visible )
{
updateFromSurfaceCollection();
auto* surfaceInView = findSurfaceInView( surface );
if ( !surfaceInView ) return false;
surfaceInView->setActive( visible );
surfaceInView->updateConnectedEditors();
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::expected<void, QString> RimSurfaceInViewCollection::setSurfaceProperty( RimSurface* surface, const QString& propertyName )
{
updateFromSurfaceCollection();
auto* surfaceInView = findSurfaceInView( surface );
if ( !surfaceInView )
{
return std::unexpected( QString( "Surface is not available in this view." ) );
}
surface->loadDataIfRequired();
auto* surfaceData = surface->surfaceData();
if ( !surfaceData )
{
return std::unexpected( QString( "Surface '%1' has no surface data." ).arg( surface->fullName() ) );
}
const auto propertyNames = surfaceData->propertyNames();
if ( std::find( propertyNames.begin(), propertyNames.end(), propertyName ) == propertyNames.end() )
{
return std::unexpected( QString( "Property '%1' is not available for surface '%2'." ).arg( propertyName, surface->fullName() ) );
}
surfaceInView->surfaceResultDefinition()->setPropertyName( propertyName );
return {};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimSurfaceInViewCollection::isSurfaceVisible( const RimSurface* surface ) const
{
auto* surfaceInView = findSurfaceInView( surface );
return surfaceInView && surfaceInView->isActive();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RimSurfaceInViewCollection::surfaceProperty( const RimSurface* surface ) const
{
auto* surfaceInView = findSurfaceInView( surface );
if ( !surfaceInView ) return {};
return surfaceInView->surfaceResultDefinition()->propertyName();
}
2020-01-08 11:56:48 +01:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceInViewCollection::loadData( int timeStep )
{
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
coll->loadData( timeStep );
}
for ( RimSurfaceInView* surf : m_surfacesInView )
{
if ( surf->isActive() )
{
surf->loadDataAndUpdate( timeStep );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceInViewCollection::clearGeometry()
{
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
coll->clearGeometry();
}
for ( RimSurfaceInView* surf : m_surfacesInView )
{
surf->clearGeometry();
}
}
//--------------------------------------------------------------------------------------------------
2020-01-08 11:56:48 +01:00
///
//--------------------------------------------------------------------------------------------------
2023-08-07 16:35:59 +02:00
void RimSurfaceInViewCollection::appendPartsToModel( cvf::ModelBasicList* model, cvf::Transform* scaleTransform, bool onlyNativeParts )
2020-01-08 11:56:48 +01:00
{
if ( !isChecked() ) return;
2020-01-08 11:56:48 +01:00
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
if ( coll->isChecked() )
{
2023-08-07 16:35:59 +02:00
coll->appendPartsToModel( model, scaleTransform, onlyNativeParts );
}
}
2020-01-08 11:56:48 +01:00
for ( RimSurfaceInView* surf : m_surfacesInView )
{
if ( surf->isActive() )
{
2023-08-07 16:35:59 +02:00
if ( onlyNativeParts )
{
surf->nativeSurfacePartMgr()->appendNativeGeometryPartsToModel( model, scaleTransform );
}
else
{
surf->surfacePartMgr()->appendIntersectionGeometryPartsToModel( model, scaleTransform );
}
2020-01-08 11:56:48 +01:00
}
}
model->updateBoundingBoxesRecursive();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-02-26 10:48:40 +01:00
void RimSurfaceInViewCollection::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue )
2020-01-08 11:56:48 +01:00
{
if ( changedField == &m_isChecked || changedField == &m_surfaceColorMode )
2020-01-08 11:56:48 +01:00
{
2023-08-07 16:35:59 +02:00
auto ownerView = firstAncestorOrThisOfTypeAsserted<Rim3dView>();
2020-01-08 11:56:48 +01:00
ownerView->scheduleCreateDisplayModelAndRedraw();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurfaceInView* RimSurfaceInViewCollection::getSurfaceInViewForSurface( const RimSurface* surf ) const
{
for ( auto surfInView : m_surfacesInView )
{
if ( surfInView->surface() == surf )
{
return surfInView;
}
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimSurfaceInView* RimSurfaceInViewCollection::findSurfaceInView( const RimSurface* surface ) const
{
if ( auto* surfaceInView = getSurfaceInViewForSurface( surface ) ) return surfaceInView;
for ( auto collection : m_collectionsInView )
{
if ( auto* surfaceInView = collection->findSurfaceInView( surface ) ) return surfaceInView;
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-02-26 10:48:40 +01:00
RimSurfaceInViewCollection* RimSurfaceInViewCollection::getCollectionInViewForCollection( const RimSurfaceCollection* coll ) const
{
for ( auto collInView : m_collectionsInView )
{
if ( collInView->surfaceCollection() == coll )
{
return collInView;
}
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-01-18 14:42:33 +01:00
void RimSurfaceInViewCollection::updateCellResultColor( bool hasGeneralCellResult, int timeStepIndex )
{
if ( !isChecked() ) return;
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
if ( coll->isChecked() )
{
coll->updateCellResultColor( hasGeneralCellResult, timeStepIndex );
}
}
for ( RimSurfaceInView* surf : m_surfacesInView )
{
if ( surf->isActive() )
{
bool useNativeSurfaceColors = false;
if ( surf->isNativeSurfaceResultsActive() ) useNativeSurfaceColors = true;
if ( !useNativeSurfaceColors )
{
2023-02-26 10:48:40 +01:00
bool showResults = surf->activeSeparateResultDefinition() ? surf->activeSeparateResultDefinition()->hasResult()
: hasGeneralCellResult;
if ( showResults )
{
surf->surfacePartMgr()->updateCellResultColor( timeStepIndex );
}
else
{
useNativeSurfaceColors = true;
}
}
if ( useNativeSurfaceColors )
{
surf->surfacePartMgr()->updateNativeSurfaceColors();
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimSurfaceInViewCollection::applySingleColorEffect()
{
if ( !isChecked() ) return;
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
if ( coll->isChecked() )
{
coll->applySingleColorEffect();
}
}
for ( RimSurfaceInView* surf : m_surfacesInView )
{
if ( surf->isActive() )
{
surf->surfacePartMgr()->updateNativeSurfaceColors();
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimSurfaceInViewCollection::hasAnyActiveSeparateResults()
{
if ( !isChecked() ) return false;
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
if ( coll->isChecked() )
{
bool found = coll->hasAnyActiveSeparateResults();
if ( found ) return true;
}
}
for ( RimSurfaceInView* surf : m_surfacesInView )
{
2023-02-26 10:48:40 +01:00
if ( surf->isActive() && surf->activeSeparateResultDefinition() && surf->activeSeparateResultDefinition()->hasResult() )
{
return true;
}
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-02-26 10:48:40 +01:00
void RimSurfaceInViewCollection::updateLegendRangesTextAndVisibility( RiuViewer* nativeOrOverrideViewer, bool isUsingOverrideViewer )
{
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
2023-02-26 10:48:40 +01:00
if ( coll->isChecked() ) coll->updateLegendRangesTextAndVisibility( nativeOrOverrideViewer, isUsingOverrideViewer );
}
for ( RimSurfaceInView* surf : m_surfacesInView )
{
surf->updateLegendRangesTextAndVisibility( nativeOrOverrideViewer, isUsingOverrideViewer );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimRegularLegendConfig*> RimSurfaceInViewCollection::legendConfigs()
{
std::vector<RimRegularLegendConfig*> configs;
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
if ( coll->isChecked() )
{
std::vector<RimRegularLegendConfig*> collconfigs = coll->legendConfigs();
configs.insert( configs.end(), collconfigs.begin(), collconfigs.end() );
}
}
for ( RimSurfaceInView* surf : m_surfacesInView )
{
if ( surf->isActive() && surf->surfaceResultDefinition() )
{
configs.push_back( surf->surfaceResultDefinition()->legendConfig() );
}
}
return configs;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimSurfaceInView*> RimSurfaceInViewCollection::visibleSurfacesInView() const
{
if ( !isChecked() ) return {};
std::vector<RimSurfaceInView*> surfaces;
for ( RimSurfaceInViewCollection* coll : m_collectionsInView )
{
if ( !coll ) continue;
std::vector<RimSurfaceInView*> collSurfaces = coll->visibleSurfacesInView();
surfaces.insert( surfaces.end(), collSurfaces.begin(), collSurfaces.end() );
}
for ( RimSurfaceInView* surf : m_surfacesInView )
{
if ( surf && surf->isActive() ) surfaces.push_back( surf );
}
return surfaces;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<const RivIntersectionGeometryGeneratorInterface*> RimSurfaceInViewCollection::intersectionGeometryGenerators() const
{
std::vector<const RivIntersectionGeometryGeneratorInterface*> generators;
for ( auto surf : m_surfacesInView )
{
if ( surf->isActive() && surf->isNativeSurfaceResultsActive() )
{
auto generator = surf->intersectionGeometryGenerator();
if ( generator ) generators.push_back( generator );
}
}
for ( auto child : m_collectionsInView )
{
auto childGenerators = child->intersectionGeometryGenerators();
generators.insert( generators.end(), childGenerators.begin(), childGenerators.end() );
}
return generators;
}