1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <PlaneGCSSolver_Storage.h>
22 #include <PlaneGCSSolver_Solver.h>
23 #include <PlaneGCSSolver_ConstraintWrapper.h>
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_PointWrapper.h>
26 #include <PlaneGCSSolver_Tools.h>
28 #include <PlaneGCSSolver_AttributeBuilder.h>
29 #include <PlaneGCSSolver_FeatureBuilder.h>
30 #include <PlaneGCSSolver_EntityDestroyer.h>
32 #include <GeomAPI_Dir2d.h>
33 #include <GeomAPI_Pnt2d.h>
34 #include <GeomAPI_XY.h>
35 #include <GeomDataAPI_Point2D.h>
36 #include <ModelAPI_AttributeRefAttr.h>
37 #include <SketchPlugin_Projection.h>
42 static void constraintsToSolver(const ConstraintWrapperPtr& theConstraint,
43 const SolverPtr& theSolver)
45 const std::list<GCSConstraintPtr>& aConstraints =
46 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint)->constraints();
47 theSolver->addConstraint(theConstraint->id(), aConstraints);
51 PlaneGCSSolver_Storage::PlaneGCSSolver_Storage(const SolverPtr& theSolver)
52 : SketchSolver_Storage(theSolver),
53 myConstraintLastID(CID_UNKNOWN)
57 void PlaneGCSSolver_Storage::addConstraint(
58 ConstraintPtr theConstraint,
59 ConstraintWrapperPtr theSolverConstraint)
61 SketchSolver_Storage::addConstraint(theConstraint, theSolverConstraint);
63 theSolverConstraint->setId(++myConstraintLastID);
64 constraintsToSolver(theSolverConstraint, mySketchSolver);
67 void PlaneGCSSolver_Storage::addMovementConstraint(
68 const ConstraintWrapperPtr& theSolverConstraint)
70 // before adding movement constraint to solver, re-check its DOF
71 if (mySketchSolver->dof() == 0)
72 mySketchSolver->diagnose();
74 theSolverConstraint->setId(CID_MOVEMENT);
75 constraintsToSolver(theSolverConstraint, mySketchSolver);
79 EntityWrapperPtr PlaneGCSSolver_Storage::createFeature(
80 const FeaturePtr& theFeature,
81 PlaneGCSSolver_EntityBuilder* theBuilder)
83 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
84 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin();
85 for (; anIt != anAttributes.end(); ++anIt)
86 createAttribute(*anIt, theBuilder);
88 EntityWrapperPtr aResult = theBuilder->createFeature(theFeature);
90 addEntity(theFeature, aResult);
94 EntityWrapperPtr PlaneGCSSolver_Storage::createAttribute(
95 const AttributePtr& theAttribute,
96 PlaneGCSSolver_EntityBuilder* theBuilder)
98 EntityWrapperPtr aResult = theBuilder->createAttribute(theAttribute);
100 addEntity(theAttribute, aResult);
104 /// \brief Update value
105 static bool updateValue(const double& theSource, double& theDest)
107 static const double aTol = 1000. * tolerance;
108 bool isUpdated = fabs(theSource - theDest) > aTol;
114 /// \brief Update coordinates of the point or scalar using its base attribute
115 static bool updateValues(AttributePtr& theAttribute, EntityWrapperPtr& theEntity)
117 bool isUpdated = false;
119 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
120 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
122 const GCSPointPtr& aGCSPoint =
123 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity)->point();
124 isUpdated = updateValue(aPoint2D->x(), *(aGCSPoint->x)) || isUpdated;
125 isUpdated = updateValue(aPoint2D->y(), *(aGCSPoint->y)) || isUpdated;
127 AttributeDoublePtr aScalar =
128 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
130 ScalarWrapperPtr aWrapper =
131 std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theEntity);
132 // There is possible angular value, which is converted between degrees and radians.
133 // So, we use its value instead of using direct pointer to value.
134 double aValue = aWrapper->value();
135 isUpdated = updateValue(aScalar->value(), aValue);
137 aWrapper->setValue(aValue);
144 static bool hasReference(std::shared_ptr<SketchPlugin_Feature> theFeature,
145 const std::string& theFeatureKind)
147 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
148 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
149 aRefIt != aRefs.end(); ++aRefIt) {
150 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
151 if (anOwner && anOwner->getKind() == theFeatureKind)
157 static bool isCopyFeature(std::shared_ptr<SketchPlugin_Feature> theFeature)
159 return theFeature && theFeature->isCopy();
162 bool PlaneGCSSolver_Storage::update(FeaturePtr theFeature, bool theForce)
164 bool sendNotify = false;
165 bool isUpdated = false;
166 EntityWrapperPtr aRelated = entity(theFeature);
167 if (aRelated) // send signal to subscribers
169 else { // Feature is not exist, create it
170 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
171 std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
172 bool isCopy = isCopyFeature(aSketchFeature);
173 bool isProjReferred = hasReference(aSketchFeature, SketchPlugin_Projection::ID());
174 // the feature is a copy in "Multi" constraint and does not used in other constraints
175 if (!theForce && (isCopy && !isProjReferred) &&
176 myFeatureMap.find(theFeature) == myFeatureMap.end())
179 // external feature processing
181 (aSketchFeature && (aSketchFeature->isExternal() || isCopy || isProjReferred));
183 PlaneGCSSolver_FeatureBuilder aBuilder(isExternal ? 0 : this);
185 // Reserve the feature in the map of features
186 // (do not want to add several copies of it while adding attributes)
187 aRelated = createFeature(theFeature, &aBuilder);
188 myFeatureMap[theFeature] = aRelated;
189 createArcConstraints(aRelated);
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;
200 // send notification to listeners due to at least one attribute is changed
201 if (sendNotify && isUpdated)
205 if (aRelated && aRelated->type() == ENTITY_ARC) {
206 /// TODO: this code should be shared with FeatureBuilder somehow
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());
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));
218 *anArc->rad = aStart->distance(aCenter);
220 std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(aStart->xy()->decreased(aCenter->xy())));
221 *anArc->startAngle = OX->angle(aDir);
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);
231 bool PlaneGCSSolver_Storage::update(AttributePtr theAttribute, bool theForce)
233 if (!theAttribute->isInitialized())
236 AttributePtr anAttribute = theAttribute;
237 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
239 if (aRefAttr->isObject()) {
241 /// TODO: Check resultToFeatureOrAttribute() precisely.
242 resultToFeatureOrAttribute(aRefAttr->object(), aFeature, anAttribute);
244 return update(aFeature, theForce);
246 anAttribute = aRefAttr->attr();
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;
258 bool isUpdated = updateValues(anAttribute, aRelated);
260 setNeedToResolve(true);
266 void PlaneGCSSolver_Storage::makeExternal(const EntityWrapperPtr& theEntity)
268 if (theEntity->isExternal())
271 removeArcConstraints(theEntity);
273 GCS::SET_pD aParameters = PlaneGCSSolver_Tools::parameters(theEntity);
274 mySketchSolver->removeParameters(aParameters);
275 theEntity->setExternal(true);
276 myNeedToResolve = true;
279 void PlaneGCSSolver_Storage::makeNonExternal(const EntityWrapperPtr& theEntity)
281 if (!theEntity->isExternal())
284 GCS::SET_pD aParameters = PlaneGCSSolver_Tools::parameters(theEntity);
285 mySketchSolver->addParameters(aParameters);
286 theEntity->setExternal(false);
288 createArcConstraints(theEntity);
290 myNeedToResolve = true;
294 void PlaneGCSSolver_Storage::createArcConstraints(const EntityWrapperPtr& theArc)
296 if (!theArc || theArc->type() != ENTITY_ARC || theArc->isExternal())
299 EdgeWrapperPtr anEdge = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theArc);
300 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEdge->entity());
302 // Additional constaints to fix arc's extra DoF (if the arc is not external):
303 std::list<GCSConstraintPtr> anArcConstraints;
304 // 1. distances from center till start and end points are equal to radius
305 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PDistance(
306 anArc->center, anArc->start, anArc->rad)));
307 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PDistance(
308 anArc->center, anArc->end, anArc->rad)));
309 // 2. angles of start and end points should be equal to the arc angles
310 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PAngle(
311 anArc->center, anArc->start, anArc->startAngle)));
312 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PAngle(
313 anArc->center, anArc->end, anArc->endAngle)));
315 ConstraintWrapperPtr aWrapper(
316 new PlaneGCSSolver_ConstraintWrapper(anArcConstraints, CONSTRAINT_UNKNOWN));
317 aWrapper->setId(++myConstraintLastID);
318 constraintsToSolver(aWrapper, mySketchSolver);
320 myArcConstraintMap[theArc] = aWrapper;
323 void PlaneGCSSolver_Storage::removeArcConstraints(const EntityWrapperPtr& theArc)
325 std::map<EntityWrapperPtr, ConstraintWrapperPtr>::iterator
326 aFound = myArcConstraintMap.find(theArc);
327 if (aFound != myArcConstraintMap.end()) {
328 mySketchSolver->removeConstraint(aFound->second->id());
329 myArcConstraintMap.erase(aFound);
334 bool PlaneGCSSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
336 std::map<ConstraintPtr, ConstraintWrapperPtr>::iterator
337 aFound = myConstraintMap.find(theConstraint);
338 if (aFound != myConstraintMap.end()) {
339 ConstraintWrapperPtr aCW = aFound->second;
340 ConstraintID anID = aCW->id();
342 // Remove solver's constraints
343 mySketchSolver->removeConstraint(anID);
345 // Remove value if exists
346 const ScalarWrapperPtr& aValue = aCW->valueParameter();
348 GCS::SET_pD aParToRemove;
349 aParToRemove.insert(aValue->scalar());
350 removeParameters(aParToRemove);
354 myConstraintMap.erase(aFound);
356 if (anID != CID_MOVEMENT)
357 myNeedToResolve = true;
360 notify(theConstraint);
365 void PlaneGCSSolver_Storage::removeInvalidEntities()
367 PlaneGCSSolver_EntityDestroyer aDestroyer;
369 // Remove invalid constraints
370 std::list<ConstraintPtr> anInvalidConstraints;
371 std::map<ConstraintPtr, ConstraintWrapperPtr>::const_iterator
372 aCIter = myConstraintMap.begin();
373 for (; aCIter != myConstraintMap.end(); ++aCIter)
374 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
375 anInvalidConstraints.push_back(aCIter->first);
376 std::list<ConstraintPtr>::const_iterator anInvCIt = anInvalidConstraints.begin();
377 for (; anInvCIt != anInvalidConstraints.end(); ++anInvCIt)
378 removeConstraint(*anInvCIt);
380 // Remove invalid features
381 std::list<FeaturePtr> anInvalidFeatures;
382 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
383 for (; aFIter != myFeatureMap.end(); aFIter++)
384 if (!aFIter->first->data() || !aFIter->first->data()->isValid()) {
385 anInvalidFeatures.push_back(aFIter->first);
387 aDestroyer.remove(aFIter->second);
389 // remove invalid arc
390 removeArcConstraints(aFIter->second);
392 std::list<FeaturePtr>::const_iterator anInvFIt = anInvalidFeatures.begin();
393 for (; anInvFIt != anInvalidFeatures.end(); ++anInvFIt)
394 removeFeature(*anInvFIt);
396 // Remove invalid attributes
397 std::list<AttributePtr> anInvalidAttributes;
398 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anAttrIt = myAttributeMap.begin();
399 for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
400 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttrIt->first->owner());
401 if (!anOwner || !anOwner->data() || !anOwner->data()->isValid()) {
402 anInvalidAttributes.push_back(anAttrIt->first);
403 aDestroyer.remove(anAttrIt->second);
406 std::list<AttributePtr>::const_iterator anInvAtIt = anInvalidAttributes.begin();
407 for (; anInvAtIt != anInvalidAttributes.end(); ++anInvAtIt)
408 removeAttribute(*anInvAtIt);
410 // free memory occupied by parameters
411 removeParameters(aDestroyer.parametersToRemove());
413 /// TODO: Think on optimization of checking invalid features and attributes
418 double* PlaneGCSSolver_Storage::createParameter()
420 return mySketchSolver->createParameter();
423 void PlaneGCSSolver_Storage::removeParameters(const GCS::SET_pD& theParams)
425 mySketchSolver->removeParameters(theParams);
428 // indicates attribute containing in the external feature
429 static bool isExternalAttribute(const AttributePtr& theAttribute)
433 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
434 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
435 return aSketchFeature.get() && aSketchFeature->isExternal();
438 static void addOwnerToSet(const AttributePtr& theAttribute, std::set<FeaturePtr>& theFeatures)
440 FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
442 theFeatures.insert(anOwner);
445 void PlaneGCSSolver_Storage::refresh() const
447 const double aTol = 1000. * tolerance; // tolerance to prevent frequent updates
449 std::set<FeaturePtr> anUpdatedFeatures;
451 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
452 for (; anIt != myAttributeMap.end(); ++anIt) {
453 if (!anIt->first->isInitialized())
456 // the external feature always should keep the up to date values, so,
457 // refresh from the solver is never needed
458 if (isExternalAttribute(anIt->first))
461 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
462 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
464 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPointWrapper =
465 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(anIt->second);
466 GCSPointPtr aGCSPoint = aPointWrapper->point();
467 if (fabs(aPoint2D->x() - (*aGCSPoint->x)) > aTol ||
468 fabs(aPoint2D->y() - (*aGCSPoint->y)) > aTol) {
469 aPoint2D->setValue(*aGCSPoint->x, *aGCSPoint->y);
470 addOwnerToSet(anIt->first, anUpdatedFeatures);
474 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
476 ScalarWrapperPtr aScalarWrapper =
477 std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(anIt->second);
478 if (fabs(aScalar->value() - aScalarWrapper->value()) > aTol) {
479 aScalar->setValue(aScalarWrapper->value());
480 addOwnerToSet(anIt->first, anUpdatedFeatures);
486 // notify listeners about features update
487 std::set<FeaturePtr>::const_iterator aFIt = anUpdatedFeatures.begin();
488 for (; aFIt != anUpdatedFeatures.end(); ++aFIt)