From 3de7efb71b474ebe1c6c33c9f5475a5f1fa09250 Mon Sep 17 00:00:00 2001 From: Nicolas Rechatin Date: Tue, 31 Aug 2021 12:04:27 +0200 Subject: [PATCH] feature AutoColor Alogirthm rework --- src/ModelAPI/ModelAPI_Tools.cpp | 98 ++++++++++++++------------------- src/XGUI/XGUI_Workshop.cpp | 3 +- 2 files changed, 41 insertions(+), 60 deletions(-) diff --git a/src/ModelAPI/ModelAPI_Tools.cpp b/src/ModelAPI/ModelAPI_Tools.cpp index a669033db..482d4ac1a 100644 --- a/src/ModelAPI/ModelAPI_Tools.cpp +++ b/src/ModelAPI/ModelAPI_Tools.cpp @@ -17,6 +17,8 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // +#include + #include "ModelAPI_Tools.h" #include #include @@ -1116,30 +1118,13 @@ std::list referencedFeatures( return aResList; } -// contains global cash for integer index of the color -> RGB of this color -static std::map > myColorMap; - -void appendValues(std::vector& theRGB, const int theRed, const int theGreen, const int theBlue) +void setValues(std::vector& theRGB, const int theRed, const int theGreen, const int theBlue) { theRGB.push_back(theRed); theRGB.push_back(theGreen); theRGB.push_back(theBlue); } -bool containsValues(std::map >& theColorMap, std::vector& theValues) -{ - std::map >::const_iterator anIt = theColorMap.begin(), - aLast = theColorMap.end(); - bool isFound = false; - for (; anIt != aLast && !isFound; anIt++) { - std::vector aValues = anIt->second; - isFound = aValues[0] == theValues[0] && - aValues[1] == theValues[1] && - aValues[2] == theValues[2]; - } - return isFound; -} - std::vector HSVtoRGB(int theH, int theS, int theV) { std::vector aRGB; @@ -1149,15 +1134,11 @@ std::vector HSVtoRGB(int theH, int theS, int theV) return aRGB; int aHi = (int)theH/60; - double aV = theV; double aVmin = (100 - theS)*theV/100; - double anA = (theV - aVmin)* (theH % 60) / 60; - double aVinc = aVmin + anA; double aVdec = theV - anA; - double aPercentToValue = 255./100; int aV_int = (int)(aV*aPercentToValue); int aVinc_int = (int)(aVinc*aPercentToValue); @@ -1165,51 +1146,52 @@ std::vector HSVtoRGB(int theH, int theS, int theV) int aVdec_int = (int)(aVdec*aPercentToValue); switch(aHi) { - case 0: appendValues(aRGB, aV_int, aVinc_int, aVmin_int); break; - case 1: appendValues(aRGB, aVdec_int, aV_int, aVmin_int); break; - case 2: appendValues(aRGB, aVmin_int, aV_int, aVinc_int); break; - case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break; - case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break; - case 5: appendValues(aRGB, aV_int, aVmin_int, aVdec_int); break; + case 0: setValues(aRGB, aV_int, aVinc_int, aVmin_int); break; + case 1: setValues(aRGB, aVdec_int, aV_int, aVmin_int); break; + case 2: setValues(aRGB, aVmin_int, aV_int, aVinc_int); break; + case 3: setValues(aRGB, aVmin_int, aVdec_int, aV_int); break; + case 4: setValues(aRGB, aVinc_int, aVmin_int, aV_int); break; + case 5: setValues(aRGB, aV_int, aVmin_int, aVdec_int); break; default: break; } return aRGB; } - -void fillColorMap() -{ - if (!myColorMap.empty()) - return; - - int i = 0; - for (int s = 100; s > 0; s = s - 50) - { - for (int v = 100; v >= 40; v = v - 20) - { - for (int h = 0; h < 359 ; h = h + 60) - { - std::vector aColor = HSVtoRGB(h, s, v); - if (containsValues(myColorMap, aColor)) - continue; - myColorMap[i] = aColor; - i++; - } - } - } -} +std::array, 10> myColorTab = { + std::vector {255, 0, 0}, + std::vector {0, 255, 0}, + std::vector {0, 0, 255}, + std::vector {255, 255, 0}, + std::vector {0, 255, 255}, + std::vector {255, 0, 255}, + std::vector {255, 94, 0}, + std::vector {132, 255, 0}, + std::vector {132, 0, 255}, + std::vector {0, 0, 0}, +}; void findRandomColor(std::vector& theValues) { - theValues.clear(); - if (myColorMap.empty()) { - fillColorMap(); - } + static int i = 0; + static std::vector> usedGeneratedColor; - size_t aSize = myColorMap.size(); - int anIndex = rand() % aSize; - if (myColorMap.find(anIndex) != myColorMap.end()) { - theValues = myColorMap.at(anIndex); + theValues.clear(); + if (i < myColorTab.size()) { + theValues = myColorTab[i++]; + } else { + int timeout = 0; + std::vector aHSVColor; + std::vector aRGBColor; + + do { + aHSVColor = {rand() % 360 , rand()%(100 - 75 + 1) + 60, rand()%(100 - 60 + 1) + 75}; + aRGBColor = HSVtoRGB(aHSVColor[0], aHSVColor[1], aHSVColor[2]); + timeout++; + } while (timeout < 20 && + std::find(usedGeneratedColor.begin(), usedGeneratedColor.end(), aHSVColor) != usedGeneratedColor.end() && + std::find(myColorTab.begin(), myColorTab.end(), aRGBColor) != myColorTab.end()); + usedGeneratedColor.push_back(aHSVColor); + theValues = aRGBColor; } } diff --git a/src/XGUI/XGUI_Workshop.cpp b/src/XGUI/XGUI_Workshop.cpp index 0e0277085..74731815a 100644 --- a/src/XGUI/XGUI_Workshop.cpp +++ b/src/XGUI/XGUI_Workshop.cpp @@ -2541,7 +2541,6 @@ void XGUI_Workshop::changeAutoColor(const QObjectPtrList& theObjects) return; std::vector aColor; - ModelAPI_Tools::findRandomColor(aColor); // abort the previous operation and start a new one SessionPtr aMgr = ModelAPI_Session::get(); @@ -2552,7 +2551,7 @@ void XGUI_Workshop::changeAutoColor(const QObjectPtrList& theObjects) if (aProp) { bool anIsAutoColor = Config_PropManager::boolean("Visualization", "result_group_Auto_color"); - + if (anIsAutoColor) { contextMenuMgr()->action("AUTOCOLOR_CMD")->setText(tr("Auto color")); aProp->setValue("false"); -- 2.39.2