Salome HOME
Use tolerance while calculating center of mass.
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_FeatureStore.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModelHighAPI_FeatureStore.h>
22
23 #include <ModelAPI_Tools.h>
24 #include <ModelAPI_ResultPart.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_AttributeBoolean.h>
27 #include <ModelAPI_AttributeDocRef.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeIntArray.h>
30 #include <ModelAPI_AttributeInteger.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32 #include <ModelAPI_AttributeRefAttrList.h>
33 #include <ModelAPI_AttributeReference.h>
34 #include <ModelAPI_AttributeRefList.h>
35 #include <ModelAPI_AttributeSelection.h>
36 #include <ModelAPI_AttributeSelectionList.h>
37 #include <ModelAPI_AttributeString.h>
38 #include <ModelAPI_AttributeStringArray.h>
39 #include <ModelAPI_AttributeDoubleArray.h>
40 #include <ModelAPI_AttributeTables.h>
41 #include <ModelAPI_Validator.h>
42
43 #include <GeomDataAPI_Dir.h>
44 #include <GeomDataAPI_Point.h>
45 #include <GeomDataAPI_Point2D.h>
46 #include <GeomAlgoAPI_ShapeTools.h>
47 #include <GeomAPI_Pnt.h>
48
49 #include <TopoDS_Shape.hxx>
50 #include <TopExp_Explorer.hxx>
51
52 #include <ios>
53 #include <cmath>
54
55 #define PRECISION 6
56 #define TOLERANCE (1.e-7)
57
58 ModelHighAPI_FeatureStore::ModelHighAPI_FeatureStore(ObjectPtr theObject) {
59   storeData(theObject->data(), myAttrs);
60
61   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
62   if (aFeature) {
63     // iterate results to store
64     std::list<ResultPtr> allResults;
65     ModelAPI_Tools::allResults(aFeature, allResults);
66     std::list<ResultPtr>::iterator aRes = allResults.begin();
67     for(; aRes != allResults.end(); aRes++) {
68       std::map<std::string, std::string> aResDump;
69       storeData((*aRes)->data(), aResDump);
70       myRes.push_back(aResDump);
71     }
72   }
73 }
74
75 std::string ModelHighAPI_FeatureStore::compare(ObjectPtr theObject) {
76   std::string anError = compareData(theObject->data(), myAttrs);
77   if (!anError.empty()) {
78     return "Features '" + theObject->data()->name() + "' differ:" + anError;
79   }
80
81   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
82   if (aFeature) {
83     std::list<ResultPtr> allResults;
84     ModelAPI_Tools::allResults(aFeature, allResults);
85     std::list<ResultPtr>::iterator aRes = allResults.begin();
86     std::list<std::map<std::string, std::string> >::iterator aResIter = myRes.begin();
87     for(; aRes != allResults.end() && aResIter != myRes.end(); aRes++, aResIter++) {
88       anError = compareData((*aRes)->data(), *aResIter);
89       if (!anError.empty())
90         return "Results of feature '" + aFeature->name() + "' '" + (*aRes)->data()->name() +
91         "' differ:" + anError;
92     }
93     if (aRes != allResults.end()) {
94       return "Current model has more results '" + (*aRes)->data()->name() + "'";
95     }
96     if (aResIter != myRes.end()) {
97       return "Original model had more results '" + (*aResIter)["__name__"] + "'";
98     }
99   }
100   return ""; // ok
101 }
102
103 void ModelHighAPI_FeatureStore::storeData(std::shared_ptr<ModelAPI_Data> theData,
104   std::map<std::string, std::string>& theAttrs)
105 {
106   // store name to keep also this information and output if needed
107   theAttrs["__name__"] = theData->name();
108   std::list<std::shared_ptr<ModelAPI_Attribute> > allAttrs = theData->attributes("");
109   std::list<std::shared_ptr<ModelAPI_Attribute> >::iterator anAttr = allAttrs.begin();
110   for(; anAttr != allAttrs.end(); anAttr++) {
111     theAttrs[(*anAttr)->id()] = dumpAttr(*anAttr);
112   }
113   ResultPtr aShapeOwner = std::dynamic_pointer_cast<ModelAPI_Result>(theData->owner());
114   if (aShapeOwner.get() && aShapeOwner->shape().get()) {
115     std::shared_ptr<GeomAPI_Shape> aShape = aShapeOwner->shape();
116     theAttrs["__shape__"] = dumpShape(aShape);
117   }
118 }
119
120 std::string ModelHighAPI_FeatureStore::compareData(std::shared_ptr<ModelAPI_Data> theData,
121   std::map<std::string, std::string>& theAttrs)
122 {
123   std::map<std::string, std::string> aThis;
124   storeData(theData, aThis);
125   std::map<std::string, std::string>::iterator aThisIter = aThis.begin();
126   for(; aThisIter != aThis.end(); aThisIter++) {
127     if (theAttrs.find(aThisIter->first) == theAttrs.end()) {
128       return "original model had no attribute '" + aThisIter->first + "'";
129     }
130     if (theAttrs[aThisIter->first] != aThisIter->second) {
131       return "attribute '" + aThisIter->first + "' is different (original != current) '" +
132         theAttrs[aThisIter->first] + "' != '" + aThisIter->second + "'";
133     }
134   }
135   // iterate back to find lack attribute in the current model
136   std::map<std::string, std::string>::iterator anOrigIter = theAttrs.begin();
137   for(; anOrigIter != theAttrs.end(); anOrigIter++) {
138     if (aThis.find(anOrigIter->first) == aThis.end()) {
139       return "current model had no attribute '" + anOrigIter->first + "'";
140     }
141   }
142   return "";
143 }
144
145 static void dumpArray(std::ostringstream& theOutput, const double theArray[],
146                       int theSize, int thePrecision = PRECISION)
147 {
148   for (int i = 0; i < theSize; ++i) {
149     if (i > 0)
150       theOutput << " ";
151     theOutput << std::fixed << setprecision(thePrecision)
152               << (fabs(theArray[i]) < TOLERANCE ? 0.0 : theArray[i]);
153   }
154 }
155
156 std::string ModelHighAPI_FeatureStore::dumpAttr(const AttributePtr& theAttr) {
157   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
158   FeaturePtr aFeatOwner = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner());
159   if (aFeatOwner.get() && !aFactory->isCase(aFeatOwner, theAttr->id())) {
160     return "__notcase__";
161   }
162   std::string aType = theAttr->attributeType();
163   std::ostringstream aResult;
164   if (!theAttr->isInitialized()) {
165     if (aType == ModelAPI_AttributeBoolean::typeId()) {
166       // special case for Boolean attribute (mean it false if not initialized)
167       aResult << false;
168       return aResult.str();
169     } else if (aType == ModelAPI_AttributeString::typeId()) {
170       // special case for attribute "SolverError"
171       if (theAttr->id() == "SolverError" &&
172           std::dynamic_pointer_cast<ModelAPI_Feature>(theAttr->owner())->getKind() == "Sketch")
173         return "";
174     }
175
176     return "__notinitialized__";
177   }
178   if (aType == ModelAPI_AttributeDocRef::typeId()) {
179     AttributeDocRefPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(theAttr);
180     DocumentPtr aDoc = ModelAPI_Session::get()->moduleDocument();
181     if (anAttr->value() != aDoc) {
182       ResultPtr aRes = ModelAPI_Tools::findPartResult(aDoc, anAttr->value());
183       if (aRes.get()) {
184         aResult<<aRes->data()->name(); // Part result name (the same as saved file name)
185       }
186     } else {
187       aResult<<aDoc->kind(); // PartSet
188     }
189   } else if (aType == ModelAPI_AttributeInteger::typeId()) {
190     AttributeIntegerPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
191     if (anAttr->text().empty())
192       aResult<<anAttr->value();
193     else
194       aResult<<anAttr->text();
195   } else if (aType == ModelAPI_AttributeDouble::typeId()) {
196     AttributeDoublePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
197     int aPrecision = PRECISION;
198     // Special case - precision for the arc angle. It is calculated with tolerance 1e-4,
199     // so the value has only 4 correct digits
200     if (anAttr->id() == "ArcAngle")
201       aPrecision = 1;
202     if (anAttr->text().empty()) {
203       double aVal = anAttr->value();
204       dumpArray(aResult, &aVal, 1, aPrecision);
205     } else
206       aResult<<anAttr->text();
207   } else if (aType == ModelAPI_AttributeBoolean::typeId()) {
208     AttributeBooleanPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(theAttr);
209     aResult<<anAttr->value();
210   } else if (aType == ModelAPI_AttributeString::typeId()) {
211     AttributeStringPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttr);
212     // do not dump solver DOF for sketch as it may be changed unexpectedly
213     if(anAttr->id() == "SolverDOF") {
214       return "";
215     }
216     aResult<<anAttr->value();
217   } else if (aType == ModelAPI_AttributeReference::typeId()) {
218     AttributeReferencePtr anAttr =
219       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttr);
220     if (anAttr->value().get()) {
221       aResult<<anAttr->value()->data()->name();
222     } else {
223       aResult<<"__empty__";
224     }
225   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
226     AttributeSelectionPtr anAttr =
227       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttr);
228     aResult<<anAttr->namingName();
229   } else if (aType == ModelAPI_AttributeSelectionList::typeId()) {
230     AttributeSelectionListPtr anAttr =
231       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttr);
232     for(int a = 0; a < anAttr->size(); a++) {
233       if (a != 0)
234         aResult<<" ";
235       aResult<<anAttr->value(a)->namingName();
236     }
237   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
238     AttributeRefAttrPtr anAttr =
239       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttr);
240     ObjectPtr anObj = anAttr->isObject() ? anAttr->object() : anAttr->attr()->owner();
241     if (anObj.get()) {
242       aResult<<anObj->data()->name();
243       if (!anAttr->isObject()) {
244         aResult<<" "<<anAttr->attr()->id();
245       }
246     } else {
247       aResult<<"__empty__";
248     }
249   } else if (aType == ModelAPI_AttributeRefList::typeId()) {
250     AttributeRefListPtr anAttr =
251       std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttr);
252     // for sketch sub-features the empty values may be skipped and order is not important
253     bool isSketchFeatures = anAttr->id() == "Features" &&
254       std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner())->getKind() == "Sketch";
255     std::list<ObjectPtr> aList = anAttr->list();
256     std::list<std::string> aResList; // list of resulting strings
257     for(std::list<ObjectPtr>::iterator aL = aList.begin(); aL != aList.end(); aL++) {
258       if (aL->get()) {
259         aResList.push_back((*aL)->data()->name());
260       } else if (!isSketchFeatures) {
261         aResList.push_back("__empty__");
262       }
263     }
264     if (isSketchFeatures)
265       aResList.sort();
266     for(std::list<std::string>::iterator aR = aResList.begin(); aR != aResList.end(); aR++) {
267       aResult<<*aR<<" ";
268     }
269   } else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
270     AttributeRefAttrListPtr anAttr =
271       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttr);
272     std::list<std::pair<ObjectPtr, AttributePtr> > aList = anAttr->list();
273     std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aL = aList.begin();
274     for(; aL != aList.end(); aL++) {
275       if (aL != aList.begin())
276         aResult<<" ";
277       ObjectPtr anObj = aL->second.get() ? aL->second->owner() : aL->first;
278       if (anObj.get()) {
279         aResult<<anObj->data()->name();
280         if (aL->second.get()) {
281           aResult<<" "<<aL->second->id();
282         }
283       } else {
284         aResult<<"__empty__";
285       }
286     }
287   } else if (aType == ModelAPI_AttributeIntArray::typeId()) {
288     AttributeIntArrayPtr anAttr =
289       std::dynamic_pointer_cast<ModelAPI_AttributeIntArray>(theAttr);
290     for(int a = 0; a < anAttr->size(); a++)
291       aResult<<anAttr->value(a)<<" ";
292   } else if (aType == ModelAPI_AttributeDoubleArray::typeId()) {
293     AttributeDoubleArrayPtr anAttr =
294       std::dynamic_pointer_cast<ModelAPI_AttributeDoubleArray>(theAttr);
295     for(int a = 0; a < anAttr->size(); a++)
296       aResult<<anAttr->value(a)<<" ";
297   } else if (aType == ModelAPI_AttributeStringArray::typeId()) {
298     AttributeStringArrayPtr anAttr =
299       std::dynamic_pointer_cast<ModelAPI_AttributeStringArray>(theAttr);
300     for(int a = 0; a < anAttr->size(); a++)
301       aResult<<"'"<<anAttr->value(a)<<"'"<<" ";
302   } else if (aType == ModelAPI_AttributeTables::typeId()) {
303     AttributeTablesPtr anAttr =
304       std::dynamic_pointer_cast<ModelAPI_AttributeTables>(theAttr);
305     aResult<<anAttr->tables()<<"x"<<anAttr->rows()<<"x"<<anAttr->columns()<<" ";
306     for(int aTab = 0; aTab < anAttr->tables(); aTab++) {
307       for(int aRow = 0; aRow < anAttr->rows(); aRow++) {
308         for( int aCol = 0; aCol < anAttr->columns(); aCol++) {
309           switch(anAttr->type()) {
310           case ModelAPI_AttributeTables::BOOLEAN:
311             aResult<<anAttr->value(aRow, aCol, aTab).myBool<<" ";
312             break;
313           case ModelAPI_AttributeTables::INTEGER:
314             aResult<<anAttr->value(aRow, aCol, aTab).myInt<<" ";
315             break;
316           case ModelAPI_AttributeTables::DOUBLE:
317             aResult<<anAttr->value(aRow, aCol, aTab).myDouble<<" ";
318             break;
319           case ModelAPI_AttributeTables::STRING:
320             aResult<<"'"<<anAttr->value(aRow, aCol, aTab).myStr.c_str()<<"' ";
321             break;
322           }
323         }
324       }
325     }
326   } else if (aType == GeomDataAPI_Point::typeId()) {
327     AttributePointPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttr);
328     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
329     dumpArray(aResult, aValues, 3);
330   } else if (aType == GeomDataAPI_Dir::typeId()) {
331     AttributeDirPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Dir>(theAttr);
332     double aValues[3] = {anAttr->x(), anAttr->y(), anAttr->z()};
333     dumpArray(aResult, aValues, 3);
334   } else if (aType == GeomDataAPI_Point2D::typeId()) {
335     // do not dump flyout point for constraints as it may be changed unexpectedly
336     if (theAttr->id() == "ConstraintFlyoutValuePnt")
337       return "__notinitialized__";
338     AttributePoint2DPtr anAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttr);
339     double aValues[2] = {anAttr->x(), anAttr->y()};
340     dumpArray(aResult, aValues, 2);
341   } else {
342     aResult<<"__unknownattribute__";
343   }
344   return aResult.str();
345 }
346
347 std::string ModelHighAPI_FeatureStore::dumpShape(std::shared_ptr<GeomAPI_Shape>& theShape) {
348   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
349   if (aShape.IsNull()) {
350     return "null";
351   }
352   std::ostringstream aResult;
353   // output the number of shapes of different types
354   TopAbs_ShapeEnum aType = TopAbs_COMPOUND;
355   for(; aType <= TopAbs_VERTEX; aType = TopAbs_ShapeEnum((int)aType + 1)) {
356     TopExp_Explorer anExp(aShape, aType);
357     int aCount = 0;
358     for(; anExp.More(); anExp.Next()) aCount++;
359     TopAbs::Print(aType, aResult);
360     aResult<<": "<<aCount<<std::endl;
361   }
362   // output the main characteristics
363   if (GeomAlgoAPI_ShapeTools::volume(theShape) > 1.e-5) {
364     aResult<<"Volume: "<<
365       std::fixed<<setprecision(3)<<GeomAlgoAPI_ShapeTools::volume(theShape)<<std::endl;
366   }
367   std::shared_ptr<GeomAPI_Pnt> aCenter = GeomAlgoAPI_ShapeTools::centreOfMass(theShape);
368   aResult<<"Center of mass: ";
369   double aCenterVals[3] = {aCenter->x(), aCenter->y(), aCenter->z()};
370   dumpArray(aResult, aCenterVals, 3, 5);
371   aResult<<std::endl;
372   return aResult.str();
373 }