Skip to content

Commit

Permalink
Add feature and image processing dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Jan 13, 2025
1 parent 4b2798e commit 816d6d7
Show file tree
Hide file tree
Showing 9 changed files with 737 additions and 18 deletions.
6 changes: 6 additions & 0 deletions ApplicationLibCode/Commands/CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RicNewWellTargetCandidatesGeneratorFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicNewStatisticsContourMapFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicNewStatisticsContourMapViewFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicCreateContourMapPolygonFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicCreateContourMapPolygonTools.h
${CMAKE_CURRENT_LIST_DIR}/RicPolygonFromImageDialog.h
)

set(SOURCE_GROUP_SOURCE_FILES
Expand Down Expand Up @@ -202,6 +205,9 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicNewWellTargetCandidatesGeneratorFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicNewStatisticsContourMapFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicNewStatisticsContourMapViewFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicCreateContourMapPolygonFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicCreateContourMapPolygonTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RicPolygonFromImageDialog.cpp
)

if(RESINSIGHT_USE_QT_CHARTS)
Expand Down
132 changes: 132 additions & 0 deletions ApplicationLibCode/Commands/RicCreateContourMapPolygonFeature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- 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 "RicCreateContourMapPolygonFeature.h"

#include "RiaLogging.h"

#include "RicExportContourMapToTextFeature.h"
#include "RicPolygonFromImageDialog.h"

#include "Polygons/RimPolygon.h"
#include "Polygons/RimPolygonCollection.h"
#include "RimContourMapProjection.h"
#include "RimEclipseContourMapView.h"
#include "RimGeoMechContourMapView.h"
#include "RimTools.h"

#include "RigContourMapProjection.h"
#include "RigPolygonTools.h"

#include "cafSelectionManager.h"

#include <QAction>

CAF_CMD_SOURCE_INIT( RicCreateContourMapPolygonFeature, "RicCreateContourMapPolygonFeature" );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateContourMapPolygonFeature::onActionTriggered( bool isChecked )
{
RimContourMapProjection* contourMapProjection = nullptr;

auto [existingEclipseContourMap, existingGeoMechContourMap] = RicExportContourMapToTextFeature::findContourMapView();
if ( existingEclipseContourMap ) contourMapProjection = existingEclipseContourMap->contourMapProjection();
if ( existingGeoMechContourMap ) contourMapProjection = existingGeoMechContourMap->contourMapProjection();

if ( !contourMapProjection ) return;

auto rigContourMapProjection = contourMapProjection->mapProjection();
if ( !rigContourMapProjection ) return;

auto vertexSizeIJ = rigContourMapProjection->numberOfVerticesIJ();

std::vector<std::vector<int>> image( vertexSizeIJ.x(), std::vector<int>( vertexSizeIJ.y(), 0 ) );

for ( cvf::uint i = 0; i < vertexSizeIJ.x(); i++ )
{
for ( cvf::uint j = 0; j < vertexSizeIJ.y(); j++ )
{
double valueAtVertex = rigContourMapProjection->valueAtVertex( i, j );

if ( !std::isinf( valueAtVertex ) )
{
image[i][j] = 1;
}
else
{
image[i][j] = 0;
}
}
}

ImageProcessingDialog dlg;
dlg.show();
dlg.setImageData( image );
dlg.updateAndShowImages();

if ( dlg.exec() == QDialog::Rejected ) return;

auto finalImage = dlg.finalImageData();
if ( finalImage.empty() ) return;

auto boundaryPoints = RigPolygonTools::boundary( finalImage );

std::vector<cvf::Vec3d> polygonDomainCoords;
{
auto xVertexPositions = rigContourMapProjection->xVertexPositions();
auto yVertexPositions = rigContourMapProjection->yVertexPositions();
auto origin3d = rigContourMapProjection->origin3d();

for ( auto [i, j] : boundaryPoints )
{
double xDomain = xVertexPositions.at( i ) + origin3d.x();
double yDomain = yVertexPositions.at( j ) + origin3d.y();

polygonDomainCoords.emplace_back( cvf::Vec3d( xDomain, yDomain, origin3d.z() ) );
}

// Epsilon used to simplify polygon. Useful range typical value in [5..30]
const double defaultEpsilon = 20.0;
RigPolygonTools::simplifyPolygon( polygonDomainCoords, defaultEpsilon );
}

if ( polygonDomainCoords.size() >= 3 )
{
auto polygonCollection = RimTools::polygonCollection();

auto newPolygon = polygonCollection->appendUserDefinedPolygon();

newPolygon->setPointsInDomainCoords( polygonDomainCoords );
newPolygon->coordinatesChanged.send();

polygonCollection->uiCapability()->updateAllRequiredEditors();
}
}

//--------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateContourMapPolygonFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setIcon( QIcon( ":/PolylinesFromFile16x16.png" ) );
actionToSetup->setText( "Create Polygon From Contour Map" );
}
33 changes: 33 additions & 0 deletions ApplicationLibCode/Commands/RicCreateContourMapPolygonFeature.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- 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.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "cafCmdFeature.h"

//==================================================================================================
///
//==================================================================================================
class RicCreateContourMapPolygonFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;

protected:
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
};
144 changes: 144 additions & 0 deletions ApplicationLibCode/Commands/RicCreateContourMapPolygonTools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- 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 "RicCreateContourMapPolygonTools.h"

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QImage RicCreateContourMapPolygonTools::convertBinaryToImage( const std::vector<std::vector<int>>& data, QColor color, int transparency )
{
if ( data.empty() || data[0].empty() )
{
qWarning( "Data is empty. Cannot export an image." );
return {};
}

// Get dimensions
int height = static_cast<int>( data[0].size() );
int width = static_cast<int>( data.size() );

// Create a QImage
QImage image( width, height, QImage::Format_ARGB32 );

// Fill QImage with data
for ( int y = 0; y < height; ++y )
{
for ( int x = 0; x < width; ++x )
{
int value = std::clamp( data[x][y], 0, 255 );
if ( value > 0 )
image.setPixel( x, height - y - 1, qRgba( color.red(), color.green(), color.blue(), transparency ) ); // Grayscale
else
{
image.setPixel( x, height - y - 1, qRgba( 0, 0, 0, transparency ) );
}
}
}

return image;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QImage RicCreateContourMapPolygonTools::convertBinaryToGrayscaleImage( const std::vector<std::vector<int>>& data, int colorValue )
{
if ( data.empty() || data[0].empty() )
{
qWarning( "Data is empty. Cannot export an image." );
return {};
}

// Get dimensions
int height = static_cast<int>( data[0].size() );
int width = static_cast<int>( data.size() );

// Create a QImage
QImage image( width, height, QImage::Format_Grayscale8 );

// Fill QImage with data
for ( int y = 0; y < height; ++y )
{
for ( int x = 0; x < width; ++x )
{
int value = std::clamp( data[x][y] * colorValue, 0, 255 ); // Ensure value is in [0, 255]
image.setPixel( x, height - y - 1, qRgb( value, value, value ) ); // Grayscale
}
}

return image;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateContourMapPolygonTools::exportVectorAsImage( const std::vector<std::vector<int>>& data, int transparency, const QString& filename )
{
if ( data.empty() || data[0].empty() )
{
qWarning( "Data is empty. Cannot export an image." );
return;
}

auto image = convertBinaryToImage( data, QColorConstants::Green, transparency );

if ( !image.save( filename, "PNG" ) )
{
qWarning( "Failed to save image as PNG." );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateContourMapPolygonTools::exportVectorAsGrayscaleImage( const std::vector<std::vector<int>>& data, const QString& filename )
{
if ( data.empty() || data[0].empty() )
{
qWarning( "Data is empty. Cannot export an image." );
return;
}

auto image = convertBinaryToGrayscaleImage( data, 255 );

// Save the QImage as a PNG file
if ( !image.save( filename, "PNG" ) )
{
qWarning( "Failed to save image as PNG." );
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<std::vector<int>> RicCreateContourMapPolygonTools::convertImageToBinary( QImage image )
{
std::vector<std::vector<int>> binaryImage( image.width(), std::vector<int>( image.height(), 0 ) );
for ( int i = 0; i < image.width(); ++i )
{
for ( int j = 0; j < image.height(); ++j )
{
auto pixelColor = image.pixel( i, j );
auto gray = qGray( pixelColor );

binaryImage[i][image.height() - j - 1] = gray > 0 ? 1 : 0;
}
}
return binaryImage;
}
38 changes: 38 additions & 0 deletions ApplicationLibCode/Commands/RicCreateContourMapPolygonTools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024- 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.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include <QImage>
#include <QString>
#include <vector>

//==================================================================================================
///
//==================================================================================================
namespace RicCreateContourMapPolygonTools
{
QImage convertBinaryToImage( const std::vector<std::vector<int>>& data, QColor color, int transparency );
QImage convertBinaryToGrayscaleImage( const std::vector<std::vector<int>>& data, int colorValue );

void exportVectorAsImage( const std::vector<std::vector<int>>& data, int transparency, const QString& filename );
void exportVectorAsGrayscaleImage( const std::vector<std::vector<int>>& data, const QString& filename );

std::vector<std::vector<int>> convertImageToBinary( QImage image );

}; // namespace RicCreateContourMapPolygonTools
Loading

0 comments on commit 816d6d7

Please sign in to comment.