]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintManager.cpp
Salome HOME
Constraint solving methods are implemented
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintManager.cpp
1 // File:    SketchSolver_ConstraintManager.cpp
2 // Created: 08 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchSolver_ConstraintManager.h"
6
7 #include <Events_Loop.h>
8 #include <GeomDataAPI_Dir.h>
9 #include <GeomDataAPI_Point.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_Data.h>
14 #include <Model_Events.h>
15 #include <SketchPlugin_Constraint.h>
16 #include <SketchPlugin_ConstraintCoincidence.h>
17 #include <SketchPlugin_Line.h>
18 #include <SketchPlugin_Point.h>
19 #include <SketchPlugin_Sketch.h>
20
21 #include <math.h>
22
23 /// Tolerance for value of parameters
24 const double tolerance = 1.e-10;
25
26 // Initialization of constraint manager self pointer
27 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::_self = 0;
28
29 /// Global constaint manager object
30 SketchSolver_ConstraintManager* myManager = SketchSolver_ConstraintManager::Instance();
31
32 /// This value is used to give unique index to the groups
33 static Slvs_hGroup myGroupIndexer = 0;
34
35 /** \brief Search the entity/parameter with specified ID in the list of elements
36  *  \param[in] theEntityID unique ID of the element
37  *  \param[in] theEntities list of elements
38  *  \return position of the found element or -1 if the element is not found
39  */
40 template <typename T>
41 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
42
43
44
45 // ========================================================
46 // ========= SketchSolver_ConstraintManager ===============
47 // ========================================================
48 SketchSolver_ConstraintManager* SketchSolver_ConstraintManager::Instance()
49 {
50   if (!_self)
51     _self = new SketchSolver_ConstraintManager();
52   return _self;
53 }
54
55 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
56 {
57   myGroups.clear();
58
59   // Register in event loop
60   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
61   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
62   Events_Loop::loop()->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
63 }
64
65 SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
66 {
67   myGroups.clear();
68 }
69
70 void SketchSolver_ConstraintManager::processEvent(const Events_Message* theMessage)
71 {
72   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED) ||
73       theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED))
74   {
75     const Model_FeatureUpdatedMessage* aUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
76
77     // Only sketches and constraints can be added by Create event
78     boost::shared_ptr<SketchPlugin_Sketch> aSketch = 
79       boost::dynamic_pointer_cast<SketchPlugin_Sketch>(aUpdateMsg->feature());
80     if (aSketch)
81     {
82       changeWorkplane(aSketch);
83       return ;
84     }
85     boost::shared_ptr<SketchPlugin_Constraint> aConstraint = 
86       boost::dynamic_pointer_cast<SketchPlugin_Constraint>(aUpdateMsg->feature());
87     if (aConstraint)
88     {
89       changeConstraint(aConstraint);
90
91       // Solve the set of constraints
92       ResolveConstraints();
93       return ;
94     }
95
96     /// \todo Implement feature update handling
97     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
98       boost::dynamic_pointer_cast<SketchPlugin_Feature>(aUpdateMsg->feature());
99     if (aFeature)
100     {
101       updateEntity(aFeature);
102
103       // Solve the set of constraints
104       ResolveConstraints();
105     }
106   }
107   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED))
108   {
109     const Model_FeatureDeletedMessage* aDeleteMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
110     /// \todo Implement deleting objects on event
111   }
112 }
113
114 bool SketchSolver_ConstraintManager::changeWorkplane(boost::shared_ptr<SketchPlugin_Sketch> theSketch)
115 {
116   bool aResult = true; // changed when a workplane wrongly updated
117   bool isUpdated = false;
118   // Try to update specified workplane in all groups
119   std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
120   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
121     if ((*aGroupIter)->isBaseWorkplane(theSketch))
122     {
123       isUpdated = true;
124       if (!(*aGroupIter)->updateWorkplane())
125         aResult = false;
126     }
127   // If the workplane is not updated, so this is a new workplane
128   if (!isUpdated)
129   {
130     SketchSolver_ConstraintGroup* aNewGroup = new SketchSolver_ConstraintGroup(theSketch);
131     // Verify that the group is created successfully
132     if (!aNewGroup->isBaseWorkplane(theSketch))
133     {
134       delete aNewGroup;
135       return false;
136     }
137     myGroups.push_back(aNewGroup);
138   }
139   return aResult;
140 }
141
142 bool SketchSolver_ConstraintManager::changeConstraint(
143               boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
144 {
145   // Search the groups which this constraint touchs
146   std::vector<Slvs_hGroup> aGroups;
147   findGroups(theConstraint, aGroups);
148
149   // Process the groups list
150   if (aGroups.size() == 0)
151   { // There are no groups applicable for this constraint => create new one
152     boost::shared_ptr<SketchPlugin_Sketch> aWP = findWorkplaneForConstraint(theConstraint);
153     if (!aWP) return false;
154     SketchSolver_ConstraintGroup* aGroup = new SketchSolver_ConstraintGroup(aWP);
155     if (!aGroup->changeConstraint(theConstraint))
156     {
157       delete aGroup;
158       return false;
159     }
160     myGroups.push_back(aGroup);
161     return true;
162   }
163   else if (aGroups.size() == 1)
164   { // Only one group => add constraint into it
165     Slvs_hGroup aGroupId = *(aGroups.begin());
166     std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
167     for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
168       if ((*aGroupIter)->getId() == aGroupId)
169         return (*aGroupIter)->changeConstraint(theConstraint);
170   }
171   else if (aGroups.size() > 1)
172   { // Several groups applicable for this constraint => need to merge them
173     /// \todo Implement merging of groups
174   }
175
176   // Something goes wrong
177   return false;
178 }
179
180 void SketchSolver_ConstraintManager::updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature)
181 {
182   // Create list of attributes depending on type of the feature
183   std::vector<std::string> anAttrList;
184   // Point
185   boost::shared_ptr<SketchPlugin_Point> aPoint = 
186     boost::dynamic_pointer_cast<SketchPlugin_Point>(theFeature);
187   if (aPoint)
188     anAttrList.push_back(POINT_ATTR_COORD);
189   // Line
190   boost::shared_ptr<SketchPlugin_Line> aLine = 
191     boost::dynamic_pointer_cast<SketchPlugin_Line>(theFeature);
192   if (aLine)
193   {
194     anAttrList.push_back(LINE_ATTR_START);
195     anAttrList.push_back(LINE_ATTR_END);
196   }
197   /// \todo Other types of features should be implemented
198
199   // Check changing of feature's attributes (go through the groups and search usage of the attributes)
200   std::vector<std::string>::const_iterator anAttrIter;
201   for (anAttrIter = anAttrList.begin(); anAttrIter != anAttrList.end(); anAttrIter++)
202   {
203     std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
204     for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
205     {
206       boost::shared_ptr<ModelAPI_Attribute> anAttribute = 
207         boost::dynamic_pointer_cast<ModelAPI_Attribute>(theFeature->data()->attribute(*anAttrIter));
208       (*aGroupIter)->updateEntityIfPossible(anAttribute);
209     }
210   }
211 }
212
213
214 void SketchSolver_ConstraintManager::findGroups(
215               boost::shared_ptr<SketchPlugin_Constraint> theConstraint, 
216               std::vector<Slvs_hGroup>&                  theGroupIDs) const
217 {
218   boost::shared_ptr<SketchPlugin_Sketch> aWP = findWorkplaneForConstraint(theConstraint);
219
220   std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
221   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
222     if (aWP == (*aGroupIter)->getWorkplane() && (*aGroupIter)->isInteract(theConstraint))
223       theGroupIDs.push_back((*aGroupIter)->getId());
224 }
225
226 boost::shared_ptr<SketchPlugin_Sketch> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
227               boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
228 {
229   std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
230   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
231   {
232     boost::shared_ptr<SketchPlugin_Sketch> aWP = (*aGroupIter)->getWorkplane();
233     boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures = 
234       boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aWP->data()->attribute(SKETCH_ATTR_FEATURES));
235     std::list< boost::shared_ptr<ModelAPI_Feature> > aFeaturesList = aWPFeatures->list();
236     std::list< boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIter;
237     for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
238       if (*anIter == theConstraint)
239         return aWP; // workplane is found
240   }
241
242   return boost::shared_ptr<SketchPlugin_Sketch>();
243 }
244
245 void SketchSolver_ConstraintManager::ResolveConstraints()
246 {
247   std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter; 
248   for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
249     (*aGroupIter)->ResolveConstraints();
250 }
251
252
253
254 // ========================================================
255 // =========  SketchSolver_ConstraintGroup  ===============
256 // ========================================================
257
258 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::
259   SketchSolver_ConstraintGroup(boost::shared_ptr<SketchPlugin_Sketch> theWorkplane)
260   : myID(++myGroupIndexer),
261     myParamMaxID(0),
262     myEntityMaxID(0),
263     myConstrMaxID(0),
264     myConstraintMap(),
265     myNeedToSolve(false),
266     myConstrSolver()
267 {
268   myParams.clear();
269   myEntities.clear();
270   myConstraints.clear();
271
272   // Initialize workplane
273   myWorkplane.h = SLVS_E_UNKNOWN;
274   addWorkplane(theWorkplane);
275 }
276
277 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
278 {
279   myParams.clear();
280   myEntities.clear();
281   myConstraints.clear();
282   myConstraintMap.clear();
283 }
284
285 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane(
286                 boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const
287 {
288   return theWorkplane == mySketch;
289 }
290
291 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
292                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
293 {
294   // Check the group is empty
295   if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
296     return true;
297
298   /// \todo Should be implemented
299   return false;
300 }
301
302 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstraint(
303                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
304 {
305   // There is no workplane yet, something wrong
306   if (myWorkplane.h == SLVS_E_UNKNOWN)
307     return false;
308
309   // Search this constraint in the current group to update it
310   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
311     aConstrMapIter = myConstraintMap.find(theConstraint);
312   std::vector<Slvs_Constraint>::iterator aConstrIter;
313   if (aConstrMapIter != myConstraintMap.end())
314   {
315     int aConstrPos = Search(aConstrMapIter->second, myConstraints);
316     aConstrIter = myConstraints.begin() + aConstrPos;
317   }
318
319   // Get constraint type and verify the constraint parameters are correct
320   int aConstrType = getConstraintType(theConstraint);
321   if (aConstrType == SLVS_C_UNKNOWN || 
322      (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
323     return false;
324
325   // Create constraint parameters
326   double aDistance = 0.0; // scalar value of the constraint
327   boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
328     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
329   if (aDistAttr)
330   {
331     aDistance = aDistAttr->value();
332     if (aConstrMapIter != myConstraintMap.end() && aConstrIter->valA != aDistance)
333     {
334       myNeedToSolve = true;
335       aConstrIter->valA = aDistance;
336     }
337   }
338
339   Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
340   for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
341   {
342     aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
343     boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = 
344       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
345         theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
346       );
347     if (!aConstrAttr) continue;
348     aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
349   }
350
351   if (aConstrMapIter == myConstraintMap.end())
352   {
353     // Create SolveSpace constraint structure
354     Slvs_Constraint aConstraint = 
355       Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h, 
356                           aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
357     myConstraints.push_back(aConstraint);
358     myConstraintMap[theConstraint] = aConstraint.h;
359   }
360   return true;
361 }
362
363 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeEntity(
364                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
365 {
366   // If the entity is already in the group, try to find it
367   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
368     aEntIter = myEntityMap.find(theEntity);
369   std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
370   if (aEntIter == myEntityMap.end()) // no such entity => should be created
371     aParamIter = myParams.end();
372   else
373   { // the entity already exists
374     int aEntPos = Search(aEntIter->second, myEntities);
375     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
376     aParamIter = myParams.begin() + aParamPos;
377   }
378
379   // Look over supported types of entities
380
381   // Point in 3D
382   boost::shared_ptr<GeomDataAPI_Point> aPoint = 
383     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
384   if (aPoint)
385   {
386     Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
387     Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
388     Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
389
390     if (aEntIter != myEntityMap.end()) // the entity already exists
391       return aEntIter->second;
392
393     // New entity
394     Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
395     myEntities.push_back(aPtEntity);
396     myEntityMap[theEntity] = aPtEntity.h;
397     return aPtEntity.h;
398   }
399
400   // Point in 2D
401   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = 
402     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
403   if (aPoint2D)
404   {
405     // The 2D points are created on workplane. So, if there is no workplane yet, then error
406     if (myWorkplane.h == SLVS_E_UNKNOWN)
407       return SLVS_E_UNKNOWN;
408     Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
409     Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
410
411     if (aEntIter != myEntityMap.end()) // the entity already exists
412       return aEntIter->second;
413
414     // New entity
415     Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
416     myEntities.push_back(aPt2DEntity);
417     myEntityMap[theEntity] = aPt2DEntity.h;
418     return aPt2DEntity.h;
419   }
420
421   /// \todo Other types of entities
422   
423   // Unsupported or wrong entity type
424   return SLVS_E_UNKNOWN;
425 }
426
427 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeNormal(
428                 boost::shared_ptr<ModelAPI_Attribute> theDirX, 
429                 boost::shared_ptr<ModelAPI_Attribute> theDirY, 
430                 boost::shared_ptr<ModelAPI_Attribute> theNorm)
431 {
432   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
433     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
434   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
435     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
436   if (!aDirX || !aDirY || 
437      (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
438      (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
439     return SLVS_E_UNKNOWN;
440
441   // quaternion parameters of normal vector
442   double qw, qx, qy, qz;
443   Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), 
444                       aDirY->x(), aDirY->y(), aDirY->z(), 
445                       &qw, &qx, &qy, &qz);
446   double aNormCoord[4] = {qw, qx, qy, qz};
447
448   // Try to find existent normal
449   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
450     aEntIter = myEntityMap.find(theNorm);
451   std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
452   if (aEntIter == myEntityMap.end()) // no such entity => should be created
453     aParamIter = myParams.end();
454   else
455   { // the entity already exists, update it
456     int aEntPos = Search(aEntIter->second, myEntities);
457     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
458     aParamIter = myParams.begin() + aParamPos;
459   }
460
461   // Change parameters of the normal
462   Slvs_hParam aNormParams[4];
463   for (int i = 0; i < 4; i++)
464     aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
465
466   if (aEntIter != myEntityMap.end()) // the entity already exists
467     return aEntIter->second;
468
469   // Create a normal
470   Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID, 
471                 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
472   myEntities.push_back(aNormal);
473   myEntityMap[theNorm] = aNormal.h;
474   return aNormal.h;
475 }
476
477
478 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
479                 boost::shared_ptr<SketchPlugin_Sketch> theSketch)
480 {
481   if (myWorkplane.h)
482     return false; // the workplane already exists
483
484   mySketch = theSketch;
485   return updateWorkplane();
486 }
487
488 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateWorkplane()
489 {
490   // Get parameters of workplane
491   boost::shared_ptr<ModelAPI_Attribute> aDirX    = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
492   boost::shared_ptr<ModelAPI_Attribute> aDirY    = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
493   boost::shared_ptr<ModelAPI_Attribute> aNorm    = mySketch->data()->attribute(SKETCH_ATTR_NORM);
494   boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
495   // Transform them into SolveSpace format
496   Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
497   if (!aNormalWP) return false;
498   Slvs_hEntity anOriginWP = changeEntity(anOrigin);
499   if (!anOriginWP) return false;
500
501   if (!myWorkplane.h)
502   {
503     // Create workplane
504     myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
505     // Workplane should be added to the list of entities
506     myEntities.push_back(myWorkplane);
507   }
508   return true;
509 }
510
511
512 Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeParameter(
513                 const double&                            theParam, 
514                 std::vector<Slvs_Param>::const_iterator& thePrmIter)
515 {
516   if (thePrmIter != myParams.end())
517   { // Parameter should be updated
518     if (thePrmIter->val != theParam)
519       myNeedToSolve = true; // parameter is changed, need to resolve constraints
520     int aParamPos = thePrmIter - myParams.begin();
521     myParams[aParamPos].val = theParam;
522     thePrmIter++;
523     return myParams[aParamPos].h;
524   }
525
526   // Newly created parameter
527   Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
528   myParams.push_back(aParam);
529   myNeedToSolve = true;
530   // The list of parameters is changed, move iterator to the end of the list to avoid problems
531   thePrmIter = myParams.end();
532   return aParam.h;
533 }
534
535 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
536                 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
537 {
538   // Constraint for coincidence of two points
539   boost::shared_ptr<SketchPlugin_ConstraintCoincidence> aPtEquiv = 
540     boost::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidence>(theConstraint);
541   if (aPtEquiv)
542   {
543     // Verify the constraint has only two attributes and they are points
544     int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point 
545     int aPt3d = 0; // bit-mapped field, the same information for 3D points
546     for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
547     {
548       boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
549         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
550           theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
551         );
552       if (!anAttr) continue;
553       // Verify the attribute is a 2D point
554       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = 
555         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
556       if (aPoint2D)
557       {
558         aPt2d |= (1 << indAttr);
559         continue;
560       }
561       // Verify the attribute is a 3D point
562       boost::shared_ptr<GeomDataAPI_Point> aPoint3D = 
563         boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
564       if (aPoint3D)
565       {
566         aPt3d |= (1 << indAttr);
567         continue;
568       }
569       // Attribute neither 2D nor 3D point is not supported by this type of constraint
570       return SLVS_C_UNKNOWN;
571     }
572     // The constrained points should be in first and second positions,
573     // so the expected value of aPt2d or aPt3d is 3
574     if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
575       return SLVS_C_POINTS_COINCIDENT;
576     // Constraint parameters are wrong
577     return SLVS_C_UNKNOWN;
578   }
579
580   /// \todo Implement other kind of constrtaints
581
582   return SLVS_C_UNKNOWN;
583 }
584
585 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::ResolveConstraints()
586 {
587   if (!myNeedToSolve)
588     return;
589
590   myConstrSolver.setGroupID(myID);
591   myConstrSolver.setParameters(myParams);
592   myConstrSolver.setEntities(myEntities);
593   myConstrSolver.setConstraints(myConstraints);
594
595   if (myConstrSolver.solve() == SLVS_RESULT_OKAY)
596   { // solution succeeded, store results into correspondent attributes
597     // Obtain result into the same list of parameters
598     if (!myConstrSolver.getResult(myParams))
599       return;
600
601     std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
602       anEntIter = myEntityMap.begin();
603     for ( ; anEntIter != myEntityMap.end(); anEntIter++)
604       updateAttribute(anEntIter->first, anEntIter->second);
605   }
606   /// \todo Implement error handling
607
608   myNeedToSolve = false;
609 }
610
611
612 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateAttribute(
613                 boost::shared_ptr<ModelAPI_Attribute> theAttribute, 
614                 const Slvs_hEntity&                   theEntityID)
615 {
616   // Search the position of the first parameter of the entity
617   int anEntPos = Search(theEntityID, myEntities);
618   int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
619
620   // Look over supported types of entities
621
622   // Point in 3D
623   boost::shared_ptr<GeomDataAPI_Point> aPoint = 
624     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
625   if (aPoint)
626   {
627     aPoint->setValue(myParams[aFirstParamPos].val,
628                      myParams[aFirstParamPos+1].val,
629                      myParams[aFirstParamPos+2].val);
630     return ;
631   }
632
633   // Point in 2D
634   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = 
635     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
636   if (aPoint2D)
637   {
638     aPoint2D->setValue(myParams[aFirstParamPos].val,
639                        myParams[aFirstParamPos+1].val);
640     return ;
641   }
642
643   /// \todo Support other types of entities
644 }
645
646 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityIfPossible(
647                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
648 {
649   if (myEntityMap.find(theEntity) != myEntityMap.end())
650     changeEntity(theEntity);
651 }
652
653
654
655 // ========================================================
656 // =========      Auxiliary functions       ===============
657 // ========================================================
658
659 template <typename T>
660 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
661 {
662   std::vector<T>::const_iterator aEntIter = theEntities.begin() + theEntityID - 1;
663   while (aEntIter != theEntities.end() && aEntIter->h > theEntityID)
664     aEntIter--;
665   while (aEntIter != theEntities.end() && aEntIter->h < theEntityID)
666     aEntIter++;
667   if (aEntIter == theEntities.end())
668     return -1;
669   return aEntIter - theEntities.begin();
670 }