]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/PlaneGCSSolver/PlaneGCSSolver_Storage.cpp
Salome HOME
Task 2.4. Ability to modify the radius of circles and arcs of circle with the mouse
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Storage.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_Storage.cpp
4 // Created: 14 Dec 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <PlaneGCSSolver_Storage.h>
8 #include <PlaneGCSSolver_Solver.h>
9 #include <PlaneGCSSolver_ConstraintWrapper.h>
10 #include <PlaneGCSSolver_EdgeWrapper.h>
11 #include <PlaneGCSSolver_PointWrapper.h>
12
13 #include <PlaneGCSSolver_AttributeBuilder.h>
14 #include <PlaneGCSSolver_FeatureBuilder.h>
15 #include <PlaneGCSSolver_EntityDestroyer.h>
16
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_XY.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <SketchPlugin_Projection.h>
23
24 #include <cmath>
25
26
27 static void constraintsToSolver(const ConstraintWrapperPtr& theConstraint,
28                                 const SolverPtr& theSolver)
29 {
30   const std::list<GCSConstraintPtr>& aConstraints =
31       std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint)->constraints();
32   std::list<GCSConstraintPtr>::const_iterator anIt = aConstraints.begin();
33   for (; anIt != aConstraints.end(); ++anIt)
34     theSolver->addConstraint(*anIt);
35 }
36
37
38 PlaneGCSSolver_Storage::PlaneGCSSolver_Storage(const SolverPtr& theSolver)
39   : SketchSolver_Storage(theSolver),
40     myConstraintLastID(CID_UNKNOWN)
41 {
42 }
43
44 void PlaneGCSSolver_Storage::addConstraint(
45     ConstraintPtr        theConstraint,
46     ConstraintWrapperPtr theSolverConstraint)
47 {
48   SketchSolver_Storage::addConstraint(theConstraint, theSolverConstraint);
49
50   theSolverConstraint->setId(++myConstraintLastID);
51   constraintsToSolver(theSolverConstraint, mySketchSolver);
52 }
53
54 void PlaneGCSSolver_Storage::addMovementConstraint(
55     const ConstraintWrapperPtr& theSolverConstraint)
56 {
57   if (myConstraintMap.empty())
58     return; // no need to process temporary constraints if there is no active constraint
59
60   // before adding movement constraint to solver, re-check its DOF
61   if (mySketchSolver->dof() == 0)
62     mySketchSolver->diagnose();
63
64   theSolverConstraint->setId(CID_MOVEMENT);
65   constraintsToSolver(theSolverConstraint, mySketchSolver);
66 }
67
68
69 EntityWrapperPtr PlaneGCSSolver_Storage::createFeature(
70     const FeaturePtr&             theFeature,
71     PlaneGCSSolver_EntityBuilder* theBuilder)
72 {
73   std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
74   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin();
75   for (; anIt != anAttributes.end(); ++anIt)
76     createAttribute(*anIt, theBuilder);
77
78   EntityWrapperPtr aResult = theBuilder->createFeature(theFeature);
79   if (aResult)
80     addEntity(theFeature, aResult);
81   return aResult;
82 }
83
84 EntityWrapperPtr PlaneGCSSolver_Storage::createAttribute(
85     const AttributePtr&           theAttribute,
86     PlaneGCSSolver_EntityBuilder* theBuilder)
87 {
88   EntityWrapperPtr aResult = theBuilder->createAttribute(theAttribute);
89   if (aResult)
90     addEntity(theAttribute, aResult);
91   return aResult;
92 }
93
94 /// \brief Update value
95 static bool updateValue(const double& theSource, double& theDest)
96 {
97   static const double aTol = 1000. * tolerance;
98   bool isUpdated = fabs(theSource - theDest) > aTol;
99   if (isUpdated)
100     theDest = theSource;
101   return isUpdated;
102 }
103
104 /// \brief Update coordinates of the point or scalar using its base attribute
105 static bool updateValues(AttributePtr& theAttribute, EntityWrapperPtr& theEntity)
106 {
107   bool isUpdated = false;
108
109   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
110       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
111   if (aPoint2D) {
112     const GCSPointPtr& aGCSPoint =
113         std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity)->point();
114     isUpdated = updateValue(aPoint2D->x(), *(aGCSPoint->x)) || isUpdated;
115     isUpdated = updateValue(aPoint2D->y(), *(aGCSPoint->y)) || isUpdated;
116   } else {
117     AttributeDoublePtr aScalar =
118         std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
119     if (aScalar) {
120       ScalarWrapperPtr aWrapper =
121           std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theEntity);
122       // There is possible angular value, which is converted between degrees and radians.
123       // So, we use its value instead of using direct pointer to value.
124       double aValue = aWrapper->value();
125       isUpdated = updateValue(aScalar->value(), aValue);
126       if (isUpdated)
127         aWrapper->setValue(aValue);
128     }
129   }
130
131   return isUpdated;
132 }
133
134 static bool hasReference(std::shared_ptr<SketchPlugin_Feature> theFeature,
135                          const std::string& theFeatureKind)
136 {
137   const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
138   for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
139        aRefIt != aRefs.end(); ++aRefIt) {
140      FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
141      if (anOwner->getKind() == theFeatureKind)
142        return true;
143   }
144   return false;
145 }
146
147 static bool isCopyFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
148 {
149   return theFeature && theFeature->isCopy();
150 }
151
152 bool PlaneGCSSolver_Storage::update(FeaturePtr theFeature, bool theForce)
153 {
154   bool sendNotify = false;
155   bool isUpdated = false;
156   EntityWrapperPtr aRelated = entity(theFeature);
157   if (aRelated) // send signal to subscribers
158     sendNotify = true;
159   else { // Feature is not exist, create it
160     std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
161         std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
162     bool isCopy = isCopyFeature(aSketchFeature);
163     bool isProjReferred = hasReference(aSketchFeature, SketchPlugin_Projection::ID());
164     // the feature is a copy in "Multi" constraint and does not used in other constraints
165     if (!theForce && (isCopy && !isProjReferred) &&
166         myFeatureMap.find(theFeature) == myFeatureMap.end())
167       return false;
168
169     // external feature processing
170     bool isExternal =
171         (aSketchFeature && (aSketchFeature->isExternal() || isCopy || isProjReferred));
172
173     PlaneGCSSolver_FeatureBuilder aBuilder(isExternal ? 0 : this);
174
175     // Reserve the feature in the map of features
176     // (do not want to add several copies of it while adding attributes)
177     aRelated = createFeature(theFeature, &aBuilder);
178     myFeatureMap[theFeature] = aRelated;
179
180     const std::list<GCSConstraintPtr>& aConstraints = aBuilder.constraints();
181     if (!aConstraints.empty()) { // the feature is arc
182       /// TODO: avoid this workaround
183       ConstraintWrapperPtr aWrapper(
184           new PlaneGCSSolver_ConstraintWrapper(aConstraints, CONSTRAINT_UNKNOWN));
185       aWrapper->setId(++myConstraintLastID);
186       constraintsToSolver(aWrapper, mySketchSolver);
187
188       myArcConstraintMap[myFeatureMap[theFeature]] = aWrapper;
189     }
190     isUpdated = true;
191   }
192
193   std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
194   std::list<AttributePtr>::iterator anAttrIt = anAttributes.begin();
195   for (; anAttrIt != anAttributes.end(); ++anAttrIt)
196     if ((*anAttrIt)->attributeType() == GeomDataAPI_Point2D::typeId() ||
197         (*anAttrIt)->attributeType() == ModelAPI_AttributeDouble::typeId())
198       isUpdated = update(*anAttrIt) || isUpdated;
199
200   // send notification to listeners due to at least one attribute is changed
201   if (sendNotify && isUpdated)
202     notify(theFeature);
203
204   // update arc
205   if (aRelated && aRelated->type() == ENTITY_ARC) {
206     /// TODO: this code should be shared with FeatureBuilder somehow
207
208     std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
209         std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aRelated);
210     std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEntity->entity());
211
212     static std::shared_ptr<GeomAPI_Dir2d> OX(new GeomAPI_Dir2d(1.0, 0.0));
213     std::shared_ptr<GeomAPI_Pnt2d> aCenter(
214         new GeomAPI_Pnt2d(*anArc->center.x, *anArc->center.y));
215     std::shared_ptr<GeomAPI_Pnt2d> aStart(
216         new GeomAPI_Pnt2d(*anArc->start.x, *anArc->start.y));
217
218     *anArc->rad = aStart->distance(aCenter);
219
220     std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(aStart->xy()->decreased(aCenter->xy())));
221     *anArc->startAngle = OX->angle(aDir);
222
223     aDir = std::shared_ptr<GeomAPI_Dir2d>(
224         new GeomAPI_Dir2d((*anArc->end.x) - aCenter->x(), (*anArc->end.y) - aCenter->y()));
225     *anArc->endAngle = OX->angle(aDir);
226   }
227
228   return isUpdated;
229 }
230
231 bool PlaneGCSSolver_Storage::update(AttributePtr theAttribute, bool theForce)
232 {
233   if (!theAttribute->isInitialized())
234     return false;
235
236   AttributePtr anAttribute = theAttribute;
237   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
238   if (aRefAttr) {
239     if (aRefAttr->isObject()) {
240       FeaturePtr aFeature;
241       /// TODO: Check resultToFeatureOrAttribute() precisely.
242       resultToFeatureOrAttribute(aRefAttr->object(), aFeature, anAttribute);
243       if (aFeature)
244         return update(aFeature, theForce);
245     } else
246       anAttribute = aRefAttr->attr();
247   }
248
249   EntityWrapperPtr aRelated = entity(anAttribute);
250   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
251   if (!aRelated) { // Attribute does not exist, create it.
252     // First of all check if the parent feature exists. If not, add it.
253     if (aFeature && myFeatureMap.find(aFeature) == myFeatureMap.end())
254       return update(aFeature, theForce); // theAttribute has been processed while adding feature
255     return aRelated.get() != 0;
256   }
257
258   bool isUpdated = updateValues(anAttribute, aRelated);
259   if (isUpdated) {
260     setNeedToResolve(true);
261     notify(aFeature);
262   }
263   return isUpdated;
264 }
265
266
267
268 bool PlaneGCSSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
269 {
270   std::map<ConstraintPtr, ConstraintWrapperPtr>::iterator
271       aFound = myConstraintMap.find(theConstraint);
272   if (aFound != myConstraintMap.end()) {
273     ConstraintWrapperPtr aCW = aFound->second;
274     ConstraintID anID = aCW->id();
275
276     // Remove solver's constraints
277     mySketchSolver->removeConstraint(anID);
278
279     // Remove value if exists
280     const ScalarWrapperPtr& aValue = aCW->valueParameter();
281     if (aValue) {
282       GCS::SET_pD aParToRemove;
283       aParToRemove.insert(aValue->scalar());
284       removeParameters(aParToRemove);
285     }
286
287     // Remove constraint
288     myConstraintMap.erase(aFound);
289
290     if (anID != CID_MOVEMENT)
291       myNeedToResolve = true;
292
293     // notify subscibers
294     notify(theConstraint);
295   }
296   return true;
297 }
298
299 void PlaneGCSSolver_Storage::removeInvalidEntities()
300 {
301   PlaneGCSSolver_EntityDestroyer aDestroyer;
302
303   // Remove invalid constraints
304   std::list<ConstraintPtr> anInvalidConstraints;
305   std::map<ConstraintPtr, ConstraintWrapperPtr>::const_iterator
306       aCIter = myConstraintMap.begin();
307   for (; aCIter != myConstraintMap.end(); ++aCIter)
308     if (!aCIter->first->data() || !aCIter->first->data()->isValid())
309       anInvalidConstraints.push_back(aCIter->first);
310   std::list<ConstraintPtr>::const_iterator anInvCIt = anInvalidConstraints.begin();
311   for (; anInvCIt != anInvalidConstraints.end(); ++anInvCIt)
312     removeConstraint(*anInvCIt);
313
314   // Remove invalid features
315   std::list<FeaturePtr> anInvalidFeatures;
316   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
317   for (; aFIter != myFeatureMap.end(); aFIter++)
318     if (!aFIter->first->data() || !aFIter->first->data()->isValid()) {
319       anInvalidFeatures.push_back(aFIter->first);
320       if (aFIter->second)
321         aDestroyer.remove(aFIter->second);
322
323       // remove invalid arc
324       std::map<EntityWrapperPtr, ConstraintWrapperPtr>::iterator
325           aFound = myArcConstraintMap.find(aFIter->second);
326       if (aFound != myArcConstraintMap.end()) {
327         mySketchSolver->removeConstraint(aFound->second->id());
328         myArcConstraintMap.erase(aFound);
329       }
330     }
331   std::list<FeaturePtr>::const_iterator anInvFIt = anInvalidFeatures.begin();
332   for (; anInvFIt != anInvalidFeatures.end(); ++anInvFIt)
333     removeFeature(*anInvFIt);
334
335   // Remove invalid attributes
336   std::list<AttributePtr> anInvalidAttributes;
337   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anAttrIt = myAttributeMap.begin();
338   for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
339     FeaturePtr anOwner = ModelAPI_Feature::feature(anAttrIt->first->owner());
340     if (!anOwner || !anOwner->data() || !anOwner->data()->isValid()) {
341       anInvalidAttributes.push_back(anAttrIt->first);
342       aDestroyer.remove(anAttrIt->second);
343     }
344   }
345   std::list<AttributePtr>::const_iterator anInvAtIt = anInvalidAttributes.begin();
346   for (; anInvAtIt != anInvalidAttributes.end(); ++anInvAtIt)
347     removeAttribute(*anInvAtIt);
348
349   // free memory occupied by parameters
350   removeParameters(aDestroyer.parametersToRemove());
351
352   /// TODO: Think on optimization of checking invalid features and attributes
353 }
354
355
356
357 double* PlaneGCSSolver_Storage::createParameter()
358 {
359   return mySketchSolver->createParameter();
360 }
361
362 void PlaneGCSSolver_Storage::removeParameters(const GCS::SET_pD& theParams)
363 {
364   mySketchSolver->removeParameters(theParams);
365 }
366
367 // indicates attribute containing in the external feature
368 static bool isExternalAttribute(const AttributePtr& theAttribute)
369 {
370   if (!theAttribute)
371     return false;
372   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
373       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
374   return aSketchFeature.get() && aSketchFeature->isExternal();
375 }
376
377 static void addOwnerToSet(const AttributePtr& theAttribute, std::set<FeaturePtr>& theFeatures)
378 {
379   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
380   if (anOwner)
381     theFeatures.insert(anOwner);
382 }
383
384 void PlaneGCSSolver_Storage::refresh() const
385 {
386   const double aTol = 1000. * tolerance; // tolerance to prevent frequent updates
387
388   std::set<FeaturePtr> anUpdatedFeatures;
389
390   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
391   for (; anIt != myAttributeMap.end(); ++anIt) {
392     // the external feature always should keep the up to date values, so,
393     // refresh from the solver is never needed
394     if (isExternalAttribute(anIt->first))
395       continue;
396
397     std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
398         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
399     if (aPoint2D) {
400       std::shared_ptr<PlaneGCSSolver_PointWrapper> aPointWrapper =
401           std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(anIt->second);
402       GCSPointPtr aGCSPoint = aPointWrapper->point();
403       if (fabs(aPoint2D->x() - (*aGCSPoint->x)) > aTol ||
404           fabs(aPoint2D->y() - (*aGCSPoint->y)) > aTol) {
405         aPoint2D->setValue(*aGCSPoint->x, *aGCSPoint->y);
406         addOwnerToSet(anIt->first, anUpdatedFeatures);
407       }
408       continue;
409     }
410     AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
411     if (aScalar) {
412       ScalarWrapperPtr aScalarWrapper =
413           std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(anIt->second);
414       if (fabs(aScalar->value() - aScalarWrapper->value()) > aTol) {
415         aScalar->setValue(aScalarWrapper->value());
416         addOwnerToSet(anIt->first, anUpdatedFeatures);
417       }
418       continue;
419     }
420   }
421
422   // notify listeners about features update
423   std::set<FeaturePtr>::const_iterator aFIt = anUpdatedFeatures.begin();
424   for (; aFIt != anUpdatedFeatures.end(); ++aFIt)
425     notify(*aFIt);
426 }