Improves robustness for curve merging operations

- Ensures a more robust size multiplier calculation by validating the weighted mean and preventing division by zero.
- Addresses a potential division by zero during curve interpolation when the X values are very close, ensuring a more robust and accurate result.
This commit is contained in:
Magne Sjaastad committed 2025-08-22 06:47:07 +02:00
1 parent e455011199
commit 8dff7b46c9
2 files changed
+22 -10

No files matched your search

@@ -344,15 +344,22 @@ double RiaCurveMerger<XValueType>::interpolatedYValue( const XValueType&
return firstValue;
}
double firstDiff = fabs( XComparator::diff( interpolationXValue, xValues.at( firstI ) ) );
double secondDiff = fabs( XComparator::diff( xValues.at( secondI ), interpolationXValue ) );
const double firstDiff = fabs( XComparator::diff( interpolationXValue, xValues.at( firstI ) ) );
const double secondDiff = fabs( XComparator::diff( xValues.at( secondI ), interpolationXValue ) );
const double totalDiff = firstDiff + secondDiff;
double firstWeight = secondDiff / ( firstDiff + secondDiff );
double secondWeight = firstDiff / ( firstDiff + secondDiff );
double val = ( firstValue * firstWeight ) + ( secondValue * secondWeight );
CVF_ASSERT( RiaCurveDataTools::isValidValue( val, removeInterpolatedValues ) );
double val;
if ( totalDiff < std::numeric_limits<double>::epsilon() )
{
// Points are essentially identical - use either value
val = firstValue; // or secondValue, or average: (firstValue + secondValue) * 0.5
}
else
{
const double firstWeight = secondDiff / totalDiff;
const double secondWeight = firstDiff / totalDiff;
val = ( firstValue * firstWeight ) + ( secondValue * secondWeight );
}
return val;
}