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