]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintManager.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SolveSpace
[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   // If the group with maximal identifier is deleted, decrease the indexer
285   if (myID == myGroupIndexer)
286     myGroupIndexer--;
287 }
288
289 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isBaseWorkplane(
290                 boost::shared_ptr<SketchPlugin_Sketch> theWorkplane) const
291 {
292   return theWorkplane == mySketch;
293 }
294
295 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
296                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
297 {
298   // Check the group is empty
299   if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
300     return true;
301
302   /// \todo Should be implemented
303   return false;
304 }
305
306 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeConstraint(
307                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
308 {
309   // There is no workplane yet, something wrong
310   if (myWorkplane.h == SLVS_E_UNKNOWN)
311     return false;
312
313   // Search this constraint in the current group to update it
314   std::map<boost::shared_ptr<SketchPlugin_Constraint>, Slvs_hConstraint>::const_iterator
315     aConstrMapIter = myConstraintMap.find(theConstraint);
316   std::vector<Slvs_Constraint>::iterator aConstrIter;
317   if (aConstrMapIter != myConstraintMap.end())
318   {
319     int aConstrPos = Search(aConstrMapIter->second, myConstraints);
320     aConstrIter = myConstraints.begin() + aConstrPos;
321   }
322
323   // Get constraint type and verify the constraint parameters are correct
324   int aConstrType = getConstraintType(theConstraint);
325   if (aConstrType == SLVS_C_UNKNOWN || 
326      (aConstrMapIter != myConstraintMap.end() && aConstrIter->type != aConstrType))
327     return false;
328
329   // Create constraint parameters
330   double aDistance = 0.0; // scalar value of the constraint
331   boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
332     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
333   if (aDistAttr)
334   {
335     aDistance = aDistAttr->value();
336     if (aConstrMapIter != myConstraintMap.end() && fabs(aConstrIter->valA - aDistance) > tolerance)
337     {
338       myNeedToSolve = true;
339       aConstrIter->valA = aDistance;
340     }
341   }
342
343   Slvs_hEntity aConstrEnt[CONSTRAINT_ATTR_SIZE]; // parameters of the constraint
344   for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
345   {
346     aConstrEnt[indAttr] = SLVS_E_UNKNOWN;
347     boost::shared_ptr<ModelAPI_AttributeRefAttr> aConstrAttr = 
348       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
349         theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
350       );
351     if (!aConstrAttr) continue;
352     aConstrEnt[indAttr] = changeEntity(aConstrAttr->attr());
353   }
354
355   if (aConstrMapIter == myConstraintMap.end())
356   {
357     // Create SolveSpace constraint structure
358     Slvs_Constraint aConstraint = 
359       Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h, 
360                           aDistance, aConstrEnt[0], aConstrEnt[1], aConstrEnt[2], aConstrEnt[3]);
361     myConstraints.push_back(aConstraint);
362     myConstraintMap[theConstraint] = aConstraint.h;
363   }
364   return true;
365 }
366
367 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeEntity(
368                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
369 {
370   // If the entity is already in the group, try to find it
371   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
372     aEntIter = myEntityMap.find(theEntity);
373   std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
374   if (aEntIter == myEntityMap.end()) // no such entity => should be created
375     aParamIter = myParams.end();
376   else
377   { // the entity already exists
378     int aEntPos = Search(aEntIter->second, myEntities);
379     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
380     aParamIter = myParams.begin() + aParamPos;
381   }
382
383   // Look over supported types of entities
384
385   // Point in 3D
386   boost::shared_ptr<GeomDataAPI_Point> aPoint = 
387     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
388   if (aPoint)
389   {
390     Slvs_hParam aX = changeParameter(aPoint->x(), aParamIter);
391     Slvs_hParam aY = changeParameter(aPoint->y(), aParamIter);
392     Slvs_hParam aZ = changeParameter(aPoint->z(), aParamIter);
393
394     if (aEntIter != myEntityMap.end()) // the entity already exists
395       return aEntIter->second;
396
397     // New entity
398     Slvs_Entity aPtEntity = Slvs_MakePoint3d(++myEntityMaxID, myID, aX, aY, aZ);
399     myEntities.push_back(aPtEntity);
400     myEntityMap[theEntity] = aPtEntity.h;
401     return aPtEntity.h;
402   }
403
404   // Point in 2D
405   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = 
406     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
407   if (aPoint2D)
408   {
409     // The 2D points are created on workplane. So, if there is no workplane yet, then error
410     if (myWorkplane.h == SLVS_E_UNKNOWN)
411       return SLVS_E_UNKNOWN;
412     Slvs_hParam aU = changeParameter(aPoint2D->x(), aParamIter);
413     Slvs_hParam aV = changeParameter(aPoint2D->y(), aParamIter);
414
415     if (aEntIter != myEntityMap.end()) // the entity already exists
416       return aEntIter->second;
417
418     // New entity
419     Slvs_Entity aPt2DEntity = Slvs_MakePoint2d(++myEntityMaxID, myID, myWorkplane.h, aU, aV);
420     myEntities.push_back(aPt2DEntity);
421     myEntityMap[theEntity] = aPt2DEntity.h;
422     return aPt2DEntity.h;
423   }
424
425   /// \todo Other types of entities
426   
427   // Unsupported or wrong entity type
428   return SLVS_E_UNKNOWN;
429 }
430
431 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeNormal(
432                 boost::shared_ptr<ModelAPI_Attribute> theDirX, 
433                 boost::shared_ptr<ModelAPI_Attribute> theDirY, 
434                 boost::shared_ptr<ModelAPI_Attribute> theNorm)
435 {
436   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
437     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirX);
438   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
439     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theDirY);
440   if (!aDirX || !aDirY || 
441      (fabs(aDirX->x()) + fabs(aDirX->y()) + fabs(aDirX->z()) < tolerance) ||
442      (fabs(aDirY->x()) + fabs(aDirY->y()) + fabs(aDirY->z()) < tolerance))
443     return SLVS_E_UNKNOWN;
444
445   // quaternion parameters of normal vector
446   double qw, qx, qy, qz;
447   Slvs_MakeQuaternion(aDirX->x(), aDirX->y(), aDirX->z(), 
448                       aDirY->x(), aDirY->y(), aDirY->z(), 
449                       &qw, &qx, &qy, &qz);
450   double aNormCoord[4] = {qw, qx, qy, qz};
451
452   // Try to find existent normal
453   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
454     aEntIter = myEntityMap.find(theNorm);
455   std::vector<Slvs_Param>::const_iterator aParamIter; // looks at first parameter of already existent entity or at the end of vector otherwise
456   if (aEntIter == myEntityMap.end()) // no such entity => should be created
457     aParamIter = myParams.end();
458   else
459   { // the entity already exists, update it
460     int aEntPos = Search(aEntIter->second, myEntities);
461     int aParamPos = Search(myEntities[aEntPos].param[0], myParams);
462     aParamIter = myParams.begin() + aParamPos;
463   }
464
465   // Change parameters of the normal
466   Slvs_hParam aNormParams[4];
467   for (int i = 0; i < 4; i++)
468     aNormParams[i] = changeParameter(aNormCoord[i], aParamIter);
469
470   if (aEntIter != myEntityMap.end()) // the entity already exists
471     return aEntIter->second;
472
473   // Create a normal
474   Slvs_Entity aNormal = Slvs_MakeNormal3d(++myEntityMaxID, myID, 
475                 aNormParams[0], aNormParams[1], aNormParams[2], aNormParams[3]);
476   myEntities.push_back(aNormal);
477   myEntityMap[theNorm] = aNormal.h;
478   return aNormal.h;
479 }
480
481
482 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
483                 boost::shared_ptr<SketchPlugin_Sketch> theSketch)
484 {
485   if (myWorkplane.h)
486     return false; // the workplane already exists
487
488   mySketch = theSketch;
489   return updateWorkplane();
490 }
491
492 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateWorkplane()
493 {
494   // Get parameters of workplane
495   boost::shared_ptr<ModelAPI_Attribute> aDirX    = mySketch->data()->attribute(SKETCH_ATTR_DIRX);
496   boost::shared_ptr<ModelAPI_Attribute> aDirY    = mySketch->data()->attribute(SKETCH_ATTR_DIRY);
497   boost::shared_ptr<ModelAPI_Attribute> aNorm    = mySketch->data()->attribute(SKETCH_ATTR_NORM);
498   boost::shared_ptr<ModelAPI_Attribute> anOrigin = mySketch->data()->attribute(SKETCH_ATTR_ORIGIN);
499   // Transform them into SolveSpace format
500   Slvs_hEntity aNormalWP = changeNormal(aDirX, aDirY, aNorm);
501   if (!aNormalWP) return false;
502   Slvs_hEntity anOriginWP = changeEntity(anOrigin);
503   if (!anOriginWP) return false;
504
505   if (!myWorkplane.h)
506   {
507     // Create workplane
508     myWorkplane = Slvs_MakeWorkplane(++myEntityMaxID, myID, anOriginWP, aNormalWP);
509     // Workplane should be added to the list of entities
510     myEntities.push_back(myWorkplane);
511   }
512   return true;
513 }
514
515
516 Slvs_hParam SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::changeParameter(
517                 const double&                            theParam, 
518                 std::vector<Slvs_Param>::const_iterator& thePrmIter)
519 {
520   if (thePrmIter != myParams.end())
521   { // Parameter should be updated
522     int aParamPos = thePrmIter - myParams.begin();
523     if (fabs(thePrmIter->val - theParam) > tolerance)
524     {
525       myNeedToSolve = true; // parameter is changed, need to resolve constraints
526       myParams[aParamPos].val = theParam;
527     }
528     thePrmIter++;
529     return myParams[aParamPos].h;
530   }
531
532   // Newly created parameter
533   Slvs_Param aParam = Slvs_MakeParam(++myParamMaxID, myID, theParam);
534   myParams.push_back(aParam);
535   myNeedToSolve = true;
536   // The list of parameters is changed, move iterator to the end of the list to avoid problems
537   thePrmIter = myParams.end();
538   return aParam.h;
539 }
540
541 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
542                 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
543 {
544   // Constraint for coincidence of two points
545   boost::shared_ptr<SketchPlugin_ConstraintCoincidence> aPtEquiv = 
546     boost::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidence>(theConstraint);
547   if (aPtEquiv)
548   {
549     // Verify the constraint has only two attributes and they are points
550     int aPt2d = 0; // bit-mapped field, each bit indicates whether the attribute is 2D point 
551     int aPt3d = 0; // bit-mapped field, the same information for 3D points
552     for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++)
553     {
554       boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
555         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
556           theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[indAttr])
557         );
558       if (!anAttr) continue;
559       // Verify the attribute is a 2D point
560       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = 
561         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
562       if (aPoint2D)
563       {
564         aPt2d |= (1 << indAttr);
565         continue;
566       }
567       // Verify the attribute is a 3D point
568       boost::shared_ptr<GeomDataAPI_Point> aPoint3D = 
569         boost::dynamic_pointer_cast<GeomDataAPI_Point>(anAttr->attr());
570       if (aPoint3D)
571       {
572         aPt3d |= (1 << indAttr);
573         continue;
574       }
575       // Attribute neither 2D nor 3D point is not supported by this type of constraint
576       return SLVS_C_UNKNOWN;
577     }
578     // The constrained points should be in first and second positions,
579     // so the expected value of aPt2d or aPt3d is 3
580     if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
581       return SLVS_C_POINTS_COINCIDENT;
582     // Constraint parameters are wrong
583     return SLVS_C_UNKNOWN;
584   }
585
586   /// \todo Implement other kind of constrtaints
587
588   return SLVS_C_UNKNOWN;
589 }
590
591 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::ResolveConstraints()
592 {
593   if (!myNeedToSolve)
594     return;
595
596   myConstrSolver.setGroupID(myID);
597   myConstrSolver.setParameters(myParams);
598   myConstrSolver.setEntities(myEntities);
599   myConstrSolver.setConstraints(myConstraints);
600
601   if (myConstrSolver.solve() == SLVS_RESULT_OKAY)
602   { // solution succeeded, store results into correspondent attributes
603     // Obtain result into the same list of parameters
604     if (!myConstrSolver.getResult(myParams))
605       return;
606
607     std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::iterator
608       anEntIter = myEntityMap.begin();
609     for ( ; anEntIter != myEntityMap.end(); anEntIter++)
610       updateAttribute(anEntIter->first, anEntIter->second);
611   }
612   /// \todo Implement error handling
613
614   removeTemporaryConstraints();
615   myNeedToSolve = false;
616 }
617
618
619 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateAttribute(
620                 boost::shared_ptr<ModelAPI_Attribute> theAttribute, 
621                 const Slvs_hEntity&                   theEntityID)
622 {
623   // Search the position of the first parameter of the entity
624   int anEntPos = Search(theEntityID, myEntities);
625   int aFirstParamPos = Search(myEntities[anEntPos].param[0], myParams);
626
627   // Look over supported types of entities
628
629   // Point in 3D
630   boost::shared_ptr<GeomDataAPI_Point> aPoint = 
631     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
632   if (aPoint)
633   {
634     aPoint->setValue(myParams[aFirstParamPos].val,
635                      myParams[aFirstParamPos+1].val,
636                      myParams[aFirstParamPos+2].val);
637     return ;
638   }
639
640   // Point in 2D
641   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = 
642     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
643   if (aPoint2D)
644   {
645     aPoint2D->setValue(myParams[aFirstParamPos].val,
646                        myParams[aFirstParamPos+1].val);
647     return ;
648   }
649
650   /// \todo Support other types of entities
651 }
652
653 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::updateEntityIfPossible(
654                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
655 {
656   if (myEntityMap.find(theEntity) != myEntityMap.end())
657   {
658     // If the attribute is a point and it is changed (the group needs to rebuild), 
659     // probably user has dragged this point into this position, 
660     // so it is necessary to add constraint which will quarantee the point will not change
661
662     // Store myNeedToSolve flag to verify the entity is really changed
663     bool aNeedToSolveCopy = myNeedToSolve;
664     myNeedToSolve = false;
665
666     changeEntity(theEntity);
667
668     if (myNeedToSolve) // the entity is changed
669     {
670       // Verify the entity is a point and add temporary constraint of permanency
671       boost::shared_ptr<GeomDataAPI_Point> aPoint = 
672         boost::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
673       boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D = 
674         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
675       if (aPoint || aPoint2D)
676         addTemporaryConstraintWhereDragged(theEntity);
677     }
678
679     // Restore flag of changes
680     myNeedToSolve = myNeedToSolve || aNeedToSolveCopy;
681   }
682 }
683
684 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addTemporaryConstraintWhereDragged(
685                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
686 {
687   // Find identifier of the entity
688   std::map<boost::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator
689     anEntIter = myEntityMap.find(theEntity);
690
691   // Create WHERE_DRAGGED constraint
692   Slvs_Constraint aWDConstr = Slvs_MakeConstraint(++myConstrMaxID, myID, SLVS_C_WHERE_DRAGGED, 
693                                                   myWorkplane.h, 0.0, anEntIter->second, 0, 0, 0);
694   myConstraints.push_back(aWDConstr);
695   myTempConstraints.push_back(aWDConstr.h);
696 }
697
698 void SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::removeTemporaryConstraints()
699 {
700   std::list<Slvs_hConstraint>::reverse_iterator aTmpConstrIter;
701   for (aTmpConstrIter = myTempConstraints.rbegin(); aTmpConstrIter != myTempConstraints.rend(); aTmpConstrIter++)
702   {
703     int aConstrPos = Search(*aTmpConstrIter, myConstraints);
704     myConstraints.erase(myConstraints.begin() + aConstrPos);
705
706     // If the removing constraint has higher index, decrease the indexer
707     if (*aTmpConstrIter == myConstrMaxID)
708       myConstrMaxID--;
709   }
710   myTempConstraints.clear();
711 }
712
713
714
715 // ========================================================
716 // =========      Auxiliary functions       ===============
717 // ========================================================
718
719 template <typename T>
720 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
721 {
722   std::vector<T>::const_iterator aEntIter = theEntities.begin() + theEntityID - 1;
723   while (aEntIter != theEntities.end() && aEntIter->h > theEntityID)
724     aEntIter--;
725   while (aEntIter != theEntities.end() && aEntIter->h < theEntityID)
726     aEntIter++;
727   if (aEntIter == theEntities.end())
728     return -1;
729   return aEntIter - theEntities.begin();
730 }