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 if (aSlvsConstr.type == SLVS_C_MULTI_ROTATION || aSlvsConstr.type == SLVS_C_MULTI_TRANSLATION ||
82 (theConstraint->baseConstraint()->getKind() == SketchPlugin_ConstraintMiddle::ID() &&
83 aSlvsConstr.type != SLVS_C_AT_MIDPOINT))
86 Slvs_hEntity anID = (Slvs_hEntity)(*anIt)->id();
87 if ((*anIt)->type() == ENTITY_POINT) {
88 if (*(aPnts[aPtInd]) != anID) {
89 *(aPnts[aPtInd]) = anID;
94 if (*(anEnts[aEntInd]) != anID) {
95 *(anEnts[aEntInd]) = anID;
102 // update constraint itself (do not update constraints Multi)
103 if (aSlvsConstr.type != SLVS_C_MULTI_ROTATION && aSlvsConstr.type != SLVS_C_MULTI_TRANSLATION) {
104 if (aSlvsConstr.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
105 aSlvsConstr.wrkpl = myWorkplaneID;
106 if (aSlvsConstr.group == SLVS_G_UNKNOWN)
107 aSlvsConstr.group = (Slvs_hGroup)myGroupID;
108 bool hasDupConstraints = myDuplicatedConstraint;
109 Slvs_hConstraint aConstrID = updateConstraint(aSlvsConstr);
110 if (aSlvsConstr.h == SLVS_C_UNKNOWN) {
111 aConstraint->changeConstraint() = getConstraint(aConstrID);
113 // check duplicated constraints based on different attributes
114 if (myDuplicatedConstraint && findSameConstraint(aConstraint) && !hasDupConstraints)
115 myDuplicatedConstraint = false;
121 bool SolveSpaceSolver_Storage::update(EntityWrapperPtr theEntity)
123 bool isUpdated = false;
124 std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity =
125 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
126 Slvs_Entity aSlvsEnt = getEntity((Slvs_hEntity)anEntity->id());
127 if (aSlvsEnt.h == SLVS_E_UNKNOWN)
128 aSlvsEnt = anEntity->entity();
130 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
131 std::list<ParameterWrapperPtr>::iterator aPIt;
132 // if the entity is an attribute, need to update its coordinates
133 if (anEntity->baseAttribute()) {
134 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
135 EntityWrapperPtr anUpdAttr = aBuilder->createAttribute(anEntity->baseAttribute(), GID_UNKNOWN);
137 std::list<ParameterWrapperPtr> anUpdParams = anUpdAttr->parameters();
138 std::list<ParameterWrapperPtr>::iterator anUpdIt = anUpdParams.begin();
139 for (aPIt = aParams.begin(); aPIt != aParams.end() && anUpdIt != anUpdParams.end();
141 (*aPIt)->update(*anUpdIt);
148 for (aPIt = aParams.begin(); aPIt != aParams.end(); ++aPIt, ++anInd) {
150 isUpdated = update(*aPIt) || isUpdated;
151 if (aSlvsEnt.param[anInd] != (Slvs_hEntity)(*aPIt)->id()) {
153 aSlvsEnt.param[anInd] = (Slvs_hEntity)(*aPIt)->id();
157 // update sub-entities
158 std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
159 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
160 for (anInd = 0; aSIt != aSubEntities.end(); ++aSIt, ++anInd) {
162 isUpdated = update(*aSIt) || isUpdated;
164 Slvs_hEntity anID = Slvs_hEntity((*aSIt)->id());
165 if ((*aSIt)->type() == ENTITY_NORMAL)
166 aSlvsEnt.normal = anID;
167 else if ((*aSIt)->type() == ENTITY_SCALAR)
168 aSlvsEnt.distance = anID;
169 else if (aSlvsEnt.point[anInd] != anID) {
170 aSlvsEnt.point[anInd] = anID;
171 if ((*aSIt)->baseAttribute())
172 SketchSolver_Storage::addEntity((*aSIt)->baseAttribute(), *aSIt);
176 if (theEntity->type() == ENTITY_POINT && aSubEntities.size() == 1) {
177 // theEntity is based on SketchPlugin_Point => need to substitute its attribute instead
178 bool isNew = (aSlvsEnt.h == SLVS_E_UNKNOWN);
179 aSlvsEnt = getEntity(aSlvsEnt.point[0]);
181 anEntity->changeEntity() = aSlvsEnt;
186 // update entity itself
187 if (aSlvsEnt.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
188 aSlvsEnt.wrkpl = myWorkplaneID;
189 if (aSlvsEnt.group == SLVS_G_UNKNOWN)
190 aSlvsEnt.group = (Slvs_hGroup)myGroupID;
191 Slvs_hEntity anEntID = updateEntity(aSlvsEnt);
192 if (aSlvsEnt.h == SLVS_E_UNKNOWN || isUpdated) {
193 anEntity->changeEntity() = getEntity(anEntID);
196 if (anEntity->type() == ENTITY_SKETCH)
197 storeWorkplane(anEntity);
203 bool SolveSpaceSolver_Storage::update(ParameterWrapperPtr theParameter)
205 std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aParameter =
206 std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(theParameter);
207 const Slvs_Param& aParam = getParameter((Slvs_hParam)aParameter->id());
208 if (aParam.h != SLVS_E_UNKNOWN && fabs(aParam.val - aParameter->value()) < tolerance)
210 Slvs_Param aParamToUpd = aParameter->parameter();
211 if (aParamToUpd.group == SLVS_G_UNKNOWN)
212 aParamToUpd.group = aParameter->isParametric() ? (Slvs_hGroup)GID_OUTOFGROUP : (Slvs_hGroup)myGroupID;
213 Slvs_hParam anID = updateParameter(aParamToUpd);
214 if (aParam.h == SLVS_E_UNKNOWN) // new parameter
215 aParameter->changeParameter() = getParameter(anID);
219 void SolveSpaceSolver_Storage::storeWorkplane(EntityWrapperPtr theSketch)
221 mySketchID = theSketch->id();
222 myWorkplaneID = (Slvs_hEntity)mySketchID;
224 // Update sub-entities of the sketch
225 std::list<EntityWrapperPtr> aSubEntities = theSketch->subEntities();
226 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
227 for (; aSIt != aSubEntities.end(); ++aSIt) {
228 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSub =
229 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*aSIt);
230 aSub->changeEntity().wrkpl = myWorkplaneID;
233 // Update all stored entities
234 std::vector<Slvs_Entity>::iterator anIt = myEntities.begin();
235 for (; anIt != myEntities.end(); ++anIt)
236 anIt->wrkpl = myWorkplaneID;
239 void SolveSpaceSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
241 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
242 std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
243 for (; aPIt != aParams.end(); ++aPIt)
244 changeGroup(*aPIt, theGroup);
246 std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
247 std::list<EntityWrapperPtr>::iterator aSIt = aSubs.begin();
248 for (; aSIt != aSubs.end(); ++aSIt)
249 changeGroup(*aSIt, theGroup);
251 theEntity->setGroup(theGroup);
252 int aPos = Search((Slvs_hEntity)theEntity->id(), myEntities);
253 if (aPos >= 0 && aPos < (int)myEntities.size()) {
254 if (myEntities[aPos].group != (Slvs_hGroup)theGroup)
255 setNeedToResolve(true);
256 myEntities[aPos].group = (Slvs_hGroup)theGroup;
260 void SolveSpaceSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
262 GroupID aGroup = theGroup;
263 if (theParam->isParametric())
264 aGroup = GID_OUTOFGROUP;
265 if (theParam->group() == aGroup)
268 theParam->setGroup(aGroup);
269 int aPos = Search((Slvs_hParam)theParam->id(), myParameters);
270 if (aPos >= 0 && aPos < (int)myParameters.size()) {
271 myParameters[aPos].group = (Slvs_hGroup)aGroup;
272 setNeedToResolve(true);
276 void SolveSpaceSolver_Storage::addCoincidentPoints(
277 EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
279 if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
281 if (!theMaster->subEntities().empty() || !theSlave->subEntities().empty()) {
282 EntityWrapperPtr aSubMaster = theMaster->subEntities().empty() ?
283 theMaster : theMaster->subEntities().front();
284 EntityWrapperPtr aSubSlave = theSlave->subEntities().empty() ?
285 theSlave : theSlave->subEntities().front();
286 return addCoincidentPoints(aSubMaster, aSubSlave);
289 // Search available coincidence
290 CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(theMaster);
291 CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(theSlave);
292 if (aMasterFound == myCoincidentPoints.end() || aSlaveFound == myCoincidentPoints.end()) {
293 // try to find master and slave points in the lists of slaves of already existent coincidences
294 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
295 for (; anIt != myCoincidentPoints.end(); ++anIt) {
296 if (anIt->second.find(theMaster) != anIt->second.end())
298 else if (anIt->second.find(theSlave) != anIt->second.end())
301 if (aMasterFound != myCoincidentPoints.end() && aSlaveFound != myCoincidentPoints.end())
306 if (aMasterFound == myCoincidentPoints.end()) {
308 myCoincidentPoints[theMaster] = std::set<EntityWrapperPtr>();
309 aMasterFound = myCoincidentPoints.find(theMaster);
310 } else if (aMasterFound == aSlaveFound)
311 return; // already coincident
313 if (aSlaveFound != myCoincidentPoints.end()) {
314 // A slave has been found, we need to attach all points coincident with it to the new master
315 std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
316 aNewSlaves.insert(aSlaveFound->first);
317 myCoincidentPoints.erase(aSlaveFound);
319 std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
320 for (; aSlIt != aNewSlaves.end(); ++aSlIt)
321 addCoincidentPoints(theMaster, *aSlIt);
323 // Update the slave if it was used in constraints and features
324 replaceInFeatures(theSlave, theMaster);
325 replaceInConstraints(theSlave, theMaster);
327 // Remove slave entity (if the IDs are equal no need to remove slave entity, just update it)
328 if (theMaster->id() != theSlave->id())
329 removeEntity((Slvs_hEntity)theSlave->id());
331 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointMaster =
332 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMaster);
333 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointSlave =
334 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSlave);
335 aPointSlave->changeEntity() = aPointMaster->entity();
336 aPointSlave->setParameters(aPointMaster->parameters());
338 aMasterFound->second.insert(theSlave);
342 void SolveSpaceSolver_Storage::replaceInFeatures(
343 EntityWrapperPtr theSource, EntityWrapperPtr theDest)
345 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator anIt = myFeatureMap.begin();
346 for (; anIt != myFeatureMap.end(); ++anIt) {
349 bool isUpdated = false;
350 std::list<EntityWrapperPtr> aSubs = anIt->second->subEntities();
351 std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
352 for (; aSubIt != aSubs.end(); ++aSubIt)
353 if ((*aSubIt)->id() == theSource->id()) {
354 (*aSubIt)->update(theDest);
361 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aWrapper =
362 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(anIt->second);
363 // update SolveSpace entity
364 Slvs_Entity anEnt = aWrapper->entity();
365 for (int i = 0; i < 4; ++i)
366 if (anEnt.point[i] == (Slvs_hEntity)theSource->id())
367 anEnt.point[i] = (Slvs_hEntity)theDest->id();
368 anEnt.h = updateEntity(anEnt);
369 aWrapper->changeEntity() = anEnt;
371 // update sub-entities
372 aWrapper->setSubEntities(aSubs);
376 void SolveSpaceSolver_Storage::replaceInConstraints(
377 EntityWrapperPtr theSource, EntityWrapperPtr theDest)
379 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
380 anIt = myConstraintMap.begin();
381 std::list<ConstraintWrapperPtr>::const_iterator aCIt;
382 for (; anIt != myConstraintMap.end(); ++anIt)
383 for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt) {
384 // Do not process coincidence between points and "multi" constraints
385 // (these constraints are stored to keep the structure of constraints).
386 if ((*aCIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
387 (*aCIt)->type() == CONSTRAINT_MULTI_ROTATION ||
388 (*aCIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
391 bool isUpdated = false;
392 std::list<EntityWrapperPtr> aSubs = (*aCIt)->entities();
393 std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
394 for (; aSubIt != aSubs.end(); ++aSubIt)
395 if ((*aSubIt)->id() == theSource->id()) {
396 (*aSubIt)->update(theDest);
403 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aWrapper =
404 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(*aCIt);
405 if (theSource->id() == theDest->id()) {
406 // No need to update SolveSpace constraint if the entities are the same
407 aWrapper->changeConstraint() = getConstraint((Slvs_hConstraint)aWrapper->id());
408 aWrapper->setEntities(aSubs);
412 // change constraint entities
413 Slvs_Constraint aConstr = aWrapper->constraint();
414 if (aConstr.ptA == (Slvs_hEntity)theSource->id())
415 aConstr.ptA = (Slvs_hEntity)theDest->id();
416 if (aConstr.ptB == (Slvs_hEntity)theSource->id())
417 aConstr.ptB = (Slvs_hEntity)theDest->id();
419 // check the constraint is duplicated
420 std::vector<Slvs_Constraint>::const_iterator aSlvsCIt = myConstraints.begin();
421 for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
422 if (aConstr.h != aSlvsCIt->h &&
423 aConstr.type == aSlvsCIt->type &&
424 aConstr.ptA == aSlvsCIt->ptA && aConstr.ptB == aSlvsCIt->ptB &&
425 aConstr.entityA == aSlvsCIt->entityA && aConstr.entityB == aSlvsCIt->entityB &&
426 aConstr.entityC == aSlvsCIt->entityC && aConstr.entityD == aSlvsCIt->entityD) {
427 Slvs_hConstraint anIDToRemove = aConstr.h;
429 int aShift = (int)(aSlvsCIt - myConstraints.begin());
430 removeConstraint(anIDToRemove);
431 aSlvsCIt = myConstraints.begin() + aShift - 1;
432 for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
433 if (aSlvsCIt->h == aConstr.h)
438 if (aSlvsCIt != myConstraints.end()) {
439 // constraint is duplicated, search its wrapper to add the mapping
440 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
441 anIt2 = myConstraintMap.begin();
442 std::list<ConstraintWrapperPtr>::const_iterator aCIt2;
443 for (; anIt2 != myConstraintMap.end(); ++anIt2)
444 for (aCIt2 = anIt2->second.begin(); aCIt2 != anIt2->second.end(); ++aCIt2)
445 if ((Slvs_hConstraint)(*aCIt2)->id() == aConstr.h) {
446 addSameConstraints(*aCIt2, aWrapper);
450 aConstr.h = updateConstraint(aConstr);
451 aWrapper->changeConstraint() = aConstr;
453 // update sub-entities
454 aWrapper->setEntities(aSubs);
458 void SolveSpaceSolver_Storage::addSameConstraints(ConstraintWrapperPtr theConstraint1,
459 ConstraintWrapperPtr theConstraint2)
461 SameConstraintMap::iterator anIt = myEqualConstraints.begin();
462 for (; anIt != myEqualConstraints.end(); ++anIt) {
463 if (anIt->find(theConstraint1) != anIt->end()) {
464 anIt->insert(theConstraint2);
467 else if (anIt->find(theConstraint2) != anIt->end()) {
468 anIt->insert(theConstraint1);
472 // group not found => create new one
473 std::set<ConstraintWrapperPtr> aNewGroup;
474 aNewGroup.insert(theConstraint1);
475 aNewGroup.insert(theConstraint2);
476 myEqualConstraints.push_back(aNewGroup);
479 bool SolveSpaceSolver_Storage::findSameConstraint(ConstraintWrapperPtr theConstraint)
481 if (theConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT ||
482 theConstraint->type() == CONSTRAINT_MULTI_ROTATION ||
483 theConstraint->type() == CONSTRAINT_MULTI_TRANSLATION)
486 const Slvs_Constraint& aCBase =
487 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint)->constraint();
489 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
490 aCIt = myConstraintMap.begin();
491 std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
492 for (; aCIt != myConstraintMap.end(); ++aCIt)
493 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt) {
494 if ((*aCWIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
495 (*aCWIt)->type() == CONSTRAINT_MULTI_ROTATION ||
496 (*aCWIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
498 if ((*aCWIt)->type() == theConstraint->type()) {
499 const Slvs_Constraint& aCComp = getConstraint((Slvs_hConstraint)(*aCWIt)->id());
501 if (aCBase.ptA == aCComp.ptA && aCBase.ptB == aCComp.ptB &&
502 aCBase.entityA == aCComp.entityA && aCBase.entityB == aCComp.entityB &&
503 aCBase.entityC == aCComp.entityC && aCBase.entityD == aCComp.entityD &&
504 fabs(aCBase.valA -aCComp.valA) < tolerance) {
505 addSameConstraints(*aCWIt, theConstraint);
514 EntityWrapperPtr SolveSpaceSolver_Storage::calculateMiddlePoint(
515 EntityWrapperPtr theBase, double theCoeff)
517 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
519 std::shared_ptr<GeomAPI_XY> aMidPoint;
520 if (theBase->type() == ENTITY_LINE) {
521 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
522 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
523 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
524 for (int i = 0; i < 2; ++i, ++anIt)
525 aPoints[i] = aBuilder->point(*anIt);
526 aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
527 aPoints[1]->xy()->multiplied(theCoeff));
529 else if (theBase->type() == ENTITY_ARC) {
531 double anArcPoint[3][2];
532 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
533 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
534 for (int i = 0; i < 3; ++i, ++anIt) {
535 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
536 anArcPoint[i][0] = aPoint->x();
537 anArcPoint[i][1] = aPoint->y();
539 // project last point of arc on the arc
540 double x = anArcPoint[1][0] - anArcPoint[0][0];
541 double y = anArcPoint[1][1] - anArcPoint[0][1];
542 double aRad = sqrt(x*x + y*y);
543 x = anArcPoint[2][0] - anArcPoint[0][0];
544 y = anArcPoint[2][1] - anArcPoint[0][1];
545 double aNorm = sqrt(x*x + y*y);
546 if (aNorm >= tolerance) {
547 anArcPoint[2][0] = x * aRad / aNorm;
548 anArcPoint[2][1] = y * aRad / aNorm;
550 anArcPoint[1][0] -= anArcPoint[0][0];
551 anArcPoint[1][1] -= anArcPoint[0][1];
552 if (theCoeff < tolerance) {
553 theX = anArcPoint[0][0] + anArcPoint[1][0];
554 theY = anArcPoint[0][1] + anArcPoint[1][1];
555 } else if (1 - theCoeff < tolerance) {
556 theX = anArcPoint[0][0] + anArcPoint[2][0];
557 theY = anArcPoint[0][1] + anArcPoint[2][1];
559 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
560 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
561 double anAngle = aStartDir->angle(aEndDir);
565 double aCos = cos(anAngle);
566 double aSin = sin(anAngle);
567 theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
568 theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
570 aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
574 return EntityWrapperPtr();
576 std::list<ParameterWrapperPtr> aParameters;
577 Slvs_Param aParam1 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->x());
578 aParam1.h = addParameter(aParam1);
579 aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam1)));
580 Slvs_Param aParam2 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->y());
581 aParam2.h = addParameter(aParam2);
582 aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam2)));
583 // Create entity (parameters are not filled)
584 Slvs_Entity anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
585 (Slvs_hEntity)myWorkplaneID, aParam1.h, aParam2.h);
586 anEntity.h = addEntity(anEntity);
588 EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(AttributePtr(), anEntity));
589 aResult->setParameters(aParameters);
598 Slvs_hParam SolveSpaceSolver_Storage::addParameter(const Slvs_Param& theParam)
600 if (theParam.h > 0 && theParam.h <= myParamMaxID) {
601 // parameter is already used, rewrite it
602 return updateParameter(theParam);
605 Slvs_Param aParam = theParam;
606 if (aParam.h > myParamMaxID)
607 myParamMaxID = aParam.h;
609 aParam.h = ++myParamMaxID;
610 myParameters.push_back(aParam);
611 myNeedToResolve = true;
615 Slvs_hParam SolveSpaceSolver_Storage::updateParameter(const Slvs_Param& theParam)
617 if (theParam.h > 0 && theParam.h <= myParamMaxID) {
618 // parameter already used, rewrite it
619 int aPos = Search(theParam.h, myParameters);
620 if (aPos >= 0 && aPos < (int)myParameters.size()) {
621 if (IsNotEqual(myParameters[aPos], theParam))
622 setNeedToResolve(true);
623 myParameters[aPos] = theParam;
628 // Parameter is not found, add new one
629 Slvs_Param aParam = theParam;
631 return addParameter(aParam);
634 bool SolveSpaceSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
636 int aPos = Search(theParamID, myParameters);
637 if (aPos >= 0 && aPos < (int)myParameters.size()) {
638 // Firstly, search the parameter is not used elsewhere
639 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
640 for (; anEntIter != myEntities.end(); anEntIter++) {
641 for (int i = 0; i < 4; i++)
642 if (anEntIter->param[i] == theParamID)
646 myParameters.erase(myParameters.begin() + aPos);
647 myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
648 myNeedToResolve = true;
653 const Slvs_Param& SolveSpaceSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
655 int aPos = Search(theParamID, myParameters);
656 if (aPos >= 0 && aPos < (int)myParameters.size())
657 return myParameters[aPos];
659 // Parameter is not found, return empty object
660 static Slvs_Param aDummy;
666 Slvs_hEntity SolveSpaceSolver_Storage::addEntity(const Slvs_Entity& theEntity)
668 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
669 // Entity is already used, rewrite it
670 return updateEntity(theEntity);
673 Slvs_Entity aEntity = theEntity;
674 if (aEntity.h > myEntityMaxID)
675 myEntityMaxID = aEntity.h;
677 aEntity.h = ++myEntityMaxID;
678 myEntities.push_back(aEntity);
679 myNeedToResolve = true;
683 Slvs_hEntity SolveSpaceSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
685 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
686 // Entity already used, rewrite it
687 int aPos = Search(theEntity.h, myEntities);
688 if (aPos >= 0 && aPos < (int)myEntities.size()) {
689 myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
690 myEntities[aPos] = theEntity;
695 // Entity is not found, add new one
696 Slvs_Entity aEntity = theEntity;
698 return addEntity(aEntity);
701 bool SolveSpaceSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
704 int aPos = Search(theEntityID, myEntities);
705 if (aPos >= 0 && aPos < (int)myEntities.size()) {
706 // Firstly, check the entity and its attributes is not used elsewhere
707 std::set<Slvs_hEntity> anEntAndSubs;
708 anEntAndSubs.insert(theEntityID);
709 for (int i = 0; i < 4; i++)
710 if (myEntities[aPos].point[i] != SLVS_E_UNKNOWN)
711 anEntAndSubs.insert(myEntities[aPos].point[i]);
713 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
714 for (; anEntIter != myEntities.end(); anEntIter++) {
715 if (anEntAndSubs.find(anEntIter->h) != anEntAndSubs.end())
717 for (int i = 0; i < 4; i++)
718 if (anEntAndSubs.find(anEntIter->point[i]) != anEntAndSubs.end())
720 if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
723 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
724 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
725 Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
726 aConstrIter->entityA, aConstrIter->entityB,
727 aConstrIter->entityC, aConstrIter->entityD};
728 for (int i = 0; i < 6; i++)
729 if (anEntAndSubs.find(anEntIDs[i]) != anEntAndSubs.end())
732 // The entity is not used, remove it and its parameters
733 Slvs_Entity anEntity = myEntities[aPos];
734 myEntities.erase(myEntities.begin() + aPos);
735 myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
736 if (anEntity.distance != SLVS_E_UNKNOWN)
737 aResult = aResult && removeEntity(anEntity.distance);
738 for (int i = 0; i < 4; i++)
739 if (anEntity.param[i] != SLVS_E_UNKNOWN)
740 aResult = removeParameter(anEntity.param[i]) && aResult;
741 myNeedToResolve = true;
746 const Slvs_Entity& SolveSpaceSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
748 int aPos = Search(theEntityID, myEntities);
749 if (aPos >= 0 && aPos < (int)myEntities.size())
750 return myEntities[aPos];
752 // Entity is not found, return empty object
753 static Slvs_Entity aDummy;
754 aDummy.h = SLVS_E_UNKNOWN;
759 Slvs_hConstraint SolveSpaceSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
761 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
762 // Constraint is already used, rewrite it
763 return updateConstraint(theConstraint);
766 Slvs_Constraint aConstraint = theConstraint;
768 // Find a constraint with same type uses same arguments to show user overconstraint situation
769 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
770 for (; aCIt != myConstraints.end(); aCIt++) {
771 if (aConstraint.type != aCIt->type)
773 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
774 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
775 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
776 myDuplicatedConstraint = true;
781 if (aConstraint.h > myConstrMaxID)
782 myConstrMaxID = aConstraint.h;
784 aConstraint.h = ++myConstrMaxID;
785 myConstraints.push_back(aConstraint);
786 myNeedToResolve = true;
787 return aConstraint.h;
790 Slvs_hConstraint SolveSpaceSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
792 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
793 // Constraint already used, rewrite it
794 int aPos = Search(theConstraint.h, myConstraints);
795 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
796 myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
797 myConstraints[aPos] = theConstraint;
798 return theConstraint.h;
802 // Constraint is not found, add new one
803 Slvs_Constraint aConstraint = theConstraint;
806 // Firstly, check middle-point constraint conflicts with point-on-line
807 if (aConstraint.type == SLVS_C_AT_MIDPOINT) {
808 std::vector<Slvs_Constraint>::const_iterator anIt = myConstraints.begin();
809 for (; anIt != myConstraints.end(); ++anIt)
810 if (anIt->type == SLVS_C_PT_ON_LINE &&
811 anIt->ptA == aConstraint.ptA &&
812 anIt->entityA == aConstraint.entityA)
814 if (anIt != myConstraints.end()) {
815 // change the constraint to the lengths equality to avoid conflicts
816 Slvs_Entity aLine = getEntity(aConstraint.entityA);
817 Slvs_Entity aNewLine1 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
818 myWorkplaneID, aLine.point[0], aConstraint.ptA);
819 aNewLine1.h = addEntity(aNewLine1);
820 Slvs_Entity aNewLine2 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
821 myWorkplaneID, aLine.point[1], aConstraint.ptA);
822 aNewLine2.h = addEntity(aNewLine2);
823 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, SLVS_C_EQUAL_LENGTH_LINES,
824 myWorkplaneID, 0.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aNewLine1.h, aNewLine2.h);
828 return addConstraint(aConstraint);
831 bool SolveSpaceSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
833 int aPos = Search(theConstraintID, myConstraints);
834 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
835 Slvs_Constraint aConstraint = myConstraints[aPos];
836 myConstraints.erase(myConstraints.begin() + aPos);
837 myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
838 myNeedToResolve = true;
840 if (myDuplicatedConstraint) {
841 // Find a constraint with same type uses same arguments
842 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
843 for (; aCIt != myConstraints.end(); aCIt++) {
844 if (aConstraint.type != aCIt->type)
846 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
847 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
848 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
849 myDuplicatedConstraint = false;
858 const Slvs_Constraint& SolveSpaceSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
860 int aPos = Search(theConstraintID, myConstraints);
861 if (aPos >= 0 && aPos < (int)myConstraints.size())
862 return myConstraints[aPos];
864 // Constraint is not found, return empty object
865 static Slvs_Constraint aDummy;
871 void SolveSpaceSolver_Storage::initializeSolver(SolverPtr theSolver)
873 std::shared_ptr<SolveSpaceSolver_Solver> aSolver =
874 std::dynamic_pointer_cast<SolveSpaceSolver_Solver>(theSolver);
881 if (myConstraints.empty()) {
882 // Adjust all arc to place their points correctly
883 std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
884 for (; anEntIt != myEntities.end(); ++anEntIt)
885 if (anEntIt->type == SLVS_E_ARC_OF_CIRCLE)
889 aSolver->setParameters(myParameters.data(), (int)myParameters.size());
890 aSolver->setEntities(myEntities.data(), (int)myEntities.size());
891 aSolver->setConstraints(myConstraints.data(), (int)myConstraints.size());
895 bool SolveSpaceSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
897 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
898 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
900 // verify whether the constraint has duplicated
901 bool hasSameID = false;
902 SameConstraintMap::iterator anEqIt = myEqualConstraints.begin();
903 for (; anEqIt != myEqualConstraints.end(); ++anEqIt) {
904 std::set<ConstraintWrapperPtr>::const_iterator aFound = anEqIt->find(aConstraint);
905 if (aFound != anEqIt->end()) {
906 // verify there is a constraint with same ID
907 std::set<ConstraintWrapperPtr>::const_iterator anIt = anEqIt->begin();
908 ConstraintID anID = (*aFound)->id();
909 for (++anIt; anIt != anEqIt->end() && !hasSameID; ++anIt)
910 if ((*anIt)->id() == anID && aFound != anIt)
913 anEqIt->erase(aConstraint);
917 if (anEqIt != myEqualConstraints.end() && hasSameID)
920 bool isFullyRemoved = removeConstraint((Slvs_hConstraint)aConstraint->id());
921 // remove point-point coincidence
922 if (aConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT)
923 isFullyRemoved = removeCoincidence(theConstraint) && isFullyRemoved;
924 return SketchSolver_Storage::remove(theConstraint) && isFullyRemoved;
927 bool SolveSpaceSolver_Storage::remove(EntityWrapperPtr theEntity)
932 // Additional check for entity to be used in point-point coincidence
933 bool isCoincide = false;
934 if (theEntity->type() == ENTITY_POINT) {
935 CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
936 std::set<EntityWrapperPtr>::const_iterator aCIt;
937 for (; anIt != myCoincidentPoints.end(); ++anIt) {
938 if (anIt->first == theEntity)
940 for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt)
941 if (*aCIt == theEntity)
943 if (aCIt != anIt->second.end())
946 if (anIt != myCoincidentPoints.end()) {
947 if (anIt->first != theEntity && isUsed(anIt->first->baseAttribute()))
949 for (aCIt = anIt->second.begin(); !isCoincide && aCIt != anIt->second.end(); ++aCIt)
950 if (*aCIt != theEntity && isUsed((*aCIt)->baseAttribute()))
955 std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity =
956 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
957 bool isFullyRemoved = isCoincide ? true : removeEntity((Slvs_hEntity)anEntity->id());
958 return (SketchSolver_Storage::remove(theEntity) || isCoincide) && isFullyRemoved;
961 bool SolveSpaceSolver_Storage::remove(ParameterWrapperPtr theParameter)
963 return removeParameter((Slvs_hParam)theParameter->id());
967 void SolveSpaceSolver_Storage::refresh(bool theFixedOnly) const
971 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
972 std::list<ParameterWrapperPtr> aParams;
973 std::list<ParameterWrapperPtr>::const_iterator aParIt;
974 for (; anIt != myAttributeMap.end(); ++anIt) {
977 // the external feature always should keep the up to date values, so,
978 // refresh from the solver is never needed
979 if (anIt->first.get()) {
980 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
981 std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
982 if (aSketchFeature.get() && aSketchFeature->isExternal())
984 // not need to refresh here sketch's origin and normal vector
985 CompositeFeaturePtr aSketch =
986 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anIt->first->owner());
991 // update parameter wrappers and obtain values of attributes
992 aParams = anIt->second->parameters();
994 bool isUpd[3] = {false};
996 for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
997 std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aWrapper =
998 std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(*aParIt);
999 if (!theFixedOnly || aWrapper->group() == GID_OUTOFGROUP || aWrapper->isParametric()) {
1000 aWrapper->changeParameter().val = getParameter((Slvs_hParam)aWrapper->id()).val;
1001 aCoords[i] = aWrapper->value();
1005 if (!isUpd[0] && !isUpd[1] && !isUpd[2])
1006 continue; // nothing is updated
1008 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1009 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
1011 if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
1012 (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance)) {
1013 if (!isUpd[0]) aCoords[0] = aPoint2D->x();
1014 if (!isUpd[1]) aCoords[1] = aPoint2D->y();
1015 aPoint2D->setValue(aCoords[0], aCoords[1]);
1016 // Find points coincident with this one (probably not in GID_OUTOFGROUP)
1017 std::map<AttributePtr, EntityWrapperPtr>::const_iterator aLocIt;
1019 aLocIt = myAttributeMap.begin();
1024 for (; aLocIt != myAttributeMap.end(); ++aLocIt) {
1025 if (!aLocIt->second)
1027 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1028 std::dynamic_pointer_cast<SketchPlugin_Feature>(aLocIt->first->owner());
1029 if (aSketchFeature && aSketchFeature->isExternal())
1031 if (anIt->second->id() == aLocIt->second->id()) {
1032 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aLocIt->first);
1033 aPoint2D->setValue(aCoords[0], aCoords[1]);
1039 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
1041 if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
1042 aScalar->setValue(aCoords[0]);
1045 std::shared_ptr<GeomDataAPI_Point> aPoint =
1046 std::dynamic_pointer_cast<GeomDataAPI_Point>(anIt->first);
1048 if ((isUpd[0] && fabs(aPoint->x() - aCoords[0]) > tolerance) ||
1049 (isUpd[1] && fabs(aPoint->y() - aCoords[1]) > tolerance) ||
1050 (isUpd[2] && fabs(aPoint->z() - aCoords[2]) > tolerance))
1051 if (!isUpd[0]) aCoords[0] = aPoint->x();
1052 if (!isUpd[1]) aCoords[1] = aPoint->y();
1053 if (!isUpd[2]) aCoords[2] = aPoint->z();
1054 aPoint->setValue(aCoords[0], aCoords[1], aCoords[2]);
1059 //blockEvents(false);
1062 void SolveSpaceSolver_Storage::verifyFixed()
1064 std::map<AttributePtr, EntityWrapperPtr>::iterator anAttrIt = myAttributeMap.begin();
1065 for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
1066 if (!anAttrIt->second)
1068 if (anAttrIt->second->group() == GID_OUTOFGROUP) {
1069 Slvs_Entity anEnt = getEntity((Slvs_hEntity)anAttrIt->second->id());
1070 if (anEnt.group != (Slvs_hEntity)GID_OUTOFGROUP)
1071 anEnt.group = (Slvs_hEntity)GID_OUTOFGROUP;
1072 updateEntity(anEnt);
1075 const std::list<ParameterWrapperPtr>& aParameters = anAttrIt->second->parameters();
1076 std::list<ParameterWrapperPtr>::const_iterator aParIt = aParameters.begin();
1077 for (; aParIt != aParameters.end(); ++aParIt)
1078 if (anAttrIt->second->group() == GID_OUTOFGROUP || (*aParIt)->group() == GID_OUTOFGROUP) {
1079 Slvs_Param aParam = getParameter((Slvs_hParam)(*aParIt)->id());
1080 if (aParam.group != (Slvs_hParam)GID_OUTOFGROUP) {
1081 aParam.group = (Slvs_hParam)GID_OUTOFGROUP;
1082 updateParameter(aParam);
1089 void SolveSpaceSolver_Storage::adjustArc(const Slvs_Entity& theArc)
1091 double anArcPoints[3][2];
1092 double aDist[3] = {0.0};
1093 bool isFixed[3] = {false};
1094 for (int i = 0; i < 3; ++i) {
1095 Slvs_Entity aPoint = getEntity(theArc.point[i]);
1096 isFixed[i] = (aPoint.group != (Slvs_hGroup)myGroupID);
1097 anArcPoints[i][0] = getParameter(aPoint.param[0]).val;
1098 anArcPoints[i][1] = getParameter(aPoint.param[1]).val;
1100 anArcPoints[i][0] -= anArcPoints[0][0];
1101 anArcPoints[i][1] -= anArcPoints[0][1];
1102 aDist[i] = sqrt(anArcPoints[i][0] * anArcPoints[i][0] +
1103 anArcPoints[i][1] * anArcPoints[i][1]);
1107 if (fabs(aDist[1] - aDist[2]) < tolerance)
1111 while (anInd > 0 && isFixed[anInd])
1114 return; // adjust only start or end point of the arc
1116 anArcPoints[anInd][0] /= aDist[anInd];
1117 anArcPoints[anInd][1] /= aDist[anInd];
1119 Slvs_Entity aPoint = getEntity(theArc.point[anInd]);
1120 for (int i = 0; i < 2; ++i) {
1121 Slvs_Param aParam = getParameter(aPoint.param[i]);
1122 aParam.val = anArcPoints[0][i] + aDist[3-anInd] * anArcPoints[anInd][i];
1123 updateParameter(aParam);
1133 // ========================================================
1134 // ========= Auxiliary functions ===============
1135 // ========================================================
1137 template<typename T>
1138 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1140 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1141 int aVecSize = (int)theEntities.size();
1142 if (theEntities.empty())
1144 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1146 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1148 if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1149 aResIndex = aVecSize;
1153 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1155 return fabs(theParam1.val - theParam2.val) > tolerance;
1158 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1161 for (; theEntity1.param[i] != 0 && i < 4; i++)
1162 if (theEntity1.param[i] != theEntity2.param[i])
1165 for (; theEntity1.point[i] != 0 && i < 4; i++)
1166 if (theEntity1.point[i] != theEntity2.point[i])
1171 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1173 return theConstraint1.ptA != theConstraint2.ptA ||
1174 theConstraint1.ptB != theConstraint2.ptB ||
1175 theConstraint1.entityA != theConstraint2.entityA ||
1176 theConstraint1.entityB != theConstraint2.entityB ||
1177 theConstraint1.entityC != theConstraint2.entityC ||
1178 theConstraint1.entityD != theConstraint2.entityD ||
1179 fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;