Salome HOME
It replaces using of direct value "Sketch" to SKETCH_KIND, which is defined in the...
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintGroup.cpp
1 // File:    SketchSolver_ConstraintGroup.cpp
2 // Created: 27 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchSolver_ConstraintGroup.h"
6
7 #include <SketchSolver_Constraint.h>
8
9 #include <Events_Loop.h>
10 #include <GeomDataAPI_Dir.h>
11 #include <GeomDataAPI_Point.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeRefList.h>
15 #include <Model_Events.h>
16
17 #include <SketchPlugin_Constraint.h>
18
19 #include <SketchPlugin_Arc.h>
20 #include <SketchPlugin_Circle.h>
21 #include <SketchPlugin_Line.h>
22 #include <SketchPlugin_Point.h>
23 #include <SketchPlugin_Sketch.h>
24
25 #include <math.h>
26 #include <assert.h>
27
28 /// Tolerance for value of parameters
29 const double tolerance = 1.e-10;
30
31 /// This value is used to give unique index to the groups
32 static Slvs_hGroup myGroupIndexer = 0;
33
34 /** \brief Search the entity/parameter with specified ID in the list of elements
35  *  \param[in] theEntityID unique ID of the element
36  *  \param[in] theEntities list of elements
37  *  \return position of the found element or -1 if the element is not found
38  */
39 template <typename T>
40 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
41
42
43 // ========================================================
44 // =========  SketchSolver_ConstraintGroup  ===============
45 // ========================================================
46
47 SketchSolver_ConstraintGroup::
48   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Feature> theWorkplane)
49   : myID(++myGroupIndexer),
50     myParamMaxID(0),
51     myEntityMaxID(0),
52     myConstrMaxID(0),
53     myConstraintMap(),
54     myNeedToSolve(false),
55     myConstrSolver()
56 {
57   myParams.clear();
58   myEntities.clear();
59   myConstraints.clear();
60   
61   myTempConstraints.clear();
62   myTempPointWhereDragged.clear();
63   myTempPointWDrgdID = 0;
64
65   // Initialize workplane
66   myWorkplane.h = SLVS_E_UNKNOWN;
67 #ifndef NDEBUG
68   assert(addWorkplane(theWorkplane));
69 #else
70   addWorkplane(theWorkplane);
71 #endif
72 }
73
74 SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
75 {
76   myParams.clear();
77   myEntities.clear();
78   myConstraints.clear();
79   myConstraintMap.clear();
80   myTempConstraints.clear();
81   myTempPointWhereDragged.clear();
82
83   // If the group with maximal identifier is deleted, decrease the indexer
84   if (myID == myGroupIndexer)
85     myGroupIndexer--;
86 }
87
88 // ============================================================================
89 //  Function: isBaseWorkplane
90 //  Class:    SketchSolver_ConstraintGroup
91 //  Purpose:  verify the group is based on the given workplane
92 // ============================================================================
93 bool SketchSolver_ConstraintGroup::isBaseWorkplane(
94                 boost::shared_ptr<SketchPlugin_Feature> theWorkplane) const
95 {
96   return theWorkplane == mySketch;
97 }
98
99 // ============================================================================
100 //  Function: isInteract
101 //  Class:    SketchSolver_ConstraintGroup
102 //  Purpose:  verify are there any entities in the group used by given constraint
103 // ============================================================================
104 bool SketchSolver_ConstraintGroup::isInteract(
105                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
106 {
107   // Check the group is empty
108   if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
109     return true;
110
111   // Go through constraint entities and verify if some of them already in the group
112   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
113   {
114     boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
115       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
116         theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
117       );
118     if (!aCAttrRef) continue;
119     if (!aCAttrRef->isFeature() && 
120         myEntityAttrMap.find(aCAttrRef->attr()) != myEntityAttrMap.end())
121       return true;
122     if (aCAttrRef->isFeature() && 
123         myEntityFeatMap.find(aCAttrRef->feature()) != myEntityFeatMap.end())
124       return true;
125   }
126
127   // Entities did not found
128   return false;
129 }
130
131 // ============================================================================
132 //  Function: changeConstraint
133 //  Class:    SketchSolver_ConstraintGroup
134 //  Purpose:  create/update the constraint in the group
135 // ============================================================================
136 bool SketchSolver_ConstraintGroup::changeConstraint(
137                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
138 {
139   // There is no workplane yet, something wrong
140   if (myWorkplane.h == SLVS_E_UNKNOWN)
141     return false;
142
143   // Search this constraint in the current group to update it
144   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
145     aConstrMapIter = myConstraintMap.find(theConstraint);
146   std::vector<Slvs_Constraint>::iterator aConstrIter;
147   if (aConstrMapIter != myConstraintMap.end())
148   {
149     int aConstrPos = Search(aConstrMapIter->second, myConstraints);
150     aConstrIter = myConstraints.begin() + aConstrPos;
151   }
152
153   // Get constraint type and verify the constraint parameters are correct
154   SketchSolver_Constraint aConstraint(theConstraint);
155   int aConstrType = aConstraint.getType();
156   if (aConstrType == SLVS_C_UNKNOWN ||
157      (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
158     return false;
159   const std::vector<std::string>& aConstraintAttributes = aConstraint.getAttributes();
160
161   // Create constraint parameters
162   double aDistance = 0.0; // scalar value of the constraint
163   boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
164     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
165   if (aDistAttr)
166   {
167     aDistance = aDistAttr->value();
168     if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
169     {
170       myNeedToSolve = true;
171       aConstrIter->valA = aDistance;
172     }
173     // SketchPlugin circle defined by its radius, but SolveSpace uses constraint for diameter
174     if (aConstrType == SLVS_C_DIAMETER)
175       aDistance *= 2.0;
176   }
177
178   Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
179   for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
180   {
181     aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
182     boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr =
183       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
184         theConstraint->data()->attribute(aConstraintAttributes[indAttr])
185       );
186     if (!aConstrAttr) continue;
187
188     // For the length constraint the start and end points of the line should be added to the entities list instead of line
189     if (aConstrType == SLVS_C_PT_PT_DISTANCE && theConstraint->getKind().compare("SketchConstraintLength") == 0)
190     {
191       boost::shared_ptr<ModelAPI_Data> aData = aConstrAttr->feature()->data();
192       aConstrEnt[indAttr]   = changeEntity(aData->attribute(LINE_ATTR_START));
193       aConstrEnt[indAttr+1] = changeEntity(aData->attribute(LINE_ATTR_END));
194       break; // there should be no other entities
195     }
196     else if (aConstrAttr->isFeature())
197       aConstrEnt[indAttr] = changeEntity(aConstrAttr->feature());
198     else
199       aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
200   }
201
202   if (aConstrMapIter == myConstraintMap.end())
203   {
204     // Several points may be coincident, it is not necessary to store all constraints between them.
205     // Try to find sequence of coincident points which connects the points of new constraint
206     if (aConstrType == SLVS_C_POINTS_COINCIDENT &&
207         !addCoincidentPoints(aConstrEnt[0], aConstrEnt[1]))
208     {
209       myExtraCoincidence.insert(theConstraint); // the constraint is stored for further purposes
210       return false;
211     }
212
213     // Create SolveSpace constraint structure
214     Slvs_Constraint aConstraint =
215       Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
216                           aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
217     myConstraints.push_back(aConstraint);
218     myConstraintMap[theConstraint] = aConstraint.h;
219   }
220   return true;
221 }
222
223 // ============================================================================
224 //  Function: changeEntity
225 //  Class:    SketchSolver_ConstraintGroup
226 //  Purpose:  create/update the element affected by any constraint
227 // ============================================================================
228 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
229                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
230 {
231   // If the entity is already in the group, try to find it
232   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
233     aEntIter = myEntityAttrMap.find(theEntity);
234   std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
235   if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
236     aParamIter = myParams.end();
237   else
238   { // the entity already exists
239     int aEntPos = Search(aEntIter->second, myEntities);
240     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
241     aParamIter = myParams.begin() + aParamPos;
242   }
243   const bool isEntExists = (aEntIter != myEntityAttrMap.end()); // defines that the entity already exists
244
245   // Look over supported types of entities
246
247   // Point in 3D
248   boost::shared_ptr<GeomDataAPI_Point> aPoint =
249     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
250   if (aPoint)
251   {
252     Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
253     Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
254     Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
255
256     if (isEntExists)
257       return aEntIter->second;
258
259     // New entity
260     Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
261     myEntities.push_back(aPtEntity);
262     myEntityAttrMap[theEntity] = aPtEntity.h;
263     return aPtEntity.h;
264   }
265
266   // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
267   if (myWorkplane.h == SLVS_E_UNKNOWN)
268     return SLVS_E_UNKNOWN;
269
270   // Point in 2D
271   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
272     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
273   if (aPoint2D)
274   {
275     Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
276     Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
277
278     if (isEntExists)
279       return aEntIter->second;
280
281     // New entity
282     Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
283     myEntities.push_back(aPt2DEntity);
284     myEntityAttrMap[theEntity] = aPt2DEntity.h;
285     return aPt2DEntity.h;
286   }
287
288   // Scalar value (used for the distance entities)
289   boost::shared_ptr<ModelAPI_AttributeDouble> aScalar = 
290     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
291   if (aScalar)
292   {
293     Slvs_hParam aValue = changeParameter(aScalar->value(), aParamIter);
294
295     if (isEntExists)
296       return aEntIter->second;
297
298     // New entity
299     Slvs_Entity aDistance = Slvs_MakeDistance(++myEntityMaxID, myID, myWorkplane.h, aValue);
300     myEntities.push_back(aDistance);
301     myEntityAttrMap[theEntity] = aDistance.h;
302     return aDistance.h;
303   }
304
305   /// \todo Other types of entities
306
307   // Unsupported or wrong entity type
308   return SLVS_E_UNKNOWN;
309 }
310
311
312 // ============================================================================
313 //  Function: changeEntity
314 //  Class:    SketchSolver_ConstraintGroup
315 //  Purpose:  create/update the element defined by the feature affected by any constraint
316 // ============================================================================
317 Slvs_hEntity SketchSolver_ConstraintGroup::changeEntity(
318                 boost::shared_ptr<ModelAPI_Feature> theEntity)
319 {
320   // If the entity is already in the group, try to find it
321   std::map<boost::shared_ptr<ModelAPI_Feature>, Slvs_hEntity>::const_iterator
322     aEntIter = myEntityFeatMap.find(theEntity);
323   // defines that the entity already exists
324   const bool isEntExists = (myEntityFeatMap.find(theEntity) != myEntityFeatMap.end());
325
326   // SketchPlugin features
327   boost::shared_ptr<SketchPlugin_Feature> aFeature;
328     boost::dynamic_pointer_cast<SketchPlugin_Feature>(theEntity);
329   if (aFeature)
330   { // Verify the feature by its kind
331     const std::string& aFeatureKind = aFeature->getKind();
332
333     // Line
334     if (aFeatureKind.compare("SketchLine") == 0)
335     {
336       Slvs_hEntity aStart = changeEntity(aFeature->data()->attribute(LINE_ATTR_START));
337       Slvs_hEntity aEnd   = changeEntity(aFeature->data()->attribute(LINE_ATTR_END));
338
339       if (isEntExists)
340         return aEntIter->second;
341
342       // New entity
343       Slvs_Entity aLineEntity = Slvs_MakeLineSegment(++myEntityMaxID, myID, myWorkplane.h, aStart, aEnd);
344       myEntities.push_back(aLineEntity);
345       myEntityFeatMap[theEntity] = aLineEntity.h;
346       return aLineEntity.h;
347     }
348     // Circle
349     else if (aFeatureKind.compare("SketchCircle") == 0)
350     {
351       Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(CIRCLE_ATTR_CENTER));
352       Slvs_hEntity aRadius = changeEntity(aFeature->data()->attribute(CIRCLE_ATTR_RADIUS));
353
354       if (isEntExists)
355         return aEntIter->second;
356
357       // New entity
358       Slvs_Entity aCircleEntity = 
359         Slvs_MakeCircle(++myEntityMaxID, myID, myWorkplane.h, aCenter, myWorkplane.normal, aRadius);
360       myEntities.push_back(aCircleEntity);
361       myEntityFeatMap[theEntity] = aCircleEntity.h;
362       return aCircleEntity.h;
363     }
364     // Arc
365     else if (aFeatureKind.compare("SketchArc") == 0)
366     {
367       Slvs_hEntity aCenter = changeEntity(aFeature->data()->attribute(ARC_ATTR_CENTER));
368       Slvs_hEntity aStart  = changeEntity(aFeature->data()->attribute(ARC_ATTR_START));
369       Slvs_hEntity aEnd    = changeEntity(aFeature->data()->attribute(ARC_ATTR_END));
370
371       if (isEntExists)
372         return aEntIter->second;
373
374       Slvs_Entity anArcEntity = Slvs_MakeArcOfCircle(++myEntityMaxID, myID, 
375                                   myWorkplane.h, myWorkplane.normal, aCenter, aStart, aEnd);
376       myEntities.push_back(anArcEntity);
377       myEntityFeatMap[theEntity] = anArcEntity.h;
378       return anArcEntity.h;
379     }
380     // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
381     else if (aFeatureKind.compare("SketchPoint") == 0)
382     {
383       Slvs_hEntity aPoint = changeEntity(aFeature->data()->attribute(POINT_ATTR_COORD));
384
385       if (isEntExists)
386         return aEntIter->second;
387
388       // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
389       myEntityFeatMap[theEntity] = aPoint;
390       return aPoint;
391     }
392   }
393
394   /// \todo Other types of features
395
396   // Unsupported or wrong entity type
397   return SLVS_E_UNKNOWN;
398 }
399
400 // ============================================================================
401 //  Function: changeNormal
402 //  Class:    SketchSolver_ConstraintGroup
403 //  Purpose:  create/update the normal of workplane
404 // ============================================================================
405 Slvs_hEntity SketchSolver_ConstraintGroup::changeNormal(
406                 boost::shared_ptr<ModelAPI_Attribute> theDirX,
407                 boost::shared_ptr<ModelAPI_Attribute> theDirY,
408                 boost::shared_ptr<ModelAPI_Attribute> theNorm)
409 {
410   boost::shared_ptr<GeomDataAPI_Dir> aDirX =
411     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
412   boost::shared_ptr<GeomDataAPI_Dir> aDirY =
413     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
414   if (!aDirX || !aDirY ||
415      (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
416      (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
417     return SLVS_E_UNKNOWN;
418
419   // quaternion parameters of normal vector
420   double qw, qx, qy, qz;
421   Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(),
422                       aDirY->x(), aDirY->y(), aDirY->z(),
423                       &qw, &qx, &qy, &qz);
424   double aNormCoord[4] = {qw, qx, qy, qz};
425
426   // Try to find existent normal
427   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
428     aEntIter = myEntityAttrMap.find(theNorm);
429   std::vector<Slvs_Param>::const_iterator aParamIter; // looks to the first parameter of already existent entity or to the end of vector otherwise
430   if (aEntIter == myEntityAttrMap.end()) // no such entity => should be created
431     aParamIter = myParams.end();
432   else
433   { // the entity already exists, update it
434     int aEntPos = Search(aEntIter->second, myEntities);
435     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
436     aParamIter = myParams.begin() + aParamPos;
437   }
438
439   // Change parameters of the normal
440   Slvs_hParam aNormParams[4];
441   for (int i = 0; i < 4; i++)
442     aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
443
444   if (aEntIter != myEntityAttrMap.end()) // the entity already exists
445     return aEntIter->second;
446
447   // Create a normal
448   Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID,
449                 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
450   myEntities.push_back(aNormal);
451   myEntityAttrMap[theNorm] = aNormal.h;
452   return aNormal.h;
453 }
454
455
456 // ============================================================================
457 //  Function: addWorkplane
458 //  Class:    SketchSolver_ConstraintGroup
459 //  Purpose:  create workplane for the group
460 // ============================================================================
461 bool SketchSolver_ConstraintGroup::addWorkplane(
462                 boost::shared_ptr<SketchPlugin_Feature> theSketch)
463 {
464   if (myWorkplane.h || theSketch->getKind().compare(SKETCH_KIND) != 0)
465     return false; // the workplane already exists or the function parameter is not Sketch
466
467   mySketch = theSketch;
468   updateWorkplane();
469   return true;
470 }
471
472 // ============================================================================
473 //  Function: updateWorkplane
474 //  Class:    SketchSolver_ConstraintGroup
475 //  Purpose:  update parameters of workplane
476 // ============================================================================
477 bool SketchSolver_ConstraintGroup::updateWorkplane()
478 {
479   // Get parameters of workplane
480   boost::shared_ptr<ModelAPI_Attribute> aDirX    = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
481   boost::shared_ptr<ModelAPI_Attribute> aDirY    = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
482   boost::shared_ptr<ModelAPI_Attribute> aNorm    = mySketch->data()->attribute(SKETCH_ATTR_NORM);
483   boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
484   // Transform them into SolveSpace format
485   Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
486   if (!aNormalWP) return false;
487   Slvs_hEntity anOriginWP = changeEntity(anOrigin);
488   if (!anOriginWP) return false;
489
490   if (!myWorkplane.h)
491   {
492     // Create workplane
493     myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
494     // Workplane should be added to the list of entities
495     myEntities.push_back(myWorkplane);
496   }
497   return true;
498 }
499
500 // ============================================================================
501 //  Function: changeParameter
502 //  Class:    SketchSolver_ConstraintGroup
503 //  Purpose:  create/update value of parameter
504 // ============================================================================
505 Slvs_hParam SketchSolver_ConstraintGroup::changeParameter(
506                 const double&                            theParam,
507                 std::vector<Slvs_Param>::const_iterator& thePrmIter)
508 {
509   if (thePrmIter != myParams.end())
510   { // Parameter should be updated
511     int aParamPos = thePrmIter - myParams.begin();
512     if (fabs(thePrmIter->val - theParam) > tolerance)
513     {
514       myNeedToSolve = true; // parameter is changed, need to resolve constraints
515       myParams[aParamPos].val = theParam;
516     }
517     thePrmIter++;
518     return myParams[aParamPos].h;
519   }
520
521   // Newly created parameter
522   Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
523   myParams.push_back(aParam);
524   myNeedToSolve = true;
525   // The list of parameters is changed, move iterator to the end of the list to avoid problems
526   thePrmIter = myParams.end();
527   return aParam.h;
528 }
529
530 // ============================================================================
531 //  Function: resolveConstraints
532 //  Class:    SketchSolver_ConstraintGroup
533 //  Purpose:  solve the set of constraints for the current group
534 // ============================================================================
535 void SketchSolver_ConstraintGroup::resolveConstraints()
536 {
537   if (!myNeedToSolve)
538     return;
539
540   myConstrSolver.setGroupID(myID);
541   myConstrSolver.setParameters(myParams);
542   myConstrSolver.setEntities(myEntities);
543   myConstrSolver.setConstraints(myConstraints);
544   myConstrSolver.setDraggedParameters(myTempPointWhereDragged);
545
546   int aResult = myConstrSolver.solve();
547   if (aResult == SLVS_RESULT_OKAY)
548   { // solution succeeded, store results into correspondent attributes
549     // Obtain result into the same list of parameters
550     if (!myConstrSolver.getResult(myParams))
551       return;
552
553     // We should go through the attributes map, because only attributes have valued parameters
554     std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
555       anEntIter = myEntityAttrMap.begin();
556     for ( ; anEntIter != myEntityAttrMap.end(); anEntIter++)
557       updateAttribute(anEntIter->first, anEntIter->second);
558   }
559   /// \todo Implement error handling
560
561   removeTemporaryConstraints();
562   myNeedToSolve = false;
563 }
564
565 // ============================================================================
566 //  Function: mergeGroups
567 //  Class:    SketchSolver_ConstraintGroup
568 //  Purpose:  append specified group to the current group
569 // ============================================================================
570 void SketchSolver_ConstraintGroup::mergeGroups(
571                 const SketchSolver_ConstraintGroup& theGroup)
572 {
573   // If specified group is empty, no need to merge
574   if (theGroup.myConstraintMap.empty())
575     return ;
576
577   // Map between old and new indexes of SolveSpace constraints
578   std::map<Slvs_hConstraint, Slvs_hConstraint> aConstrMap;
579
580   // Add all constraints from theGroup to the current group
581   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
582     aConstrIter = theGroup.myConstraintMap.begin();
583   for ( ; aConstrIter != theGroup.myConstraintMap.end(); aConstrIter++)
584     if (changeConstraint(aConstrIter->first))
585       aConstrMap[aConstrIter->second] = myConstrMaxID; // the constraint was added => store its ID
586
587   // Add temporary constraints from theGroup
588   std::list<Slvs_hConstraint>::const_iterator aTempConstrIter = theGroup.myTempConstraints.begin();
589   for ( ; aTempConstrIter != theGroup.myTempConstraints.end(); aTempConstrIter++)
590   {
591     std::map<Slvs_hConstraint, Slvs_hConstraint>::iterator aFind = aConstrMap.find(*aTempConstrIter);
592     if (aFind != aConstrMap.end())
593       myTempConstraints.push_back(aFind->second);
594   }
595
596   if (myTempPointWhereDragged.empty())
597     myTempPointWhereDragged = theGroup.myTempPointWhereDragged;
598   else if (!theGroup.myTempPointWhereDragged.empty())
599   { // Need to create additional transient constraint
600     std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
601       aFeatureIter = theGroup.myEntityAttrMap.begin();
602     for (; aFeatureIter != theGroup.myEntityAttrMap.end(); aFeatureIter++)
603       if (aFeatureIter->second == myTempPointWDrgdID)
604       {
605         addTemporaryConstraintWhereDragged(aFeatureIter->first);
606         break;
607       }
608   }
609
610   myNeedToSolve = myNeedToSolve || theGroup.myNeedToSolve;
611 }
612
613 // ============================================================================
614 //  Function: splitGroup
615 //  Class:    SketchSolver_ConstraintGroup
616 //  Purpose:  divide the group into several subgroups
617 // ============================================================================
618 void SketchSolver_ConstraintGroup::splitGroup(std::vector<SketchSolver_ConstraintGroup*>& theCuts)
619 {
620   // Divide constraints and entities into several groups
621   std::vector< std::set<Slvs_hEntity> >     aGroupsEntities;
622   std::vector< std::set<Slvs_hConstraint> > aGroupsConstr;
623   int aMaxNbEntities = 0; // index of the group with maximal nuber of elements (this group will be left in the current)
624   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
625   for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
626   {
627     Slvs_hEntity aConstrEnt[] = {
628       aConstrIter->ptA,     aConstrIter->ptB,
629       aConstrIter->entityA, aConstrIter->entityB};
630     std::vector<int> anIndexes;
631     // Go through the groupped entities and find even one of entities of current constraint
632     std::vector< std::set<Slvs_hEntity> >::iterator aGrEntIter;
633     for (aGrEntIter = aGroupsEntities.begin(); aGrEntIter != aGroupsEntities.end(); aGrEntIter++)
634     {
635       bool isFound = false;
636       for (int i = 0; i < 4 && !isFound; i++)
637         if (aConstrEnt[i] != 0)
638           isFound = (aGrEntIter->find(aConstrEnt[i]) != aGrEntIter->end());
639       if (isFound)
640         anIndexes.push_back(aGrEntIter - aGroupsEntities.begin());
641     }
642     // Add new group if no one is found
643     if (anIndexes.empty())
644     {
645       std::set<Slvs_hEntity> aNewGrEnt;
646       for (int i = 0; i < 4; i++)
647         if (aConstrEnt[i] != 0)
648           aNewGrEnt.insert(aConstrEnt[i]);
649       std::set<Slvs_hConstraint> aNewGrConstr;
650       aNewGrConstr.insert(aConstrIter->h);
651
652       aGroupsEntities.push_back(aNewGrEnt);
653       aGroupsConstr.push_back(aNewGrConstr);
654       if (aNewGrEnt.size() > aGroupsEntities[aMaxNbEntities].size())
655         aMaxNbEntities = aGroupsEntities.size() - 1;
656     }
657     else if (anIndexes.size() == 1)
658     { // Add entities indexes into the found group
659       aGrEntIter = aGroupsEntities.begin() + anIndexes.front();
660       for (int i = 0; i < 4; i++)
661         if (aConstrEnt[i] != 0)
662           aGrEntIter->insert(aConstrEnt[i]);
663       aGroupsConstr[anIndexes.front()].insert(aConstrIter->h);
664       if (aGrEntIter->size() > aGroupsEntities[aMaxNbEntities].size())
665         aMaxNbEntities = aGrEntIter - aGroupsEntities.begin();
666     }
667     else 
668     { // There are found several connected groups, merge them
669       std::vector< std::set<Slvs_hEntity> >::iterator aFirstGroup = 
670         aGroupsEntities.begin() + anIndexes.front();
671       std::vector< std::set<Slvs_hConstraint> >::iterator aFirstConstr = 
672         aGroupsConstr.begin() + anIndexes.front();
673       std::vector<int>::iterator anInd = anIndexes.begin();
674       for (++anInd; anInd != anIndexes.end(); anInd++)
675       {
676         aFirstGroup->insert(aGroupsEntities[*anInd].begin(), aGroupsEntities[*anInd].end());
677         aFirstConstr->insert(aGroupsConstr[*anInd].begin(), aGroupsConstr[*anInd].end());
678       }
679       if (aFirstGroup->size() > aGroupsEntities[aMaxNbEntities].size())
680         aMaxNbEntities = anIndexes.front();
681       // Remove merged groups
682       for (anInd = anIndexes.end() - 1; anInd != anIndexes.begin(); anInd--)
683       {
684         aGroupsEntities.erase(aGroupsEntities.begin() + (*anInd));
685         aGroupsConstr.erase(aGroupsConstr.begin() + (*anInd));
686       }
687     }
688   }
689
690   if (aGroupsEntities.size() <= 1)
691     return ;
692
693   // Remove the group with maximum elements as it will be left in the current group
694   aGroupsEntities.erase(aGroupsEntities.begin() + aMaxNbEntities);
695   aGroupsConstr.erase(aGroupsConstr.begin() + aMaxNbEntities);
696
697   // Add new groups of constraints and divide current group
698   std::vector<SketchSolver_ConstraintGroup*> aNewGroups;
699   for (int i = aGroupsEntities.size(); i > 0; i--)
700   {
701     SketchSolver_ConstraintGroup* aG = new SketchSolver_ConstraintGroup(mySketch);
702     aNewGroups.push_back(aG);
703   }
704   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
705     aConstrMapIter = myConstraintMap.begin();
706   int aConstrMapPos = 0; // position of iterator in the map (used to restore iterator after removing constraint)
707   while (aConstrMapIter != myConstraintMap.end())
708   {
709     std::vector< std::set<Slvs_hConstraint> >::const_iterator aGIter = aGroupsConstr.begin();
710     std::vector<SketchSolver_ConstraintGroup*>::iterator aGroup = aNewGroups.begin();
711     for ( ; aGIter != aGroupsConstr.end(); aGIter++, aGroup++)
712       if (aGIter->find(aConstrMapIter->second) != aGIter->end())
713       {
714         (*aGroup)->changeConstraint(aConstrMapIter->first);
715         removeConstraint(aConstrMapIter->first);
716         // restore iterator
717         aConstrMapIter = myConstraintMap.begin();
718         for (int i = 0; i < aConstrMapPos; i++)
719           aConstrMapIter++;
720         break;
721       }
722     if (aGIter == aGroupsConstr.end())
723     {
724       aConstrMapIter++;
725       aConstrMapPos++;
726     }
727   }
728
729   theCuts.insert(theCuts.end(), aNewGroups.begin(), aNewGroups.end());
730 }
731
732 // ============================================================================
733 //  Function: updateGroup
734 //  Class:    SketchSolver_ConstraintGroup
735 //  Purpose:  search removed entities and constraints
736 // ============================================================================
737 bool SketchSolver_ConstraintGroup::updateGroup()
738 {
739   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
740     aConstrIter = myConstraintMap.rbegin();
741   bool isAllValid = true;
742   bool isCCRemoved = false; // indicates that at least one of coincidence constraints was removed
743   while (isAllValid && aConstrIter != myConstraintMap.rend())
744   {
745     if (!aConstrIter->first->data()->isValid())
746     {
747       if (aConstrIter->first->getKind().compare("SketchConstraintCoincidence") == 0)
748         isCCRemoved = true;
749       std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::reverse_iterator
750         aCopyIter = aConstrIter++;
751       removeConstraint(aCopyIter->first);
752       isAllValid = false;
753     }
754     else aConstrIter++;
755   }
756
757   // Probably, need to update coincidence constraints
758   if (isCCRemoved && !myExtraCoincidence.empty())
759   {
760     // Make a copy, because the new list of unused constrtaints will be generated
761     std::set< boost::shared_ptr<SketchPlugin_Constraint> > anExtraCopy = myExtraCoincidence;
762     myExtraCoincidence.clear();
763
764     std::set< boost::shared_ptr<SketchPlugin_Constraint> >::iterator
765       aCIter = anExtraCopy.begin();
766     for ( ; aCIter != anExtraCopy.end(); aCIter++)
767       if ((*aCIter)->data()->isValid())
768         changeConstraint(*aCIter);
769   }
770
771   return !isAllValid;
772 }
773
774 // ============================================================================
775 //  Function: updateAttribute
776 //  Class:    SketchSolver_ConstraintGroup
777 //  Purpose:  update features of sketch after resolving constraints
778 // ============================================================================
779 void SketchSolver_ConstraintGroup::updateAttribute(
780                 boost::shared_ptr<ModelAPI_Attribute> theAttribute,
781                 const Slvs_hEntity&                   theEntityID)
782 {
783   // Search the position of the first parameter of the entity
784   int anEntPos = Search(theEntityID, myEntities);
785   int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
786
787   // Look over supported types of entities
788
789   // Point in 3D
790   boost::shared_ptr<GeomDataAPI_Point> aPoint =
791     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
792   if (aPoint)
793   {
794     aPoint->setValue(myParams[aFirstParamPos].val,
795                      myParams[aFirstParamPos+1].val,
796                      myParams[aFirstParamPos+2].val);
797     return ;
798   }
799
800   // Point in 2D
801   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
802     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
803   if (aPoint2D)
804   {
805     aPoint2D->setValue(myParams[aFirstParamPos].val,
806                        myParams[aFirstParamPos+1].val);
807     return ;
808   }
809
810   // Scalar value
811   boost::shared_ptr<ModelAPI_AttributeDouble> aScalar = 
812     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
813   if (aScalar)
814   {
815     aScalar->setValue(myParams[aFirstParamPos].val);
816     return ;
817   }
818
819   /// \todo Support other types of entities
820 }
821
822 // ============================================================================
823 //  Function: updateEntityIfPossible
824 //  Class:    SketchSolver_ConstraintGroup
825 //  Purpose:  search the entity in this group and update it
826 // ============================================================================
827 void SketchSolver_ConstraintGroup::updateEntityIfPossible(
828                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
829 {
830   if (myEntityAttrMap.find(theEntity) != myEntityAttrMap.end())
831   {
832     // If the attribute is a point and it is changed (the group needs to rebuild),
833     // probably user has dragged this point into this position,
834     // so it is necessary to add constraint which will guarantee the point will not change
835
836     // Store myNeedToSolve flag to verify the entity is really changed
837     bool aNeedToSolveCopy = myNeedToSolve;
838     myNeedToSolve = false;
839
840     changeEntity(theEntity);
841
842     if (myNeedToSolve) // the entity is changed
843     {
844       // Verify the entity is a point and add temporary constraint of permanency
845       boost::shared_ptr<GeomDataAPI_Point> aPoint =
846         boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
847       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
848         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
849       if (aPoint || aPoint2D)
850         addTemporaryConstraintWhereDragged(theEntity);
851     }
852
853     // Restore flag of changes
854     myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
855   }
856 }
857
858 // ============================================================================
859 //  Function: addTemporaryConstraintWhereDragged
860 //  Class:    SketchSolver_ConstraintGroup
861 //  Purpose:  add transient constraint SLVS_C_WHERE_DRAGGED for the entity, 
862 //            which was moved by user
863 // ============================================================================
864 void SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
865                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
866 {
867   // Find identifier of the entity
868   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
869     anEntIter = myEntityAttrMap.find(theEntity);
870   if (anEntIter == myEntityAttrMap.end())
871     return ;
872
873   // If this is a first dragged point, its parameters should be placed 
874   // into Slvs_System::dragged field to avoid system inconsistense
875   if (myTempPointWhereDragged.empty())
876   {
877     int anEntPos = Search(anEntIter->second, myEntities);
878     Slvs_hParam* aDraggedParam = myEntities[anEntPos].param;
879     for (int i = 0; i < 4; i++, aDraggedParam++)
880       if (*aDraggedParam != 0)
881         myTempPointWhereDragged.push_back(*aDraggedParam);
882     myTempPointWDrgdID = myEntities[anEntPos].h;
883     return ;
884   }
885
886   // Get identifiers of all dragged points
887   std::set<Slvs_hEntity> aDraggedPntID;
888   aDraggedPntID.insert(myTempPointWDrgdID);
889   std::list<Slvs_hConstraint>::iterator aTmpCoIter = myTempConstraints.begin();
890   for ( ; aTmpCoIter != myTempConstraints.end(); aTmpCoIter++)
891   {
892     unsigned int aConstrPos = Search(*aTmpCoIter, myConstraints);
893     if (aConstrPos < myConstraints.size())
894       aDraggedPntID.insert(myConstraints[aConstrPos].ptA);
895   }
896   // Find whether there is a point coincident with theEntity, which already has SLVS_C_WHERE_DRAGGED
897   std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
898   for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
899   {
900     if (aCoPtIter->find(anEntIter->second) == aCoPtIter->end())
901       continue; // the entity was not found in current set
902
903     // Find one of already created SLVS_C_WHERE_DRAGGED constraints in current set of coincident points
904     std::set<Slvs_hEntity>::const_iterator aDrgIter = aDraggedPntID.begin();
905     for ( ; aDrgIter != aDraggedPntID.end(); aDrgIter++)
906       if (aCoPtIter->find(*aDrgIter) != aCoPtIter->end())
907         return ; // the SLVS_C_WHERE_DRAGGED constraint already exists
908   }
909
910   // Create additional SLVS_C_WHERE_DRAGGED constraint if myTempPointWhereDragged field is not empty
911   Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED,
912                                                   myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
913   myConstraints.push_back(aWDConstr);
914   myTempConstraints.push_back(aWDConstr.h);
915 }
916
917 // ============================================================================
918 //  Function: removeTemporaryConstraints
919 //  Class:    SketchSolver_ConstraintGroup
920 //  Purpose:  remove all transient SLVS_C_WHERE_DRAGGED constraints after
921 //            resolving the set of constraints
922 // ============================================================================
923 void SketchSolver_ConstraintGroup::removeTemporaryConstraints()
924 {
925   std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
926   for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
927   {
928     unsigned int aConstrPos = Search(*aTmpConstrIter, myConstraints);
929     if (aConstrPos >= myConstraints.size())
930       continue;
931     myConstraints.erase(myConstraints.begin() + aConstrPos);
932
933     // If the removing constraint has higher index, decrease the indexer
934     if (*aTmpConstrIter == myConstrMaxID)
935       myConstrMaxID--;
936   }
937   myTempConstraints.clear();
938
939   // Clear basic dragged point
940   myTempPointWhereDragged.clear();
941 }
942
943 // ============================================================================
944 //  Function: removeConstraint
945 //  Class:    SketchSolver_ConstraintGroup
946 //  Purpose:  remove constraint and all unused entities
947 // ============================================================================
948 void SketchSolver_ConstraintGroup::removeConstraint(boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
949 {
950   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::iterator
951     anIterToRemove = myConstraintMap.find(theConstraint);
952   if (anIterToRemove == myConstraintMap.end())
953      return ;
954
955   Slvs_hConstraint aCnstrToRemove = anIterToRemove->second;
956   // Remove constraint from the map
957   myConstraintMap.erase(anIterToRemove);
958
959   // Find unused entities
960   int aConstrPos = Search(aCnstrToRemove, myConstraints);
961   std::set<Slvs_hEntity> anEntToRemove;
962   Slvs_hEntity aCnstEnt[] = {myConstraints[aConstrPos].ptA,     myConstraints[aConstrPos].ptB, 
963                              myConstraints[aConstrPos].entityA, myConstraints[aConstrPos].entityB};
964   for (int i = 0; i < 4; i++)
965     if (aCnstEnt[i] != 0)
966       anEntToRemove.insert(aCnstEnt[i]);
967   myConstraints.erase(myConstraints.begin() + aConstrPos);
968   if (aCnstrToRemove == myConstrMaxID)
969     myConstrMaxID--;
970   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
971   for ( ; aConstrIter != myConstraints.end(); aConstrIter++)
972   {
973     Slvs_hEntity aEnts[] = {aConstrIter->ptA,     aConstrIter->ptB, 
974                             aConstrIter->entityA, aConstrIter->entityB};
975     for (int i = 0; i < 4; i++)
976       if (aEnts[i] != 0 && anEntToRemove.find(aEnts[i]) != anEntToRemove.end())
977         anEntToRemove.erase(aEnts[i]);
978   }
979
980   if (anEntToRemove.empty())
981     return ;
982
983   // Remove unused entities
984   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
985     anEntAttrIter = myEntityAttrMap.begin();
986   while (anEntAttrIter != myEntityAttrMap.end())
987   {
988     if (anEntToRemove.find(anEntAttrIter->second) != anEntToRemove.end())
989     {
990       std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
991         aRemovedIter = anEntAttrIter;
992       anEntAttrIter++;
993       myEntityAttrMap.erase(aRemovedIter);
994     }
995     else anEntAttrIter++;
996   }
997   std::map<boost::shared_ptr<ModelAPI_Feature>, Slvs_hEntity>::iterator
998     anEntFeatIter = myEntityFeatMap.begin();
999   while (anEntFeatIter != myEntityFeatMap.end())
1000   {
1001     if (anEntToRemove.find(anEntFeatIter->second) != anEntToRemove.end())
1002     {
1003       std::map<boost::shared_ptr<ModelAPI_Feature>, Slvs_hEntity>::iterator
1004         aRemovedIter = anEntFeatIter;
1005       anEntFeatIter++;
1006       myEntityFeatMap.erase(aRemovedIter);
1007     }
1008     else anEntFeatIter++;
1009   }
1010   std::set<Slvs_hEntity>::const_reverse_iterator aRemIter = anEntToRemove.rbegin();
1011   for ( ; aRemIter != anEntToRemove.rend(); aRemIter++)
1012   {
1013     unsigned int anEntPos = Search(*aRemIter, myEntities);
1014     if (anEntPos >= myEntities.size())
1015       continue;
1016     unsigned int aParamPos = Search(myEntities[anEntPos].param[0], myParams);
1017     if (aParamPos >= myParams.size())
1018       continue;
1019     int aNbParams = 0;
1020     while (myEntities[anEntPos].param[aNbParams] != 0) 
1021       aNbParams++;
1022     if (myEntities[anEntPos].param[aNbParams-1] == myParamMaxID)
1023       myParamMaxID -= aNbParams;
1024     myParams.erase(myParams.begin() + aParamPos, myParams.begin() + aParamPos + aNbParams);
1025     if (*aRemIter == myEntityMaxID)
1026       myEntityMaxID--;
1027     myEntities.erase(myEntities.begin() + anEntPos);
1028
1029     // Remove entity's ID from the lists of conincident points
1030     std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1031     for ( ; aCoPtIter != myCoincidentPoints.end(); aCoPtIter++)
1032       aCoPtIter->erase(*aRemIter);
1033   }
1034   if (myCoincidentPoints.size() == 1 && myCoincidentPoints.front().empty())
1035     myCoincidentPoints.clear();
1036 }
1037
1038
1039 // ============================================================================
1040 //  Function: addCoincidentPoints
1041 //  Class:    SketchSolver_ConstraintGroup
1042 //  Purpose:  add coincident point the appropriate list of such points
1043 // ============================================================================
1044 bool SketchSolver_ConstraintGroup::addCoincidentPoints(
1045                 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
1046 {
1047   std::vector< std::set<Slvs_hEntity> >::iterator aCoPtIter = myCoincidentPoints.begin();
1048   std::vector< std::set<Slvs_hEntity> >::iterator aFirstFound = myCoincidentPoints.end();
1049   while (aCoPtIter != myCoincidentPoints.end())
1050   {
1051     bool isFound[2] = { // indicate which point ID was already in coincidence constraint
1052       aCoPtIter->find(thePoint1) != aCoPtIter->end(),
1053       aCoPtIter->find(thePoint2) != aCoPtIter->end(),
1054     };
1055     if (isFound[0] && isFound[1]) // points are already connected by coincidence constraints => no need additional one
1056       return false;
1057     if ((isFound[0] && !isFound[1]) || (!isFound[0] && isFound[1]))
1058     {
1059       if (aFirstFound != myCoincidentPoints.end())
1060       { // there are two groups of coincident points connected by created constraint => merge them
1061         int aFirstFoundShift = aFirstFound - myCoincidentPoints.begin();
1062         int aCurrentShift = aCoPtIter - myCoincidentPoints.begin();
1063         aFirstFound->insert(aCoPtIter->begin(), aCoPtIter->end());
1064         myCoincidentPoints.erase(aCoPtIter);
1065         aFirstFound = myCoincidentPoints.begin() + aFirstFoundShift;
1066         aCoPtIter = myCoincidentPoints.begin() + aCurrentShift;
1067         continue;
1068       }
1069       else
1070       {
1071         aCoPtIter->insert(isFound[0] ? thePoint2 : thePoint1);
1072         aFirstFound = aCoPtIter;
1073       }
1074     }
1075     aCoPtIter++;
1076   }
1077   // No points were found, need to create new set
1078   if (aFirstFound == myCoincidentPoints.end())
1079   {
1080     std::set<Slvs_hEntity> aNewSet;
1081     aNewSet.insert(thePoint1);
1082     aNewSet.insert(thePoint2);
1083     myCoincidentPoints.push_back(aNewSet);
1084   }
1085
1086   return true;
1087 }
1088
1089
1090
1091
1092 // ========================================================
1093 // =========      Auxiliary functions       ===============
1094 // ========================================================
1095
1096 template <typename T>
1097 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1098 {
1099   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1100   int aVecSize = theEntities.size();
1101   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1102     aResIndex--;
1103   while (aResIndex < aVecSize && theEntities[aResIndex].h < theEntityID)
1104     aResIndex++;
1105   if (aResIndex == -1)
1106     aResIndex = aVecSize;
1107   return aResIndex;
1108 }