diff --git a/ApplicationLibCode/ProjectDataModel/CellFilters/RimPolygonFilter.cpp b/ApplicationLibCode/ProjectDataModel/CellFilters/RimPolygonFilter.cpp index b0a3503b12..7fb3d7649b 100644 --- a/ApplicationLibCode/ProjectDataModel/CellFilters/RimPolygonFilter.cpp +++ b/ApplicationLibCode/ProjectDataModel/CellFilters/RimPolygonFilter.cpp @@ -54,6 +54,8 @@ #include +#include + namespace caf { template <> @@ -902,6 +904,45 @@ int RimPolygonFilter::findEclipseKLayer( const std::vector& points, } } + auto findKLayerBelowPoint = []( const cvf::Vec3d& point, RigMainGrid* mainGrid ) { + // Create a bounding box (ie a ray) from the point down to minimum of grid + cvf::Vec3d lowestPoint( point.x(), point.y(), mainGrid->boundingBox().min().z() ); + + cvf::BoundingBox rayBBox; + rayBBox.add( point ); + rayBBox.add( lowestPoint ); + + // Find the cells intersecting the ray + std::vector allCellIndices; + mainGrid->findIntersectingCells( rayBBox, &allCellIndices ); + + // Get the minimum K layer index + int minK = std::numeric_limits::max(); + bool anyHits = false; + for ( size_t cIdx : allCellIndices ) + { + if ( cIdx != cvf::UNDEFINED_SIZE_T ) + { + size_t ni, nj, nk; + mainGrid->ijkFromCellIndexUnguarded( cIdx, &ni, &nj, &nk ); + if ( mainGrid->isCellValid( ni, nj, nk ) ) + { + anyHits = true; + minK = std::min( minK, static_cast( nk ) ); + } + } + } + + return anyHits ? minK : -1; + }; + + // shoot a ray down from each point to try to find a valid hit there + for ( size_t p = 0; p < points.size() - 1; p++ ) + { + int k = findKLayerBelowPoint( points[p], data->mainGrid() ); + if ( k != -1 ) return k; + } + // loop over all sub-grids to find one with a cell hit in case main grid search failed for ( size_t gridIndex = 1; gridIndex < data->gridCount(); gridIndex++ ) {