]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_FieldStepPrs.cpp
Salome HOME
Implement colored shape
[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
30
31 IMPLEMENT_STANDARD_RTTIEXT(PartSet_FieldStepPrs, ViewerData_AISShape);
32
33
34 void emptyDeleter(ModelAPI_ResultField* theF)
35 {
36   // Do nothing
37 }
38
39 PartSet_FieldStepPrs::PartSet_FieldStepPrs(FieldStepPtr theStep)
40 : ViewerData_AISShape(TopoDS_Shape()), myStep(theStep)
41 {
42   ModelAPI_ResultField* aField = theStep->field();
43   GeomShapePtr aShapePtr = aField->shape();
44   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
45   Set(aShape);
46
47   // Get parameters of the Field
48   // Make shared_ptr which will not delete original pointer after exit
49   std::shared_ptr<ModelAPI_ResultField> aFieldPtr(aField, emptyDeleter);
50   myFeature = ModelAPI_Feature::feature(aFieldPtr);
51 }
52
53
54 ModelAPI_AttributeTables::ValueType PartSet_FieldStepPrs::dataType() const
55 {
56   DataPtr aData = myFeature->data();
57   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
58   return aTablesAttr->type();
59 }
60
61 bool PartSet_FieldStepPrs::dataRange(double& theMin, double& theMax) const
62 {
63   ModelAPI_AttributeTables::ValueType aType = dataType();
64   if ((aType == ModelAPI_AttributeTables::DOUBLE) || (aType == ModelAPI_AttributeTables::INTEGER)) {
65     range(theMin, theMax);
66     return true;
67   }
68   return false;
69 }
70
71 QList<double> PartSet_FieldStepPrs::range(double& theMin, double& theMax) const
72 {
73   ModelAPI_AttributeTables::ValueType aType = dataType();
74   DataPtr aData = myFeature->data();
75   int aStep = myStep->id();
76   AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
77   int aRows = aTablesAttr->rows();
78   int aCols = aTablesAttr->columns();
79
80   QList<double> aFieldStepData;
81   for (int j = 0; j < aCols; j++) {
82     for (int k = 1; k < aRows; k++) { // Do not use default values
83       ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
84       switch (aType) {
85       case ModelAPI_AttributeTables::DOUBLE:
86         aFieldStepData << aVal.myDouble;
87         break;
88       case ModelAPI_AttributeTables::INTEGER:
89         aFieldStepData << aVal.myInt;
90         break;
91       }
92     }
93   }
94   QList<double> aShapeData;
95   double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
96   for (int aRow = 0; aRow < aRows - 1; aRow++) {
97     double aNorm = 0;
98     int aBaseIndex = aRow * aCols;
99     for (int aCol = 0; aCol < aCols; aCol++) {
100       int anIndex = aCol + aBaseIndex;
101       double aValue = aFieldStepData.at(anIndex);
102       aNorm += aValue * aValue;
103     }
104     aNorm = pow(aNorm, 0.5);
105     aRangeMin = Min(aRangeMin, aNorm);
106     aRangeMax = Max(aRangeMax, aNorm);
107     aShapeData << aNorm;
108   }
109   theMin = aRangeMin;
110   theMax = aRangeMax;
111   return aShapeData;
112 }
113
114
115 void PartSet_FieldStepPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
116   const Handle(Prs3d_Presentation)& thePrs, const Standard_Integer theMode)
117 {
118   ModelAPI_AttributeTables::ValueType aType = dataType();
119   if ((aType == ModelAPI_AttributeTables::DOUBLE) ||
120     (aType == ModelAPI_AttributeTables::INTEGER)) {
121     double aMin, aMax;
122     QList<double> aShapeData = range(aMin, aMax);
123     int aNbIntertvals = 20;
124
125     DataPtr aData = myFeature->data();
126     AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
127     for (int i = 0; i < aSelList->size(); i++) {
128       AttributeSelectionPtr aSelection = aSelList->value(i);
129       GeomShapePtr aShapePtr = aSelection->value();
130       TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
131       double aValue = aShapeData.at(i);
132       Quantity_Color aColor;
133       if (AIS_ColorScale::FindColor(aValue, aMin, aMax, aNbIntertvals, aColor))
134         SetCustomColor(aShape, aColor);
135     }
136   }
137   ViewerData_AISShape::Compute(thePrsMgr, thePrs, theMode);
138 }