Salome HOME
Fix for the issue #686: now compounds produced by the Boolean operations can be corre...
[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 #include <ModelAPI_ResultPart.h>
14 #include <ModelAPI_AttributeDocRef.h>
15 #include <list>
16 #include <map>
17
18 namespace ModelAPI_Tools {
19
20 std::shared_ptr<GeomAPI_Shape> shape(const ResultPtr& theResult)
21 {
22 /*
23   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theResult);
24   if (aBody)
25     return aBody->shape();
26
27   ResultConstructionPtr aConstruct = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
28     theResult);
29   if (aConstruct)
30     return aConstruct->shape();
31
32   ResultGroupPtr aGroup = std::dynamic_pointer_cast<ModelAPI_ResultGroup>(theResult);
33   if (aGroup)
34     return aGroup->shape();
35   return std::shared_ptr<GeomAPI_Shape>();
36   */
37   return theResult->shape();
38 }
39
40 bool findVariable(const std::string& theName, double& outValue, ResultParameterPtr& theParam)
41 {
42   SessionPtr aSession = ModelAPI_Session::get();
43   std::list<DocumentPtr> aDocList;
44   DocumentPtr aDocument = aSession->activeDocument();
45   DocumentPtr aRootDocument = aSession->moduleDocument();
46   aDocList.push_back(aDocument);
47   if (aDocument != aRootDocument) {
48     aDocList.push_back(aRootDocument);
49   }
50   for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
51     ObjectPtr aParamObj = (*it)->objectByName(ModelAPI_ResultParameter::group(), theName);
52     theParam = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aParamObj);
53     if(!theParam.get())
54       continue;
55     AttributeDoublePtr aValueAttribute = theParam->data()->real(ModelAPI_ResultParameter::VALUE());
56     outValue = aValueAttribute->value();
57     return true;
58   }
59   return false;
60 }
61
62 static std::map<int, std::vector<int> > myColorMap;
63
64 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
65 {
66   theRGB.push_back(theRed);
67   theRGB.push_back(theGreen);
68   theRGB.push_back(theBlue);
69 }
70
71 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
72 {
73   std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(), aLast = theColorMap.end();
74   bool isFound = false;
75   for (; anIt != aLast && !isFound; anIt++) {
76     std::vector<int> aValues = anIt->second;
77     isFound = aValues[0] == theValues[0] &&
78               aValues[1] == theValues[1] &&
79               aValues[2] == theValues[2];
80   }
81   return isFound;
82 }
83
84 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
85 {
86   std::vector<int> aRGB;
87   if (theH < 0 || theH > 360 ||
88       theS < 0 || theS > 100 ||
89       theV < 0 || theV > 100)
90     return aRGB;
91
92   int aHi = (int)theH/60;
93
94   double aV = theV;
95   double aVmin = (100 - theS)*theV/100;
96
97   double anA = (theV - aVmin)* (theH % 60) / 60;
98
99   double aVinc = aVmin + anA;
100   double aVdec = theV - anA;
101
102   double aPercentToValue = 255./100;
103   int aV_int    = (int)(aV*aPercentToValue);
104   int aVinc_int = (int)(aVinc*aPercentToValue);
105   int aVmin_int = (int)(aVmin*aPercentToValue);
106   int aVdec_int = (int)(aVdec*aPercentToValue);
107
108   switch(aHi) {
109     case 0: appendValues(aRGB, aV_int,    aVinc_int, aVmin_int); break;
110     case 1: appendValues(aRGB, aVdec_int, aV_int,    aVmin_int); break;
111     case 2: appendValues(aRGB, aVmin_int, aV_int,    aVinc_int); break;
112     case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
113     case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
114     case 5: appendValues(aRGB, aV_int,    aVmin_int, aVdec_int); break;
115     default: break;
116   }
117   return aRGB;
118 }
119
120
121 void fillColorMap()
122 {
123   if (!myColorMap.empty())
124     return;
125
126   int i = 0;
127   for (int s = 100; s > 0; s = s - 50)
128   {
129     for (int v = 100; v >= 40; v = v - 20)
130     {
131       for (int h = 0; h < 359 ; h = h + 60)
132       {
133         std::vector<int> aColor = HSVtoRGB(h, s, v);
134         if (containsValues(myColorMap, aColor))
135           continue;
136         myColorMap[i] = aColor;
137         i++;
138       }
139     }
140   }
141 }
142
143 void findRandomColor(std::vector<int>& theValues)
144 {
145   theValues.clear();
146   if (myColorMap.empty()) {
147     fillColorMap();
148   }
149
150   int aSize = myColorMap.size();
151   int anIndex = rand() % aSize;
152   if (myColorMap.find(anIndex) != myColorMap.end()) {
153     theValues = myColorMap.at(anIndex);
154   }
155 }
156
157 ResultPtr findPartResult(const DocumentPtr& theMain, const DocumentPtr& theSub)
158 {
159   for (int a = theMain->size(ModelAPI_ResultPart::group()) - 1; a >= 0; a--) {
160     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(
161         theMain->object(ModelAPI_ResultPart::group(), a));
162     if (aPart && aPart->data()->document(ModelAPI_ResultPart::DOC_REF())->value() == theSub) {
163       return aPart;
164     }
165   }
166   return ResultPtr();
167 }
168
169 } // namespace ModelAPI_Tools