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