Salome HOME
3a16235768086f153cbd7a827527520778865a89
[modules/shaper.git] / src / ModelAPI / ModelAPI_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Tools.cpp
4 // Created:     06 Aug 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModelAPI_Tools.h"
8 #include <ModelAPI_Session.h>
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Object.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_ResultParameter.h>
13
14 #include <list>
15 #include <map>
16
17 namespace ModelAPI_Tools {
18
19 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
20 {
21 /*
22   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
23   if (aBody)
24     return aBody->shape();
25
26   ResultConstructionPtr aConstruct = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
27     theResult);
28   if (aConstruct)
29     return aConstruct->shape();
30
31   ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(theResult);
32   if (aGroup)
33     return aGroup->shape();
34   return std::shared_ptr<GeomAPI_Shape>();
35   */
36   return theResult->shape();
37 }
38
39 bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam)
40 {
41   SessionPtr aSession = ModelAPI_Session::get();
42   std::list<DocumentPtr> aDocList;
43   DocumentPtr aDocument = aSession->activeDocument();
44   DocumentPtr aRootDocument = aSession->moduleDocument();
45   aDocList.push_back(aDocument);
46   if (aDocument != aRootDocument) {
47     aDocList.push_back(aRootDocument);
48   }
49   for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
50     ObjectPtr aParamObj = (*it)->objectByName(ModelAPI_ResultParameter::group(), theName);
51     theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
52     if(!theParam.get())
53       continue;
54     AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
55     outValue = aValueAttribute->value();
56     return true;
57   }
58   return false;
59 }
60
61 static std::map<int, std::vector<int> > myColorMap;
62
63 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
64 {
65   theRGB.push_back(theRed);
66   theRGB.push_back(theGreen);
67   theRGB.push_back(theBlue);
68 }
69
70 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
71 {
72   std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
73   bool isFound = false;
74   for (; anIt != aLast && !isFound; anIt++) {
75     std::vector<int> aValues = anIt->second;
76     isFound = aValues[0] == theValues[0] &&
77               aValues[1] == theValues[1] &&
78               aValues[2] == theValues[2];
79   }
80   return isFound;
81 }
82
83 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
84 {
85   std::vector<int> aRGB;
86   if (theH < 0 || theH > 360 ||
87       theS < 0 || theS > 100 ||
88       theV < 0 || theV > 100)
89     return aRGB;
90
91   int aHi = (int)theH/60;
92
93   double aV = theV;
94   double aVmin = (100 - theS)*theV/100;
95
96   double anA = (theV - aVmin)* (theH % 60) / 60;
97
98   double aVinc = aVmin + anA;
99   double aVdec = theV - anA;
100
101   double aPercentToValue = 255./100;
102   int aV_int    = (int)(aV*aPercentToValue);
103   int aVinc_int = (int)(aVinc*aPercentToValue);
104   int aVmin_int = (int)(aVmin*aPercentToValue);
105   int aVdec_int = (int)(aVdec*aPercentToValue);
106
107   switch(aHi) {
108     case 0: appendValues(aRGB, aV_int,    aVinc_int, aVmin_int); break;
109     case 1: appendValues(aRGB, aVdec_int, aV_int,    aVmin_int); break;
110     case 2: appendValues(aRGB, aVmin_int, aV_int,    aVinc_int); break;
111     case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
112     case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
113     case 5: appendValues(aRGB, aV_int,    aVmin_int, aVdec_int); break;
114     default: break;
115   }
116   return aRGB;
117 }
118
119
120 void fillColorMap()
121 {
122   if (!myColorMap.empty())
123     return;
124
125   int i = 0;
126   for (int s = 100; s > 0; s = s - 50)
127   {
128     for (int v = 100; v >= 40; v = v - 20)
129     {
130       for (int h = 0; h < 359 ; h = h + 60)
131       {
132         std::vector<int> aColor = HSVtoRGB(h, s, v);
133         if (containsValues(myColorMap, aColor))
134           continue;
135         myColorMap[i] = aColor;
136         i++;
137       }
138     }
139   }
140 }
141
142 void findRandomColor(std::vector<int>& theValues)
143 {
144   theValues.clear();
145   if (myColorMap.empty()) {
146     fillColorMap();
147   }
148
149   int aSize = myColorMap.size();
150   int anIndex = rand() % aSize;
151   if (myColorMap.find(anIndex) != myColorMap.end()) {
152     theValues = myColorMap.at(anIndex);
153   }
154 }
155
156 } // namespace ModelAPI_Tools