1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "PartSet_FieldStepPrs.h"
22 #include <ModuleBase_Preferences.h>
24 #include <CollectionPlugin_Field.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeIntArray.h>
28 #include <ModelAPI_AttributeStringArray.h>
30 #include <SUIT_ResourceMgr.h>
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>
38 #include <TopoDS_Vertex.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <Prs3d_Root.hxx>
43 IMPLEMENT_STANDARD_RTTIEXT(PartSet_FieldStepPrs, ViewerData_AISShape);
48 void emptyDeleter(ModelAPI_ResultField* theF)
53 PartSet_FieldStepPrs::PartSet_FieldStepPrs(FieldStepPtr theStep)
54 : ViewerData_AISShape(TopoDS_Shape()), myStep(theStep)
56 ModelAPI_ResultField* aField = theStep->field();
57 GeomShapePtr aShapePtr = aField->shape();
58 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
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);
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);
73 aDrawer->SetPointAspect(
74 new Prs3d_PointAspect(Aspect_TOM_POINT, Quantity_NOC_YELLOW, POINT_SIZE));
76 SetMaterial(Graphic3d_NOM_PLASTIC);
80 ModelAPI_AttributeTables::ValueType PartSet_FieldStepPrs::dataType() const
82 DataPtr aData = myFeature->data();
83 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
84 return aTablesAttr->type();
87 bool PartSet_FieldStepPrs::dataRange(double& theMin, double& theMax) const
89 ModelAPI_AttributeTables::ValueType aType = dataType();
90 if ((aType == ModelAPI_AttributeTables::DOUBLE) || (aType == ModelAPI_AttributeTables::INTEGER)) {
91 range(theMin, theMax);
97 QList<double> PartSet_FieldStepPrs::range(double& theMin, double& theMax) const
99 ModelAPI_AttributeTables::ValueType aType = dataType();
100 DataPtr aData = myFeature->data();
101 AttributeSelectionListPtr aSelList = aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
102 std::string aTypeStr = aSelList->selectionType();
104 int aStep = myStep->id();
105 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
106 int aRows = aTablesAttr->rows();
107 int aCols = aTablesAttr->columns();
109 QList<double> aFieldStepData;
110 int aStart = (aTypeStr == "part")? 0:1;
111 for (int k = aStart; k < aRows; k++) { // Do not use default values
112 for (int j = 0; j < aCols; j++) {
113 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
115 case ModelAPI_AttributeTables::DOUBLE:
116 aFieldStepData << aVal.myDouble;
118 case ModelAPI_AttributeTables::INTEGER:
119 aFieldStepData << aVal.myInt;
124 QList<double> aShapeData;
125 double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
126 for (int aRow = 0; aRow < aRows - 1; aRow++) {
128 int aBaseIndex = aRow * aCols;
129 for (int aCol = 0; aCol < aCols; aCol++) {
130 int anIndex = aCol + aBaseIndex;
131 double aValue = aFieldStepData.at(anIndex);
132 aNorm += aValue * aValue;
134 aNorm = pow(aNorm, 0.5);
135 aRangeMin = Min(aRangeMin, aNorm);
136 aRangeMax = Max(aRangeMax, aNorm);
145 void PartSet_FieldStepPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
146 const Handle(Prs3d_Presentation)& thePrs, const Standard_Integer theMode)
148 SUIT_ResourceMgr* aResMgr = ModuleBase_Preferences::resourceMgr();
149 QColor aQColor = aResMgr->colorValue("Viewer", "scalar_bar_text_color", Qt::black);
150 Quantity_Color aLabelColor = Quantity_Color(aQColor.redF(), aQColor.greenF(), aQColor.blueF(),
153 ModelAPI_AttributeTables::ValueType aType = dataType();
154 DataPtr aData = myFeature->data();
156 case ModelAPI_AttributeTables::DOUBLE:
157 case ModelAPI_AttributeTables::INTEGER:
160 QList<double> aShapeData = range(aMin, aMax);
161 int aNbIntertvals = aResMgr->integerValue("Viewer", "scalar_bar_nb_intervals", 20);
163 AttributeSelectionListPtr aSelList =
164 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
165 for (int i = 0; i < aSelList->size(); i++) {
166 AttributeSelectionPtr aSelection = aSelList->value(i);
167 GeomShapePtr aShapePtr = aSelection->value();
168 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
169 double aValue = aShapeData.at(i);
170 Quantity_Color aColor;
171 if (AIS_ColorScale::FindColor(aValue, aMin, aMax, aNbIntertvals, aColor))
172 SetCustomColor(aShape, aColor);
176 case ModelAPI_AttributeTables::BOOLEAN:
178 QList<double> aShapeData = booleanValues();
180 AttributeSelectionListPtr aSelList =
181 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
182 for (int i = 0; i < aSelList->size(); i++) {
183 AttributeSelectionPtr aSelection = aSelList->value(i);
184 GeomShapePtr aShapePtr = aSelection->value();
185 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
186 double aValue = aShapeData.at(i);
187 Quantity_Color aColor;
188 if (AIS_ColorScale::FindColor(aValue, 0., 1., 2, aColor))
189 SetCustomColor(aShape, aColor);
193 case ModelAPI_AttributeTables::STRING:
195 QStringList aValues = strings();
196 AttributeSelectionListPtr aSelList =
197 aData->selectionList(CollectionPlugin_Field::SELECTED_ID());
198 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePrs);
199 for (int i = 0; i < aSelList->size(); i++) {
200 AttributeSelectionPtr aSelection = aSelList->value(i);
201 GeomShapePtr aShapePtr = aSelection->value();
202 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
204 if (computeMassCenter(aShape, aCenter)) {
205 Graphic3d_Vertex aVertex(aCenter.X(), aCenter.Y(), aCenter.Z());
207 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
208 anAspectText3d->SetStyle(Aspect_TOST_ANNOTATION);
209 anAspectText3d->SetColor(aLabelColor);
210 aGroup->SetPrimitivesAspect(anAspectText3d);
212 int aT = aResMgr->integerValue("Viewer", "scalar_bar_text_height", 14);
213 QString aString = aValues.at(i);
214 aGroup->Text(aString.toUtf8().constData(), aVertex, aT);
220 ViewerData_AISShape::Compute(thePrsMgr, thePrs, theMode);
223 QList<double> PartSet_FieldStepPrs::booleanValues() const
225 DataPtr aData = myFeature->data();
226 int aStep = myStep->id();
227 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
228 int aRows = aTablesAttr->rows();
229 int aCols = aTablesAttr->columns();
230 QList<int> aFieldStepData;
231 for (int k = 1; k < aRows; k++) { // Do not use default values
232 for (int j = 0; j < aCols; j++) {
233 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
234 aFieldStepData << (aVal.myBool ? 1 : 0);
237 QList<double> aShapeData;
238 double aRangeMin = aFieldStepData.first(), aRangeMax = aFieldStepData.last();
239 for (int aRow = 0; aRow < aRows - 1; aRow++) {
241 int aBaseIndex = aRow * aCols;
242 for (int aCol = 0; aCol < aCols; aCol++) {
243 aNorm += aFieldStepData.at(aCol + aBaseIndex);
251 QStringList PartSet_FieldStepPrs::strings() const
253 DataPtr aData = myFeature->data();
254 int aStep = myStep->id();
255 AttributeTablesPtr aTablesAttr = aData->tables(CollectionPlugin_Field::VALUES_ID());
256 int aRows = aTablesAttr->rows();
257 int aCols = aTablesAttr->columns();
258 QStringList aFieldStepData;
259 for (int k = 1; k < aRows; k++) { // Do not use default values
260 for (int j = 0; j < aCols; j++) {
261 ModelAPI_AttributeTables::Value aVal = aTablesAttr->value(k, j, aStep);
262 aFieldStepData << aVal.myStr.c_str();
265 QStringList aShapeData;
266 for (int aRow = 0; aRow < aRows - 1; aRow++) {
267 QStringList aRowStrings;
268 int aBaseIndex = aRow * aCols;
269 for (int aCol = 0; aCol < aCols; aCol++) {
270 aRowStrings << aFieldStepData.at(aCol + aBaseIndex);
272 aRowStrings.join('\n');
273 aShapeData << aRowStrings;
278 bool PartSet_FieldStepPrs::computeMassCenter(const TopoDS_Shape& theShape, gp_Pnt& theCenter)
280 theCenter.SetCoord(0, 0, 0);
283 if (theShape.ShapeType() == TopAbs_EDGE) {
285 Handle(Geom_Curve) curve = BRep_Tool::Curve(TopoDS::Edge(theShape), f, l);
286 if (!curve.IsNull()) {
287 theCenter = curve->Value(0.5 * (f + l));
291 else if (theShape.ShapeType() == TopAbs_FACE) {
292 const TopoDS_Face& F = TopoDS::Face(theShape);
293 BRepAdaptor_Surface surface(F);
296 Handle(Poly_Triangulation) triangulation = BRep_Tool::Triangulation(F, L);
297 if (!triangulation.IsNull() && triangulation->HasUVNodes()) {
300 const TColgp_Array1OfPnt2d& uvArray = triangulation->UVNodes();
301 const Poly_Array1OfTriangle& trias = triangulation->Triangles();
303 for (int iT = trias.Lower(); iT <= trias.Upper(); ++iT) {
304 trias(iT).Get(n1, n2, n3);
305 const gp_Pnt2d& uv1 = uvArray(n1);
306 const gp_Pnt2d& uv2 = uvArray(n2);
307 const gp_Pnt2d& uv3 = uvArray(n3);
308 double a = 0.5 * sqrt((uv1.X() - uv3.X()) * (uv2.Y() - uv1.Y()) -
309 (uv1.X() - uv2.X()) * (uv3.Y() - uv1.Y()));
310 C += (uv1.XY() + uv2.XY() + uv3.XY()) / 3. * a;
313 if (A > std::numeric_limits<double>::min()) {
315 theCenter = surface.Value(C.X(), C.Y());
319 if (aNbPoints == 0) {
320 theCenter = surface.Value(0.5 * (surface.FirstUParameter() + surface.LastUParameter()),
321 0.5 * (surface.FirstVParameter() + surface.LastVParameter()));
326 if (aNbPoints == 0) {
327 TopExp_Explorer anExp;
328 for (anExp.Init(theShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
329 TopoDS_Vertex aVertex = TopoDS::Vertex(anExp.Current());
330 if (!aVertex.IsNull()) {
331 gp_Pnt aPnt = BRep_Tool::Pnt(aVertex);
332 theCenter.ChangeCoord() += aPnt.XYZ();
339 theCenter.ChangeCoord() /= (double)aNbPoints;