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 if (anIt->second.empty())
81 ConstraintWrapperPtr aCurrentConstr = anIt->second.front();
82 if (aCurrentConstr->type() != CONSTRAINT_PT_ON_LINE)
84 const std::list<EntityWrapperPtr>& aCurSubs = aCurrentConstr->entities();
85 std::list<EntityWrapperPtr>::const_iterator aSIt1, aSIt2;
86 for (aSIt1 = aSubs.begin(); aSIt1 != aSubs.end(); ++aSIt1) {
87 if ((*aSIt1)->type() == ENTITY_POINT)
89 else if((*aSIt1)->type() == ENTITY_LINE)
93 for (aSIt2 = aCurSubs.begin(); aSIt2 != aCurSubs.end(); ++aSIt2)
94 if ((*aSIt1)->id() == (*aSIt2)->id())
96 if (aSIt2 == aCurSubs.end())
99 // point-on-line found, change it to bisector
100 if (aSIt1 == aSubs.end()) {
101 std::list<GCSConstraintPtr> aConstrList = aConstraint->constraints();
102 aConstrList.pop_front();
103 aConstraint->setConstraints(aConstrList);
109 // Change ID of constraints
110 aConstraint->setId(++myConstraintLastID);
116 /// \brief Update coordinates of the point or scalar using its base attribute
117 static bool updateValues(EntityWrapperPtr& theEntity)
119 bool isUpdated = false;
120 AttributePtr anAttr = theEntity->baseAttribute();
121 const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
125 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
126 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
128 aCoord[0] = aPoint2D->x();
129 aCoord[1] = aPoint2D->y();
131 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
133 aCoord[0] = aScalar->value();
136 std::list<ParameterWrapperPtr>::const_iterator anIt = aParams.begin();
137 for (int i = 0; anIt != aParams.end(); ++anIt, ++i)
138 if (fabs((*anIt)->value() - aCoord[i]) > tolerance) {
139 (*anIt)->setValue(aCoord[i]);
145 bool PlaneGCSSolver_Storage::update(EntityWrapperPtr theEntity)
147 if (theEntity->type() == ENTITY_SKETCH)
148 return true; // sketch is not necessary for PlaneGCS, so it is always says true
150 bool isUpdated = false;
152 if (theEntity->baseAttribute()) {
153 isUpdated = updateValues(theEntity);
155 setNeedToResolve(true);
156 if (theEntity->type() == ENTITY_POINT && theEntity->group() != myGroupID)
157 updateCoincident(theEntity);
162 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
163 std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
164 for (; aPIt != aParams.end(); ++aPIt)
165 isUpdated = update(*aPIt) || isUpdated;
167 // update sub-entities
168 std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
169 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
170 for (; aSIt != aSubEntities.end(); ++aSIt)
171 isUpdated = update(*aSIt) || isUpdated;
173 // additional constraints for the arc processing
174 if (theEntity->type() == ENTITY_ARC)
175 processArc(theEntity);
177 // Change entity's ID, if necessary
178 if (theEntity->id() == EID_UNKNOWN) {
179 if (theEntity->type() == ENTITY_POINT) {
180 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
181 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
183 aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
184 theEntity->subEntities().front());
186 aPoint->setId(++myEntityLastID);
187 } else if (theEntity->type() == ENTITY_SCALAR) {
188 std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aScalar =
189 std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theEntity);
190 aScalar->setId(++myEntityLastID);
192 std::shared_ptr<PlaneGCSSolver_EntityWrapper> aGCSEnt =
193 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity);
194 aGCSEnt->setId(++myEntityLastID);
200 bool PlaneGCSSolver_Storage::update(ParameterWrapperPtr theParameter)
202 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
203 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
204 if (aParam->isProcessed())
206 if (theParameter->group() != myGroupID || theParameter->isParametric())
207 myConst.push_back(aParam->parameter());
209 myParameters.push_back(aParam->parameter());
210 aParam->setProcessed(true);
215 bool PlaneGCSSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
217 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
218 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
220 bool isFullyRemoved = true;
221 // remove point-point coincidence
222 if (aConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT)
223 isFullyRemoved = removeCoincidence(theConstraint) && isFullyRemoved;
224 // remove sub-entities
225 const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
226 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
227 for (; aSIt != aSubs.end(); ++ aSIt)
228 isFullyRemoved = remove(*aSIt) && isFullyRemoved;
230 if (aConstraint->valueParameter())
231 isFullyRemoved = remove(aConstraint->valueParameter()) && isFullyRemoved;
232 if (!isFullyRemoved && aConstraint->baseConstraint() &&
233 (!aConstraint->baseConstraint()->data() || !aConstraint->baseConstraint()->data()->isValid()))
234 isFullyRemoved = true;
235 setNeedToResolve(true);
236 myRemovedConstraints.insert(myRemovedConstraints.end(),
237 aConstraint->constraints().begin(), aConstraint->constraints().end());
239 if (isFullyRemoved && theConstraint->id() == myConstraintLastID)
240 --myConstraintLastID;
242 return isFullyRemoved;
245 bool PlaneGCSSolver_Storage::remove(EntityWrapperPtr theEntity)
247 // do not remove entity, if it is used by constraints or other entities
248 if ((theEntity->baseFeature() && isUsed(theEntity->baseFeature())) ||
249 (theEntity->baseAttribute() && isUsed(theEntity->baseAttribute())))
252 bool isFullyRemoved = SketchSolver_Storage::remove(theEntity);
253 if (isFullyRemoved) {
254 if (theEntity->type() == ENTITY_ARC) {
255 // remove arc additional constraints
256 std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::iterator
257 aFound = myArcConstraintMap.find(theEntity);
258 if (aFound != myArcConstraintMap.end()) {
259 myRemovedConstraints.insert(myRemovedConstraints.end(),
260 aFound->second.begin(), aFound->second.end());
261 myArcConstraintMap.erase(aFound);
264 if (theEntity->id() == myEntityLastID)
267 return isFullyRemoved;
270 bool PlaneGCSSolver_Storage::remove(ParameterWrapperPtr theParameter)
272 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
273 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
274 if (aParam->isProcessed()) {
275 double* aValPtr = aParam->parameter();
276 GCS::VEC_pD::iterator anIt = myParameters.begin();
277 for (; anIt != myParameters.end(); ++anIt)
278 if (*anIt == aValPtr)
280 if (anIt != myParameters.end()) {
281 myParameters.erase(anIt);
282 setNeedToResolve(true);
283 aParam->setProcessed(false);
286 for (anIt = myConst.begin(); anIt != myConst.end(); ++anIt)
287 if (*anIt == aValPtr)
289 if (anIt != myConst.end()) {
291 setNeedToResolve(true);
292 aParam->setProcessed(false);
300 void PlaneGCSSolver_Storage::addCoincidentPoints(
301 EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
303 if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
306 std::shared_ptr<PlaneGCSSolver_PointWrapper> aMaster =
307 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theMaster);
309 aMaster = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
310 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theMaster)->subEntities().front());
311 std::shared_ptr<PlaneGCSSolver_PointWrapper> aSlave =
312 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theSlave);
314 aSlave = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
315 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theSlave)->subEntities().front());
317 // Search available coincidence
318 CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(aMaster);
319 CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(aSlave);
320 if (aMasterFound == myCoincidentPoints.end() && aSlaveFound == myCoincidentPoints.end()) {
321 // try to find master and slave points in the lists of slaves of already existent coincidences
322 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
323 for (; anIt != myCoincidentPoints.end(); ++anIt) {
324 if (anIt->second.find(aMaster) != anIt->second.end())
326 else if (anIt->second.find(aSlave) != anIt->second.end())
329 if (aMasterFound != myCoincidentPoints.end() && aSlaveFound != myCoincidentPoints.end())
334 if (aMasterFound == myCoincidentPoints.end()) {
336 myCoincidentPoints[aMaster] = std::set<EntityWrapperPtr>();
337 aMasterFound = myCoincidentPoints.find(aMaster);
338 } else if (aMasterFound == aSlaveFound)
339 return; // already coincident
341 if (aSlaveFound != myCoincidentPoints.end()) {
342 // A slave has been found, we need to attach all points coincident with it to the new master
343 std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
344 aNewSlaves.insert(aSlaveFound->first);
345 myCoincidentPoints.erase(aSlaveFound);
347 std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
348 for (; aSlIt != aNewSlaves.end(); ++aSlIt)
349 addCoincidentPoints(aMaster, *aSlIt);
351 //std::list<ParameterWrapperPtr> aSlaveParams = aSlave->parameters();
352 //aSlave->setParameters(aMaster->parameters());
354 //// Remove slave's parameters
355 //std::list<ParameterWrapperPtr>::iterator aParIt = aSlaveParams.begin();
356 //for (; aParIt != aSlaveParams.end(); ++aParIt)
359 aMasterFound->second.insert(aSlave);
364 void PlaneGCSSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
366 theEntity->setGroup(theGroup);
367 if (theGroup == myGroupID)
368 makeVariable(theEntity);
370 if (theEntity->type() == ENTITY_POINT)
372 makeConstant(theEntity);
376 void PlaneGCSSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
381 void PlaneGCSSolver_Storage::verifyFixed()
386 void PlaneGCSSolver_Storage::processArc(const EntityWrapperPtr& theArc)
388 // Calculate additional parameters necessary for PlaneGCS
389 const std::list<EntityWrapperPtr>& aSubs = theArc->subEntities();
390 std::list<EntityWrapperPtr>::const_iterator aSubIt = aSubs.begin();
391 while ((*aSubIt)->type() == ENTITY_POINT) // search scalar entities
393 double* aStartAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
394 double* aEndAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
395 double* aRadius = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt)->scalar();
397 std::shared_ptr<SketchPlugin_Feature> anArcFeature =
398 std::dynamic_pointer_cast<SketchPlugin_Feature>(theArc->baseFeature());
399 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
400 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
401 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
402 anArcFeature->attribute(SketchPlugin_Arc::START_ID()));
403 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
404 anArcFeature->attribute(SketchPlugin_Arc::END_ID()));
405 if (!aCenterAttr || !aStartAttr || !aEndAttr)
407 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = aCenterAttr->pnt();
408 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aStartAttr->pnt();
409 std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aEndAttr->pnt();
411 *aRadius = aCenterPnt->distance(aStartPnt);
412 if (!anArcFeature->lastResult())
414 std::shared_ptr<GeomAPI_Edge> anArcEdge =
415 std::dynamic_pointer_cast<GeomAPI_Edge>(anArcFeature->lastResult()->shape());
418 anArcEdge->getRange(*aStartAngle, *aEndAngle);
419 // verify the range is correct and not shifted to an angle
420 std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(cos(*aStartAngle), sin(*aStartAngle)));
421 std::shared_ptr<GeomAPI_Pnt2d> aCalcStartPnt(
422 new GeomAPI_Pnt2d(aCenterPnt->xy()->added(aDir->xy()->multiplied(*aRadius))));
423 if (aCalcStartPnt->distance(aStartPnt) > tolerance) {
424 std::shared_ptr<GeomAPI_Dir2d> aDirToStart(
425 new GeomAPI_Dir2d(aStartPnt->xy()->decreased(aCenterPnt->xy())));
426 double anAngle = aDir->angle(aDirToStart);
427 *aStartAngle += anAngle;
428 *aEndAngle += anAngle;
431 // no need to constraint a copied arc
432 if (anArcFeature->isCopy())
434 // No need to add constraints if they are already exist
435 std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
436 aFound = myArcConstraintMap.find(theArc);
437 if (aFound != myArcConstraintMap.end())
440 // Prepare additional constraints to produce the arc
441 std::vector<GCSConstraintPtr> anArcConstraints;
442 std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt =
443 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theArc);
444 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt->entity());
445 // Distances from center till start and end points are equal to radius
446 GCSConstraintPtr aNew = GCSConstraintPtr(new GCS::ConstraintP2PDistance(
447 anArc->center, anArc->start, anArc->rad));
448 // aNew->setTag((int)(++myConstraintLastID));
449 anArcConstraints.push_back(aNew);
450 aNew = GCSConstraintPtr(new GCS::ConstraintP2PDistance(
451 anArc->center, anArc->end, anArc->rad));
452 // aNew->setTag((int)myConstraintLastID);
453 anArcConstraints.push_back(aNew);
454 // Angles of start and end points should be equal to given angles
455 aNew = GCSConstraintPtr(new GCS::ConstraintP2PAngle(
456 anArc->center, anArc->start, anArc->startAngle));
457 // aNew->setTag((int)myConstraintLastID);
458 anArcConstraints.push_back(aNew);
459 aNew = GCSConstraintPtr(new GCS::ConstraintP2PAngle(
460 anArc->center, anArc->end, anArc->endAngle));
461 // aNew->setTag((int)myConstraintLastID);
462 anArcConstraints.push_back(aNew);
464 myArcConstraintMap[theArc] = anArcConstraints;
468 void PlaneGCSSolver_Storage::makeConstant(const EntityWrapperPtr& theEntity)
470 toggleEntity(theEntity, myParameters, myConst);
471 if (theEntity->type() == ENTITY_POINT)
472 updateCoincident(theEntity);
475 void PlaneGCSSolver_Storage::makeVariable(const EntityWrapperPtr& theEntity)
477 toggleEntity(theEntity, myConst, myParameters);
480 static void getParametersToMove(const EntityWrapperPtr& theEntity, std::set<double*>& theParamList)
482 const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
483 std::list<ParameterWrapperPtr>::const_iterator aPIt = aParams.begin();
484 for (; aPIt != aParams.end(); ++aPIt)
486 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*aPIt)->parameter());
488 const std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
489 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
491 if (theEntity->type() == ENTITY_ARC) {
492 // workaround for the arc processing, because the arc is fixed by a set of constraints,
493 // which will conflict with all parameters fixed:
495 getParametersToMove(*aSIt++, theParamList);
496 // 2. skip start and end points
498 // 3. take radius, start angle and end angle parameters
499 getParametersToMove(*(++aSIt), theParamList);
500 getParametersToMove(*(++aSIt), theParamList);
501 getParametersToMove(*(++aSIt), theParamList);
503 for (; aSIt != aSubs.end(); ++aSIt)
504 getParametersToMove(*aSIt, theParamList);
508 void PlaneGCSSolver_Storage::toggleEntity(
509 const EntityWrapperPtr& theEntity, GCS::VEC_pD& theFrom, GCS::VEC_pD& theTo)
511 std::set<double*> aParamsToMove;
512 getParametersToMove(theEntity, aParamsToMove);
514 GCS::VEC_pD::iterator anIt = theFrom.begin();
515 while (anIt != theFrom.end()) {
516 if (aParamsToMove.find(*anIt) == aParamsToMove.end()) {
521 theTo.push_back(*anIt);
522 int aShift = int(anIt - theFrom.begin());
524 anIt = theFrom.begin() + aShift;
528 void PlaneGCSSolver_Storage::updateCoincident(const EntityWrapperPtr& thePoint)
530 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
531 for (; anIt != myCoincidentPoints.end(); ++anIt) {
532 if (anIt->first == thePoint || anIt->second.find(thePoint) != anIt->second.end()) {
533 std::set<EntityWrapperPtr> aCoincident = anIt->second;
534 aCoincident.insert(anIt->first);
536 const std::list<ParameterWrapperPtr>& aBaseParams = thePoint->parameters();
537 std::list<ParameterWrapperPtr> aParams;
538 std::list<ParameterWrapperPtr>::const_iterator aBaseIt, anUpdIt;
540 std::set<EntityWrapperPtr>::const_iterator aCoincIt = aCoincident.begin();
541 for (; aCoincIt != aCoincident.end(); ++aCoincIt)
542 if (*aCoincIt != thePoint && (*aCoincIt)->group() != GID_OUTOFGROUP) {
543 aParams = (*aCoincIt)->parameters();
544 aBaseIt = aBaseParams.begin();
545 for (anUpdIt = aParams.begin(); anUpdIt != aParams.end(); ++anUpdIt, ++aBaseIt)
546 (*anUpdIt)->setValue((*aBaseIt)->value());
555 bool PlaneGCSSolver_Storage::isRedundant(
556 GCSConstraintPtr theCheckedConstraint,
557 ConstraintWrapperPtr theParentConstraint,
558 std::list<std::set<double*> >& theCoincidentPoints) const
560 if (theParentConstraint->type() == CONSTRAINT_SYMMETRIC) {
561 if (theCheckedConstraint->getTypeId() == GCS::Perpendicular) {
562 BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
563 // check the initial point is placed on the mirror line
564 std::list<EntityWrapperPtr> aSubs = theParentConstraint->entities();
565 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(aSubs.front());
566 std::shared_ptr<GeomAPI_Lin2d> aLine = aBuilder->line(aSubs.back());
567 return aLine->distance(aPoint) < tolerance;
570 else if (theParentConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT) {
571 // Verify that the coincidence between points is already added
572 GCS::VEC_pD aParams = theCheckedConstraint->params();
574 std::list<std::set<double*> >::iterator aCoincIt, aFound1, aFound2;
575 aFound1 = aFound2 = theCoincidentPoints.end();
576 for (aCoincIt = theCoincidentPoints.begin(); aCoincIt != theCoincidentPoints.end(); ++aCoincIt) {
577 if (aFound1 == theCoincidentPoints.end() && aCoincIt->find(aParams[0]) != aCoincIt->end())
579 if (aFound2 == theCoincidentPoints.end() && aCoincIt->find(aParams[1]) != aCoincIt->end())
581 if (aFound1 != theCoincidentPoints.end() && aFound2 != theCoincidentPoints.end())
584 if (aCoincIt != theCoincidentPoints.end()) { // both point are found
585 if (aFound1 == aFound2)
587 // merge two groups of coincidence
588 aFound1->insert(aFound2->begin(), aFound2->end());
589 theCoincidentPoints.erase(aFound2);
591 if (aFound1 != theCoincidentPoints.end())
592 aFound1->insert(aParams[1]);
593 else if (aFound2 != theCoincidentPoints.end())
594 aFound2->insert(aParams[0]);
596 std::set<double*> aNewCoincidence;
597 aNewCoincidence.insert(aParams[0]);
598 aNewCoincidence.insert(aParams[1]);
599 theCoincidentPoints.push_back(aNewCoincidence);
607 void PlaneGCSSolver_Storage::initializeSolver(SolverPtr theSolver)
609 std::shared_ptr<PlaneGCSSolver_Solver> aSolver =
610 std::dynamic_pointer_cast<PlaneGCSSolver_Solver>(theSolver);
618 // initialize constraints
619 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
620 aCIt = myConstraintMap.begin();
621 GCS::SET_I aTangentIDs;
622 std::list<std::set<double*> > aCoincidentPoints;
623 for (; aCIt != myConstraintMap.end(); ++aCIt) {
624 std::list<ConstraintWrapperPtr>::const_iterator aCWIt = aCIt->second.begin();
625 for (; aCWIt != aCIt->second.end(); ++ aCWIt) {
626 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aGCS =
627 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(*aCWIt);
628 std::list<GCSConstraintPtr>::const_iterator anIt = aGCS->constraints().begin();
629 for (; anIt != aGCS->constraints().end(); ++anIt)
630 if (!isRedundant(*anIt, aGCS, aCoincidentPoints))
631 aSolver->addConstraint(*anIt, aGCS->type());
633 // store IDs of tangent constraints to avoid incorrect report of redundant constraints
634 if (aCIt->first && aCIt->first->getKind() == SketchPlugin_ConstraintTangent::ID())
635 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++ aCWIt)
636 aTangentIDs.insert((int)(*aCWIt)->id());
638 // additional constraints for arcs
639 std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
640 anArcIt = myArcConstraintMap.begin();
641 for (; anArcIt != myArcConstraintMap.end(); ++anArcIt) {
642 std::vector<GCSConstraintPtr>::const_iterator anIt = anArcIt->second.begin();
643 for (; anIt != anArcIt->second.end(); ++anIt)
644 aSolver->addConstraint(*anIt, CONSTRAINT_UNKNOWN);
646 // removed waste constraints
647 std::list<GCSConstraintPtr>::const_iterator aRemIt = myRemovedConstraints.begin();
648 for (; aRemIt != myRemovedConstraints.end(); ++aRemIt)
649 aSolver->removeConstraint(*aRemIt);
650 myRemovedConstraints.clear();
651 // set list of tangent constraints
652 aSolver->setTangent(aTangentIDs);
653 // initialize unknowns
654 aSolver->setParameters(myParameters);
657 void PlaneGCSSolver_Storage::refresh(bool theFixedOnly) const
661 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
662 std::list<ParameterWrapperPtr> aParams;
663 std::list<ParameterWrapperPtr>::const_iterator aParIt;
664 for (; anIt != myAttributeMap.end(); ++anIt) {
665 // the external feature always should keep the up to date values, so,
666 // refresh from the solver is never needed
667 bool isExternal = false;
668 if (anIt->first.get()) {
669 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
670 std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
671 if (aSketchFeature.get() && aSketchFeature->isExternal())
675 // update parameter wrappers and obtain values of attributes
676 aParams = anIt->second->parameters();
678 bool isUpd[3] = {false};
680 for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
681 if (!theFixedOnly || isExternal ||
682 (*aParIt)->group() == GID_OUTOFGROUP || (*aParIt)->isParametric()) {
683 aCoords[i] = (*aParIt)->value();
687 if (!isUpd[0] && !isUpd[1] && !isUpd[2])
688 continue; // nothing is updated
690 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
691 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
693 if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
694 (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance) || isExternal) {
695 if (!isUpd[0] || isExternal) aCoords[0] = aPoint2D->x();
696 if (!isUpd[1] || isExternal) aCoords[1] = aPoint2D->y();
697 aPoint2D->setValue(aCoords[0], aCoords[1]);
698 // Find points coincident with this one (probably not in GID_OUTOFGROUP)
699 CoincidentPointsMap::const_iterator aCoincIt = myCoincidentPoints.begin();
700 for (; aCoincIt != myCoincidentPoints.end(); ++aCoincIt)
701 if (aCoincIt->first == anIt->second ||
702 aCoincIt->second.find(anIt->second) != aCoincIt->second.end())
704 if (aCoincIt != myCoincidentPoints.end()) {
705 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
706 aCoincIt->first->baseAttribute());
708 aPoint2D->setValue(aCoords[0], aCoords[1]);
709 std::set<EntityWrapperPtr>::const_iterator aSlaveIt = aCoincIt->second.begin();
710 for (; aSlaveIt != aCoincIt->second.end(); ++aSlaveIt) {
711 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aSlaveIt)->baseAttribute());
713 aPoint2D->setValue(aCoords[0], aCoords[1]);
719 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
720 if (aScalar && !isExternal) {
721 if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
722 aScalar->setValue(aCoords[0]);
727 //blockEvents(false);
730 EntityWrapperPtr PlaneGCSSolver_Storage::calculateMiddlePoint(
731 EntityWrapperPtr theBase, double theCoeff)
733 std::shared_ptr<PlaneGCSSolver_Builder> aBuilder =
734 std::dynamic_pointer_cast<PlaneGCSSolver_Builder>(PlaneGCSSolver_Builder::getInstance());
736 std::shared_ptr<GeomAPI_XY> aMidPoint;
737 if (theBase->type() == ENTITY_LINE) {
738 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
739 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
740 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
741 for (int i = 0; i < 2; ++i, ++anIt)
742 aPoints[i] = aBuilder->point(*anIt);
743 aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
744 aPoints[1]->xy()->multiplied(theCoeff));
746 else if (theBase->type() == ENTITY_ARC) {
748 double anArcPoint[3][2];
749 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
750 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
751 for (int i = 0; i < 3; ++i, ++anIt) {
752 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
753 anArcPoint[i][0] = aPoint->x();
754 anArcPoint[i][1] = aPoint->y();
756 // project last point of arc on the arc
757 double x = anArcPoint[1][0] - anArcPoint[0][0];
758 double y = anArcPoint[1][1] - anArcPoint[0][1];
759 double aRad = sqrt(x*x + y*y);
760 x = anArcPoint[2][0] - anArcPoint[0][0];
761 y = anArcPoint[2][1] - anArcPoint[0][1];
762 double aNorm = sqrt(x*x + y*y);
763 if (aNorm >= tolerance) {
764 anArcPoint[2][0] = x * aRad / aNorm;
765 anArcPoint[2][1] = y * aRad / aNorm;
767 anArcPoint[1][0] -= anArcPoint[0][0];
768 anArcPoint[1][1] -= anArcPoint[0][1];
769 if (theCoeff < tolerance) {
770 theX = anArcPoint[0][0] + anArcPoint[1][0];
771 theY = anArcPoint[0][1] + anArcPoint[1][1];
772 } else if (1 - theCoeff < tolerance) {
773 theX = anArcPoint[0][0] + anArcPoint[2][0];
774 theY = anArcPoint[0][1] + anArcPoint[2][1];
776 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
777 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
778 double anAngle = aStartDir->angle(aEndDir);
782 double aCos = cos(anAngle);
783 double aSin = sin(anAngle);
784 theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
785 theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
787 aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
791 return EntityWrapperPtr();
793 std::list<ParameterWrapperPtr> aParameters;
794 aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->x()));
795 aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->y()));
796 // Create entity (parameters are not filled)
797 GCSPointPtr aPnt(new GCS::Point);
798 aPnt->x = std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.front())->parameter();
799 aPnt->y = std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.back())->parameter();
801 EntityWrapperPtr aResult(new PlaneGCSSolver_PointWrapper(AttributePtr(), aPnt));
802 aResult->setGroup(myGroupID);
803 aResult->setParameters(aParameters);