Salome HOME
Copyright update 2020
[modules/shaper.git] / src / PartSet / PartSet_FieldStepPrs.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_FieldStepPrs.h"
21
22 #include <ModuleBase_Preferences.h>
23
24 #include <CollectionPlugin_Field.h>
25
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeIntArray.h>
28 #include <ModelAPI_AttributeStringArray.h>
29
30 #include <SUIT_ResourceMgr.h>
31
32 #include <AIS_ColorScale.hxx>
33 #include <Prs3d_Drawer.hxx>
34 #include <Prs3d_PointAspect.hxx>
35 #include <BRep_Tool.hxx>
36 #include <BRepAdaptor_Surface.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <Prs3d_Root.hxx>
41
42
43 IMPLEMENT_STANDARD_RTTIEXT(PartSet_FieldStepPrs, ViewerData_AISShape);
44
45 #define POINT_SIZE 8
46
47
48 void emptyDeleter(ModelAPI_ResultField* theF)
49 {
50   // Do nothing
51 }
52
53 PartSet_FieldStepPrs::PartSet_FieldStepPrs(FieldStepPtr theStep)
54 : ViewerData_AISShape(TopoDS_Shape()), myStep(theStep)
55 {
56   ModelAPI_ResultField* aField = theStep->field();
57   GeomShapePtr aShapePtr = aField->shape();
58   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
59   Set(aShape);
60
61   // Get parameters of the Field
62   // Make shared_ptr which will not delete original pointer after exit
63   std::shared_ptr<ModelAPI_ResultField> aFieldPtr(aField, emptyDeleter);
64   myFeature = ModelAPI_Feature::feature(aFieldPtr);
65
66   if (dataType() != ModelAPI_AttributeTables::STRING) {
67     Handle(Prs3d_Drawer) aDrawer = Attributes();
68     if (aDrawer->HasOwnPointAspect()) {
69       aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_POINT);
70       aDrawer->PointAspect()->SetScale(POINT_SIZE);
71     }
72     else
73       aDrawer->SetPointAspect(
74         new Prs3d_PointAspect(Aspect_TOM_POINT, Quantity_NOC_YELLOW, POINT_SIZE));
75   }
76   SetMaterial(Graphic3d_NOM_PLASTIC);
77 }
78
79
80 ModelAPI_AttributeTables::ValueType PartSet_FieldStepPrs::dataType() const
81 {
82   DataPtr aData = myFeature->data();
83   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
84   return aTablesAttr->type();
85 }
86
87 bool PartSet_FieldStepPrs::dataRange(double& theMin, double& theMax) const
88 {
89   ModelAPI_AttributeTables::ValueType aType = dataType();
90   if ((aType == ModelAPI_AttributeTables::DOUBLE) || (aType == ModelAPI_AttributeTables::INTEGER)) {
91     range(theMin, theMax);
92     return true;
93   }
94   return false;
95 }
96
97 QList<double> PartSet_FieldStepPrs::range(double& theMin, double& theMax) const
98 {
99   ModelAPI_AttributeTables::ValueType aType = dataType();
100   DataPtr aData = myFeature->data();
101   AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
102   std::string aTypeStr = aSelList->selectionType();
103
104   int aStep = myStep->id();
105   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
106   int aRows = aTablesAttr->rows();
107   int aCols = aTablesAttr->columns();
108
109   QList<double> aFieldStepData;
110   int aStart = (aTypeStr == "part")? 0:1;
111   for (int k = aStart; k < aRows; k++) { // Do not use default values
112     for (int j = 0; j < aCols; j++) {
113       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
114       switch (aType) {
115       case ModelAPI_AttributeTables::DOUBLE:
116         aFieldStepData << aVal.myDouble;
117         break;
118       case ModelAPI_AttributeTables::INTEGER:
119         aFieldStepData << aVal.myInt;
120         break;
121       }
122     }
123   }
124   QList<double> aShapeData;
125   double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
126   if (aCols == 1) {
127     for (int aRow = 0; aRow < aRows - aStart; aRow++) {
128       double aValue = aFieldStepData.at(aRow);
129       aRangeMin = Min(aRangeMin, aValue);
130       aRangeMax = Max(aRangeMax, aValue);
131       aShapeData << aValue;
132     }
133   }
134   else {
135     for (int aRow = 0; aRow < aRows - aStart; aRow++) {
136       double aNorm = 0;
137       int aBaseIndex = aRow * aCols;
138       for (int aCol = 0; aCol < aCols; aCol++) {
139         int anIndex = aCol + aBaseIndex;
140         double aValue = aFieldStepData.at(anIndex);
141         aNorm += aValue * aValue;
142       }
143       aNorm = pow(aNorm, 0.5);
144       aRangeMin = Min(aRangeMin, aNorm);
145       aRangeMax = Max(aRangeMax, aNorm);
146       aShapeData << aNorm;
147     }
148   }
149   theMin = aRangeMin;
150   theMax = aRangeMax;
151   return aShapeData;
152 }
153
154
155 void PartSet_FieldStepPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
156   const Handle(Prs3d_Presentation)& thePrs, const Standard_Integer theMode)
157 {
158   SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
159   QColor aQColor = aResMgr->colorValue("Viewer", "scalar_bar_text_color", Qt::black);
160   Quantity_Color aLabelColor = Quantity_Color(aQColor.redF(), aQColor.greenF(), aQColor.blueF(),
161     Quantity_TOC_RGB);
162
163   ModelAPI_AttributeTables::ValueType aType = dataType();
164   DataPtr aData = myFeature->data();
165   switch (aType) {
166   case ModelAPI_AttributeTables::DOUBLE:
167   case ModelAPI_AttributeTables::INTEGER:
168   {
169     double aMin, aMax;
170     QList<double> aShapeData = range(aMin, aMax);
171
172     AttributeSelectionListPtr aSelList =
173       aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
174     std::string aTypeStr = aSelList->selectionType();
175     if (aTypeStr == "part") {
176       Quantity_Color aColor;
177       if (AIS_ColorScale::FindColor(aMin, aMin, aMax, 1, aColor)) {
178         SetColor(aColor);
179       }
180     }
181     else {
182       int aNbIntertvals = aResMgr->integerValue("Viewer", "scalar_bar_nb_intervals", 20);
183       for (int i = 0; i < aSelList->size(); i++) {
184         AttributeSelectionPtr aSelection = aSelList->value(i);
185         GeomShapePtr aShapePtr = aSelection->value();
186         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
187         double aValue = aShapeData.at(i);
188         Quantity_Color aColor;
189         if (AIS_ColorScale::FindColor(aValue, aMin, aMax, aNbIntertvals, aColor))
190           SetCustomColor(aShape, aColor);
191       }
192     }
193   }
194   break;
195   case ModelAPI_AttributeTables::BOOLEAN:
196   {
197     QList<double> aShapeData = booleanValues();
198
199     AttributeSelectionListPtr aSelList =
200       aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
201     for (int i = 0; i < aSelList->size(); i++) {
202       AttributeSelectionPtr aSelection = aSelList->value(i);
203       GeomShapePtr aShapePtr = aSelection->value();
204       TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
205       double aValue = aShapeData.at(i);
206       Quantity_Color aColor;
207       if (AIS_ColorScale::FindColor(aValue, 0., 1., 2, aColor))
208         SetCustomColor(aShape, aColor);
209     }
210   }
211   break;
212   case ModelAPI_AttributeTables::STRING:
213   {
214     QStringList aValues = strings();
215     AttributeSelectionListPtr aSelList =
216       aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
217     Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
218     for (int i = 0; i < aSelList->size(); i++) {
219       AttributeSelectionPtr aSelection = aSelList->value(i);
220       GeomShapePtr aShapePtr = aSelection->value();
221       TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
222       gp_Pnt aCenter;
223       if (computeMassCenter(aShape, aCenter)) {
224         Graphic3d_Vertex aVertex(aCenter.X(), aCenter.Y(), aCenter.Z());
225
226         Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
227         anAspectText3d->SetStyle(Aspect_TOST_ANNOTATION);
228         anAspectText3d->SetColor(aLabelColor);
229         aGroup->SetPrimitivesAspect(anAspectText3d);
230
231         int aT = aResMgr->integerValue("Viewer", "scalar_bar_text_height", 14);
232         QString aString = aValues.at(i);
233         aGroup->Text(aString.toUtf8().constData(), aVertex, aT);
234       }
235     }
236   }
237   break;
238   }
239   ViewerData_AISShape::Compute(thePrsMgr, thePrs, theMode);
240 }
241
242 QList<double> PartSet_FieldStepPrs::booleanValues() const
243 {
244   DataPtr aData = myFeature->data();
245   int aStep = myStep->id();
246   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
247   int aRows = aTablesAttr->rows();
248   int aCols = aTablesAttr->columns();
249   QList<int> aFieldStepData;
250   for (int k = 1; k < aRows; k++) { // Do not use default values
251     for (int j = 0; j < aCols; j++) {
252       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
253       aFieldStepData << (aVal.myBool ? 1 : 0);
254     }
255   }
256   QList<double> aShapeData;
257   double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
258   for (int aRow = 0; aRow < aRows - 1; aRow++) {
259     double aNorm = 0;
260     int aBaseIndex = aRow * aCols;
261     for (int aCol = 0; aCol < aCols; aCol++) {
262       aNorm += aFieldStepData.at(aCol + aBaseIndex);
263     }
264     aNorm /= aCols;
265     aShapeData << aNorm;
266   }
267   return aShapeData;
268 }
269
270 QStringList PartSet_FieldStepPrs::strings() const
271 {
272   DataPtr aData = myFeature->data();
273   int aStep = myStep->id();
274   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
275   int aRows = aTablesAttr->rows();
276   int aCols = aTablesAttr->columns();
277   QStringList aFieldStepData;
278   for (int k = 1; k < aRows; k++) { // Do not use default values
279     for (int j = 0; j < aCols; j++) {
280       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
281       aFieldStepData << aVal.myStr.c_str();
282     }
283   }
284   QStringList aShapeData;
285   for (int aRow = 0; aRow < aRows - 1; aRow++) {
286     QStringList aRowStrings;
287     int aBaseIndex = aRow * aCols;
288     for (int aCol = 0; aCol < aCols; aCol++) {
289       aRowStrings << aFieldStepData.at(aCol + aBaseIndex);
290     }
291     aRowStrings.join('\n');
292     aShapeData << aRowStrings;
293   }
294   return aShapeData;
295 }
296
297 bool PartSet_FieldStepPrs::computeMassCenter(const TopoDS_Shape& theShape, gp_Pnt& theCenter)
298 {
299   theCenter.SetCoord(0, 0, 0);
300   int aNbPoints = 0;
301
302   if (theShape.ShapeType() == TopAbs_EDGE) {
303     double f, l;
304     Handle(Geom_Curve) curve = BRep_Tool::Curve(TopoDS::Edge(theShape), f, l);
305     if (!curve.IsNull()) {
306       theCenter = curve->Value(0.5 * (f + l));
307       aNbPoints = 1;
308     }
309   }
310   else if (theShape.ShapeType() == TopAbs_FACE) {
311     const TopoDS_Face& F = TopoDS::Face(theShape);
312     BRepAdaptor_Surface surface(F);
313
314     TopLoc_Location L;
315     Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation(F, L);
316     if (!triangulation.IsNull() && triangulation->HasUVNodes()) {
317       gp_XY C(0, 0);
318       double A = 0;
319       const TColgp_Array1OfPnt2d& uvArray = triangulation->UVNodes();
320       const Poly_Array1OfTriangle&  trias = triangulation->Triangles();
321       int n1, n2, n3;
322       for (int iT = trias.Lower(); iT <= trias.Upper(); ++iT) {
323         trias(iT).Get(n1, n2, n3);
324         const gp_Pnt2d& uv1 = uvArray(n1);
325         const gp_Pnt2d& uv2 = uvArray(n2);
326         const gp_Pnt2d& uv3 = uvArray(n3);
327         double a = 0.5 * sqrt((uv1.X() - uv3.X()) * (uv2.Y() - uv1.Y()) -
328           (uv1.X() - uv2.X()) * (uv3.Y() - uv1.Y()));
329         C += (uv1.XY() + uv2.XY() + uv3.XY()) / 3. * a;
330         A += a;
331       }
332       if (A > std::numeric_limits<double>::min()) {
333         C /= A;
334         theCenter = surface.Value(C.X(), C.Y());
335         aNbPoints = 1;
336       }
337     }
338     if (aNbPoints == 0) {
339       theCenter = surface.Value(0.5 * (surface.FirstUParameter() + surface.LastUParameter()),
340         0.5 * (surface.FirstVParameter() + surface.LastVParameter()));
341     }
342     aNbPoints = 1;
343   }
344
345   if (aNbPoints == 0) {
346     TopExp_Explorer anExp;
347     for (anExp.Init(theShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
348       TopoDS_Vertex aVertex = TopoDS::Vertex(anExp.Current());
349       if (!aVertex.IsNull()) {
350         gp_Pnt aPnt = BRep_Tool::Pnt(aVertex);
351         theCenter.ChangeCoord() += aPnt.XYZ();
352         aNbPoints++;
353       }
354     }
355   }
356
357   if (aNbPoints > 0)
358     theCenter.ChangeCoord() /= (double)aNbPoints;
359
360   return aNbPoints;
361 }