1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PlaneGCSSolver_Storage.cpp
4 // Created: 14 Dec 2015
5 // Author: Artem ZHIDKOV
7 #include <PlaneGCSSolver_Storage.h>
8 #include <PlaneGCSSolver_Solver.h>
9 #include <PlaneGCSSolver_ConstraintWrapper.h>
10 #include <PlaneGCSSolver_EntityWrapper.h>
11 #include <PlaneGCSSolver_PointWrapper.h>
13 #include <PlaneGCSSolver_AttributeBuilder.h>
14 #include <PlaneGCSSolver_FeatureBuilder.h>
15 #include <PlaneGCSSolver_EntityDestroyer.h>
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>
27 static void constraintsToSolver(const ConstraintWrapperPtr& theConstraint,
28 const SolverPtr& theSolver)
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, theConstraint->type());
38 PlaneGCSSolver_Storage::PlaneGCSSolver_Storage(const SolverPtr& theSolver)
39 : SketchSolver_Storage(theSolver),
40 myConstraintLastID(CID_UNKNOWN)
44 void PlaneGCSSolver_Storage::addConstraint(
45 ConstraintPtr theConstraint,
46 ConstraintWrapperPtr theSolverConstraint)
48 SketchSolver_Storage::addConstraint(theConstraint, theSolverConstraint);
50 theSolverConstraint->setId(++myConstraintLastID);
51 constraintsToSolver(theSolverConstraint, mySketchSolver);
54 void PlaneGCSSolver_Storage::addTemporaryConstraint(
55 const ConstraintWrapperPtr& theSolverConstraint)
57 if (myConstraintMap.empty())
58 return; // no need to process temporary constraints if there is no active constraint
60 theSolverConstraint->setId(CID_MOVEMENT);
61 constraintsToSolver(theSolverConstraint, mySketchSolver);
65 EntityWrapperPtr PlaneGCSSolver_Storage::createFeature(
66 const FeaturePtr& theFeature,
67 PlaneGCSSolver_EntityBuilder* theBuilder)
69 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
70 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin();
71 for (; anIt != anAttributes.end(); ++anIt)
72 createAttribute(*anIt, theBuilder);
74 EntityWrapperPtr aResult = theBuilder->createFeature(theFeature);
76 addEntity(theFeature, aResult);
80 EntityWrapperPtr PlaneGCSSolver_Storage::createAttribute(
81 const AttributePtr& theAttribute,
82 PlaneGCSSolver_EntityBuilder* theBuilder)
84 EntityWrapperPtr aResult = theBuilder->createAttribute(theAttribute);
86 addEntity(theAttribute, aResult);
90 /// \brief Update value
91 static bool updateValue(const double& theSource, double& theDest)
93 static const double aTol = 1000. * tolerance;
94 bool isUpdated = fabs(theSource - theDest) > aTol;
100 /// \brief Update coordinates of the point or scalar using its base attribute
101 static bool updateValues(AttributePtr& theAttribute, EntityWrapperPtr& theEntity)
103 bool isUpdated = false;
105 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
106 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
108 const GCSPointPtr& aGCSPoint =
109 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity)->point();
110 isUpdated = updateValue(aPoint2D->x(), *(aGCSPoint->x)) || isUpdated;
111 isUpdated = updateValue(aPoint2D->y(), *(aGCSPoint->y)) || isUpdated;
113 AttributeDoublePtr aScalar =
114 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
116 ScalarWrapperPtr aWrapper =
117 std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theEntity);
118 // There is possible angular value, which is converted between degrees and radians.
119 // So, we use its value instead of using direct pointer to value.
120 double aValue = aWrapper->value();
121 isUpdated = updateValue(aScalar->value(), aValue);
123 aWrapper->setValue(aValue);
130 static bool isCopyInMulti(std::shared_ptr<SketchPlugin_Feature> theFeature)
135 bool aResult = theFeature->isCopy();
137 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
138 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
139 aRefIt != aRefs.end() && aResult; ++aRefIt) {
140 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
141 if (anOwner->getKind() == SketchPlugin_Projection::ID())
148 bool PlaneGCSSolver_Storage::update(FeaturePtr theFeature, bool theForce)
150 bool isUpdated = false;
151 EntityWrapperPtr aRelated = entity(theFeature);
152 if (aRelated) // send signal to subscribers
154 else { // Feature is not exist, create it
155 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
156 std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
157 bool isCopy = isCopyInMulti(aSketchFeature);
158 // the feature is a copy in "Multi" constraint and does not used in other constraints
159 if (!theForce && isCopy && myFeatureMap.find(theFeature) == myFeatureMap.end())
162 // external feature processing
163 bool isExternal = (aSketchFeature && (aSketchFeature->isExternal() || isCopy));
165 PlaneGCSSolver_FeatureBuilder aBuilder(isExternal ? 0 : this);
167 // Reserve the feature in the map of features
168 // (do not want to add several copies of it while adding attributes)
169 aRelated = createFeature(theFeature, &aBuilder);
170 myFeatureMap[theFeature] = aRelated;
172 const std::list<GCSConstraintPtr>& aConstraints = aBuilder.constraints();
173 if (!aConstraints.empty()) { // the feature is arc
174 /// TODO: avoid this workaround
175 ConstraintWrapperPtr aWrapper(
176 new PlaneGCSSolver_ConstraintWrapper(aConstraints, CONSTRAINT_UNKNOWN));
177 aWrapper->setId(++myConstraintLastID);
178 constraintsToSolver(aWrapper, mySketchSolver);
180 myArcConstraintMap[myFeatureMap[theFeature]] = aWrapper;
185 std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
186 std::list<AttributePtr>::iterator anAttrIt = anAttributes.begin();
187 for (; anAttrIt != anAttributes.end(); ++anAttrIt)
188 if ((*anAttrIt)->attributeType() == GeomDataAPI_Point2D::typeId() ||
189 (*anAttrIt)->attributeType() == ModelAPI_AttributeDouble::typeId())
190 isUpdated = update(*anAttrIt) || isUpdated;
193 if (aRelated && aRelated->type() == ENTITY_ARC) {
194 /// TODO: this code should be shared with FeatureBuilder somehow
196 std::shared_ptr<PlaneGCSSolver_EntityWrapper> anEntity =
197 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(aRelated);
198 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEntity->entity());
200 static std::shared_ptr<GeomAPI_Dir2d> OX(new GeomAPI_Dir2d(1.0, 0.0));
201 std::shared_ptr<GeomAPI_Pnt2d> aCenter(
202 new GeomAPI_Pnt2d(*anArc->center.x, *anArc->center.y));
203 std::shared_ptr<GeomAPI_Pnt2d> aStart(
204 new GeomAPI_Pnt2d(*anArc->start.x, *anArc->start.y));
206 *anArc->rad = aStart->distance(aCenter);
208 std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(aStart->xy()->decreased(aCenter->xy())));
209 *anArc->startAngle = OX->angle(aDir);
211 aDir = std::shared_ptr<GeomAPI_Dir2d>(
212 new GeomAPI_Dir2d((*anArc->end.x) - aCenter->x(), (*anArc->end.y) - aCenter->y()));
213 *anArc->endAngle = OX->angle(aDir);
219 bool PlaneGCSSolver_Storage::update(AttributePtr theAttribute, bool theForce)
221 if (!theAttribute->isInitialized())
224 AttributePtr anAttribute = theAttribute;
225 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
227 if (aRefAttr->isObject()) {
229 /// TODO: Check resultToFeatureOrAttribute() precisely.
230 resultToFeatureOrAttribute(aRefAttr->object(), aFeature, anAttribute);
232 return update(aFeature, theForce);
234 anAttribute = aRefAttr->attr();
237 EntityWrapperPtr aRelated = entity(anAttribute);
238 if (!aRelated) { // Attribute does not exist, create it.
239 // First of all check if the parent feature exists. If not, add it.
240 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
241 if (aFeature && myFeatureMap.find(aFeature) == myFeatureMap.end())
242 return update(aFeature, theForce); // theAttribute has been processed while adding feature
244 //// PlaneGCSSolver_AttributeBuilder aBuilder(this);
245 //// aRelated = createAttribute(anAttribute, &aBuilder);
246 return aRelated.get() != 0;
249 bool isUpdated = updateValues(anAttribute, aRelated);
251 setNeedToResolve(true);
257 bool PlaneGCSSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
259 std::map<ConstraintPtr, ConstraintWrapperPtr>::iterator
260 aFound = myConstraintMap.find(theConstraint);
261 if (aFound != myConstraintMap.end()) {
262 ConstraintID anID = aFound->second->id();
263 // Remove solver's constraints
264 mySketchSolver->removeConstraint(anID);
266 myConstraintMap.erase(aFound);
268 if (anID != CID_MOVEMENT)
269 myNeedToResolve = true;
272 notify(theConstraint);
277 void PlaneGCSSolver_Storage::removeInvalidEntities()
279 PlaneGCSSolver_EntityDestroyer aDestroyer;
281 // Remove invalid constraints
282 std::list<ConstraintPtr> anInvalidConstraints;
283 std::map<ConstraintPtr, ConstraintWrapperPtr>::const_iterator
284 aCIter = myConstraintMap.begin();
285 for (; aCIter != myConstraintMap.end(); ++aCIter)
286 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
287 anInvalidConstraints.push_back(aCIter->first);
288 std::list<ConstraintPtr>::const_iterator anInvCIt = anInvalidConstraints.begin();
289 for (; anInvCIt != anInvalidConstraints.end(); ++anInvCIt)
290 removeConstraint(*anInvCIt);
292 // Remove invalid features
293 std::list<FeaturePtr> anInvalidFeatures;
294 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
295 for (; aFIter != myFeatureMap.end(); aFIter++)
296 if (!aFIter->first->data() || !aFIter->first->data()->isValid()) {
297 anInvalidFeatures.push_back(aFIter->first);
298 aDestroyer.remove(aFIter->second);
300 // remove invalid arc
301 std::map<EntityWrapperPtr, ConstraintWrapperPtr>::iterator
302 aFound = myArcConstraintMap.find(aFIter->second);
303 if (aFound != myArcConstraintMap.end()) {
304 mySketchSolver->removeConstraint(aFound->second->id());
305 myArcConstraintMap.erase(aFound);
308 std::list<FeaturePtr>::const_iterator anInvFIt = anInvalidFeatures.begin();
309 for (; anInvFIt != anInvalidFeatures.end(); ++anInvFIt)
310 removeFeature(*anInvFIt);
312 // Remove invalid attributes
313 std::list<AttributePtr> anInvalidAttributes;
314 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anAttrIt = myAttributeMap.begin();
315 for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
316 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttrIt->first->owner());
317 if (!anOwner || !anOwner->data() || !anOwner->data()->isValid()) {
318 anInvalidAttributes.push_back(anAttrIt->first);
319 aDestroyer.remove(anAttrIt->second);
322 std::list<AttributePtr>::const_iterator anInvAtIt = anInvalidAttributes.begin();
323 for (; anInvAtIt != anInvalidAttributes.end(); ++anInvAtIt)
324 removeAttribute(*anInvAtIt);
326 // free memory occupied by parameters
327 removeParameters(aDestroyer.parametersToRemove());
329 /// TODO: Think on optimization of checking invalid features and attributes
334 double* PlaneGCSSolver_Storage::createParameter()
336 return mySketchSolver->createParameter();
339 void PlaneGCSSolver_Storage::removeParameters(const GCS::SET_pD& theParams)
341 mySketchSolver->removeParameters(theParams);
344 // indicates attribute containing in the external feature
345 bool isExternalAttribute(const AttributePtr& theAttribute)
349 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
350 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
351 return aSketchFeature.get() && aSketchFeature->isExternal();
354 void PlaneGCSSolver_Storage::refresh() const
356 const double aTol = 1000. * tolerance; // tolerance to prevent frequent updates
358 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
359 for (; anIt != myAttributeMap.end(); ++anIt) {
360 // the external feature always should keep the up to date values, so,
361 // refresh from the solver is never needed
362 if (isExternalAttribute(anIt->first))
365 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
366 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
368 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPointWrapper =
369 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(anIt->second);
370 GCSPointPtr aGCSPoint = aPointWrapper->point();
371 if (fabs(aPoint2D->x() - (*aGCSPoint->x)) > aTol ||
372 fabs(aPoint2D->y() - (*aGCSPoint->y)) > aTol)
373 aPoint2D->setValue(*aGCSPoint->x, *aGCSPoint->y);
376 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
378 ScalarWrapperPtr aScalarWrapper =
379 std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(anIt->second);
380 if (fabs(aScalar->value() - aScalarWrapper->value()) > aTol)
381 aScalar->setValue(aScalarWrapper->value());