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,
40 const Slvs_Constraint& theConstraint2);
43 SolveSpaceSolver_Storage::SolveSpaceSolver_Storage(const GroupID& theGroup)
44 : SketchSolver_Storage(theGroup),
45 myWorkplaneID(SLVS_E_UNKNOWN),
46 myParamMaxID(SLVS_E_UNKNOWN),
47 myEntityMaxID(SLVS_E_UNKNOWN),
48 myConstrMaxID(SLVS_C_UNKNOWN),
49 myDuplicatedConstraint(false)
53 bool SolveSpaceSolver_Storage::update(ConstraintWrapperPtr theConstraint)
55 bool isUpdated = false;
56 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
57 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
58 Slvs_Constraint aSlvsConstr = getConstraint((Slvs_hConstraint)aConstraint->id());
59 if (aSlvsConstr.h == SLVS_C_UNKNOWN)
60 aSlvsConstr = aConstraint->constraint();
62 // update value of constraint if exist
63 double aCoeff = aSlvsConstr.type == SLVS_C_DIAMETER ? 2.0 : 1.0;
64 if (fabs(aSlvsConstr.valA - theConstraint->value() * aCoeff) > tolerance) {
65 aSlvsConstr.valA = theConstraint->value() * aCoeff;
69 // update constrained entities
70 Slvs_hEntity* aPnts[2] = {&aSlvsConstr.ptA, &aSlvsConstr.ptB};
71 Slvs_hEntity* anEnts[4] = {&aSlvsConstr.entityA, &aSlvsConstr.entityB,
72 &aSlvsConstr.entityC, &aSlvsConstr.entityD};
76 std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
77 std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
78 for (; anIt != anEntities.end(); ++anIt) {
79 isUpdated = update(*anIt) || isUpdated;
80 // do not update constrained entities for Multi constraints,
81 // and for middle point constraint translated to equal lines
82 ConstraintPtr aBaseConstraint = theConstraint->baseConstraint();
83 if (aSlvsConstr.type == SLVS_C_MULTI_ROTATION || aSlvsConstr.type == SLVS_C_MULTI_TRANSLATION ||
84 (aBaseConstraint && aBaseConstraint->getKind() == SketchPlugin_ConstraintMiddle::ID() &&
85 aSlvsConstr.type != SLVS_C_AT_MIDPOINT))
88 Slvs_hEntity anID = (Slvs_hEntity)(*anIt)->id();
89 if ((*anIt)->type() == ENTITY_POINT) {
90 if (*(aPnts[aPtInd]) != anID) {
91 *(aPnts[aPtInd]) = anID;
96 if (*(anEnts[aEntInd]) != anID) {
97 *(anEnts[aEntInd]) = anID;
104 // update constraint itself (do not update constraints Multi)
105 if (aSlvsConstr.type != SLVS_C_MULTI_ROTATION && aSlvsConstr.type != SLVS_C_MULTI_TRANSLATION) {
106 if (aSlvsConstr.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
107 aSlvsConstr.wrkpl = myWorkplaneID;
108 if (aSlvsConstr.group == SLVS_G_UNKNOWN)
109 aSlvsConstr.group = (Slvs_hGroup)myGroupID;
110 bool hasDupConstraints = myDuplicatedConstraint;
111 Slvs_hConstraint aConstrID = updateConstraint(aSlvsConstr);
112 if (aSlvsConstr.h == SLVS_C_UNKNOWN) {
113 aConstraint->changeConstraint() = getConstraint(aConstrID);
115 // check duplicated constraints based on different attributes
116 if (myDuplicatedConstraint && findSameConstraint(aConstraint) && !hasDupConstraints)
117 myDuplicatedConstraint = false;
123 bool SolveSpaceSolver_Storage::update(EntityWrapperPtr theEntity)
125 bool isUpdated = false;
126 std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity =
127 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
128 Slvs_Entity aSlvsEnt = getEntity((Slvs_hEntity)anEntity->id());
129 if (aSlvsEnt.h == SLVS_E_UNKNOWN)
130 aSlvsEnt = anEntity->entity();
132 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
133 std::list<ParameterWrapperPtr>::iterator aPIt;
134 // if the entity is an attribute, need to update its coordinates
135 if (anEntity->baseAttribute()) {
136 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
137 EntityWrapperPtr anUpdAttr = aBuilder->createAttribute(anEntity->baseAttribute(), GID_UNKNOWN);
139 std::list<ParameterWrapperPtr> anUpdParams = anUpdAttr->parameters();
140 std::list<ParameterWrapperPtr>::iterator anUpdIt = anUpdParams.begin();
141 for (aPIt = aParams.begin(); aPIt != aParams.end() && anUpdIt != anUpdParams.end();
143 (*aPIt)->update(*anUpdIt);
150 for (aPIt = aParams.begin(); aPIt != aParams.end(); ++aPIt, ++anInd) {
152 isUpdated = update(*aPIt) || isUpdated;
153 if (aSlvsEnt.param[anInd] != (Slvs_hEntity)(*aPIt)->id()) {
155 aSlvsEnt.param[anInd] = (Slvs_hEntity)(*aPIt)->id();
159 // update sub-entities
160 std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
161 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
162 for (anInd = 0; aSIt != aSubEntities.end(); ++aSIt, ++anInd) {
164 isUpdated = update(*aSIt) || isUpdated;
166 Slvs_hEntity anID = Slvs_hEntity((*aSIt)->id());
167 if ((*aSIt)->type() == ENTITY_NORMAL)
168 aSlvsEnt.normal = anID;
169 else if ((*aSIt)->type() == ENTITY_SCALAR)
170 aSlvsEnt.distance = anID;
171 else if (aSlvsEnt.point[anInd] != anID) {
172 aSlvsEnt.point[anInd] = anID;
173 if ((*aSIt)->baseAttribute())
174 SketchSolver_Storage::addEntity((*aSIt)->baseAttribute(), *aSIt);
178 if (theEntity->type() == ENTITY_POINT && aSubEntities.size() == 1) {
179 // theEntity is based on SketchPlugin_Point => need to substitute its attribute instead
180 bool isNew = (aSlvsEnt.h == SLVS_E_UNKNOWN);
181 aSlvsEnt = getEntity(aSlvsEnt.point[0]);
183 anEntity->changeEntity() = aSlvsEnt;
188 // update entity itself
189 if (aSlvsEnt.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
190 aSlvsEnt.wrkpl = myWorkplaneID;
191 if (aSlvsEnt.group == SLVS_G_UNKNOWN)
192 aSlvsEnt.group = (Slvs_hGroup)myGroupID;
193 Slvs_hEntity anEntID = updateEntity(aSlvsEnt);
194 if (aSlvsEnt.h == SLVS_E_UNKNOWN || isUpdated) {
195 anEntity->changeEntity() = getEntity(anEntID);
198 if (anEntity->type() == ENTITY_SKETCH)
199 storeWorkplane(anEntity);
205 bool SolveSpaceSolver_Storage::update(ParameterWrapperPtr theParameter)
207 std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aParameter =
208 std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(theParameter);
209 const Slvs_Param& aParam = getParameter((Slvs_hParam)aParameter->id());
210 if (aParam.h != SLVS_E_UNKNOWN && fabs(aParam.val - aParameter->value()) < tolerance)
212 Slvs_Param aParamToUpd = aParameter->parameter();
213 if (aParamToUpd.group == SLVS_G_UNKNOWN)
214 aParamToUpd.group = aParameter->isParametric() ? (Slvs_hGroup)GID_OUTOFGROUP :
215 (Slvs_hGroup)myGroupID;
216 Slvs_hParam anID = updateParameter(aParamToUpd);
217 if (aParam.h == SLVS_E_UNKNOWN) // new parameter
218 aParameter->changeParameter() = getParameter(anID);
222 void SolveSpaceSolver_Storage::storeWorkplane(EntityWrapperPtr theSketch)
224 mySketchID = theSketch->id();
225 myWorkplaneID = (Slvs_hEntity)mySketchID;
227 // Update sub-entities of the sketch
228 std::list<EntityWrapperPtr> aSubEntities = theSketch->subEntities();
229 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
230 for (; aSIt != aSubEntities.end(); ++aSIt) {
231 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSub =
232 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*aSIt);
233 aSub->changeEntity().wrkpl = myWorkplaneID;
236 // Update all stored entities
237 std::vector<Slvs_Entity>::iterator anIt = myEntities.begin();
238 for (; anIt != myEntities.end(); ++anIt)
239 anIt->wrkpl = myWorkplaneID;
242 void SolveSpaceSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
244 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
245 std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
246 for (; aPIt != aParams.end(); ++aPIt)
247 changeGroup(*aPIt, theGroup);
249 std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
250 std::list<EntityWrapperPtr>::iterator aSIt = aSubs.begin();
251 for (; aSIt != aSubs.end(); ++aSIt)
252 changeGroup(*aSIt, theGroup);
254 theEntity->setGroup(theGroup);
255 int aPos = Search((Slvs_hEntity)theEntity->id(), myEntities);
256 if (aPos >= 0 && aPos < (int)myEntities.size()) {
257 if (myEntities[aPos].group != (Slvs_hGroup)theGroup)
258 setNeedToResolve(true);
259 myEntities[aPos].group = (Slvs_hGroup)theGroup;
263 void SolveSpaceSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
265 GroupID aGroup = theGroup;
266 if (theParam->isParametric())
267 aGroup = GID_OUTOFGROUP;
268 if (theParam->group() == aGroup)
271 theParam->setGroup(aGroup);
272 int aPos = Search((Slvs_hParam)theParam->id(), myParameters);
273 if (aPos >= 0 && aPos < (int)myParameters.size()) {
274 myParameters[aPos].group = (Slvs_hGroup)aGroup;
275 setNeedToResolve(true);
279 void SolveSpaceSolver_Storage::addCoincidentPoints(
280 EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
282 if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
284 if (!theMaster->subEntities().empty() || !theSlave->subEntities().empty()) {
285 EntityWrapperPtr aSubMaster = theMaster->subEntities().empty() ?
286 theMaster : theMaster->subEntities().front();
287 EntityWrapperPtr aSubSlave = theSlave->subEntities().empty() ?
288 theSlave : theSlave->subEntities().front();
289 return addCoincidentPoints(aSubMaster, aSubSlave);
292 // Search available coincidence
293 CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(theMaster);
294 CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(theSlave);
295 if (aMasterFound == myCoincidentPoints.end() || aSlaveFound == myCoincidentPoints.end()) {
296 // try to find master and slave points in the lists of slaves of already existent coincidences
297 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
298 for (; anIt != myCoincidentPoints.end(); ++anIt) {
299 if (anIt->second.find(theMaster) != anIt->second.end())
301 else if (anIt->second.find(theSlave) != anIt->second.end())
304 if (aMasterFound != myCoincidentPoints.end() && aSlaveFound != myCoincidentPoints.end())
309 if (aMasterFound == myCoincidentPoints.end()) {
311 myCoincidentPoints[theMaster] = std::set<EntityWrapperPtr>();
312 aMasterFound = myCoincidentPoints.find(theMaster);
313 } else if (aMasterFound == aSlaveFound)
314 return; // already coincident
316 if (aSlaveFound != myCoincidentPoints.end()) {
317 // A slave has been found, we need to attach all points coincident with it to the new master
318 std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
319 aNewSlaves.insert(aSlaveFound->first);
320 myCoincidentPoints.erase(aSlaveFound);
322 std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
323 for (; aSlIt != aNewSlaves.end(); ++aSlIt)
324 addCoincidentPoints(theMaster, *aSlIt);
326 // Update the slave if it was used in constraints and features
327 replaceInFeatures(theSlave, theMaster);
328 replaceInConstraints(theSlave, theMaster);
330 // Remove slave entity (if the IDs are equal no need to remove slave entity, just update it)
331 if (theMaster->id() != theSlave->id())
332 removeEntity((Slvs_hEntity)theSlave->id());
334 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointMaster =
335 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMaster);
336 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointSlave =
337 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSlave);
338 aPointSlave->changeEntity() = aPointMaster->entity();
339 aPointSlave->setParameters(aPointMaster->parameters());
341 aMasterFound->second.insert(theSlave);
345 void SolveSpaceSolver_Storage::replaceInFeatures(
346 EntityWrapperPtr theSource, EntityWrapperPtr theDest)
348 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator anIt = myFeatureMap.begin();
349 for (; anIt != myFeatureMap.end(); ++anIt) {
352 bool isUpdated = false;
353 std::list<EntityWrapperPtr> aSubs = anIt->second->subEntities();
354 std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
355 for (; aSubIt != aSubs.end(); ++aSubIt)
356 if ((*aSubIt)->id() == theSource->id()) {
357 (*aSubIt)->update(theDest);
364 std::shared_ptr<SolveSpaceSolver_EntityWrapper> aWrapper =
365 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(anIt->second);
366 // update SolveSpace entity
367 Slvs_Entity anEnt = aWrapper->entity();
368 for (int i = 0; i < 4; ++i)
369 if (anEnt.point[i] == (Slvs_hEntity)theSource->id())
370 anEnt.point[i] = (Slvs_hEntity)theDest->id();
371 anEnt.h = updateEntity(anEnt);
372 aWrapper->changeEntity() = anEnt;
374 // update sub-entities
375 aWrapper->setSubEntities(aSubs);
379 void SolveSpaceSolver_Storage::replaceInConstraints(
380 EntityWrapperPtr theSource, EntityWrapperPtr theDest)
382 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
383 anIt = myConstraintMap.begin();
384 std::list<ConstraintWrapperPtr>::const_iterator aCIt;
385 for (; anIt != myConstraintMap.end(); ++anIt)
386 for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt) {
387 // Do not process coincidence between points and "multi" constraints
388 // (these constraints are stored to keep the structure of constraints).
389 if ((*aCIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
390 (*aCIt)->type() == CONSTRAINT_MULTI_ROTATION ||
391 (*aCIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
394 bool isUpdated = false;
395 std::list<EntityWrapperPtr> aSubs = (*aCIt)->entities();
396 std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
397 for (; aSubIt != aSubs.end(); ++aSubIt)
398 if ((*aSubIt)->id() == theSource->id()) {
399 (*aSubIt)->update(theDest);
406 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aWrapper =
407 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(*aCIt);
408 if (theSource->id() == theDest->id()) {
409 // No need to update SolveSpace constraint if the entities are the same
410 aWrapper->changeConstraint() = getConstraint((Slvs_hConstraint)aWrapper->id());
411 aWrapper->setEntities(aSubs);
415 // change constraint entities
416 Slvs_Constraint aConstr = aWrapper->constraint();
417 if (aConstr.ptA == (Slvs_hEntity)theSource->id())
418 aConstr.ptA = (Slvs_hEntity)theDest->id();
419 if (aConstr.ptB == (Slvs_hEntity)theSource->id())
420 aConstr.ptB = (Slvs_hEntity)theDest->id();
422 // check the constraint is duplicated
423 std::vector<Slvs_Constraint>::const_iterator aSlvsCIt = myConstraints.begin();
424 for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
425 if (aConstr.h != aSlvsCIt->h &&
426 aConstr.type == aSlvsCIt->type &&
427 aConstr.ptA == aSlvsCIt->ptA && aConstr.ptB == aSlvsCIt->ptB &&
428 aConstr.entityA == aSlvsCIt->entityA && aConstr.entityB == aSlvsCIt->entityB &&
429 aConstr.entityC == aSlvsCIt->entityC && aConstr.entityD == aSlvsCIt->entityD) {
430 Slvs_hConstraint anIDToRemove = aConstr.h;
432 int aShift = (int)(aSlvsCIt - myConstraints.begin());
433 removeConstraint(anIDToRemove);
434 aSlvsCIt = myConstraints.begin() + aShift - 1;
435 for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
436 if (aSlvsCIt->h == aConstr.h)
441 if (aSlvsCIt != myConstraints.end()) {
442 // constraint is duplicated, search its wrapper to add the mapping
443 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
444 anIt2 = myConstraintMap.begin();
445 std::list<ConstraintWrapperPtr>::const_iterator aCIt2;
446 for (; anIt2 != myConstraintMap.end(); ++anIt2)
447 for (aCIt2 = anIt2->second.begin(); aCIt2 != anIt2->second.end(); ++aCIt2)
448 if ((Slvs_hConstraint)(*aCIt2)->id() == aConstr.h) {
449 addSameConstraints(*aCIt2, aWrapper);
453 aConstr.h = updateConstraint(aConstr);
454 aWrapper->changeConstraint() = aConstr;
456 // update sub-entities
457 aWrapper->setEntities(aSubs);
461 void SolveSpaceSolver_Storage::addSameConstraints(ConstraintWrapperPtr theConstraint1,
462 ConstraintWrapperPtr theConstraint2)
464 SameConstraintMap::iterator anIt = myEqualConstraints.begin();
465 for (; anIt != myEqualConstraints.end(); ++anIt) {
466 if (anIt->find(theConstraint1) != anIt->end()) {
467 anIt->insert(theConstraint2);
470 else if (anIt->find(theConstraint2) != anIt->end()) {
471 anIt->insert(theConstraint1);
475 // group not found => create new one
476 std::set<ConstraintWrapperPtr> aNewGroup;
477 aNewGroup.insert(theConstraint1);
478 aNewGroup.insert(theConstraint2);
479 myEqualConstraints.push_back(aNewGroup);
482 bool SolveSpaceSolver_Storage::findSameConstraint(ConstraintWrapperPtr theConstraint)
484 if (theConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT ||
485 theConstraint->type() == CONSTRAINT_MULTI_ROTATION ||
486 theConstraint->type() == CONSTRAINT_MULTI_TRANSLATION)
489 const Slvs_Constraint& aCBase =
490 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint)->constraint();
492 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
493 aCIt = myConstraintMap.begin();
494 std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
495 for (; aCIt != myConstraintMap.end(); ++aCIt)
496 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt) {
497 if ((*aCWIt)->type() == CONSTRAINT_PT_PT_COINCIDENT ||
498 (*aCWIt)->type() == CONSTRAINT_MULTI_ROTATION ||
499 (*aCWIt)->type() == CONSTRAINT_MULTI_TRANSLATION)
501 if ((*aCWIt)->type() == theConstraint->type()) {
502 const Slvs_Constraint& aCComp = getConstraint((Slvs_hConstraint)(*aCWIt)->id());
504 if (aCBase.ptA == aCComp.ptA && aCBase.ptB == aCComp.ptB &&
505 aCBase.entityA == aCComp.entityA && aCBase.entityB == aCComp.entityB &&
506 aCBase.entityC == aCComp.entityC && aCBase.entityD == aCComp.entityD &&
507 fabs(aCBase.valA -aCComp.valA) < tolerance) {
508 addSameConstraints(*aCWIt, theConstraint);
517 EntityWrapperPtr SolveSpaceSolver_Storage::calculateMiddlePoint(
518 EntityWrapperPtr theBase, double theCoeff)
520 BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
522 std::shared_ptr<GeomAPI_XY> aMidPoint;
523 if (theBase->type() == ENTITY_LINE) {
524 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
525 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
526 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
527 for (int i = 0; i < 2; ++i, ++anIt)
528 aPoints[i] = aBuilder->point(*anIt);
529 aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
530 aPoints[1]->xy()->multiplied(theCoeff));
532 else if (theBase->type() == ENTITY_ARC) {
534 double anArcPoint[3][2];
535 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
536 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
537 for (int i = 0; i < 3; ++i, ++anIt) {
538 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
539 anArcPoint[i][0] = aPoint->x();
540 anArcPoint[i][1] = aPoint->y();
542 // project last point of arc on the arc
543 double x = anArcPoint[1][0] - anArcPoint[0][0];
544 double y = anArcPoint[1][1] - anArcPoint[0][1];
545 double aRad = sqrt(x*x + y*y);
546 x = anArcPoint[2][0] - anArcPoint[0][0];
547 y = anArcPoint[2][1] - anArcPoint[0][1];
548 double aNorm = sqrt(x*x + y*y);
549 if (aNorm >= tolerance) {
550 anArcPoint[2][0] = x * aRad / aNorm;
551 anArcPoint[2][1] = y * aRad / aNorm;
553 anArcPoint[1][0] -= anArcPoint[0][0];
554 anArcPoint[1][1] -= anArcPoint[0][1];
555 if (theCoeff < tolerance) {
556 theX = anArcPoint[0][0] + anArcPoint[1][0];
557 theY = anArcPoint[0][1] + anArcPoint[1][1];
558 } else if (1 - theCoeff < tolerance) {
559 theX = anArcPoint[0][0] + anArcPoint[2][0];
560 theY = anArcPoint[0][1] + anArcPoint[2][1];
562 std::shared_ptr<GeomAPI_Dir2d>
563 aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
564 std::shared_ptr<GeomAPI_Dir2d>
565 aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
566 double anAngle = aStartDir->angle(aEndDir);
570 double aCos = cos(anAngle);
571 double aSin = sin(anAngle);
572 theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
573 theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
575 aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
579 return EntityWrapperPtr();
581 std::list<ParameterWrapperPtr> aParameters;
582 Slvs_Param aParam1 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->x());
583 aParam1.h = addParameter(aParam1);
584 aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam1)));
585 Slvs_Param aParam2 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->y());
586 aParam2.h = addParameter(aParam2);
587 aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam2)));
588 // Create entity (parameters are not filled)
589 Slvs_Entity anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
590 (Slvs_hEntity)myWorkplaneID, aParam1.h, aParam2.h);
591 anEntity.h = addEntity(anEntity);
593 EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(AttributePtr(), anEntity));
594 aResult->setParameters(aParameters);
603 Slvs_hParam SolveSpaceSolver_Storage::addParameter(const Slvs_Param& theParam)
605 if (theParam.h > 0 && theParam.h <= myParamMaxID) {
606 // parameter is already used, rewrite it
607 return updateParameter(theParam);
610 Slvs_Param aParam = theParam;
611 if (aParam.h > myParamMaxID)
612 myParamMaxID = aParam.h;
614 aParam.h = ++myParamMaxID;
615 myParameters.push_back(aParam);
616 myNeedToResolve = true;
620 Slvs_hParam SolveSpaceSolver_Storage::updateParameter(const Slvs_Param& theParam)
622 if (theParam.h > 0 && theParam.h <= myParamMaxID) {
623 // parameter already used, rewrite it
624 int aPos = Search(theParam.h, myParameters);
625 if (aPos >= 0 && aPos < (int)myParameters.size()) {
626 if (IsNotEqual(myParameters[aPos], theParam))
627 setNeedToResolve(true);
628 myParameters[aPos] = theParam;
633 // Parameter is not found, add new one
634 Slvs_Param aParam = theParam;
636 return addParameter(aParam);
639 bool SolveSpaceSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
641 int aPos = Search(theParamID, myParameters);
642 if (aPos >= 0 && aPos < (int)myParameters.size()) {
643 // Firstly, search the parameter is not used elsewhere
644 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
645 for (; anEntIter != myEntities.end(); anEntIter++) {
646 for (int i = 0; i < 4; i++)
647 if (anEntIter->param[i] == theParamID)
651 myParameters.erase(myParameters.begin() + aPos);
652 myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
653 myNeedToResolve = true;
658 const Slvs_Param& SolveSpaceSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
660 int aPos = Search(theParamID, myParameters);
661 if (aPos >= 0 && aPos < (int)myParameters.size())
662 return myParameters[aPos];
664 // Parameter is not found, return empty object
665 static Slvs_Param aDummy;
671 Slvs_hEntity SolveSpaceSolver_Storage::addEntity(const Slvs_Entity& theEntity)
673 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
674 // Entity is already used, rewrite it
675 return updateEntity(theEntity);
678 Slvs_Entity aEntity = theEntity;
679 if (aEntity.h > myEntityMaxID)
680 myEntityMaxID = aEntity.h;
682 aEntity.h = ++myEntityMaxID;
683 myEntities.push_back(aEntity);
684 myNeedToResolve = true;
688 Slvs_hEntity SolveSpaceSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
690 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
691 // Entity already used, rewrite it
692 int aPos = Search(theEntity.h, myEntities);
693 if (aPos >= 0 && aPos < (int)myEntities.size()) {
694 myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
695 myEntities[aPos] = theEntity;
700 // Entity is not found, add new one
701 Slvs_Entity aEntity = theEntity;
703 return addEntity(aEntity);
706 bool SolveSpaceSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
709 int aPos = Search(theEntityID, myEntities);
710 if (aPos >= 0 && aPos < (int)myEntities.size()) {
711 // Firstly, check the entity and its attributes is not used elsewhere
712 std::set<Slvs_hEntity> anEntAndSubs;
713 anEntAndSubs.insert(theEntityID);
714 for (int i = 0; i < 4; i++)
715 if (myEntities[aPos].point[i] != SLVS_E_UNKNOWN)
716 anEntAndSubs.insert(myEntities[aPos].point[i]);
718 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
719 for (; anEntIter != myEntities.end(); anEntIter++) {
720 if (anEntAndSubs.find(anEntIter->h) != anEntAndSubs.end())
722 for (int i = 0; i < 4; i++)
723 if (anEntAndSubs.find(anEntIter->point[i]) != anEntAndSubs.end())
725 if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
728 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
729 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
730 Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
731 aConstrIter->entityA, aConstrIter->entityB,
732 aConstrIter->entityC, aConstrIter->entityD};
733 for (int i = 0; i < 6; i++)
734 if (anEntAndSubs.find(anEntIDs[i]) != anEntAndSubs.end())
737 // The entity is not used, remove it and its parameters
738 Slvs_Entity anEntity = myEntities[aPos];
739 myEntities.erase(myEntities.begin() + aPos);
740 myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
741 if (anEntity.distance != SLVS_E_UNKNOWN)
742 aResult = aResult && removeEntity(anEntity.distance);
743 for (int i = 0; i < 4; i++)
744 if (anEntity.param[i] != SLVS_E_UNKNOWN)
745 aResult = removeParameter(anEntity.param[i]) && aResult;
746 myNeedToResolve = true;
751 const Slvs_Entity& SolveSpaceSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
753 int aPos = Search(theEntityID, myEntities);
754 if (aPos >= 0 && aPos < (int)myEntities.size())
755 return myEntities[aPos];
757 // Entity is not found, return empty object
758 static Slvs_Entity aDummy;
759 aDummy.h = SLVS_E_UNKNOWN;
764 Slvs_hConstraint SolveSpaceSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
766 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
767 // Constraint is already used, rewrite it
768 return updateConstraint(theConstraint);
771 Slvs_Constraint aConstraint = theConstraint;
773 // Find a constraint with same type uses same arguments to show user overconstraint situation
774 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
775 for (; aCIt != myConstraints.end(); aCIt++) {
776 if (aConstraint.type != aCIt->type)
778 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
779 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
780 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
781 myDuplicatedConstraint = true;
786 if (aConstraint.h > myConstrMaxID)
787 myConstrMaxID = aConstraint.h;
789 aConstraint.h = ++myConstrMaxID;
790 myConstraints.push_back(aConstraint);
791 myNeedToResolve = true;
792 return aConstraint.h;
795 Slvs_hConstraint SolveSpaceSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
797 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
798 // Constraint already used, rewrite it
799 int aPos = Search(theConstraint.h, myConstraints);
800 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
801 myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
802 myConstraints[aPos] = theConstraint;
803 return theConstraint.h;
807 // Constraint is not found, add new one
808 Slvs_Constraint aConstraint = theConstraint;
811 // Firstly, check middle-point constraint conflicts with point-on-line
812 if (aConstraint.type == SLVS_C_AT_MIDPOINT) {
813 std::vector<Slvs_Constraint>::const_iterator anIt = myConstraints.begin();
814 for (; anIt != myConstraints.end(); ++anIt)
815 if (anIt->type == SLVS_C_PT_ON_LINE &&
816 anIt->ptA == aConstraint.ptA &&
817 anIt->entityA == aConstraint.entityA)
819 if (anIt != myConstraints.end()) {
820 // change the constraint to the lengths equality to avoid conflicts
821 Slvs_Entity aLine = getEntity(aConstraint.entityA);
822 Slvs_Entity aNewLine1 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
823 myWorkplaneID, aLine.point[0], aConstraint.ptA);
824 aNewLine1.h = addEntity(aNewLine1);
825 Slvs_Entity aNewLine2 = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
826 myWorkplaneID, aLine.point[1], aConstraint.ptA);
827 aNewLine2.h = addEntity(aNewLine2);
828 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
829 SLVS_C_EQUAL_LENGTH_LINES,
830 myWorkplaneID, 0.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aNewLine1.h, aNewLine2.h);
834 return addConstraint(aConstraint);
837 bool SolveSpaceSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
839 int aPos = Search(theConstraintID, myConstraints);
840 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
841 Slvs_Constraint aConstraint = myConstraints[aPos];
842 myConstraints.erase(myConstraints.begin() + aPos);
843 myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
844 myNeedToResolve = true;
846 if (myDuplicatedConstraint) {
847 // Find a constraint with same type uses same arguments
848 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
849 for (; aCIt != myConstraints.end(); aCIt++) {
850 if (aConstraint.type != aCIt->type)
852 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
853 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
854 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
855 myDuplicatedConstraint = false;
864 const Slvs_Constraint& SolveSpaceSolver_Storage::
865 getConstraint(const Slvs_hConstraint& theConstraintID) const
867 int aPos = Search(theConstraintID, myConstraints);
868 if (aPos >= 0 && aPos < (int)myConstraints.size())
869 return myConstraints[aPos];
871 // Constraint is not found, return empty object
872 static Slvs_Constraint aDummy;
878 void SolveSpaceSolver_Storage::initializeSolver(SolverPtr theSolver)
880 std::shared_ptr<SolveSpaceSolver_Solver> aSolver =
881 std::dynamic_pointer_cast<SolveSpaceSolver_Solver>(theSolver);
888 if (myConstraints.empty()) {
889 // Adjust all arc to place their points correctly
890 std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
891 for (; anEntIt != myEntities.end(); ++anEntIt)
892 if (anEntIt->type == SLVS_E_ARC_OF_CIRCLE)
896 aSolver->setParameters(myParameters.data(), (int)myParameters.size());
897 aSolver->setEntities(myEntities.data(), (int)myEntities.size());
898 aSolver->setConstraints(myConstraints.data(), (int)myConstraints.size());
902 bool SolveSpaceSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
904 std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
905 std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
907 // verify whether the constraint has duplicated
908 bool hasSameID = false;
909 SameConstraintMap::iterator anEqIt = myEqualConstraints.begin();
910 for (; anEqIt != myEqualConstraints.end(); ++anEqIt) {
911 std::set<ConstraintWrapperPtr>::const_iterator aFound = anEqIt->find(aConstraint);
912 if (aFound != anEqIt->end()) {
913 // verify there is a constraint with same ID
914 std::set<ConstraintWrapperPtr>::const_iterator anIt = anEqIt->begin();
915 ConstraintID anID = (*aFound)->id();
916 for (++anIt; anIt != anEqIt->end() && !hasSameID; ++anIt)
917 if ((*anIt)->id() == anID && aFound != anIt)
920 anEqIt->erase(aConstraint);
924 if (anEqIt != myEqualConstraints.end() && hasSameID)
927 bool isFullyRemoved = removeConstraint((Slvs_hConstraint)aConstraint->id());
928 // remove point-point coincidence
929 if (aConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT)
930 isFullyRemoved = removeCoincidence(theConstraint) && isFullyRemoved;
931 return SketchSolver_Storage::remove(theConstraint) && isFullyRemoved;
934 bool SolveSpaceSolver_Storage::remove(EntityWrapperPtr theEntity)
939 // Additional check for entity to be used in point-point coincidence
940 bool isCoincide = false;
941 if (theEntity->type() == ENTITY_POINT) {
942 CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
943 std::set<EntityWrapperPtr>::const_iterator aCIt;
944 for (; anIt != myCoincidentPoints.end(); ++anIt) {
945 if (anIt->first == theEntity)
947 for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt)
948 if (*aCIt == theEntity)
950 if (aCIt != anIt->second.end())
953 if (anIt != myCoincidentPoints.end()) {
954 if (anIt->first != theEntity && isUsed(anIt->first->baseAttribute()))
956 for (aCIt = anIt->second.begin(); !isCoincide && aCIt != anIt->second.end(); ++aCIt)
957 if (*aCIt != theEntity && isUsed((*aCIt)->baseAttribute()))
962 std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity =
963 std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
964 bool isFullyRemoved = isCoincide ? true : removeEntity((Slvs_hEntity)anEntity->id());
965 return (SketchSolver_Storage::remove(theEntity) || isCoincide) && isFullyRemoved;
968 bool SolveSpaceSolver_Storage::remove(ParameterWrapperPtr theParameter)
970 return removeParameter((Slvs_hParam)theParameter->id());
974 void SolveSpaceSolver_Storage::refresh(bool theFixedOnly) const
978 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
979 std::list<ParameterWrapperPtr> aParams;
980 std::list<ParameterWrapperPtr>::const_iterator aParIt;
981 for (; anIt != myAttributeMap.end(); ++anIt) {
984 // the external feature always should keep the up to date values, so,
985 // refresh from the solver is never needed
986 if (anIt->first.get()) {
987 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
988 std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
989 if (aSketchFeature.get() && aSketchFeature->isExternal())
991 // not need to refresh here sketch's origin and normal vector
992 CompositeFeaturePtr aSketch =
993 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(anIt->first->owner());
998 // update parameter wrappers and obtain values of attributes
999 aParams = anIt->second->parameters();
1001 bool isUpd[3] = {false};
1003 for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
1004 std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aWrapper =
1005 std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(*aParIt);
1006 if (!theFixedOnly || aWrapper->group() == GID_OUTOFGROUP || aWrapper->isParametric()) {
1007 aWrapper->changeParameter().val = getParameter((Slvs_hParam)aWrapper->id()).val;
1008 aCoords[i] = aWrapper->value();
1012 if (!isUpd[0] && !isUpd[1] && !isUpd[2])
1013 continue; // nothing is updated
1015 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1016 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
1018 if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
1019 (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance)) {
1020 if (!isUpd[0]) aCoords[0] = aPoint2D->x();
1021 if (!isUpd[1]) aCoords[1] = aPoint2D->y();
1022 aPoint2D->setValue(aCoords[0], aCoords[1]);
1023 // Find points coincident with this one (probably not in GID_OUTOFGROUP)
1024 std::map<AttributePtr, EntityWrapperPtr>::const_iterator aLocIt;
1026 aLocIt = myAttributeMap.begin();
1031 for (; aLocIt != myAttributeMap.end(); ++aLocIt) {
1032 if (!aLocIt->second)
1034 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
1035 std::dynamic_pointer_cast<SketchPlugin_Feature>(aLocIt->first->owner());
1036 if (aSketchFeature && aSketchFeature->isExternal())
1038 if (anIt->second->id() == aLocIt->second->id()) {
1039 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aLocIt->first);
1040 aPoint2D->setValue(aCoords[0], aCoords[1]);
1046 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
1048 if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
1049 aScalar->setValue(aCoords[0]);
1052 std::shared_ptr<GeomDataAPI_Point> aPoint =
1053 std::dynamic_pointer_cast<GeomDataAPI_Point>(anIt->first);
1055 if ((isUpd[0] && fabs(aPoint->x() - aCoords[0]) > tolerance) ||
1056 (isUpd[1] && fabs(aPoint->y() - aCoords[1]) > tolerance) ||
1057 (isUpd[2] && fabs(aPoint->z() - aCoords[2]) > tolerance))
1058 if (!isUpd[0]) aCoords[0] = aPoint->x();
1059 if (!isUpd[1]) aCoords[1] = aPoint->y();
1060 if (!isUpd[2]) aCoords[2] = aPoint->z();
1061 aPoint->setValue(aCoords[0], aCoords[1], aCoords[2]);
1066 //blockEvents(false);
1069 void SolveSpaceSolver_Storage::verifyFixed()
1071 std::map<AttributePtr, EntityWrapperPtr>::iterator anAttrIt = myAttributeMap.begin();
1072 for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
1073 if (!anAttrIt->second)
1075 if (anAttrIt->second->group() == GID_OUTOFGROUP) {
1076 Slvs_Entity anEnt = getEntity((Slvs_hEntity)anAttrIt->second->id());
1077 if (anEnt.group != (Slvs_hEntity)GID_OUTOFGROUP)
1078 anEnt.group = (Slvs_hEntity)GID_OUTOFGROUP;
1079 updateEntity(anEnt);
1082 const std::list<ParameterWrapperPtr>& aParameters = anAttrIt->second->parameters();
1083 std::list<ParameterWrapperPtr>::const_iterator aParIt = aParameters.begin();
1084 for (; aParIt != aParameters.end(); ++aParIt)
1085 if (anAttrIt->second->group() == GID_OUTOFGROUP || (*aParIt)->group() == GID_OUTOFGROUP) {
1086 Slvs_Param aParam = getParameter((Slvs_hParam)(*aParIt)->id());
1087 if (aParam.group != (Slvs_hParam)GID_OUTOFGROUP) {
1088 aParam.group = (Slvs_hParam)GID_OUTOFGROUP;
1089 updateParameter(aParam);
1096 void SolveSpaceSolver_Storage::adjustArc(const Slvs_Entity& theArc)
1098 double anArcPoints[3][2];
1099 double aDist[3] = {0.0};
1100 bool isFixed[3] = {false};
1101 for (int i = 0; i < 3; ++i) {
1102 Slvs_Entity aPoint = getEntity(theArc.point[i]);
1103 isFixed[i] = (aPoint.group != (Slvs_hGroup)myGroupID);
1104 anArcPoints[i][0] = getParameter(aPoint.param[0]).val;
1105 anArcPoints[i][1] = getParameter(aPoint.param[1]).val;
1107 anArcPoints[i][0] -= anArcPoints[0][0];
1108 anArcPoints[i][1] -= anArcPoints[0][1];
1109 aDist[i] = sqrt(anArcPoints[i][0] * anArcPoints[i][0] +
1110 anArcPoints[i][1] * anArcPoints[i][1]);
1114 if (fabs(aDist[1] - aDist[2]) < tolerance)
1118 while (anInd > 0 && isFixed[anInd])
1121 return; // adjust only start or end point of the arc
1123 anArcPoints[anInd][0] /= aDist[anInd];
1124 anArcPoints[anInd][1] /= aDist[anInd];
1126 Slvs_Entity aPoint = getEntity(theArc.point[anInd]);
1127 for (int i = 0; i < 2; ++i) {
1128 Slvs_Param aParam = getParameter(aPoint.param[i]);
1129 aParam.val = anArcPoints[0][i] + aDist[3-anInd] * anArcPoints[anInd][i];
1130 updateParameter(aParam);
1140 // ========================================================
1141 // ========= Auxiliary functions ===============
1142 // ========================================================
1144 template<typename T>
1145 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1147 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1148 int aVecSize = (int)theEntities.size();
1149 if (theEntities.empty())
1151 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1153 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1155 if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1156 aResIndex = aVecSize;
1160 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1162 return fabs(theParam1.val - theParam2.val) > tolerance;
1165 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1168 for (; theEntity1.param[i] != 0 && i < 4; i++)
1169 if (theEntity1.param[i] != theEntity2.param[i])
1172 for (; theEntity1.point[i] != 0 && i < 4; i++)
1173 if (theEntity1.point[i] != theEntity2.point[i])
1178 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1180 return theConstraint1.ptA != theConstraint2.ptA ||
1181 theConstraint1.ptB != theConstraint2.ptB ||
1182 theConstraint1.entityA != theConstraint2.entityA ||
1183 theConstraint1.entityB != theConstraint2.entityB ||
1184 theConstraint1.entityC != theConstraint2.entityC ||
1185 theConstraint1.entityD != theConstraint2.entityD ||
1186 fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;