Salome HOME
53f7af56895599ea0e2afa61eff5444232ee358d
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiRotation.cpp
1 #include <SketchSolver_ConstraintMultiRotation.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_MultiRotation.h>
7
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeInteger.h>
10 #include <ModelAPI_AttributeRefAttr.h>
11 #include <ModelAPI_AttributeRefList.h>
12 #include <ModelAPI_ResultConstruction.h>
13
14 #include <GeomAPI_Dir2d.h>
15 #include <GeomAPI_XY.h>
16
17 #include <math.h>
18
19 static double squareDistance(
20     StoragePtr theStorage, const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
21 {
22   Slvs_Entity aPoint1 = theStorage->getEntity(thePoint1);
23   Slvs_Entity aPoint2 = theStorage->getEntity(thePoint2);
24   double x1 = theStorage->getParameter(aPoint1.param[0]).val;
25   double y1 = theStorage->getParameter(aPoint1.param[1]).val;
26   double x2 = theStorage->getParameter(aPoint2.param[0]).val;
27   double y2 = theStorage->getParameter(aPoint2.param[1]).val;
28   return (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2);
29 }
30
31 void SketchSolver_ConstraintMultiRotation::getAttributes(
32     Slvs_hEntity& theCenter, double& theAngle,
33     std::vector< std::vector<Slvs_hEntity> >& thePoints,
34     std::vector< std::vector<Slvs_hEntity> >& theEntities)
35 {
36   DataPtr aData = myBaseConstraint->data();
37   theAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
38       aData->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
39
40   AttributePtr aCenterAttr = aData->attribute(SketchPlugin_MultiRotation::CENTER_ID());
41   if (!aCenterAttr || !aCenterAttr->isInitialized()) {
42     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
43     return;
44   }
45   int aType = SLVS_E_UNKNOWN; // type of created entity
46   Slvs_hEntity anEntityID = myGroup->getAttributeId(aCenterAttr);
47   if (anEntityID == SLVS_E_UNKNOWN)
48     anEntityID = changeEntity(aCenterAttr, aType);
49   theCenter = anEntityID;
50
51   // Lists of objects and number of copies
52   AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
53       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
54   myNumberOfObjects = anInitialRefList->size();
55   myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID())->value();
56   AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
57       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
58   if (!aRefList) {
59     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
60     return;
61   }
62
63   // Obtain all points of initial features and store them into separate lists
64   // containing their translated copies.
65   // Also all circles and arc collected too, because they will be constrained by equal radii.
66   FeaturePtr aFeature;
67   ResultConstructionPtr aRC;
68   static const size_t MAX_POINTS = 3;
69   std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
70   std::vector<Slvs_hEntity> anEntities;
71   std::list<ObjectPtr> anObjectList = aRefList->list();
72   std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
73   while (anObjectIter != anObjectList.end()) {
74     for (size_t i = 0; i < MAX_POINTS; ++i)
75       aPoints[i].clear();
76     anEntities.clear();
77
78     for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
79       aFeature = ModelAPI_Feature::feature(*anObjectIter);
80       if (!aFeature)
81         continue;
82       anEntityID = changeEntity(aFeature, aType);
83       anEntities.push_back(anEntityID);
84       Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
85       switch (aType) {
86       case SLVS_E_POINT_IN_2D:
87       case SLVS_E_POINT_IN_3D:
88         aPoints[0].push_back(anEntityID);
89         break;
90       case SLVS_E_LINE_SEGMENT:
91         aPoints[0].push_back(anEntity.point[0]); // start point of line
92         aPoints[1].push_back(anEntity.point[1]); // end point of line
93         break;
94       case SLVS_E_CIRCLE:
95         aPoints[0].push_back(anEntity.point[0]); // center of circle
96         break;
97       case SLVS_E_ARC_OF_CIRCLE:
98         aPoints[0].push_back(anEntity.point[0]); // center of arc
99         aPoints[1].push_back(anEntity.point[1]); // start point of arc
100         aPoints[2].push_back(anEntity.point[2]); // end point of arc
101         break;
102       default:
103         myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
104         return;
105       }
106     }
107
108     for (size_t i = 0; i < MAX_POINTS; ++i)
109       if (!aPoints[i].empty())
110         thePoints.push_back(aPoints[i]);
111     if (!anEntities.empty())
112       theEntities.push_back(anEntities);
113   }
114 }
115
116 void SketchSolver_ConstraintMultiRotation::process()
117 {
118   cleanErrorMsg();
119   if (!myBaseConstraint || !myStorage || myGroup == 0) {
120     /// TODO: Put error message here
121     return;
122   }
123   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
124     update(myBaseConstraint);
125
126   std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
127   getAttributes(myRotationCenter, myAngle, myPointsAndCopies, anEntitiesAndCopies);
128   if (!myErrorMsg.empty())
129     return;
130
131   // Set the rotation center unchanged during constraint recalculation
132   Slvs_Constraint aConstraint;
133   if (!myStorage->isPointFixed(myRotationCenter, aConstraint.h, true)) {
134     aConstraint = Slvs_MakeConstraint(
135         SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
136         myRotationCenter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
137     aConstraint.h = myStorage->addConstraint(aConstraint);
138     mySlvsConstraints.push_back(aConstraint.h);
139   }
140
141   // Set all objects unchanged (only initial object may be changed by user)
142   myCircsAndCopies.clear();
143   std::vector<std::vector<Slvs_hEntity> >::const_iterator anEntIt = anEntitiesAndCopies.begin();
144   std::vector<Slvs_hEntity>::const_iterator aCpIt;
145   for (; anEntIt != anEntitiesAndCopies.end(); ++anEntIt) {
146     std::vector<Slvs_hEntity> aCircs;
147     for (aCpIt = anEntIt->begin(); aCpIt != anEntIt->end(); ++aCpIt) {
148       const Slvs_Entity& anEntity = myStorage->getEntity(*aCpIt);
149       std::vector<Slvs_hConstraint> aNewConstr;
150       if (anEntity.type == SLVS_E_CIRCLE) {
151         aCircs.push_back(anEntity.distance);
152         // for circles we fix only center
153         aNewConstr = myStorage->fixEntity(anEntity.point[0]);
154       } else
155         aNewConstr = myStorage->fixEntity(*aCpIt);
156       mySlvsConstraints.insert(mySlvsConstraints.end(), aNewConstr.begin(), aNewConstr.end());
157     }
158
159     if (!aCircs.empty())
160       myCircsAndCopies.push_back(aCircs);
161   }
162
163   adjustConstraint();
164 }
165
166 void SketchSolver_ConstraintMultiRotation::update(ConstraintPtr theConstraint)
167 {
168   cleanErrorMsg();
169   if (!theConstraint || theConstraint == myBaseConstraint) {
170     AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
171         myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
172     AttributeIntegerPtr aNbCopies = myBaseConstraint->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID());
173     if (anInitialRefList->size() != myNumberOfObjects || aNbCopies->value() != myNumberOfCopies) {
174       remove(myBaseConstraint);
175       process();
176       return;
177     }
178   }
179
180   // update angle value
181   myAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
182       myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
183
184   SketchSolver_Constraint::update();
185 }
186
187 bool SketchSolver_ConstraintMultiRotation::remove(ConstraintPtr theConstraint)
188 {
189   cleanErrorMsg();
190   if (theConstraint && theConstraint != myBaseConstraint)
191     return false;
192   bool isFullyRemoved = true;
193   std::vector<Slvs_hEntity>::iterator aCIter = mySlvsConstraints.begin();
194   for (; aCIter != mySlvsConstraints.end(); aCIter++)
195    isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
196   mySlvsConstraints.clear();
197
198   std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
199   for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
200     myStorage->removeEntity(aFeatIt->second);
201   myStorage->removeUnusedEntities();
202
203   std::map<FeaturePtr, Slvs_hEntity> aFeatureMapCopy = myFeatureMap;
204
205   if (isFullyRemoved) {
206     myFeatureMap.clear();
207     myAttributeMap.clear();
208     myValueMap.clear();
209   } else
210     cleanRemovedEntities();
211
212   // Restore initial features
213   std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = aFeatureMapCopy.begin();
214   for (; aFIter != aFeatureMapCopy.end(); ++aFIter)
215   {
216     if (myFeatureMap.find(aFIter->first) != myFeatureMap.end())
217       continue; // the feature was not removed
218     Slvs_hEntity anEntity = myGroup->getFeatureId(aFIter->first);
219     if (anEntity != SLVS_E_UNKNOWN)
220       myFeatureMap[aFIter->first] = anEntity;
221   }
222
223   // Clear list of rotated points
224   myPointsAndCopies.clear();
225
226   return true;
227 }
228
229 void SketchSolver_ConstraintMultiRotation::addFeature(FeaturePtr theFeature)
230 {
231   SketchSolver_Constraint::addFeature(theFeature);
232
233   std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.find(theFeature);
234   if (aFeatIt == myFeatureMap.end())
235     return;
236
237   // store list of points of the feature
238   const Slvs_Entity& theEntity = myStorage->getEntity(aFeatIt->second);
239   for (int i = 0; i < 4; i++)
240     if (theEntity.point[i] != SLVS_E_UNKNOWN)
241       myPointsJustUpdated.insert(theEntity.point[i]);
242 }
243
244 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
245 {
246   if (abs(myAngle) < tolerance) {
247     myStorage->setNeedToResolve(false);
248     return;
249   }
250
251   std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
252   std::list<Slvs_Constraint>::const_iterator aCoIt;
253
254   // Check overconstrained on rotation center (if it is coincident with other fixed point)
255   Slvs_hConstraint aFixedCenter;
256   if (myStorage->isPointFixed(myRotationCenter, aFixedCenter, false)) {
257     Slvs_hConstraint aFixed;
258     for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt)
259       if ((aCoIt->ptA == myRotationCenter && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
260           (aCoIt->ptB == myRotationCenter && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
261         // Un-fix the center
262         myStorage->removeConstraint(aFixedCenter);
263         std::vector<Slvs_hConstraint>::iterator aSCIt = mySlvsConstraints.begin();
264         for (; aSCIt != mySlvsConstraints.end(); ++aSCIt)
265           if (*aSCIt == aFixedCenter) {
266             mySlvsConstraints.erase(aSCIt);
267             break;
268           }
269       }
270   }
271
272   // Obtain coordinates of rotation center
273   Slvs_Entity aRotCenter = myStorage->getEntity(myRotationCenter);
274   double aCenterXY[2];
275   for (int i = 0; i < 2; i++)
276     aCenterXY[i] = myStorage->getParameter(aRotCenter.param[i]).val;
277
278   double cosA = cos(myAngle * PI / 180.0);
279   double sinA = sin(myAngle * PI / 180.0);
280
281   double aVec[2]; // coordinates of vector defining a direction from rotation center to a point
282
283   // Update positions of all points to satisfy angles
284   std::vector< std::vector<Slvs_hEntity> >::const_iterator aPointsIter = myPointsAndCopies.begin();
285   std::vector<Slvs_hEntity>::const_iterator aCopyIter;
286   for (; aPointsIter != myPointsAndCopies.end(); ++aPointsIter) {
287     aCopyIter = aPointsIter->begin();
288     const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
289     for (int i = 0; i < 2; i++)
290       aVec[i] = myStorage->getParameter(anInitial.param[i]).val - aCenterXY[i];
291
292     // if the point is coincident with another one which is temporary fixed (moved by user),
293     // we will update its position correspondingly
294     Slvs_hConstraint aFixed;
295     for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt) {
296       if ((aCoIt->ptA == anInitial.h && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
297           (aCoIt->ptB == anInitial.h && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
298         Slvs_hEntity anOtherId = aCoIt->ptA == anInitial.h ? aCoIt->ptB : aCoIt->ptA;
299         if (!myStorage->isTemporary(aFixed) &&
300             myPointsJustUpdated.find(anOtherId) == myPointsJustUpdated.end())
301           continue; // nothing to change
302
303         const Slvs_Entity& anOtherPnt = myStorage->getEntity(anOtherId);
304         for (int i = 0; i < 2; i++) {
305           Slvs_Param anInitParam = myStorage->getParameter(anInitial.param[i]);
306           const Slvs_Param& anOtherParam = myStorage->getParameter(anOtherPnt.param[i]);
307           anInitParam.val = anOtherParam.val;
308           myStorage->updateParameter(anInitParam);
309           aVec[i] = anOtherParam.val - aCenterXY[i];
310         }
311       }
312     }
313
314     // update copied points
315     aCopyIter = aPointsIter->begin();
316     for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
317       // rotate direction
318       double aTemp = aVec[0] * cosA - aVec[1] * sinA;
319       aVec[1] = aVec[0] * sinA + aVec[1] * cosA;
320       aVec[0] = aTemp;
321
322       const Slvs_Entity& aTarget = myStorage->getEntity(*aCopyIter);
323       for (int i = 0; i < 2; i++) {
324         Slvs_Param aParam = myStorage->getParameter(aTarget.param[i]);
325         aParam.val = aCenterXY[i] + aVec[i];
326         myStorage->updateParameter(aParam);
327       }
328     }
329   }
330
331   for (aPointsIter = myCircsAndCopies.begin(); aPointsIter != myCircsAndCopies.end(); ++aPointsIter) {
332     aCopyIter = aPointsIter->begin();
333     const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
334     const Slvs_Param& anInitRad = myStorage->getParameter(anInitial.param[0]);
335     for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
336       const Slvs_Entity& aCopy = myStorage->getEntity(*aCopyIter);
337       Slvs_Param aCopyRad = myStorage->getParameter(aCopy.param[0]);
338       aCopyRad.val = anInitRad.val;
339       myStorage->updateParameter(aCopyRad);
340     }
341   }
342
343   myPointsJustUpdated.clear();
344 }