]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_FieldStepPrs.cpp
Salome HOME
Issue #2966: Redisplay text field on preferences change
[modules/shaper.git] / src / PartSet / PartSet_FieldStepPrs.cpp
1 // Copyright (C) 2014-2019  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   int aStep = myStep->id();
102   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
103   int aRows = aTablesAttr->rows();
104   int aCols = aTablesAttr->columns();
105
106   QList<double> aFieldStepData;
107   for (int k = 1; k < aRows; k++) { // Do not use default values
108     for (int j = 0; j < aCols; j++) {
109       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
110       switch (aType) {
111       case ModelAPI_AttributeTables::DOUBLE:
112         aFieldStepData << aVal.myDouble;
113         break;
114       case ModelAPI_AttributeTables::INTEGER:
115         aFieldStepData << aVal.myInt;
116         break;
117       }
118     }
119   }
120   QList<double> aShapeData;
121   double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
122   for (int aRow = 0; aRow < aRows - 1; aRow++) {
123     double aNorm = 0;
124     int aBaseIndex = aRow * aCols;
125     for (int aCol = 0; aCol < aCols; aCol++) {
126       int anIndex = aCol + aBaseIndex;
127       double aValue = aFieldStepData.at(anIndex);
128       aNorm += aValue * aValue;
129     }
130     aNorm = pow(aNorm, 0.5);
131     aRangeMin = Min(aRangeMin, aNorm);
132     aRangeMax = Max(aRangeMax, aNorm);
133     aShapeData << aNorm;
134   }
135   theMin = aRangeMin;
136   theMax = aRangeMax;
137   return aShapeData;
138 }
139
140
141 void PartSet_FieldStepPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
142   const Handle(Prs3d_Presentation)& thePrs, const Standard_Integer theMode)
143 {
144   SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
145   QColor aQColor = aResMgr->colorValue("Viewer", "scalar_bar_text_color", Qt::black);
146   Quantity_Color aLabelColor = Quantity_Color(aQColor.redF(), aQColor.greenF(), aQColor.blueF(),
147     Quantity_TOC_RGB);
148
149   ModelAPI_AttributeTables::ValueType aType = dataType();
150   DataPtr aData = myFeature->data();
151   switch (aType) {
152   case ModelAPI_AttributeTables::DOUBLE:
153   case ModelAPI_AttributeTables::INTEGER:
154   {
155     double aMin, aMax;
156     QList<double> aShapeData = range(aMin, aMax);
157     int aNbIntertvals = aResMgr->integerValue("Viewer", "scalar_bar_nb_intervals", 20);
158
159     AttributeSelectionListPtr aSelList =
160       aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
161     for (int i = 0; i < aSelList->size(); i++) {
162       AttributeSelectionPtr aSelection = aSelList->value(i);
163       GeomShapePtr aShapePtr = aSelection->value();
164       TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
165       double aValue = aShapeData.at(i);
166       Quantity_Color aColor;
167       if (AIS_ColorScale::FindColor(aValue, aMin, aMax, aNbIntertvals, aColor))
168         SetCustomColor(aShape, aColor);
169     }
170   }
171   break;
172   case ModelAPI_AttributeTables::BOOLEAN:
173   {
174     QList<double> aShapeData = booleanValues();
175
176     AttributeSelectionListPtr aSelList =
177       aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
178     for (int i = 0; i < aSelList->size(); i++) {
179       AttributeSelectionPtr aSelection = aSelList->value(i);
180       GeomShapePtr aShapePtr = aSelection->value();
181       TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
182       double aValue = aShapeData.at(i);
183       Quantity_Color aColor;
184       if (AIS_ColorScale::FindColor(aValue, 0., 1., 2, aColor))
185         SetCustomColor(aShape, aColor);
186     }
187   }
188   break;
189   case ModelAPI_AttributeTables::STRING:
190   {
191     QStringList aValues = strings();
192     AttributeSelectionListPtr aSelList =
193       aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
194     Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
195     for (int i = 0; i < aSelList->size(); i++) {
196       AttributeSelectionPtr aSelection = aSelList->value(i);
197       GeomShapePtr aShapePtr = aSelection->value();
198       TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
199       gp_Pnt aCenter;
200       if (computeMassCenter(aShape, aCenter)) {
201         Graphic3d_Vertex aVertex(aCenter.X(), aCenter.Y(), aCenter.Z());
202
203         Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
204         anAspectText3d->SetStyle(Aspect_TOST_ANNOTATION);
205         anAspectText3d->SetColor(aLabelColor);
206         aGroup->SetPrimitivesAspect(anAspectText3d);
207
208         int aT = aResMgr->integerValue("Viewer", "scalar_bar_text_height", 14);
209         QString aString = aValues.at(i);
210         aGroup->Text(aString.toUtf8().constData(), aVertex, aT);
211       }
212     }
213   }
214   break;
215   }
216   ViewerData_AISShape::Compute(thePrsMgr, thePrs, theMode);
217 }
218
219 QList<double> PartSet_FieldStepPrs::booleanValues() const
220 {
221   DataPtr aData = myFeature->data();
222   int aStep = myStep->id();
223   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
224   int aRows = aTablesAttr->rows();
225   int aCols = aTablesAttr->columns();
226   QList<int> aFieldStepData;
227   for (int k = 1; k < aRows; k++) { // Do not use default values
228     for (int j = 0; j < aCols; j++) {
229       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
230       aFieldStepData << (aVal.myBool ? 1 : 0);
231     }
232   }
233   QList<double> aShapeData;
234   double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
235   for (int aRow = 0; aRow < aRows - 1; aRow++) {
236     double aNorm = 0;
237     int aBaseIndex = aRow * aCols;
238     for (int aCol = 0; aCol < aCols; aCol++) {
239       aNorm += aFieldStepData.at(aCol + aBaseIndex);
240     }
241     aNorm /= aCols;
242     aShapeData << aNorm;
243   }
244   return aShapeData;
245 }
246
247 QStringList PartSet_FieldStepPrs::strings() const
248 {
249   DataPtr aData = myFeature->data();
250   int aStep = myStep->id();
251   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
252   int aRows = aTablesAttr->rows();
253   int aCols = aTablesAttr->columns();
254   QStringList aFieldStepData;
255   for (int k = 1; k < aRows; k++) { // Do not use default values
256     for (int j = 0; j < aCols; j++) {
257       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
258       aFieldStepData << aVal.myStr.c_str();
259     }
260   }
261   QStringList aShapeData;
262   for (int aRow = 0; aRow < aRows - 1; aRow++) {
263     QStringList aRowStrings;
264     int aBaseIndex = aRow * aCols;
265     for (int aCol = 0; aCol < aCols; aCol++) {
266       aRowStrings << aFieldStepData.at(aCol + aBaseIndex);
267     }
268     aRowStrings.join('\n');
269     aShapeData << aRowStrings;
270   }
271   return aShapeData;
272 }
273
274 bool PartSet_FieldStepPrs::computeMassCenter(const TopoDS_Shape& theShape, gp_Pnt& theCenter)
275 {
276   theCenter.SetCoord(0, 0, 0);
277   int aNbPoints = 0;
278
279   if (theShape.ShapeType() == TopAbs_EDGE) {
280     double f, l;
281     Handle(Geom_Curve) curve = BRep_Tool::Curve(TopoDS::Edge(theShape), f, l);
282     if (!curve.IsNull()) {
283       theCenter = curve->Value(0.5 * (f + l));
284       aNbPoints = 1;
285     }
286   }
287   else if (theShape.ShapeType() == TopAbs_FACE) {
288     const TopoDS_Face& F = TopoDS::Face(theShape);
289     BRepAdaptor_Surface surface(F);
290
291     TopLoc_Location L;
292     Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation(F, L);
293     if (!triangulation.IsNull() && triangulation->HasUVNodes()) {
294       gp_XY C(0, 0);
295       double A = 0;
296       const TColgp_Array1OfPnt2d& uvArray = triangulation->UVNodes();
297       const Poly_Array1OfTriangle&  trias = triangulation->Triangles();
298       int n1, n2, n3;
299       for (int iT = trias.Lower(); iT <= trias.Upper(); ++iT) {
300         trias(iT).Get(n1, n2, n3);
301         const gp_Pnt2d& uv1 = uvArray(n1);
302         const gp_Pnt2d& uv2 = uvArray(n2);
303         const gp_Pnt2d& uv3 = uvArray(n3);
304         double a = 0.5 * sqrt((uv1.X() - uv3.X()) * (uv2.Y() - uv1.Y()) -
305           (uv1.X() - uv2.X()) * (uv3.Y() - uv1.Y()));
306         C += (uv1.XY() + uv2.XY() + uv3.XY()) / 3. * a;
307         A += a;
308       }
309       if (A > std::numeric_limits<double>::min()) {
310         C /= A;
311         theCenter = surface.Value(C.X(), C.Y());
312         aNbPoints = 1;
313       }
314     }
315     if (aNbPoints == 0) {
316       theCenter = surface.Value(0.5 * (surface.FirstUParameter() + surface.LastUParameter()),
317         0.5 * (surface.FirstVParameter() + surface.LastVParameter()));
318     }
319     aNbPoints = 1;
320   }
321
322   if (aNbPoints == 0) {
323     TopExp_Explorer anExp;
324     for (anExp.Init(theShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
325       TopoDS_Vertex aVertex = TopoDS::Vertex(anExp.Current());
326       if (!aVertex.IsNull()) {
327         gp_Pnt aPnt = BRep_Tool::Pnt(aVertex);
328         theCenter.ChangeCoord() += aPnt.XYZ();
329         aNbPoints++;
330       }
331     }
332   }
333
334   if (aNbPoints > 0)
335     theCenter.ChangeCoord() /= (double)aNbPoints;
336
337   return aNbPoints;
338 }