Salome HOME
Correct roes processing
[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 <CollectionPlugin_Field.h>
23
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeIntArray.h>
26 #include <ModelAPI_AttributeStringArray.h>
27
28 #include <AIS_ColorScale.hxx>
29 #include <Prs3d_Drawer.hxx>
30 #include <Prs3d_PointAspect.hxx>
31
32
33 IMPLEMENT_STANDARD_RTTIEXT(PartSet_FieldStepPrs, ViewerData_AISShape);
34
35 #define POINT_SIZE 8
36
37
38 void emptyDeleter(ModelAPI_ResultField* theF)
39 {
40   // Do nothing
41 }
42
43 PartSet_FieldStepPrs::PartSet_FieldStepPrs(FieldStepPtr theStep)
44 : ViewerData_AISShape(TopoDS_Shape()), myStep(theStep)
45 {
46   ModelAPI_ResultField* aField = theStep->field();
47   GeomShapePtr aShapePtr = aField->shape();
48   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
49   Set(aShape);
50
51   // Get parameters of the Field
52   // Make shared_ptr which will not delete original pointer after exit
53   std::shared_ptr<ModelAPI_ResultField> aFieldPtr(aField, emptyDeleter);
54   myFeature = ModelAPI_Feature::feature(aFieldPtr);
55
56   Handle(Prs3d_Drawer) aDrawer = Attributes();
57   if (aDrawer->HasOwnPointAspect()) {
58     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_POINT);
59     aDrawer->PointAspect()->SetScale(POINT_SIZE);
60   }
61   else
62     aDrawer->SetPointAspect(
63       new Prs3d_PointAspect(Aspect_TOM_POINT, Quantity_NOC_YELLOW, POINT_SIZE));
64 }
65
66
67 ModelAPI_AttributeTables::ValueType PartSet_FieldStepPrs::dataType() const
68 {
69   DataPtr aData = myFeature->data();
70   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
71   return aTablesAttr->type();
72 }
73
74 bool PartSet_FieldStepPrs::dataRange(double& theMin, double& theMax) const
75 {
76   ModelAPI_AttributeTables::ValueType aType = dataType();
77   if ((aType == ModelAPI_AttributeTables::DOUBLE) || (aType == ModelAPI_AttributeTables::INTEGER)) {
78     range(theMin, theMax);
79     return true;
80   }
81   return false;
82 }
83
84 QList<double> PartSet_FieldStepPrs::range(double& theMin, double& theMax) const
85 {
86   ModelAPI_AttributeTables::ValueType aType = dataType();
87   DataPtr aData = myFeature->data();
88   int aStep = myStep->id();
89   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
90   int aRows = aTablesAttr->rows();
91   int aCols = aTablesAttr->columns();
92
93   QList<double> aFieldStepData;
94   for (int k = 1; k < aRows; k++) { // Do not use default values
95     for (int j = 0; j < aCols; j++) {
96       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
97       switch (aType) {
98       case ModelAPI_AttributeTables::DOUBLE:
99         aFieldStepData << aVal.myDouble;
100         break;
101       case ModelAPI_AttributeTables::INTEGER:
102         aFieldStepData << aVal.myInt;
103         break;
104       }
105     }
106   }
107   QList<double> aShapeData;
108   double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
109   for (int aRow = 0; aRow < aRows - 1; aRow++) {
110     double aNorm = 0;
111     int aBaseIndex = aRow * aCols;
112     for (int aCol = 0; aCol < aCols; aCol++) {
113       int anIndex = aCol + aBaseIndex;
114       double aValue = aFieldStepData.at(anIndex);
115       aNorm += aValue * aValue;
116     }
117     aNorm = pow(aNorm, 0.5);
118     aRangeMin = Min(aRangeMin, aNorm);
119     aRangeMax = Max(aRangeMax, aNorm);
120     aShapeData << aNorm;
121   }
122   theMin = aRangeMin;
123   theMax = aRangeMax;
124   return aShapeData;
125 }
126
127
128 void PartSet_FieldStepPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
129   const Handle(Prs3d_Presentation)& thePrs, const Standard_Integer theMode)
130 {
131   ModelAPI_AttributeTables::ValueType aType = dataType();
132   DataPtr aData = myFeature->data();
133   if ((aType == ModelAPI_AttributeTables::DOUBLE) ||
134     (aType == ModelAPI_AttributeTables::INTEGER)) {
135     double aMin, aMax;
136     QList<double> aShapeData = range(aMin, aMax);
137     int aNbIntertvals = 20;
138
139     AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
140     for (int i = 0; i < aSelList->size(); i++) {
141       AttributeSelectionPtr aSelection = aSelList->value(i);
142       GeomShapePtr aShapePtr = aSelection->value();
143       TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
144       double aValue = aShapeData.at(i);
145       Quantity_Color aColor;
146       if (AIS_ColorScale::FindColor(aValue, aMin, aMax, aNbIntertvals, aColor))
147         SetCustomColor(aShape, aColor);
148     }
149   }
150   else if (aType == ModelAPI_AttributeTables::BOOLEAN) {
151     QList<double> aShapeData = booleanValues();
152
153     AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
154     for (int i = 0; i < aSelList->size(); i++) {
155       AttributeSelectionPtr aSelection = aSelList->value(i);
156       GeomShapePtr aShapePtr = aSelection->value();
157       TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
158       double aValue = aShapeData.at(i);
159       Quantity_Color aColor;
160       if (AIS_ColorScale::FindColor(aValue, 0., 1., 2, aColor))
161         SetCustomColor(aShape, aColor);
162     }
163   }
164   ViewerData_AISShape::Compute(thePrsMgr, thePrs, theMode);
165 }
166
167 QList<double> PartSet_FieldStepPrs::booleanValues() const
168 {
169   DataPtr aData = myFeature->data();
170   int aStep = myStep->id();
171   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
172   int aRows = aTablesAttr->rows();
173   int aCols = aTablesAttr->columns();
174   QList<int> aFieldStepData;
175   for (int k = 1; k < aRows; k++) { // Do not use default values
176     for (int j = 0; j < aCols; j++) {
177       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
178       aFieldStepData << (aVal.myBool ? 1 : 0);
179     }
180   }
181   QList<double> aShapeData;
182   double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
183   for (int aRow = 0; aRow < aRows - 1; aRow++) {
184     double aNorm = 0;
185     int aBaseIndex = aRow * aCols;
186     for (int aCol = 0; aCol < aCols; aCol++) {
187       aNorm += aFieldStepData.at(aCol + aBaseIndex);
188     }
189     aNorm /= aCols;
190     aShapeData << aNorm;
191   }
192   return aShapeData;
193 }