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_Builder.h>
9 #include <PlaneGCSSolver_Solver.h>
10 #include <PlaneGCSSolver_ConstraintWrapper.h>
11 #include <PlaneGCSSolver_EntityWrapper.h>
12 #include <PlaneGCSSolver_PointWrapper.h>
13 #include <PlaneGCSSolver_ScalarWrapper.h>
14 #include <PlaneGCSSolver_ParameterWrapper.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_XY.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <SketchPlugin_Arc.h>
22 #include <SketchPlugin_ConstraintTangent.h>
27 PlaneGCSSolver_Storage::PlaneGCSSolver_Storage(const GroupID& theGroup)
28 : SketchSolver_Storage(theGroup),
29 myEntityLastID(EID_SKETCH),
30 myConstraintLastID(CID_UNKNOWN)
34 void PlaneGCSSolver_Storage::addConstraint(
35 ConstraintPtr theConstraint,
36 std::list<ConstraintWrapperPtr> theSolverConstraints)
38 SketchSolver_Storage::addConstraint(theConstraint, theSolverConstraints);
40 // update point-point coincidence
41 if (!theSolverConstraints.empty() &&
42 theSolverConstraints.front()->type() == CONSTRAINT_PT_PT_COINCIDENT) {
43 std::list<ConstraintWrapperPtr>::iterator aCIt = theSolverConstraints.begin();
44 for (; aCIt != theSolverConstraints.end(); ++aCIt)
50 bool PlaneGCSSolver_Storage::update(ConstraintWrapperPtr theConstraint)
52 bool isUpdated = false;
53 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
54 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
56 // point-Line distance should be positive
57 if (aConstraint->type() == CONSTRAINT_PT_LINE_DISTANCE && aConstraint->value() < 0.0)
58 aConstraint->setValue(-aConstraint->value());
60 // make value of constraint unchangeable
61 ParameterWrapperPtr aValue = aConstraint->valueParameter();
63 isUpdated = update(aValue) || isUpdated;
65 // update constrained entities
66 std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
67 std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
68 for (; anIt != anEntities.end(); ++anIt)
69 isUpdated = update(*anIt) || isUpdated;
71 if (aConstraint->id() == CID_UNKNOWN) {
72 const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
73 // check middle-point constraint conflicts with point-on-line
74 if (aConstraint->type() == CONSTRAINT_MIDDLE_POINT) {
75 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
76 anIt = myConstraintMap.begin();
77 for (; anIt != myConstraintMap.end(); ++anIt) {
78 EntityWrapperPtr aPoint, aLine;
79 ConstraintWrapperPtr aCurrentConstr = anIt->second.front();
80 if (aCurrentConstr->type() != CONSTRAINT_PT_ON_LINE)
82 const std::list<EntityWrapperPtr>& aCurSubs = aCurrentConstr->entities();
83 std::list<EntityWrapperPtr>::const_iterator aSIt1, aSIt2;
84 for (aSIt1 = aSubs.begin(); aSIt1 != aSubs.end(); ++aSIt1) {
85 if ((*aSIt1)->type() == ENTITY_POINT)
87 else if((*aSIt1)->type() == ENTITY_LINE)
91 for (aSIt2 = aCurSubs.begin(); aSIt2 != aCurSubs.end(); ++aSIt2)
92 if ((*aSIt1)->id() == (*aSIt2)->id())
94 if (aSIt2 == aCurSubs.end())
97 // point-on-line found, change it to bisector
98 if (aSIt1 == aSubs.end()) {
99 std::list<GCSConstraintPtr> aConstrList = aConstraint->constraints();
100 aConstrList.pop_front();
101 aConstraint->setConstraints(aConstrList);
107 // Change ID of constraints
108 aConstraint->setId(++myConstraintLastID);
114 /// \brief Update coordinates of the point or scalar using its base attribute
115 static bool updateValues(EntityWrapperPtr& theEntity)
117 bool isUpdated = false;
118 AttributePtr anAttr = theEntity->baseAttribute();
119 const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
123 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
124 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
126 aCoord[0] = aPoint2D->x();
127 aCoord[1] = aPoint2D->y();
129 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
131 aCoord[0] = aScalar->value();
134 std::list<ParameterWrapperPtr>::const_iterator anIt = aParams.begin();
135 for (int i = 0; anIt != aParams.end(); ++anIt, ++i)
136 if (fabs((*anIt)->value() - aCoord[i]) > tolerance) {
137 (*anIt)->setValue(aCoord[i]);
143 bool PlaneGCSSolver_Storage::update(EntityWrapperPtr theEntity)
145 if (theEntity->type() == ENTITY_SKETCH)
146 return true; // sketch is not necessary for PlaneGCS, so it is always says true
148 bool isUpdated = false;
150 if (theEntity->baseAttribute()) {
151 isUpdated = updateValues(theEntity);
153 setNeedToResolve(true);
157 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
158 std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
159 for (; aPIt != aParams.end(); ++aPIt)
160 isUpdated = update(*aPIt) || isUpdated;
162 // update sub-entities
163 std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
164 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
165 for (; aSIt != aSubEntities.end(); ++aSIt)
166 isUpdated = update(*aSIt) || isUpdated;
168 // additional constraints for the arc processing
169 if (theEntity->type() == ENTITY_ARC)
170 processArc(theEntity);
172 // Change entity's ID, if necessary
173 if (theEntity->id() == EID_UNKNOWN) {
174 if (theEntity->type() == ENTITY_POINT) {
175 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
176 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
178 aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
179 theEntity->subEntities().front());
181 aPoint->setId(++myEntityLastID);
182 } else if (theEntity->type() == ENTITY_SCALAR) {
183 std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aScalar =
184 std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theEntity);
185 aScalar->setId(++myEntityLastID);
187 std::shared_ptr<PlaneGCSSolver_EntityWrapper> aGCSEnt =
188 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity);
189 aGCSEnt->setId(++myEntityLastID);
195 bool PlaneGCSSolver_Storage::update(ParameterWrapperPtr theParameter)
197 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
198 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
199 if (aParam->isProcessed())
201 if (theParameter->group() != myGroupID || theParameter->isParametric())
202 myConst.push_back(aParam->parameter());
204 myParameters.push_back(aParam->parameter());
205 aParam->setProcessed(true);
210 bool PlaneGCSSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
212 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
213 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
215 bool isFullyRemoved = true;
216 // remove point-point coincidence
217 if (aConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT)
218 isFullyRemoved = removeCoincidence(theConstraint) && isFullyRemoved;
219 // remove sub-entities
220 const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
221 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
222 for (; aSIt != aSubs.end(); ++ aSIt)
223 isFullyRemoved = remove(*aSIt) && isFullyRemoved;
225 if (aConstraint->valueParameter())
226 isFullyRemoved = remove(aConstraint->valueParameter()) && isFullyRemoved;
227 if (!isFullyRemoved && aConstraint->baseConstraint() &&
228 (!aConstraint->baseConstraint()->data() || !aConstraint->baseConstraint()->data()->isValid()))
229 isFullyRemoved = true;
230 setNeedToResolve(true);
231 myRemovedConstraints.insert(myRemovedConstraints.end(),
232 aConstraint->constraints().begin(), aConstraint->constraints().end());
234 if (isFullyRemoved && theConstraint->id() == myConstraintLastID)
235 --myConstraintLastID;
237 return isFullyRemoved;
240 bool PlaneGCSSolver_Storage::remove(EntityWrapperPtr theEntity)
242 bool isFullyRemoved = SketchSolver_Storage::remove(theEntity);
243 if (isFullyRemoved) {
244 if (theEntity->type() == ENTITY_ARC) {
245 // remove arc additional constraints
246 std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::iterator
247 aFound = myArcConstraintMap.find(theEntity);
248 if (aFound != myArcConstraintMap.end()) {
249 myRemovedConstraints.insert(myRemovedConstraints.end(),
250 aFound->second.begin(), aFound->second.end());
251 myArcConstraintMap.erase(aFound);
254 if (theEntity->id() == myEntityLastID)
257 return isFullyRemoved;
260 bool PlaneGCSSolver_Storage::remove(ParameterWrapperPtr theParameter)
262 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
263 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
264 if (aParam->isProcessed()) {
265 double* aValPtr = aParam->parameter();
266 GCS::VEC_pD::iterator anIt = myParameters.begin();
267 for (; anIt != myParameters.end(); ++anIt)
268 if (*anIt == aValPtr)
270 if (anIt != myParameters.end()) {
271 myParameters.erase(anIt);
272 setNeedToResolve(true);
273 aParam->setProcessed(false);
276 for (anIt = myConst.begin(); anIt != myConst.end(); ++anIt)
277 if (*anIt == aValPtr)
279 if (anIt != myConst.end()) {
281 setNeedToResolve(true);
282 aParam->setProcessed(false);
290 void PlaneGCSSolver_Storage::addCoincidentPoints(
291 EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
293 if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
296 std::shared_ptr<PlaneGCSSolver_PointWrapper> aMaster =
297 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theMaster);
299 aMaster = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
300 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theMaster)->subEntities().front());
301 std::shared_ptr<PlaneGCSSolver_PointWrapper> aSlave =
302 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theSlave);
304 aSlave = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
305 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theSlave)->subEntities().front());
307 // Search available coincidence
308 CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(aMaster);
309 CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(aSlave);
310 if (aMasterFound == myCoincidentPoints.end() && aSlaveFound == myCoincidentPoints.end()) {
311 // try to find master and slave points in the lists of slaves of already existent coincidences
312 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
313 for (; anIt != myCoincidentPoints.end(); ++anIt) {
314 if (anIt->second.find(aMaster) != anIt->second.end())
316 else if (anIt->second.find(aSlave) != anIt->second.end())
319 if (aMasterFound != myCoincidentPoints.end() && aSlaveFound != myCoincidentPoints.end())
324 if (aMasterFound == myCoincidentPoints.end()) {
326 myCoincidentPoints[aMaster] = std::set<EntityWrapperPtr>();
327 aMasterFound = myCoincidentPoints.find(aMaster);
328 } else if (aMasterFound == aSlaveFound)
329 return; // already coincident
331 if (aSlaveFound != myCoincidentPoints.end()) {
332 // A slave has been found, we need to attach all points coincident with it to the new master
333 std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
334 aNewSlaves.insert(aSlaveFound->first);
335 myCoincidentPoints.erase(aSlaveFound);
337 std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
338 for (; aSlIt != aNewSlaves.end(); ++aSlIt)
339 addCoincidentPoints(aMaster, *aSlIt);
341 //std::list<ParameterWrapperPtr> aSlaveParams = aSlave->parameters();
342 //aSlave->setParameters(aMaster->parameters());
344 //// Remove slave's parameters
345 //std::list<ParameterWrapperPtr>::iterator aParIt = aSlaveParams.begin();
346 //for (; aParIt != aSlaveParams.end(); ++aParIt)
349 aMasterFound->second.insert(aSlave);
354 void PlaneGCSSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
356 theEntity->setGroup(theGroup);
357 if (theGroup == myGroupID)
358 makeVariable(theEntity);
360 if (theEntity->type() == ENTITY_POINT)
362 makeConstant(theEntity);
366 void PlaneGCSSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
371 void PlaneGCSSolver_Storage::verifyFixed()
376 void PlaneGCSSolver_Storage::processArc(const EntityWrapperPtr& theArc)
378 // no need to constraint a fixed arc
379 if (theArc->group() == GID_OUTOFGROUP)
382 // Calculate additional parameters necessary for PlaneGCS
383 const std::list<EntityWrapperPtr>& aSubs = theArc->subEntities();
384 std::list<EntityWrapperPtr>::const_iterator aSubIt = aSubs.begin();
385 while ((*aSubIt)->type() == ENTITY_POINT) // search scalar entities
387 double* aStartAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
388 double* aEndAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
389 double* aRadius = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt)->scalar();
391 std::shared_ptr<SketchPlugin_Feature> anArcFeature =
392 std::dynamic_pointer_cast<SketchPlugin_Feature>(theArc->baseFeature());
393 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
394 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
395 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
396 anArcFeature->attribute(SketchPlugin_Arc::START_ID()));
397 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
398 anArcFeature->attribute(SketchPlugin_Arc::END_ID()));
399 if (!aCenterAttr || !aStartAttr || !aEndAttr)
401 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = aCenterAttr->pnt();
402 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aStartAttr->pnt();
403 std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aEndAttr->pnt();
405 *aRadius = aCenterPnt->distance(aStartPnt);
406 if (!anArcFeature->lastResult())
408 std::shared_ptr<GeomAPI_Edge> anArcEdge =
409 std::dynamic_pointer_cast<GeomAPI_Edge>(anArcFeature->lastResult()->shape());
412 anArcEdge->getRange(*aStartAngle, *aEndAngle);
414 // do not constraint copied arc
415 if (anArcFeature->isCopy())
417 // No need to add constraints if they are already exist
418 std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
419 aFound = myArcConstraintMap.find(theArc);
420 if (aFound != myArcConstraintMap.end())
423 // Prepare additional constraints to produce the arc
424 std::vector<GCSConstraintPtr> anArcConstraints;
425 std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt =
426 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theArc);
427 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt->entity());
428 // Distances from center till start and end points are equal to radius
429 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PDistance(
430 anArc->center, anArc->start, anArc->rad)));
431 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PDistance(
432 anArc->center, anArc->end, anArc->rad)));
433 // Angles of start and end points should be equal to given angles
434 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PAngle(
435 anArc->center, anArc->start, anArc->startAngle)));
436 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PAngle(
437 anArc->center, anArc->end, anArc->endAngle)));
439 myArcConstraintMap[theArc] = anArcConstraints;
443 void PlaneGCSSolver_Storage::makeConstant(const EntityWrapperPtr& theEntity)
445 toggleEntity(theEntity, myParameters, myConst);
446 if (theEntity->type() == ENTITY_POINT)
447 updateCoincident(theEntity);
450 void PlaneGCSSolver_Storage::makeVariable(const EntityWrapperPtr& theEntity)
452 toggleEntity(theEntity, myConst, myParameters);
455 static void getParametersToMove(const EntityWrapperPtr& theEntity, std::set<double*>& theParamList)
457 const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
458 std::list<ParameterWrapperPtr>::const_iterator aPIt = aParams.begin();
459 for (; aPIt != aParams.end(); ++aPIt)
461 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*aPIt)->parameter());
463 const std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
464 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
466 if (theEntity->type() == ENTITY_ARC) {
467 // workaround for the arc processing, because the arc is fixed by a set of constraints,
468 // which will conflict with all parameters fixed:
470 getParametersToMove(*aSIt++, theParamList);
471 // 2. take start point
472 getParametersToMove(*aSIt++, theParamList);
473 // 3. skip end point, radius and start angle, but take end angle parameter
474 getParametersToMove(*(++aSIt), theParamList);
476 for (; aSIt != aSubs.end(); ++aSIt)
477 getParametersToMove(*aSIt, theParamList);
481 void PlaneGCSSolver_Storage::toggleEntity(
482 const EntityWrapperPtr& theEntity, GCS::VEC_pD& theFrom, GCS::VEC_pD& theTo)
484 std::set<double*> aParamsToMove;
485 getParametersToMove(theEntity, aParamsToMove);
487 GCS::VEC_pD::iterator anIt = theFrom.begin();
488 while (anIt != theFrom.end()) {
489 if (aParamsToMove.find(*anIt) == aParamsToMove.end()) {
494 theTo.push_back(*anIt);
495 int aShift = int(anIt - theFrom.begin());
497 anIt = theFrom.begin() + aShift;
501 void PlaneGCSSolver_Storage::updateCoincident(const EntityWrapperPtr& thePoint)
503 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
504 for (; anIt != myCoincidentPoints.end(); ++anIt) {
505 if (anIt->first == thePoint || anIt->second.find(thePoint) != anIt->second.end()) {
506 std::set<EntityWrapperPtr> aCoincident = anIt->second;
507 aCoincident.insert(anIt->first);
509 const std::list<ParameterWrapperPtr>& aBaseParams = thePoint->parameters();
510 std::list<ParameterWrapperPtr> aParams;
511 std::list<ParameterWrapperPtr>::const_iterator aBaseIt, anUpdIt;
513 std::set<EntityWrapperPtr>::const_iterator aCoincIt = aCoincident.begin();
514 for (; aCoincIt != aCoincident.end(); ++aCoincIt)
515 if (*aCoincIt != thePoint && (*aCoincIt)->group() != GID_OUTOFGROUP) {
516 aParams = (*aCoincIt)->parameters();
517 aBaseIt = aBaseParams.begin();
518 for (anUpdIt = aParams.begin(); anUpdIt != aParams.end(); ++anUpdIt, ++aBaseIt)
519 (*anUpdIt)->setValue((*aBaseIt)->value());
528 bool PlaneGCSSolver_Storage::isRedundant(
529 GCSConstraintPtr theCheckedConstraint,
530 ConstraintWrapperPtr theParentConstraint,
531 std::list<std::set<double*> >& theCoincidentPoints) const
533 if (theParentConstraint->type() == CONSTRAINT_SYMMETRIC) {
534 if (theCheckedConstraint->getTypeId() == GCS::Perpendicular) {
535 BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
536 // check the initial point is placed on the mirror line
537 std::list<EntityWrapperPtr> aSubs = theParentConstraint->entities();
538 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(aSubs.front());
539 std::shared_ptr<GeomAPI_Lin2d> aLine = aBuilder->line(aSubs.back());
540 return aLine->distance(aPoint) < tolerance;
543 else if (theParentConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT) {
544 // Verify that the coincidence between points is already added
545 GCS::VEC_pD aParams = theCheckedConstraint->params();
547 std::list<std::set<double*> >::iterator aCoincIt, aFound1, aFound2;
548 aFound1 = aFound2 = theCoincidentPoints.end();
549 for (aCoincIt = theCoincidentPoints.begin(); aCoincIt != theCoincidentPoints.end(); ++aCoincIt) {
550 if (aFound1 == theCoincidentPoints.end() && aCoincIt->find(aParams[0]) != aCoincIt->end())
552 if (aFound2 == theCoincidentPoints.end() && aCoincIt->find(aParams[1]) != aCoincIt->end())
554 if (aFound1 != theCoincidentPoints.end() && aFound2 != theCoincidentPoints.end())
557 if (aCoincIt != theCoincidentPoints.end()) { // both point are found
558 if (aFound1 == aFound2)
560 // merge two groups of coincidence
561 aFound1->insert(aFound2->begin(), aFound2->end());
562 theCoincidentPoints.erase(aFound2);
564 if (aFound1 != theCoincidentPoints.end())
565 aFound1->insert(aParams[1]);
566 else if (aFound2 != theCoincidentPoints.end())
567 aFound2->insert(aParams[0]);
569 std::set<double*> aNewCoincidence;
570 aNewCoincidence.insert(aParams[0]);
571 aNewCoincidence.insert(aParams[1]);
572 theCoincidentPoints.push_back(aNewCoincidence);
580 void PlaneGCSSolver_Storage::initializeSolver(SolverPtr theSolver)
582 std::shared_ptr<PlaneGCSSolver_Solver> aSolver =
583 std::dynamic_pointer_cast<PlaneGCSSolver_Solver>(theSolver);
591 // initialize constraints
592 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
593 aCIt = myConstraintMap.begin();
594 GCS::SET_I aTangentIDs;
595 std::list<std::set<double*> > aCoincidentPoints;
596 for (; aCIt != myConstraintMap.end(); ++aCIt) {
597 std::list<ConstraintWrapperPtr>::const_iterator aCWIt = aCIt->second.begin();
598 for (; aCWIt != aCIt->second.end(); ++ aCWIt) {
599 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aGCS =
600 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(*aCWIt);
601 std::list<GCSConstraintPtr>::const_iterator anIt = aGCS->constraints().begin();
602 for (; anIt != aGCS->constraints().end(); ++anIt)
603 if (!isRedundant(*anIt, aGCS, aCoincidentPoints))
604 aSolver->addConstraint(*anIt);
606 // store IDs of tangent constraints to avoid incorrect report of redundant constraints
607 if (aCIt->first && aCIt->first->getKind() == SketchPlugin_ConstraintTangent::ID())
608 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++ aCWIt)
609 aTangentIDs.insert((int)(*aCWIt)->id());
611 // additional constraints for arcs
612 std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
613 anArcIt = myArcConstraintMap.begin();
614 for (; anArcIt != myArcConstraintMap.end(); ++anArcIt) {
615 std::vector<GCSConstraintPtr>::const_iterator anIt = anArcIt->second.begin();
616 for (; anIt != anArcIt->second.end(); ++anIt)
617 aSolver->addConstraint(*anIt);
619 // removed waste constraints
620 std::list<GCSConstraintPtr>::const_iterator aRemIt = myRemovedConstraints.begin();
621 for (; aRemIt != myRemovedConstraints.end(); ++aRemIt)
622 aSolver->removeConstraint(*aRemIt);
623 myRemovedConstraints.clear();
624 // set list of tangent constraints
625 aSolver->setTangent(aTangentIDs);
626 // initialize unknowns
627 aSolver->setParameters(myParameters);
630 void PlaneGCSSolver_Storage::refresh(bool theFixedOnly) const
634 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
635 std::list<ParameterWrapperPtr> aParams;
636 std::list<ParameterWrapperPtr>::const_iterator aParIt;
637 for (; anIt != myAttributeMap.end(); ++anIt) {
638 // the external feature always should keep the up to date values, so,
639 // refresh from the solver is never needed
640 bool isExternal = false;
641 if (anIt->first.get()) {
642 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
643 std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
644 if (aSketchFeature.get() && aSketchFeature->isExternal())
648 // update parameter wrappers and obtain values of attributes
649 aParams = anIt->second->parameters();
651 bool isUpd[3] = {false};
653 for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
654 if (!theFixedOnly || isExternal ||
655 (*aParIt)->group() == GID_OUTOFGROUP || (*aParIt)->isParametric()) {
656 aCoords[i] = (*aParIt)->value();
660 if (!isUpd[0] && !isUpd[1] && !isUpd[2])
661 continue; // nothing is updated
663 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
664 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
666 if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
667 (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance) || isExternal) {
668 if (!isUpd[0] || isExternal) aCoords[0] = aPoint2D->x();
669 if (!isUpd[1] || isExternal) aCoords[1] = aPoint2D->y();
670 aPoint2D->setValue(aCoords[0], aCoords[1]);
671 // Find points coincident with this one (probably not in GID_OUTOFGROUP)
672 CoincidentPointsMap::const_iterator aCoincIt = myCoincidentPoints.begin();
673 for (; aCoincIt != myCoincidentPoints.end(); ++aCoincIt)
674 if (aCoincIt->first == anIt->second ||
675 aCoincIt->second.find(anIt->second) != aCoincIt->second.end())
677 if (aCoincIt != myCoincidentPoints.end()) {
678 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
679 aCoincIt->first->baseAttribute());
681 aPoint2D->setValue(aCoords[0], aCoords[1]);
682 std::set<EntityWrapperPtr>::const_iterator aSlaveIt = aCoincIt->second.begin();
683 for (; aSlaveIt != aCoincIt->second.end(); ++aSlaveIt) {
684 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aSlaveIt)->baseAttribute());
686 aPoint2D->setValue(aCoords[0], aCoords[1]);
692 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
693 if (aScalar && !isExternal) {
694 if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
695 aScalar->setValue(aCoords[0]);
700 //blockEvents(false);
703 EntityWrapperPtr PlaneGCSSolver_Storage::calculateMiddlePoint(
704 EntityWrapperPtr theBase, double theCoeff)
706 std::shared_ptr<PlaneGCSSolver_Builder> aBuilder =
707 std::dynamic_pointer_cast<PlaneGCSSolver_Builder>(PlaneGCSSolver_Builder::getInstance());
709 std::shared_ptr<GeomAPI_XY> aMidPoint;
710 if (theBase->type() == ENTITY_LINE) {
711 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
712 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
713 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
714 for (int i = 0; i < 2; ++i, ++anIt)
715 aPoints[i] = aBuilder->point(*anIt);
716 aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
717 aPoints[1]->xy()->multiplied(theCoeff));
719 else if (theBase->type() == ENTITY_ARC) {
721 double anArcPoint[3][2];
722 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
723 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
724 for (int i = 0; i < 3; ++i, ++anIt) {
725 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
726 anArcPoint[i][0] = aPoint->x();
727 anArcPoint[i][1] = aPoint->y();
729 // project last point of arc on the arc
730 double x = anArcPoint[1][0] - anArcPoint[0][0];
731 double y = anArcPoint[1][1] - anArcPoint[0][1];
732 double aRad = sqrt(x*x + y*y);
733 x = anArcPoint[2][0] - anArcPoint[0][0];
734 y = anArcPoint[2][1] - anArcPoint[0][1];
735 double aNorm = sqrt(x*x + y*y);
736 if (aNorm >= tolerance) {
737 anArcPoint[2][0] = x * aRad / aNorm;
738 anArcPoint[2][1] = y * aRad / aNorm;
740 anArcPoint[1][0] -= anArcPoint[0][0];
741 anArcPoint[1][1] -= anArcPoint[0][1];
742 if (theCoeff < tolerance) {
743 theX = anArcPoint[0][0] + anArcPoint[1][0];
744 theY = anArcPoint[0][1] + anArcPoint[1][1];
745 } else if (1 - theCoeff < tolerance) {
746 theX = anArcPoint[0][0] + anArcPoint[2][0];
747 theY = anArcPoint[0][1] + anArcPoint[2][1];
749 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
750 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
751 double anAngle = aStartDir->angle(aEndDir);
755 double aCos = cos(anAngle);
756 double aSin = sin(anAngle);
757 theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
758 theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
760 aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
764 return EntityWrapperPtr();
766 std::list<ParameterWrapperPtr> aParameters;
767 aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->x()));
768 aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->y()));
769 // Create entity (parameters are not filled)
770 GCSPointPtr aPnt(new GCS::Point);
771 aPnt->x = std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.front())->parameter();
772 aPnt->y = std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.back())->parameter();
774 EntityWrapperPtr aResult(new PlaneGCSSolver_PointWrapper(AttributePtr(), aPnt));
775 aResult->setGroup(myGroupID);
776 aResult->setParameters(aParameters);