Files
ResInsight/ApplicationLibCode/ModelVisualization/RivTernaryTextureCoordsCreator.cpp
Magne Sjaastad fcaf62dae5 #14906: Fix crash in ternary result coloring when eclipse case is missing
Crash signature 1a259402c999.

RivTernaryTextureCoordsCreator calls cellResultColors->eclipseCase()->eclipseCaseData() unchecked in both constructors, and then eclipseCase->gridCellToResultWellIndex(). eclipseCase() is a PdmPointer and is null once the case is gone.

Read the case data only when the case exists, and create the pipe-in-cell evaluator only when the case data and well collection exist.

Call stack:
[3] RimEclipseCase::eclipseCaseData() at ResInsight/ApplicationLibCode/ProjectDataModel/RimEclipseCase.cpp:196
[4] RivTernaryTextureCoordsCreator::RivTernaryTextureCoordsCreator(RimEclipseResultDefinition const*, RimTernaryLegendConfig*, RimSimWellInViewCollection*, unsigned long, unsigned long, cvf::StructGridQuadToCellFaceMapper const*) at ResInsight/ApplicationLibCode/ModelVisualization/RivTernaryTextureCoordsCreator.cpp:55
[5] RivGridPartMgr::updateCellResultColor(unsigned long, RimEclipseCellColors*) at ResInsight/ApplicationLibCode/ModelVisualization/RivGridPartMgr.cpp:295
[6] RivReservoirPartMgr::updateCellResultColor(unsigned long, RimEclipseCellColors*) at ResInsight/ApplicationLibCode/ModelVisualization/RivReservoirPartMgr.cpp:129
[7] RimEclipseView::updateVisibleCellColors() at ResInsight/ApplicationLibCode/ProjectDataModel/RimEclipseView.cpp:1119
[8] RimEclipseView::onUpdateDisplayModelForCurrentTimeStep() at ResInsight/ApplicationLibCode/ProjectDataModel/RimEclipseView.cpp:951
[9] Rim3dView::updateDisplayModelForCurrentTimeStepAndRedraw() at ResInsight/ApplicationLibCode/ProjectDataModel/Rim3dView.cpp:673
[10] RiuViewer::setCurrentFrame(int) at ResInsight/ApplicationLibCode/UserInterface/RiuViewer.cpp:1042
[11] RimViewLinker::updateTimeStep(Rim3dView*, int) at ResInsight/ApplicationLibCode/ProjectDataModel/RimViewLinker.cpp:119
[12] RiuViewer::slotSetCurrentFrame(int) at ResInsight/ApplicationLibCode/UserInterface/RiuViewer.cpp:365
[14] caf::FrameAnimationControl::changeFrame(int) at ResInsight/cmakebuild/Fwk/AppFwk/cafAnimControl/cafAnimControl_autogen/EWIEGA46WW/moc_cafFrameAnimationControl.cpp:321
[24] RiaGuiApplication::notify(QObject*, QEvent*) at ResInsight/ApplicationLibCode/Application/RiaGuiApplication.cpp:1828
[30] RiaGuiApplication::notify(QObject*, QEvent*) at ResInsight/ApplicationLibCode/Application/RiaGuiApplication.cpp:1828
[41] main at ResInsight/ApplicationExeCode/RiaMain.cpp:267
[42] __libc_start_main at :0
2026-10-12 19:00:45 +02:00

219 lines
12 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) Statoil ASA
// Copyright (C) Ceetron Solutions AS
//
// 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 "RivTernaryTextureCoordsCreator.h"
#include "RiaResultNames.h"
#include "RigCaseCellResultsData.h"
#include "RigEclipseCaseData.h"
#include "RigEclipseResultAddress.h"
#include "RigResultAccessorFactory.h"
#include "RigTernaryResultAccessor.h"
#include "Well/RigPipeInCellEvaluator.h"
#include "RimEclipseCase.h"
#include "RimEclipseResultDefinition.h"
#include "RimEclipseView.h"
#include "RimSimWellInViewCollection.h"
#include "RimTernaryLegendConfig.h"
#include "RivTernaryResultToTextureMapper.h"
#include "RivTernaryScalarMapper.h"
#include "cvfStructGridGeometryGenerator.h"
#include "cafAssert.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RivTernaryTextureCoordsCreator::RivTernaryTextureCoordsCreator( const RimEclipseResultDefinition* cellResultColors,
RimTernaryLegendConfig* ternaryLegendConfig,
RimSimWellInViewCollection* simWellInViewCollection,
size_t timeStepIndex,
size_t gridIndex,
const cvf::StructGridQuadToCellFaceMapper* quadMapper )
{
CAF_ASSERT( quadMapper );
m_quadMapper = quadMapper;
auto rimEclipseCase = cellResultColors->eclipseCase();
RigEclipseCaseData* eclipseCase = rimEclipseCase ? rimEclipseCase->eclipseCaseData() : nullptr;
size_t resTimeStepIdx = timeStepIndex;
if ( cellResultColors->hasStaticResult() ) resTimeStepIdx = 0;
RiaDefines::PorosityModelType porosityModel = cellResultColors->porosityModel();
cvf::ref<RigResultAccessor> soil = RigResultAccessorFactory::createFromResultAddress( eclipseCase,
gridIndex,
porosityModel,
resTimeStepIdx,
RigEclipseResultAddress( RiaResultNames::soil() ) );
cvf::ref<RigResultAccessor> sgas = RigResultAccessorFactory::createFromResultAddress( eclipseCase,
gridIndex,
porosityModel,
resTimeStepIdx,
RigEclipseResultAddress( RiaResultNames::sgas() ) );
cvf::ref<RigResultAccessor> swat = RigResultAccessorFactory::createFromResultAddress( eclipseCase,
gridIndex,
porosityModel,
resTimeStepIdx,
RigEclipseResultAddress( RiaResultNames::swat() ) );
m_resultAccessor = new RigTernaryResultAccessor();
m_resultAccessor->setTernaryResultAccessors( soil.p(), sgas.p(), swat.p() );
cvf::ref<RigPipeInCellEvaluator> pipeInCellEval;
if ( eclipseCase && simWellInViewCollection )
{
pipeInCellEval = new RigPipeInCellEvaluator( simWellInViewCollection->resultWellGeometryVisibilities( timeStepIndex ),
eclipseCase->gridCellToResultWellIndex( gridIndex ) );
}
const RivTernaryScalarMapper* mapper = ternaryLegendConfig->scalarMapper();
m_texMapper = new RivTernaryResultToTextureMapper( mapper, pipeInCellEval.p() );
CAF_ASSERT( m_texMapper.notNull() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RivTernaryTextureCoordsCreator::RivTernaryTextureCoordsCreator( const RimEclipseResultDefinition* cellResultColors,
const RivTernaryScalarMapper* ternaryColorMapper,
size_t timeStepIndex )
: m_quadMapper( nullptr )
{
auto rimEclipseCase = cellResultColors->eclipseCase();
RigEclipseCaseData* eclipseCase = rimEclipseCase ? rimEclipseCase->eclipseCaseData() : nullptr;
size_t resTimeStepIdx = timeStepIndex;
if ( cellResultColors->hasStaticResult() ) resTimeStepIdx = 0;
RiaDefines::PorosityModelType porosityModel = cellResultColors->porosityModel();
size_t gridIndex = 0;
cvf::ref<RigResultAccessor> soil = RigResultAccessorFactory::createFromResultAddress( eclipseCase,
gridIndex,
porosityModel,
resTimeStepIdx,
RigEclipseResultAddress( RiaResultNames::soil() ) );
cvf::ref<RigResultAccessor> sgas = RigResultAccessorFactory::createFromResultAddress( eclipseCase,
gridIndex,
porosityModel,
resTimeStepIdx,
RigEclipseResultAddress( RiaResultNames::sgas() ) );
cvf::ref<RigResultAccessor> swat = RigResultAccessorFactory::createFromResultAddress( eclipseCase,
gridIndex,
porosityModel,
resTimeStepIdx,
RigEclipseResultAddress( RiaResultNames::swat() ) );
m_resultAccessor = new RigTernaryResultAccessor();
m_resultAccessor->setTernaryResultAccessors( soil.p(), sgas.p(), swat.p() );
// Create a texture mapper without detecting transparency using RigPipeInCellEvaluator
m_texMapper = new RivTernaryResultToTextureMapper( ternaryColorMapper, nullptr );
CAF_ASSERT( m_texMapper.notNull() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivTernaryTextureCoordsCreator::createTextureCoords( cvf::Vec2fArray* quadTextureCoords )
{
CAF_ASSERT( m_quadMapper.notNull() );
createTextureCoords( quadTextureCoords, m_quadMapper.p(), m_resultAccessor.p(), m_texMapper.p() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivTernaryTextureCoordsCreator::createTextureCoords( cvf::Vec2fArray* triTextureCoords, const std::vector<size_t>& triangleToCellIdx )
{
CAF_ASSERT( m_quadMapper.isNull() );
createTextureCoords( triTextureCoords, triangleToCellIdx, m_resultAccessor.p(), m_texMapper.p() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivTernaryTextureCoordsCreator::createTextureCoords( cvf::Vec2fArray* quadTextureCoords,
const cvf::StructGridQuadToCellFaceMapper* quadMapper,
const RigTernaryResultAccessor* resultAccessor,
const RivTernaryResultToTextureMapper* texMapper )
{
CAF_ASSERT( quadTextureCoords && quadMapper && resultAccessor && texMapper );
size_t numVertices = quadMapper->quadCount() * 4;
quadTextureCoords->resize( numVertices );
cvf::Vec2f* rawPtr = quadTextureCoords->ptr();
#pragma omp parallel for
for ( int i = 0; i < static_cast<int>( quadMapper->quadCount() ); i++ )
{
cvf::StructGridInterface::FaceType faceId = quadMapper->cellFace( i );
size_t cellIdx = quadMapper->cellIndex( i );
cvf::Vec2d resultValue = resultAccessor->cellFaceScalar( cellIdx, faceId );
cvf::Vec2f texCoord = texMapper->getTexCoord( resultValue.x(), resultValue.y(), cellIdx );
size_t j;
for ( j = 0; j < 4; j++ )
{
rawPtr[i * 4 + j] = texCoord;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivTernaryTextureCoordsCreator::createTextureCoords( cvf::Vec2fArray* textureCoords,
const std::vector<size_t>& triangleToCellIdx,
const RigTernaryResultAccessor* resultAccessor,
const RivTernaryResultToTextureMapper* texMapper )
{
CAF_ASSERT( textureCoords && resultAccessor && texMapper );
size_t numVertices = triangleToCellIdx.size() * 3;
textureCoords->resize( numVertices );
cvf::Vec2f* rawPtr = textureCoords->ptr();
#pragma omp parallel for
for ( int i = 0; i < static_cast<int>( triangleToCellIdx.size() ); i++ )
{
size_t cellIdx = triangleToCellIdx[i];
cvf::Vec2d resultValue = resultAccessor->cellScalarGlobIdx( cellIdx );
cvf::Vec2f texCoord = texMapper->getTexCoord( resultValue.x(), resultValue.y(), cellIdx );
size_t j;
for ( j = 0; j < 3; j++ )
{
rawPtr[i * 3 + j] = texCoord;
}
}
}