1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SolveSpaceSolver_Storage.cpp
4 // Created: 18 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include <SolveSpaceSolver_Storage.h>
8 #include <SolveSpaceSolver_ConstraintWrapper.h>
9 #include <SolveSpaceSolver_EntityWrapper.h>
10 #include <SolveSpaceSolver_ParameterWrapper.h>
11 #include <SolveSpaceSolver_Builder.h>
13 #include <GeomAPI_Dir2d.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_XY.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <ModelAPI_AttributeDouble.h>
22 #include <ModelAPI_AttributeRefAttr.h>
23 #include <SketchPlugin_Arc.h>
24 #include <SketchPlugin_ConstraintMiddle.h>
26 /** \brief Search the entity/parameter with specified ID in the list of elements
27 * \param[in] theEntityID unique ID of the element
28 * \param[in] theEntities list of elements
29 * \return position of the found element or -1 if the element is not found
32 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
34 /// \brief Compare two parameters to be different
35 static bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2);
36 /// \brief Compare two entities to be different
37 static bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2);
38 /// \brief Compare two constraints to be different
39 static bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2);
42 SolveSpaceSolver_Storage::SolveSpaceSolver_Storage(const GroupID& theGroup)
43 : SketchSolver_Storage(theGroup),
44 myWorkplaneID(SLVS_E_UNKNOWN),
45 myParamMaxID(SLVS_E_UNKNOWN),
46 myEntityMaxID(SLVS_E_UNKNOWN),
47 myConstrMaxID(SLVS_C_UNKNOWN),
48 myDuplicatedConstraint(false)
52 bool SolveSpaceSolver_Storage::update(ConstraintWrapperPtr theConstraint)
54 bool isUpdated = false;
55 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
56 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
57 Slvs_Constraint aSlvsConstr = getConstraint((Slvs_hConstraint)aConstraint->id());
58 if (aSlvsConstr.h == SLVS_C_UNKNOWN)
59 aSlvsConstr = aConstraint->constraint();
61 // update value of constraint if exist
62 double aCoeff = aSlvsConstr.type == SLVS_C_DIAMETER ? 2.0 : 1.0;
63 if (fabs(aSlvsConstr.valA - theConstraint->value() * aCoeff) > tolerance) {
64 aSlvsConstr.valA = theConstraint->value() * aCoeff;
68 // update constrained entities
69 Slvs_hEntity* aPnts[2] = {&aSlvsConstr.ptA, &aSlvsConstr.ptB};
70 Slvs_hEntity* anEnts[4] = {&aSlvsConstr.entityA, &aSlvsConstr.entityB,
71 &aSlvsConstr.entityC, &aSlvsConstr.entityD};
75 std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
76 std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
77 for (; anIt != anEntities.end(); ++anIt) {
78 isUpdated = update(*anIt) || isUpdated;
79 // do not update constrained entities for Multi constraints,
80 // and for middle point constraint translated to equal lines
81 ConstraintPtr aBaseConstraint = theConstraint->baseConstraint();
82 if (aSlvsConstr.type == SLVS_C_MULTI_ROTATION || aSlvsConstr.type == SLVS_C_MULTI_TRANSLATION ||
83 (aBaseConstraint && aBaseConstraint->getKind() == SketchPlugin_ConstraintMiddle::ID() &&
84 aSlvsConstr.type != SLVS_C_AT_MIDPOINT))
87 Slvs_hEntity anID = (Slvs_hEntity)(*anIt)->id();
88 if ((*anIt)->type() == ENTITY_POINT) {
89 if (*(aPnts[aPtInd]) != anID) {
90 *(aPnts[aPtInd]) = anID;
95 if (*(anEnts[aEntInd]) != anID) {
96 *(anEnts[aEntInd]) = anID;
103 // update constraint itself (do not update constraints Multi)
104 if (aSlvsConstr.type != SLVS_C_MULTI_ROTATION && aSlvsConstr.type != SLVS_C_MULTI_TRANSLATION) {
105 if (aSlvsConstr.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
106 aSlvsConstr.wrkpl = myWorkplaneID;
107 if (aSlvsConstr.group == SLVS_G_UNKNOWN)
108 aSlvsConstr.group = (Slvs_hGroup)myGroupID;
109 bool hasDupConstraints = myDuplicatedConstraint;
110 Slvs_hConstraint aConstrID = updateConstraint(aSlvsConstr);
111 if (aSlvsConstr.h == SLVS_C_UNKNOWN) {
112 aConstraint->changeConstraint() = getConstraint(aConstrID);
114 // check duplicated constraints based on different attributes
115 if (myDuplicatedConstraint && findSameConstraint(aConstraint) && !hasDupConstraints)
116 myDuplicatedConstraint = false;
122 bool SolveSpaceSolver_Storage::update(EntityWrapperPtr theEntity)
124 bool isUpdated = false;
125 std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity =
126 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
127 Slvs_Entity aSlvsEnt = getEntity((Slvs_hEntity)anEntity->id());
128 if (aSlvsEnt.h == SLVS_E_UNKNOWN)
129 aSlvsEnt = anEntity->entity();
131 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
132 std::list<ParameterWrapperPtr>::iterator aPIt;
133 // if the entity is an attribute, need to update its coordinates
134 if (anEntity->baseAttribute()) {
135 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
136 EntityWrapperPtr anUpdAttr = aBuilder->createAttribute(anEntity->baseAttribute(), GID_UNKNOWN);
138 std::list<ParameterWrapperPtr> anUpdParams = anUpdAttr->parameters();
139 std::list<ParameterWrapperPtr>::iterator anUpdIt = anUpdParams.begin();
140 for (aPIt = aParams.begin(); aPIt != aParams.end() && anUpdIt != anUpdParams.end();
142 (*aPIt)->update(*anUpdIt);
149 for (aPIt = aParams.begin(); aPIt != aParams.end(); ++aPIt, ++anInd) {
151 isUpdated = update(*aPIt) || isUpdated;
152 if (aSlvsEnt.param[anInd] != (Slvs_hEntity)(*aPIt)->id()) {
154 aSlvsEnt.param[anInd] = (Slvs_hEntity)(*aPIt)->id();
158 // update sub-entities
159 std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
160 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
161 for (anInd = 0; aSIt != aSubEntities.end(); ++aSIt, ++anInd) {
163 isUpdated = update(*aSIt) || isUpdated;
165 Slvs_hEntity anID = Slvs_hEntity((*aSIt)->id());
166 if ((*aSIt)->type() == ENTITY_NORMAL)
167 aSlvsEnt.normal = anID;
168 else if ((*aSIt)->type() == ENTITY_SCALAR)
169 aSlvsEnt.distance = anID;
170 else if (aSlvsEnt.point[anInd] != anID) {
171 aSlvsEnt.point[anInd] = anID;
172 if ((*aSIt)->baseAttribute())
173 SketchSolver_Storage::addEntity((*aSIt)->baseAttribute(), *aSIt);
177 if (theEntity->type() == ENTITY_POINT && aSubEntities.size() == 1) {
178 // theEntity is based on SketchPlugin_Point => need to substitute its attribute instead
179 bool isNew = (aSlvsEnt.h == SLVS_E_UNKNOWN);
180 aSlvsEnt = getEntity(aSlvsEnt.point[0]);
182 anEntity->changeEntity() = aSlvsEnt;
187 // update entity itself
188 if (aSlvsEnt.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
189 aSlvsEnt.wrkpl = myWorkplaneID;
190 if (aSlvsEnt.group == SLVS_G_UNKNOWN)
191 aSlvsEnt.group = (Slvs_hGroup)myGroupID;
192 Slvs_hEntity anEntID = updateEntity(aSlvsEnt);
193 if (aSlvsEnt.h == SLVS_E_UNKNOWN || isUpdated) {
194 anEntity->changeEntity() = getEntity(anEntID);
197 if (anEntity->type() == ENTITY_SKETCH)
198 storeWorkplane(anEntity);
204 bool SolveSpaceSolver_Storage::update(ParameterWrapperPtr theParameter)
206 std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aParameter =
207 std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(theParameter);
208 const Slvs_Param& aParam = getParameter((Slvs_hParam)aParameter->id());
209 if (aParam.h != SLVS_E_UNKNOWN && fabs(aParam.val - aParameter->value()) < tolerance)
211 Slvs_Param aParamToUpd = aParameter->parameter();
212 if (aParamToUpd.group == SLVS_G_UNKNOWN)
213 aParamToUpd.group = aParameter->isParametric() ? (Slvs_hGroup)GID_OUTOFGROUP : (Slvs_hGroup)myGroupID;
214 Slvs_hParam anID = updateParameter(aParamToUpd);
215 if (aParam.h == SLVS_E_UNKNOWN) // new parameter
216 aParameter->changeParameter() = getParameter(anID);
220 void SolveSpaceSolver_Storage::storeWorkplane(EntityWrapperPtr theSketch)
222 mySketchID = theSketch->id();
223 myWorkplaneID = (Slvs_hEntity)mySketchID;
225 // Update sub-entities of the sketch
226 std::list<EntityWrapperPtr> aSubEntities = theSketch->subEntities();
227 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
228 for (; aSIt != aSubEntities.end(); ++aSIt) {
229 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSub =
230 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*aSIt);
231 aSub->changeEntity().wrkpl = myWorkplaneID;
234 // Update all stored entities
235 std::vector<Slvs_Entity>::iterator anIt = myEntities.begin();
236 for (; anIt != myEntities.end(); ++anIt)
237 anIt->wrkpl = myWorkplaneID;
240 void SolveSpaceSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
242 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
243 std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
244 for (; aPIt != aParams.end(); ++aPIt)
245 changeGroup(*aPIt, theGroup);
247 std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
248 std::list<EntityWrapperPtr>::iterator aSIt = aSubs.begin();
249 for (; aSIt != aSubs.end(); ++aSIt)
250 changeGroup(*aSIt, theGroup);
252 theEntity->setGroup(theGroup);
253 int aPos = Search((Slvs_hEntity)theEntity->id(), myEntities);
254 if (aPos >= 0 && aPos < (int)myEntities.size()) {
255 if (myEntities[aPos].group != (Slvs_hGroup)theGroup)
256 setNeedToResolve(true);
257 myEntities[aPos].group = (Slvs_hGroup)theGroup;
261 void SolveSpaceSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
263 GroupID aGroup = theGroup;
264 if (theParam->isParametric())
265 aGroup = GID_OUTOFGROUP;
266 if (theParam->group() == aGroup)
269 theParam->setGroup(aGroup);
270 int aPos = Search((Slvs_hParam)theParam->id(), myParameters);
271 if (aPos >= 0 && aPos < (int)myParameters.size()) {
272 myParameters[aPos].group = (Slvs_hGroup)aGroup;
273 setNeedToResolve(true);
277 void SolveSpaceSolver_Storage::addCoincidentPoints(
278 EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
280 if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
282 if (!theMaster->subEntities().empty() || !theSlave->subEntities().empty()) {
283 EntityWrapperPtr aSubMaster = theMaster->subEntities().empty() ?
284 theMaster : theMaster->subEntities().front();
285 EntityWrapperPtr aSubSlave = theSlave->subEntities().empty() ?
286 theSlave : theSlave->subEntities().front();
287 return addCoincidentPoints(aSubMaster, aSubSlave);
290 // Search available coincidence
291 CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(theMaster);
292 CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(theSlave);
293 if (aMasterFound == myCoincidentPoints.end() || aSlaveFound == myCoincidentPoints.end()) {
294 // try to find master and slave points in the lists of slaves of already existent coincidences
295 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
296 for (; anIt != myCoincidentPoints.end(); ++anIt) {
297 if (anIt->second.find(theMaster) != anIt->second.end())
299 else if (anIt->second.find(theSlave) != anIt->second.end())
302 if (aMasterFound != myCoincidentPoints.end() && aSlaveFound != myCoincidentPoints.end())
307 if (aMasterFound == myCoincidentPoints.end()) {
309 myCoincidentPoints[theMaster] = std::set<EntityWrapperPtr>();
310 aMasterFound = myCoincidentPoints.find(theMaster);
311 } else if (aMasterFound == aSlaveFound)
312 return; // already coincident
314 if (aSlaveFound != myCoincidentPoints.end()) {
315 // A slave has been found, we need to attach all points coincident with it to the new master
316 std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
317 aNewSlaves.insert(aSlaveFound->first);
318 myCoincidentPoints.erase(aSlaveFound);
320 std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
321 for (; aSlIt != aNewSlaves.end(); ++aSlIt)
322 addCoincidentPoints(theMaster, *aSlIt);
324 // Update the slave if it was used in constraints and features
325 replaceInFeatures(theSlave, theMaster);
326 replaceInConstraints(theSlave, theMaster);
328 // Remove slave entity (if the IDs are equal no need to remove slave entity, just update it)
329 if (theMaster->id() != theSlave->id())
330 removeEntity((Slvs_hEntity)theSlave->id());
332 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointMaster =
333 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMaster);
334 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointSlave =
335 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSlave);
336 aPointSlave->changeEntity() = aPointMaster->entity();
337 aPointSlave->setParameters(aPointMaster->parameters());
339 aMasterFound->second.insert(theSlave);
343 void SolveSpaceSolver_Storage::replaceInFeatures(
344 EntityWrapperPtr theSource, EntityWrapperPtr theDest)
346 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator anIt = myFeatureMap.begin();
347 for (; anIt != myFeatureMap.end(); ++anIt) {
350 bool isUpdated = false;
351 std::list<EntityWrapperPtr> aSubs = anIt->second->subEntities();
352 std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
353 for (; aSubIt != aSubs.end(); ++aSubIt)
354 if ((*aSubIt)->id() == theSource->id()) {
355 (*aSubIt)->update(theDest);
362 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aWrapper =
363 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(anIt->second);
364 // update SolveSpace entity
365 Slvs_Entity anEnt = aWrapper->entity();
366 for (int i = 0; i < 4; ++i)
367 if (anEnt.point[i] == (Slvs_hEntity)theSource->id())
368 anEnt.point[i] = (Slvs_hEntity)theDest->id();
369 anEnt.h = updateEntity(anEnt);
370 aWrapper->changeEntity() = anEnt;
372 // update sub-entities
373 aWrapper->setSubEntities(aSubs);
377 void SolveSpaceSolver_Storage::replaceInConstraints(
378 EntityWrapperPtr theSource, EntityWrapperPtr theDest)
380 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
381 anIt = myConstraintMap.begin();
382 std::list<ConstraintWrapperPtr>::const_iterator aCIt;
383 for (; anIt != myConstraintMap.end(); ++anIt)
384 for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt) {
385 // Do not process coincidence between points and "multi" constraints
386 // (these constraints are stored to keep the structure of constraints).
387 if ((*aCIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
388 (*aCIt)->type() == CONSTRAINT_MULTI_ROTATION ||
389 (*aCIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
392 bool isUpdated = false;
393 std::list<EntityWrapperPtr> aSubs = (*aCIt)->entities();
394 std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
395 for (; aSubIt != aSubs.end(); ++aSubIt)
396 if ((*aSubIt)->id() == theSource->id()) {
397 (*aSubIt)->update(theDest);
404 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aWrapper =
405 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(*aCIt);
406 if (theSource->id() == theDest->id()) {
407 // No need to update SolveSpace constraint if the entities are the same
408 aWrapper->changeConstraint() = getConstraint((Slvs_hConstraint)aWrapper->id());
409 aWrapper->setEntities(aSubs);
413 // change constraint entities
414 Slvs_Constraint aConstr = aWrapper->constraint();
415 if (aConstr.ptA == (Slvs_hEntity)theSource->id())
416 aConstr.ptA = (Slvs_hEntity)theDest->id();
417 if (aConstr.ptB == (Slvs_hEntity)theSource->id())
418 aConstr.ptB = (Slvs_hEntity)theDest->id();
420 // check the constraint is duplicated
421 std::vector<Slvs_Constraint>::const_iterator aSlvsCIt = myConstraints.begin();
422 for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
423 if (aConstr.h != aSlvsCIt->h &&
424 aConstr.type == aSlvsCIt->type &&
425 aConstr.ptA == aSlvsCIt->ptA && aConstr.ptB == aSlvsCIt->ptB &&
426 aConstr.entityA == aSlvsCIt->entityA && aConstr.entityB == aSlvsCIt->entityB &&
427 aConstr.entityC == aSlvsCIt->entityC && aConstr.entityD == aSlvsCIt->entityD) {
428 Slvs_hConstraint anIDToRemove = aConstr.h;
430 int aShift = (int)(aSlvsCIt - myConstraints.begin());
431 removeConstraint(anIDToRemove);
432 aSlvsCIt = myConstraints.begin() + aShift - 1;
433 for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
434 if (aSlvsCIt->h == aConstr.h)
439 if (aSlvsCIt != myConstraints.end()) {
440 // constraint is duplicated, search its wrapper to add the mapping
441 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
442 anIt2 = myConstraintMap.begin();
443 std::list<ConstraintWrapperPtr>::const_iterator aCIt2;
444 for (; anIt2 != myConstraintMap.end(); ++anIt2)
445 for (aCIt2 = anIt2->second.begin(); aCIt2 != anIt2->second.end(); ++aCIt2)
446 if ((Slvs_hConstraint)(*aCIt2)->id() == aConstr.h) {
447 addSameConstraints(*aCIt2, aWrapper);
451 aConstr.h = updateConstraint(aConstr);
452 aWrapper->changeConstraint() = aConstr;
454 // update sub-entities
455 aWrapper->setEntities(aSubs);
459 void SolveSpaceSolver_Storage::addSameConstraints(ConstraintWrapperPtr theConstraint1,
460 ConstraintWrapperPtr theConstraint2)
462 SameConstraintMap::iterator anIt = myEqualConstraints.begin();
463 for (; anIt != myEqualConstraints.end(); ++anIt) {
464 if (anIt->find(theConstraint1) != anIt->end()) {
465 anIt->insert(theConstraint2);
468 else if (anIt->find(theConstraint2) != anIt->end()) {
469 anIt->insert(theConstraint1);
473 // group not found => create new one
474 std::set<ConstraintWrapperPtr> aNewGroup;
475 aNewGroup.insert(theConstraint1);
476 aNewGroup.insert(theConstraint2);
477 myEqualConstraints.push_back(aNewGroup);
480 bool SolveSpaceSolver_Storage::findSameConstraint(ConstraintWrapperPtr theConstraint)
482 if (theConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT ||
483 theConstraint->type() == CONSTRAINT_MULTI_ROTATION ||
484 theConstraint->type() == CONSTRAINT_MULTI_TRANSLATION)
487 const Slvs_Constraint& aCBase =
488 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint)->constraint();
490 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
491 aCIt = myConstraintMap.begin();
492 std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
493 for (; aCIt != myConstraintMap.end(); ++aCIt)
494 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt) {
495 if ((*aCWIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
496 (*aCWIt)->type() == CONSTRAINT_MULTI_ROTATION ||
497 (*aCWIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
499 if ((*aCWIt)->type() == theConstraint->type()) {
500 const Slvs_Constraint& aCComp = getConstraint((Slvs_hConstraint)(*aCWIt)->id());
502 if (aCBase.ptA == aCComp.ptA && aCBase.ptB == aCComp.ptB &&
503 aCBase.entityA == aCComp.entityA && aCBase.entityB == aCComp.entityB &&
504 aCBase.entityC == aCComp.entityC && aCBase.entityD == aCComp.entityD &&
505 fabs(aCBase.valA -aCComp.valA) < tolerance) {
506 addSameConstraints(*aCWIt, theConstraint);
515 EntityWrapperPtr SolveSpaceSolver_Storage::calculateMiddlePoint(
516 EntityWrapperPtr theBase, double theCoeff)
518 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
520 std::shared_ptr<GeomAPI_XY> aMidPoint;
521 if (theBase->type() == ENTITY_LINE) {
522 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
523 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
524 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
525 for (int i = 0; i < 2; ++i, ++anIt)
526 aPoints[i] = aBuilder->point(*anIt);
527 aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
528 aPoints[1]->xy()->multiplied(theCoeff));
530 else if (theBase->type() == ENTITY_ARC) {
532 double anArcPoint[3][2];
533 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
534 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
535 for (int i = 0; i < 3; ++i, ++anIt) {
536 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
537 anArcPoint[i][0] = aPoint->x();
538 anArcPoint[i][1] = aPoint->y();
540 // project last point of arc on the arc
541 double x = anArcPoint[1][0] - anArcPoint[0][0];
542 double y = anArcPoint[1][1] - anArcPoint[0][1];
543 double aRad = sqrt(x*x + y*y);
544 x = anArcPoint[2][0] - anArcPoint[0][0];
545 y = anArcPoint[2][1] - anArcPoint[0][1];
546 double aNorm = sqrt(x*x + y*y);
547 if (aNorm >= tolerance) {
548 anArcPoint[2][0] = x * aRad / aNorm;
549 anArcPoint[2][1] = y * aRad / aNorm;
551 anArcPoint[1][0] -= anArcPoint[0][0];
552 anArcPoint[1][1] -= anArcPoint[0][1];
553 if (theCoeff < tolerance) {
554 theX = anArcPoint[0][0] + anArcPoint[1][0];
555 theY = anArcPoint[0][1] + anArcPoint[1][1];
556 } else if (1 - theCoeff < tolerance) {
557 theX = anArcPoint[0][0] + anArcPoint[2][0];
558 theY = anArcPoint[0][1] + anArcPoint[2][1];
560 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
561 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
562 double anAngle = aStartDir->angle(aEndDir);
566 double aCos = cos(anAngle);
567 double aSin = sin(anAngle);
568 theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
569 theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
571 aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
575 return EntityWrapperPtr();
577 std::list<ParameterWrapperPtr> aParameters;
578 Slvs_Param aParam1 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->x());
579 aParam1.h = addParameter(aParam1);
580 aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam1)));
581 Slvs_Param aParam2 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->y());
582 aParam2.h = addParameter(aParam2);
583 aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam2)));
584 // Create entity (parameters are not filled)
585 Slvs_Entity anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
586 (Slvs_hEntity)myWorkplaneID, aParam1.h, aParam2.h);
587 anEntity.h = addEntity(anEntity);
589 EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(AttributePtr(), anEntity));
590 aResult->setParameters(aParameters);
599 Slvs_hParam SolveSpaceSolver_Storage::addParameter(const Slvs_Param& theParam)
601 if (theParam.h > 0 && theParam.h <= myParamMaxID) {
602 // parameter is already used, rewrite it
603 return updateParameter(theParam);
606 Slvs_Param aParam = theParam;
607 if (aParam.h > myParamMaxID)
608 myParamMaxID = aParam.h;
610 aParam.h = ++myParamMaxID;
611 myParameters.push_back(aParam);
612 myNeedToResolve = true;
616 Slvs_hParam SolveSpaceSolver_Storage::updateParameter(const Slvs_Param& theParam)
618 if (theParam.h > 0 && theParam.h <= myParamMaxID) {
619 // parameter already used, rewrite it
620 int aPos = Search(theParam.h, myParameters);
621 if (aPos >= 0 && aPos < (int)myParameters.size()) {
622 if (IsNotEqual(myParameters[aPos], theParam))
623 setNeedToResolve(true);
624 myParameters[aPos] = theParam;
629 // Parameter is not found, add new one
630 Slvs_Param aParam = theParam;
632 return addParameter(aParam);
635 bool SolveSpaceSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
637 int aPos = Search(theParamID, myParameters);
638 if (aPos >= 0 && aPos < (int)myParameters.size()) {
639 // Firstly, search the parameter is not used elsewhere
640 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
641 for (; anEntIter != myEntities.end(); anEntIter++) {
642 for (int i = 0; i < 4; i++)
643 if (anEntIter->param[i] == theParamID)
647 myParameters.erase(myParameters.begin() + aPos);
648 myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
649 myNeedToResolve = true;
654 const Slvs_Param& SolveSpaceSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
656 int aPos = Search(theParamID, myParameters);
657 if (aPos >= 0 && aPos < (int)myParameters.size())
658 return myParameters[aPos];
660 // Parameter is not found, return empty object
661 static Slvs_Param aDummy;
667 Slvs_hEntity SolveSpaceSolver_Storage::addEntity(const Slvs_Entity& theEntity)
669 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
670 // Entity is already used, rewrite it
671 return updateEntity(theEntity);
674 Slvs_Entity aEntity = theEntity;
675 if (aEntity.h > myEntityMaxID)
676 myEntityMaxID = aEntity.h;
678 aEntity.h = ++myEntityMaxID;
679 myEntities.push_back(aEntity);
680 myNeedToResolve = true;
684 Slvs_hEntity SolveSpaceSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
686 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
687 // Entity already used, rewrite it
688 int aPos = Search(theEntity.h, myEntities);
689 if (aPos >= 0 && aPos < (int)myEntities.size()) {
690 myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
691 myEntities[aPos] = theEntity;
696 // Entity is not found, add new one
697 Slvs_Entity aEntity = theEntity;
699 return addEntity(aEntity);
702 bool SolveSpaceSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
705 int aPos = Search(theEntityID, myEntities);
706 if (aPos >= 0 && aPos < (int)myEntities.size()) {
707 // Firstly, check the entity and its attributes is not used elsewhere
708 std::set<Slvs_hEntity> anEntAndSubs;
709 anEntAndSubs.insert(theEntityID);
710 for (int i = 0; i < 4; i++)
711 if (myEntities[aPos].point[i] != SLVS_E_UNKNOWN)
712 anEntAndSubs.insert(myEntities[aPos].point[i]);
714 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
715 for (; anEntIter != myEntities.end(); anEntIter++) {
716 if (anEntAndSubs.find(anEntIter->h) != anEntAndSubs.end())
718 for (int i = 0; i < 4; i++)
719 if (anEntAndSubs.find(anEntIter->point[i]) != anEntAndSubs.end())
721 if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
724 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
725 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
726 Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
727 aConstrIter->entityA, aConstrIter->entityB,
728 aConstrIter->entityC, aConstrIter->entityD};
729 for (int i = 0; i < 6; i++)
730 if (anEntAndSubs.find(anEntIDs[i]) != anEntAndSubs.end())
733 // The entity is not used, remove it and its parameters
734 Slvs_Entity anEntity = myEntities[aPos];
735 myEntities.erase(myEntities.begin() + aPos);
736 myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
737 if (anEntity.distance != SLVS_E_UNKNOWN)
738 aResult = aResult && removeEntity(anEntity.distance);
739 for (int i = 0; i < 4; i++)
740 if (anEntity.param[i] != SLVS_E_UNKNOWN)
741 aResult = removeParameter(anEntity.param[i]) && aResult;
742 myNeedToResolve = true;
747 const Slvs_Entity& SolveSpaceSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
749 int aPos = Search(theEntityID, myEntities);
750 if (aPos >= 0 && aPos < (int)myEntities.size())
751 return myEntities[aPos];
753 // Entity is not found, return empty object
754 static Slvs_Entity aDummy;
755 aDummy.h = SLVS_E_UNKNOWN;
760 Slvs_hConstraint SolveSpaceSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
762 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
763 // Constraint is already used, rewrite it
764 return updateConstraint(theConstraint);
767 Slvs_Constraint aConstraint = theConstraint;
769 // Find a constraint with same type uses same arguments to show user overconstraint situation
770 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
771 for (; aCIt != myConstraints.end(); aCIt++) {
772 if (aConstraint.type != aCIt->type)
774 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
775 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
776 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
777 myDuplicatedConstraint = true;
782 if (aConstraint.h > myConstrMaxID)
783 myConstrMaxID = aConstraint.h;
785 aConstraint.h = ++myConstrMaxID;
786 myConstraints.push_back(aConstraint);
787 myNeedToResolve = true;
788 return aConstraint.h;
791 Slvs_hConstraint SolveSpaceSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
793 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
794 // Constraint already used, rewrite it
795 int aPos = Search(theConstraint.h, myConstraints);
796 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
797 myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
798 myConstraints[aPos] = theConstraint;
799 return theConstraint.h;
803 // Constraint is not found, add new one
804 Slvs_Constraint aConstraint = theConstraint;
807 // Firstly, check middle-point constraint conflicts with point-on-line
808 if (aConstraint.type == SLVS_C_AT_MIDPOINT) {
809 std::vector<Slvs_Constraint>::const_iterator anIt = myConstraints.begin();
810 for (; anIt != myConstraints.end(); ++anIt)
811 if (anIt->type == SLVS_C_PT_ON_LINE &&
812 anIt->ptA == aConstraint.ptA &&
813 anIt->entityA == aConstraint.entityA)
815 if (anIt != myConstraints.end()) {
816 // change the constraint to the lengths equality to avoid conflicts
817 Slvs_Entity aLine = getEntity(aConstraint.entityA);
818 Slvs_Entity aNewLine1 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
819 myWorkplaneID, aLine.point[0], aConstraint.ptA);
820 aNewLine1.h = addEntity(aNewLine1);
821 Slvs_Entity aNewLine2 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
822 myWorkplaneID, aLine.point[1], aConstraint.ptA);
823 aNewLine2.h = addEntity(aNewLine2);
824 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, SLVS_C_EQUAL_LENGTH_LINES,
825 myWorkplaneID, 0.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aNewLine1.h, aNewLine2.h);
829 return addConstraint(aConstraint);
832 bool SolveSpaceSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
834 int aPos = Search(theConstraintID, myConstraints);
835 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
836 Slvs_Constraint aConstraint = myConstraints[aPos];
837 myConstraints.erase(myConstraints.begin() + aPos);
838 myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
839 myNeedToResolve = true;
841 if (myDuplicatedConstraint) {
842 // Find a constraint with same type uses same arguments
843 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
844 for (; aCIt != myConstraints.end(); aCIt++) {
845 if (aConstraint.type != aCIt->type)
847 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
848 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
849 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
850 myDuplicatedConstraint = false;
859 const Slvs_Constraint& SolveSpaceSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
861 int aPos = Search(theConstraintID, myConstraints);
862 if (aPos >= 0 && aPos < (int)myConstraints.size())
863 return myConstraints[aPos];
865 // Constraint is not found, return empty object
866 static Slvs_Constraint aDummy;
872 void SolveSpaceSolver_Storage::initializeSolver(SolverPtr theSolver)
874 std::shared_ptr<SolveSpaceSolver_Solver> aSolver =
875 std::dynamic_pointer_cast<SolveSpaceSolver_Solver>(theSolver);
882 if (myConstraints.empty()) {
883 // Adjust all arc to place their points correctly
884 std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
885 for (; anEntIt != myEntities.end(); ++anEntIt)
886 if (anEntIt->type == SLVS_E_ARC_OF_CIRCLE)
890 aSolver->setParameters(myParameters.data(), (int)myParameters.size());
891 aSolver->setEntities(myEntities.data(), (int)myEntities.size());
892 aSolver->setConstraints(myConstraints.data(), (int)myConstraints.size());
896 bool SolveSpaceSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
898 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
899 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
901 // verify whether the constraint has duplicated
902 bool hasSameID = false;
903 SameConstraintMap::iterator anEqIt = myEqualConstraints.begin();
904 for (; anEqIt != myEqualConstraints.end(); ++anEqIt) {
905 std::set<ConstraintWrapperPtr>::const_iterator aFound = anEqIt->find(aConstraint);
906 if (aFound != anEqIt->end()) {
907 // verify there is a constraint with same ID
908 std::set<ConstraintWrapperPtr>::const_iterator anIt = anEqIt->begin();
909 ConstraintID anID = (*aFound)->id();
910 for (++anIt; anIt != anEqIt->end() && !hasSameID; ++anIt)
911 if ((*anIt)->id() == anID && aFound != anIt)
914 anEqIt->erase(aConstraint);
918 if (anEqIt != myEqualConstraints.end() && hasSameID)
921 bool isFullyRemoved = removeConstraint((Slvs_hConstraint)aConstraint->id());
922 // remove point-point coincidence
923 if (aConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT)
924 isFullyRemoved = removeCoincidence(theConstraint) && isFullyRemoved;
925 return SketchSolver_Storage::remove(theConstraint) && isFullyRemoved;
928 bool SolveSpaceSolver_Storage::remove(EntityWrapperPtr theEntity)
933 // Additional check for entity to be used in point-point coincidence
934 bool isCoincide = false;
935 if (theEntity->type() == ENTITY_POINT) {
936 CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
937 std::set<EntityWrapperPtr>::const_iterator aCIt;
938 for (; anIt != myCoincidentPoints.end(); ++anIt) {
939 if (anIt->first == theEntity)
941 for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt)
942 if (*aCIt == theEntity)
944 if (aCIt != anIt->second.end())
947 if (anIt != myCoincidentPoints.end()) {
948 if (anIt->first != theEntity && isUsed(anIt->first->baseAttribute()))
950 for (aCIt = anIt->second.begin(); !isCoincide && aCIt != anIt->second.end(); ++aCIt)
951 if (*aCIt != theEntity && isUsed((*aCIt)->baseAttribute()))
956 std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity =
957 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
958 bool isFullyRemoved = isCoincide ? true : removeEntity((Slvs_hEntity)anEntity->id());
959 return (SketchSolver_Storage::remove(theEntity) || isCoincide) && isFullyRemoved;
962 bool SolveSpaceSolver_Storage::remove(ParameterWrapperPtr theParameter)
964 return removeParameter((Slvs_hParam)theParameter->id());
968 void SolveSpaceSolver_Storage::refresh(bool theFixedOnly) const
972 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
973 std::list<ParameterWrapperPtr> aParams;
974 std::list<ParameterWrapperPtr>::const_iterator aParIt;
975 for (; anIt != myAttributeMap.end(); ++anIt) {
978 // the external feature always should keep the up to date values, so,
979 // refresh from the solver is never needed
980 if (anIt->first.get()) {
981 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
982 std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
983 if (aSketchFeature.get() && aSketchFeature->isExternal())
985 // not need to refresh here sketch's origin and normal vector
986 CompositeFeaturePtr aSketch =
987 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anIt->first->owner());
992 // update parameter wrappers and obtain values of attributes
993 aParams = anIt->second->parameters();
995 bool isUpd[3] = {false};
997 for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
998 std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aWrapper =
999 std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(*aParIt);
1000 if (!theFixedOnly || aWrapper->group() == GID_OUTOFGROUP || aWrapper->isParametric()) {
1001 aWrapper->changeParameter().val = getParameter((Slvs_hParam)aWrapper->id()).val;
1002 aCoords[i] = aWrapper->value();
1006 if (!isUpd[0] && !isUpd[1] && !isUpd[2])
1007 continue; // nothing is updated
1009 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1010 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
1012 if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
1013 (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance)) {
1014 if (!isUpd[0]) aCoords[0] = aPoint2D->x();
1015 if (!isUpd[1]) aCoords[1] = aPoint2D->y();
1016 aPoint2D->setValue(aCoords[0], aCoords[1]);
1017 // Find points coincident with this one (probably not in GID_OUTOFGROUP)
1018 std::map<AttributePtr, EntityWrapperPtr>::const_iterator aLocIt;
1020 aLocIt = myAttributeMap.begin();
1025 for (; aLocIt != myAttributeMap.end(); ++aLocIt) {
1026 if (!aLocIt->second)
1028 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1029 std::dynamic_pointer_cast<SketchPlugin_Feature>(aLocIt->first->owner());
1030 if (aSketchFeature && aSketchFeature->isExternal())
1032 if (anIt->second->id() == aLocIt->second->id()) {
1033 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aLocIt->first);
1034 aPoint2D->setValue(aCoords[0], aCoords[1]);
1040 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
1042 if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
1043 aScalar->setValue(aCoords[0]);
1046 std::shared_ptr<GeomDataAPI_Point> aPoint =
1047 std::dynamic_pointer_cast<GeomDataAPI_Point>(anIt->first);
1049 if ((isUpd[0] && fabs(aPoint->x() - aCoords[0]) > tolerance) ||
1050 (isUpd[1] && fabs(aPoint->y() - aCoords[1]) > tolerance) ||
1051 (isUpd[2] && fabs(aPoint->z() - aCoords[2]) > tolerance))
1052 if (!isUpd[0]) aCoords[0] = aPoint->x();
1053 if (!isUpd[1]) aCoords[1] = aPoint->y();
1054 if (!isUpd[2]) aCoords[2] = aPoint->z();
1055 aPoint->setValue(aCoords[0], aCoords[1], aCoords[2]);
1060 //blockEvents(false);
1063 void SolveSpaceSolver_Storage::verifyFixed()
1065 std::map<AttributePtr, EntityWrapperPtr>::iterator anAttrIt = myAttributeMap.begin();
1066 for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
1067 if (!anAttrIt->second)
1069 if (anAttrIt->second->group() == GID_OUTOFGROUP) {
1070 Slvs_Entity anEnt = getEntity((Slvs_hEntity)anAttrIt->second->id());
1071 if (anEnt.group != (Slvs_hEntity)GID_OUTOFGROUP)
1072 anEnt.group = (Slvs_hEntity)GID_OUTOFGROUP;
1073 updateEntity(anEnt);
1076 const std::list<ParameterWrapperPtr>& aParameters = anAttrIt->second->parameters();
1077 std::list<ParameterWrapperPtr>::const_iterator aParIt = aParameters.begin();
1078 for (; aParIt != aParameters.end(); ++aParIt)
1079 if (anAttrIt->second->group() == GID_OUTOFGROUP || (*aParIt)->group() == GID_OUTOFGROUP) {
1080 Slvs_Param aParam = getParameter((Slvs_hParam)(*aParIt)->id());
1081 if (aParam.group != (Slvs_hParam)GID_OUTOFGROUP) {
1082 aParam.group = (Slvs_hParam)GID_OUTOFGROUP;
1083 updateParameter(aParam);
1090 void SolveSpaceSolver_Storage::adjustArc(const Slvs_Entity& theArc)
1092 double anArcPoints[3][2];
1093 double aDist[3] = {0.0};
1094 bool isFixed[3] = {false};
1095 for (int i = 0; i < 3; ++i) {
1096 Slvs_Entity aPoint = getEntity(theArc.point[i]);
1097 isFixed[i] = (aPoint.group != (Slvs_hGroup)myGroupID);
1098 anArcPoints[i][0] = getParameter(aPoint.param[0]).val;
1099 anArcPoints[i][1] = getParameter(aPoint.param[1]).val;
1101 anArcPoints[i][0] -= anArcPoints[0][0];
1102 anArcPoints[i][1] -= anArcPoints[0][1];
1103 aDist[i] = sqrt(anArcPoints[i][0] * anArcPoints[i][0] +
1104 anArcPoints[i][1] * anArcPoints[i][1]);
1108 if (fabs(aDist[1] - aDist[2]) < tolerance)
1112 while (anInd > 0 && isFixed[anInd])
1115 return; // adjust only start or end point of the arc
1117 anArcPoints[anInd][0] /= aDist[anInd];
1118 anArcPoints[anInd][1] /= aDist[anInd];
1120 Slvs_Entity aPoint = getEntity(theArc.point[anInd]);
1121 for (int i = 0; i < 2; ++i) {
1122 Slvs_Param aParam = getParameter(aPoint.param[i]);
1123 aParam.val = anArcPoints[0][i] + aDist[3-anInd] * anArcPoints[anInd][i];
1124 updateParameter(aParam);
1134 // ========================================================
1135 // ========= Auxiliary functions ===============
1136 // ========================================================
1138 template<typename T>
1139 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1141 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1142 int aVecSize = (int)theEntities.size();
1143 if (theEntities.empty())
1145 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1147 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1149 if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1150 aResIndex = aVecSize;
1154 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1156 return fabs(theParam1.val - theParam2.val) > tolerance;
1159 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1162 for (; theEntity1.param[i] != 0 && i < 4; i++)
1163 if (theEntity1.param[i] != theEntity2.param[i])
1166 for (; theEntity1.point[i] != 0 && i < 4; i++)
1167 if (theEntity1.point[i] != theEntity2.point[i])
1172 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1174 return theConstraint1.ptA != theConstraint2.ptA ||
1175 theConstraint1.ptB != theConstraint2.ptB ||
1176 theConstraint1.entityA != theConstraint2.entityA ||
1177 theConstraint1.entityB != theConstraint2.entityB ||
1178 theConstraint1.entityC != theConstraint2.entityC ||
1179 theConstraint1.entityD != theConstraint2.entityD ||
1180 fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;