Files
ResInsight/ApplicationLibCode/GeoMech/GeoMechVisualization/RivFemPartPartMgr.cpp
T
Magne Sjaastad 56e98ba1c4 #14476 ApplicationLibCode: Replace CVF_ASSERT with CAF_ASSERT
Migrate all assert macros in ApplicationLibCode to CAF_ASSERT and remove every
use of cvfAssert.h.

CVF_ASSERT is replaced one to one. CVF_TIGHT_ASSERT is also replaced by
CAF_ASSERT, which is semantically exact: CVF_ENABLE_TIGHT_ASSERTS is 1 only
under _DEBUG, and that is what CAF_ASSERT now does. The two CVF_FAIL_MSG sites
become CAF_ASSERT( false && "message" ), preserving the message with the idiom
already used elsewhere in the code base.

Counts before and after: CVF_ASSERT 1044 to 0, CVF_TIGHT_ASSERT 66 to 0,
CVF_FAIL_MSG 2 to 0, cvfAssert.h references 154 to 0.

Include handling: files that included cvfAssert.h directly now include
cafAssert.h instead, includes left dead by the migration are removed, and files
that were relying on cvfAssert.h transitively get an explicit cafAssert.h. Files
that reach cafAssert.h through another caf header are left unchanged; a missing
include here is a compile error, not a silently disabled assert.

ResultStatisticsCache links only LibCore and therefore had no path to
cafAssert.h. Add the cafPdmCore directory as a private include path rather than
linking the library, since cafAssert.h is header only.

Note that this stops these asserts from firing in Release and RelWithDebInfo,
where CVF_ASSERT was previously active.
2026-08-07 15:05:57 +02:00

367 lines
14 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2015- Statoil ASA
// Copyright (C) 2015- 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 "RivFemPartPartMgr.h"
#include "RiaPreferences.h"
#include "RifGeoMechReaderInterface.h"
#include "RigFemAddressDefines.h"
#include "RigFemPart.h"
#include "RigFemPartResultsCollection.h"
#include "RigFemScalarResultFrames.h"
#include "RigGeoMechCaseData.h"
#include "RimEclipseView.h"
#include "RimGeoMechCellColors.h"
#include "RimGeoMechView.h"
#include "RimRegularLegendConfig.h"
#include "RivFemPickSourceInfo.h"
#include "RivGeoMechPartMgr.h"
#include "RivMeshLinesSourceInfo.h"
#include "RivPartPriority.h"
#include "RivResultToTextureMapper.h"
#include "RivScalarMapperUtils.h"
#include "RivSourceInfo.h"
#include "RivTextureCoordsCreator.h"
#include "cafEffectGenerator.h"
#include "cvfDrawableGeo.h"
#include "cvfModelBasicList.h"
#include "cvfPart.h"
#include "cvfRenderStateBlending.h"
#include "cvfRenderStatePolygonOffset.h"
#include "cvfRenderState_FF.h"
#include "cvfShaderProgram.h"
#include "cvfShaderProgramGenerator.h"
#include "cvfShaderSourceProvider.h"
#include "cvfShaderSourceRepository.h"
#include "cvfStructGrid.h"
#include "cvfTransform.h"
#include "cvfUniform.h"
#include <cstdlib>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RivFemPartPartMgr::RivFemPartPartMgr( const RigFemPart* part, cvf::Vec3d displayOffset )
: m_surfaceGenerator( part, displayOffset )
, m_part( part )
, m_opacityLevel( 1.0f )
, m_defaultColor( cvf::Color3::WHITE )
{
CAF_ASSERT( part );
m_partIdx = part->elementPartId();
m_cellVisibility = new cvf::UByteArray;
m_surfaceFacesTextureCoords = new cvf::Vec2fArray;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RivFemPartPartMgr::~RivFemPartPartMgr()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivFemPartPartMgr::setTransform( cvf::Transform* scaleTransform )
{
m_scaleTransform = scaleTransform;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivFemPartPartMgr::setCellVisibility( cvf::UByteArray* cellVisibilities )
{
CAF_ASSERT( m_scaleTransform.notNull() );
CAF_ASSERT( cellVisibilities );
m_cellVisibility = cellVisibilities;
m_surfaceGenerator.setElementVisibility( cellVisibilities );
generatePartGeometry( m_surfaceGenerator );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivFemPartPartMgr::setDisplacements( bool useDisplacements, double scalingFactor, const std::vector<cvf::Vec3f>& displacements )
{
size_t nodeCount = m_part->nodes().coordinates.size();
m_displacedNodeCoordinates.resize( nodeCount );
const auto coords = m_part->nodes().coordinates;
useDisplacements = useDisplacements && ( displacements.size() == coords.size() );
if ( useDisplacements )
{
for ( size_t i = 0; i < nodeCount; i++ )
{
m_displacedNodeCoordinates[i] = coords[i] + displacements[i] * scalingFactor;
}
}
else
{
for ( size_t i = 0; i < nodeCount; i++ )
{
m_displacedNodeCoordinates[i] = coords[i];
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivFemPartPartMgr::generatePartGeometry( RivFemPartGeometryGenerator& geoBuilder )
{
bool useBufferObjects = true;
// Surface geometry
{
m_surfaceFaces = nullptr; // To possibly free memory before adding the new stuff
cvf::ref<cvf::DrawableGeo> geo = geoBuilder.generateSurface( m_displacedNodeCoordinates );
if ( geo.notNull() )
{
geo->computeNormals();
if ( useBufferObjects )
{
geo->setRenderMode( cvf::DrawableGeo::BUFFER_OBJECT );
}
cvf::ref<cvf::Part> part = new cvf::Part;
part->setName( "FemPart " + cvf::String( m_partIdx ) );
part->setId( m_partIdx ); // Use part index as part ID
part->setDrawable( geo.p() );
part->setTransform( m_scaleTransform.p() );
// Set mapping from triangle face index to element index
cvf::ref<RivFemPickSourceInfo> si = new RivFemPickSourceInfo( m_partIdx, geoBuilder.triangleToElementMapper() );
part->setSourceInfo( si.p() );
part->updateBoundingBox();
// Set default effect
caf::SurfaceEffectGenerator geometryEffgen( cvf::Color4f( cvf::Color3f::WHITE ), caf::PO_1 );
cvf::ref<cvf::Effect> geometryOnlyEffect = geometryEffgen.generateCachedEffect();
part->setEffect( geometryOnlyEffect.p() );
part->setEnableMask( surfaceBit );
m_surfaceFaces = part;
}
}
// Mesh geometry
{
m_surfaceGridLines = nullptr; // To possibly free memory before adding the new stuff
cvf::ref<cvf::DrawableGeo> geoMesh = geoBuilder.createMeshDrawable();
if ( geoMesh.notNull() )
{
if ( useBufferObjects )
{
geoMesh->setRenderMode( cvf::DrawableGeo::BUFFER_OBJECT );
}
cvf::ref<cvf::Part> part = new cvf::Part;
part->setName( "Grid mesh " + cvf::String( m_partIdx ) );
part->setDrawable( geoMesh.p() );
part->setTransform( m_scaleTransform.p() );
part->updateBoundingBox();
RiaPreferences* prefs = RiaPreferences::current();
cvf::ref<cvf::Effect> eff;
caf::MeshEffectGenerator effGen( prefs->defaultGridLineColors() );
eff = effGen.generateCachedEffect();
part->setPriority( RivPartPriority::PartType::MeshLines );
part->setEnableMask( meshSurfaceBit );
part->setEffect( eff.p() );
part->setSourceInfo( new RivMeshLinesSourceInfo( nullptr ) );
m_surfaceGridLines = part;
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivFemPartPartMgr::appendPartsToModel( cvf::ModelBasicList* model )
{
CAF_ASSERT( model != nullptr );
if ( m_part->enabled() )
{
if ( m_surfaceFaces.notNull() ) model->addPart( m_surfaceFaces.p() );
if ( m_surfaceGridLines.notNull() ) model->addPart( m_surfaceGridLines.p() );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const RivFemPartGeometryGenerator* RivFemPartPartMgr::surfaceGenerator() const
{
return &m_surfaceGenerator;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivFemPartPartMgr::updateCellColor( cvf::Color4f color )
{
if ( m_surfaceFaces.isNull() ) return;
if ( !m_part->enabled() ) return;
// Set default effect
caf::SurfaceEffectGenerator geometryEffgen( color, caf::PO_1 );
cvf::ref<cvf::Effect> geometryOnlyEffect = geometryEffgen.generateCachedEffect();
if ( m_surfaceFaces.notNull() ) m_surfaceFaces->setEffect( geometryOnlyEffect.p() );
if ( color.a() < 1.0f )
{
// Set priority to make sure this transparent geometry are rendered last
if ( m_surfaceFaces.notNull() ) m_surfaceFaces->setPriority( RivPartPriority::PartType::Transparent );
}
m_opacityLevel = color.a();
m_defaultColor = color.toColor3f();
// Update mesh colors as well, in case of change
RiaPreferences* prefs = RiaPreferences::current();
cvf::ref<cvf::Effect> eff;
if ( m_surfaceFaces.notNull() )
{
caf::MeshEffectGenerator effGen( prefs->defaultGridLineColors() );
eff = effGen.generateCachedEffect();
m_surfaceGridLines->setEffect( eff.p() );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivFemPartPartMgr::updateCellResultColor( int timeStepIndex, int frameIndex, RimGeoMechCellColors* cellResultColors )
{
CAF_ASSERT( cellResultColors );
if ( !m_part->enabled() ) return;
cvf::ref<cvf::Color3ubArray> surfaceFacesColorArray;
// Outer surface
if ( m_surfaceFaces.notNull() )
{
const cvf::ScalarMapper* mapper = cellResultColors->legendConfig()->scalarMapper();
RigGeoMechCaseData* caseData = cellResultColors->ownerCaseData();
if ( !caseData ) return;
RigFemResultAddress resVarAddress = RigFemAddressDefines::getResultLookupAddress( cellResultColors->resultAddress() );
const std::vector<float>& resultValues =
caseData->femPartResults()->resultValues( resVarAddress, m_partIdx, timeStepIndex, frameIndex );
const std::vector<size_t>* vxToResultMapping = nullptr;
int vxCount = 0;
if ( resVarAddress.resultPosType == RIG_NODAL )
{
vxToResultMapping = &( m_surfaceGenerator.quadVerticesToNodeIdxMapping() );
}
else if ( resVarAddress.resultPosType == RIG_ELEMENT_NODAL || resVarAddress.resultPosType == RIG_INTEGRATION_POINT ||
resVarAddress.resultPosType == RIG_FORMATION_NAMES )
{
vxToResultMapping = &( m_surfaceGenerator.quadVerticesToGlobalElmNodeIdx() );
}
else if ( resVarAddress.resultPosType == RIG_ELEMENT_NODAL_FACE )
{
vxToResultMapping = &( m_surfaceGenerator.quadVerticesToGlobalElmFaceNodeIdx() );
}
else if ( resVarAddress.resultPosType == RIG_ELEMENT )
{
vxToResultMapping = &( m_surfaceGenerator.quadVerticesToGlobalElmIdx() );
}
if ( !vxToResultMapping ) return;
vxCount = static_cast<int>( vxToResultMapping->size() );
m_surfaceFacesTextureCoords->resize( vxCount );
if ( resultValues.empty() )
{
m_surfaceFacesTextureCoords->setAll( cvf::Vec2f( 0.0, 1.0f ) );
}
else
{
cvf::Vec2f* rawPtr = m_surfaceFacesTextureCoords->ptr();
#pragma omp parallel for schedule( dynamic )
for ( int quadStartIdx = 0; quadStartIdx < vxCount; quadStartIdx += 4 )
{
float resultValue1 = resultValues[( *vxToResultMapping )[quadStartIdx + 0]];
float resultValue2 = resultValues[( *vxToResultMapping )[quadStartIdx + 1]];
float resultValue3 = resultValues[( *vxToResultMapping )[quadStartIdx + 2]];
float resultValue4 = resultValues[( *vxToResultMapping )[quadStartIdx + 3]];
if ( resultValue1 == HUGE_VAL || resultValue1 != resultValue1 // a != a is true for NAN's
|| resultValue2 == HUGE_VAL || resultValue2 != resultValue2 || resultValue3 == HUGE_VAL ||
resultValue3 != resultValue3 || resultValue4 == HUGE_VAL || resultValue4 != resultValue4 )
{
rawPtr[quadStartIdx][1] = 1.0f;
rawPtr[quadStartIdx + 1][1] = 1.0f;
rawPtr[quadStartIdx + 2][1] = 1.0f;
rawPtr[quadStartIdx + 3][1] = 1.0f;
}
else
{
rawPtr[quadStartIdx] = mapper->mapToTextureCoord( resultValue1 );
rawPtr[quadStartIdx + 1] = mapper->mapToTextureCoord( resultValue2 );
rawPtr[quadStartIdx + 2] = mapper->mapToTextureCoord( resultValue3 );
rawPtr[quadStartIdx + 3] = mapper->mapToTextureCoord( resultValue4 );
}
}
}
auto view = cellResultColors->firstAncestorOrThisOfTypeAsserted<Rim3dView>();
RivScalarMapperUtils::applyTextureResultsToPart( m_surfaceFaces.p(),
m_surfaceFacesTextureCoords.p(),
mapper,
m_opacityLevel,
caf::FC_NONE,
view->isLightingDisabled() );
}
}