#5960 Roff import: pick colors from best matching items in Rock Type color legend.

This commit is contained in:
Kristian Bendiksen 2020-05-24 11:53:54 +02:00 committed by Magne Sjaastad
parent 8a0f911856
commit f9598cfe7c
2 changed files with 77 additions and 5 deletions

View File

@ -21,6 +21,7 @@
#include "RiaApplication.h"
#include "RiaColorTables.h"
#include "RiaLogging.h"
#include "RiaStdStringTools.h"
#include "RimColorLegend.h"
#include "RimColorLegendCollection.h"
@ -76,21 +77,29 @@ void RicImportFaciesFeature::onActionTriggered( bool isChecked )
return;
}
// TODO: try to map names of facies to a sensible color
const caf::ColorTable& colorTable = RiaColorTables::contrastCategoryPaletteColors();
const caf::ColorTable& colorTable = RiaColorTables::contrastCategoryPaletteColors();
RimColorLegendCollection* colorLegendCollection = RimProject::current()->colorLegendCollection;
RimColorLegend* rockTypeColorLegend = colorLegendCollection->findByName( "Rock Types" );
RimColorLegend* colorLegend = new RimColorLegend;
colorLegend->setColorLegendName( "Facies colors" );
// Iterate over the map using Iterator till end.
for ( auto it : codeNames )
{
RimColorLegendItem* colorLegendItem = new RimColorLegendItem;
colorLegendItem->setValues( it.second, it.first, colorTable.cycledColor3f( it.first ) );
// Try to find a color from the rock type color legend by fuzzy matching names
cvf::Color3f color;
if ( !matchByName( it.second, rockTypeColorLegend, color ) )
{
// No match use a random color
color = colorTable.cycledColor3f( it.first );
}
colorLegendItem->setValues( it.second, it.first, color );
colorLegend->appendColorLegendItem( colorLegendItem );
}
RimColorLegendCollection* colorLegendCollection = RimProject::current()->colorLegendCollection;
colorLegendCollection->appendCustomColorLegend( colorLegend );
colorLegendCollection->updateConnectedEditors();
}
@ -104,3 +113,56 @@ void RicImportFaciesFeature::setupActionLook( QAction* actionToSetup )
// actionToSetup->setIcon( QIcon( ":/Formations16x16.png" ) );
actionToSetup->setText( "Import Facies" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicImportFaciesFeature::matchByName( const QString name, RimColorLegend* colorLegend, cvf::Color3f& color )
{
// No match if color legend does not exist
if ( !colorLegend ) return false;
// Find the best matching name from the legend
int bestScore = std::numeric_limits<int>::max();
RimColorLegendItem* bestItem = nullptr;
for ( RimColorLegendItem* item : colorLegend->colorLegendItems() )
{
int score = computeEditDistance( name, item->categoryName() );
if ( score < bestScore )
{
bestScore = score;
bestItem = item;
}
}
// Allow only small difference when determining if something matches
const int maximumScoreToMatch = 1;
if ( bestScore <= maximumScoreToMatch )
{
color = bestItem->color();
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RicImportFaciesFeature::computeEditDistance( const QString& a, const QString& b )
{
// Remove common words from the domain which does not help in the matching
std::vector<QString> stopWords = { "rocks", "rock", "stones", "stone" };
QString aSimplified = a.toLower();
QString bSimplified = b.toLower();
for ( auto r : stopWords )
{
aSimplified.remove( r );
bSimplified.remove( r );
}
aSimplified = aSimplified.trimmed();
bSimplified = bSimplified.trimmed();
return RiaStdStringTools::computeEditDistance( aSimplified.toStdString(), bSimplified.toStdString() );
}

View File

@ -20,6 +20,12 @@
#include "cafCmdFeature.h"
class RimColorLegend;
namespace cvf
{
class Color3f;
}
//==================================================================================================
///
//==================================================================================================
@ -31,4 +37,8 @@ protected:
bool isCommandEnabled() override;
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
private:
int computeEditDistance( const QString& a, const QString& b );
bool matchByName( const QString name, RimColorLegend* colorLegend, cvf::Color3f& color );
};