Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintCoincidence.cpp
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <SketchSolver_ConstraintCoincidence.h>
21 #include <SketchSolver_Error.h>
22 #include <PlaneGCSSolver_PointArrayWrapper.h>
23 #include <PlaneGCSSolver_Storage.h>
24 #include <PlaneGCSSolver_Tools.h>
25 #include <PlaneGCSSolver_UpdateCoincidence.h>
26
27 #include <GeomAPI_BSpline2d.h>
28 #include <GeomAPI_Pnt2d.h>
29
30 #include <GeomDataAPI_Point2D.h>
31
32 #include <ModelAPI_AttributeInteger.h>
33
34 #include <SketchPlugin_Arc.h>
35 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
36 #include <SketchPlugin_Ellipse.h>
37 #include <SketchPlugin_EllipticArc.h>
38 #include <SketchPlugin_Line.h>
39 #include <SketchPlugin_Point.h>
40
41 static void getCoincidentFeatureExtremities(const ConstraintPtr& theConstraint,
42                                             const StoragePtr& theStorage,
43                                             EntityWrapperPtr theExtremities[2])
44 {
45   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
46     AttributeRefAttrPtr aRefAttr = theConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
47     if (!aRefAttr || !aRefAttr->isObject())
48       continue;
49
50     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
51     if (!aFeature)
52       continue;
53
54     if (aFeature->getKind() == SketchPlugin_Line::ID()) {
55       theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::START_ID()));
56       theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::END_ID()));
57     } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
58       theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::START_ID()));
59       theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::END_ID()));
60     } else if (aFeature->getKind() == SketchPlugin_EllipticArc::ID()) {
61       theExtremities[0] = theStorage->entity(
62           aFeature->attribute(SketchPlugin_EllipticArc::START_POINT_ID()));
63       theExtremities[1] = theStorage->entity(
64           aFeature->attribute(SketchPlugin_EllipticArc::END_POINT_ID()));
65     }
66   }
67 }
68
69 static void getPointOwnerAndParent(const AttributeRefAttrPtr theRefAttr,
70                                    AttributePoint2DPtr& thePoint,
71                                    FeaturePtr& theOwner,
72                                    FeaturePtr& theParent)
73 {
74   AttributePtr anAttr = theRefAttr->attr();
75   if (theRefAttr->isObject()) {
76     FeaturePtr anOwner = ModelAPI_Feature::feature(theRefAttr->object());
77     if (anOwner && anOwner->getKind() == SketchPlugin_Point::ID())
78       anAttr = anOwner->attribute(SketchPlugin_Point::COORD_ID());
79     else
80       return;
81   }
82   thePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
83   if (thePoint) {
84     theOwner = ModelAPI_Feature::feature(thePoint->owner());
85     if (theOwner) {
86       AttributeReferencePtr aParentRef =
87           theOwner->reference(SketchPlugin_SketchEntity::PARENT_ID());
88       theParent = aParentRef ? ModelAPI_Feature::feature(aParentRef->value()) : FeaturePtr();
89     }
90   }
91 }
92
93 static void ellipseDiameters(FeaturePtr theEllipse,
94                              std::pair<std::string, std::string>& theMajorAxis,
95                              std::pair<std::string, std::string>& theMinorAxis)
96 {
97   if (theEllipse->getKind() == SketchPlugin_Ellipse::ID()) {
98     theMajorAxis.first = SketchPlugin_Ellipse::MAJOR_AXIS_START_ID();
99     theMajorAxis.second = SketchPlugin_Ellipse::MAJOR_AXIS_END_ID();
100     theMinorAxis.first = SketchPlugin_Ellipse::MINOR_AXIS_START_ID();
101     theMinorAxis.second = SketchPlugin_Ellipse::MINOR_AXIS_END_ID();
102   } else if (theEllipse->getKind() == SketchPlugin_EllipticArc::ID()) {
103     theMajorAxis.first = SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID();
104     theMajorAxis.second = SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
105     theMinorAxis.first = SketchPlugin_EllipticArc::MINOR_AXIS_START_ID();
106     theMinorAxis.second = SketchPlugin_EllipticArc::MINOR_AXIS_END_ID();
107   }
108 }
109
110 static void findDiameterOnEllipse(FeaturePtr theConstruction,
111                                   FeaturePtr theEllipse,
112                                   AttributePtr& theStart,
113                                   AttributePtr& theEnd)
114 {
115   AttributePtr anEllipseAttr;
116   const std::set<AttributePtr>& aRefs = theConstruction->data()->refsToMe();
117   for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
118        aRefIt != aRefs.end(); ++aRefIt) {
119     FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
120     if (anOwner && anOwner->getKind() == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
121       AttributeRefAttrPtr aRefAttr;
122       if ((*aRefIt)->id() == SketchPlugin_Constraint::ENTITY_A())
123         aRefAttr = anOwner->refattr(SketchPlugin_Constraint::ENTITY_B());
124       else
125         aRefAttr = anOwner->refattr(SketchPlugin_Constraint::ENTITY_A());
126       anEllipseAttr = aRefAttr->attr();
127       break;
128     }
129   }
130   if (!anEllipseAttr)
131     return;
132
133   std::pair<std::string, std::string> aMajorAxis, aMinorAxis;
134   ellipseDiameters(theEllipse, aMajorAxis, aMinorAxis);
135   if (anEllipseAttr->id() == aMajorAxis.first) {
136     theStart = anEllipseAttr;
137     theEnd = theEllipse->attribute(aMajorAxis.second);
138   }
139   else if (anEllipseAttr->id() == aMajorAxis.second) {
140     theStart = theEllipse->attribute(aMajorAxis.first);
141     theEnd = anEllipseAttr;
142   }
143   else if (anEllipseAttr->id() == aMinorAxis.first) {
144     theStart = anEllipseAttr;
145     theEnd = theEllipse->attribute(aMinorAxis.second);
146   }
147   else if (anEllipseAttr->id() == aMinorAxis.second) {
148     theStart = theEllipse->attribute(aMinorAxis.first);
149     theEnd = anEllipseAttr;
150   }
151 }
152
153 static void processEllipticArcExtremities(SketchSolver_ConstraintType& theType,
154                                           const ConstraintPtr& theConstraint,
155                                           const StoragePtr& theStorage,
156                                           std::vector<EntityWrapperPtr>& theAttributes,
157                                           EntityWrapperPtr theExtremities[2])
158 {
159   AttributePoint2DPtr aPointA, aPointB;
160   FeaturePtr anOwnerA, anOwnerB;
161   FeaturePtr aParentA, aParentB;
162   getPointOwnerAndParent(theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A()),
163                          aPointA, anOwnerA, aParentA);
164   getPointOwnerAndParent(theConstraint->refattr(SketchPlugin_Constraint::ENTITY_B()),
165                          aPointB, anOwnerB, aParentB);
166
167   AttributePtr anAxisStart, anAxisEnd, aPoint;
168   FeaturePtr aConstruction, anEllipticArc;
169   if (aParentA && aParentA == anOwnerB) {
170     aPoint = aPointB;
171     aConstruction = anOwnerA;
172     anEllipticArc = anOwnerB;
173   }
174   else if (aParentB && aParentB == anOwnerA) {
175     aPoint = aPointA;
176     aConstruction = anOwnerB;
177     anEllipticArc = anOwnerA;
178   }
179
180   if (!anEllipticArc || anEllipticArc->getKind() != SketchPlugin_EllipticArc::ID() ||
181       (aPoint->id() != SketchPlugin_EllipticArc::START_POINT_ID() &&
182        aPoint->id() != SketchPlugin_EllipticArc::END_POINT_ID()))
183     return;
184
185   findDiameterOnEllipse(aConstruction, anEllipticArc, anAxisStart, anAxisEnd);
186
187   if (anAxisStart && anAxisEnd) {
188     theAttributes[0] = theStorage->entity(aPoint);
189     theAttributes[1] = theStorage->entity(anAxisStart);
190     theAttributes[2] = theStorage->entity(anAxisEnd);
191     theType = CONSTRAINT_PT_ON_CURVE;
192     getCoincidentFeatureExtremities(theConstraint, theStorage, theExtremities);
193   }
194 }
195
196 static void getPointFromArray(EntityWrapperPtr& theArray,
197                               const ConstraintPtr& theConstraint,
198                               const std::string& theIndexAttrId)
199 {
200   if (theArray && theArray->type() == ENTITY_POINT_ARRAY) {
201     AttributeIntegerPtr anIndexAttr = theConstraint->integer(theIndexAttrId);
202     if (anIndexAttr) {
203       PointArrayWrapperPtr aPointsArray =
204           std::dynamic_pointer_cast<PlaneGCSSolver_PointArrayWrapper>(theArray);
205       theArray = aPointsArray->value(anIndexAttr->value());
206     }
207   }
208 }
209
210
211 void SketchSolver_ConstraintCoincidence::process()
212 {
213   cleanErrorMsg();
214   if (!myBaseConstraint || !myStorage) {
215     // Not enough parameters are assigned
216     return;
217   }
218
219   EntityWrapperPtr aValue;
220   std::vector<EntityWrapperPtr> anAttributes;
221   getAttributes(aValue, anAttributes);
222   if (!myErrorMsg.empty())
223     return;
224   if (anAttributes.empty()) {
225     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
226     return;
227   }
228
229   mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
230       myBaseConstraint, getType(),
231       myAuxValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
232
233   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
234   myStorage->notify(myBaseConstraint);
235 }
236
237 bool SketchSolver_ConstraintCoincidence::remove()
238 {
239   myInSolver = false;
240   myFeatureExtremities[0] = EntityWrapperPtr();
241   myFeatureExtremities[1] = EntityWrapperPtr();
242   if (myAuxValue) {
243     std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
244         std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
245     GCS::SET_pD aParams;
246     aParams.insert(myAuxValue->scalar());
247     aStorage->removeParameters(aParams);
248   }
249   return SketchSolver_Constraint::remove();
250 }
251
252 void SketchSolver_ConstraintCoincidence::getAttributes(
253     EntityWrapperPtr& theValue,
254     std::vector<EntityWrapperPtr>& theAttributes)
255 {
256   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
257   if (!myErrorMsg.empty() || !theAttributes[0]) {
258     theAttributes.clear();
259     return;
260   }
261
262   if (theAttributes[1]) {
263     myType = CONSTRAINT_PT_PT_COINCIDENT;
264     // if elliptic arc boundary point is connected with one of ellipse characteristics,
265     // it should be changed from point-point coincidence to coincidence between point
266     // and axis of the ellipse to decrease only 1 DoF instead of 2 DoF and avoid overconstraint.
267     processEllipticArcExtremities(myType, myBaseConstraint, myStorage,
268                                   theAttributes, myFeatureExtremities);
269   } else if (theAttributes[2]) {
270     myType = CONSTRAINT_PT_ON_CURVE;
271     // point-on-bspline requires additional parameter
272     if (theAttributes[2]->type() == ENTITY_BSPLINE) {
273       std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
274           std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
275       myAuxValue.reset(new PlaneGCSSolver_ScalarWrapper(aStorage->createParameter()));
276       // calculate the parameter of the point on B-spline nearest to the constrained point.
277       GeomPnt2dPtr aPoint = PlaneGCSSolver_Tools::point(theAttributes[0]);
278       std::shared_ptr<GeomAPI_BSpline2d> aSpline = PlaneGCSSolver_Tools::bspline(theAttributes[2]);
279       if (aPoint && aSpline)
280         aSpline->parameter(aPoint, 1.e100, *myAuxValue->scalar());
281     }
282     else {
283       // obtain extremity points of the coincident feature for further checking of multi-coincidence
284       getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
285     }
286   } else
287     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
288
289   // process internal coincidence with a point in the array of points
290   getPointFromArray(theAttributes[0], myBaseConstraint,
291                     SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_A());
292   getPointFromArray(theAttributes[1], myBaseConstraint,
293                     SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B());
294 }
295
296 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr&      theFeature,
297                                                 PlaneGCSSolver_Update* theUpdater)
298 {
299   PlaneGCSSolver_UpdateCoincidence* anUpdater =
300       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
301   bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
302   // additionally process internal coincidence, set point coincident with ellipse/elliptic arc
303   // for correct processing further coincidences set by the user
304   if (isAccepted &&
305       myBaseConstraint->getKind() == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
306     AttributeRefAttrPtr aRefAttrA = myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
307     AttributeRefAttrPtr aRefAttrB = myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
308     if (aRefAttrA && aRefAttrB) {
309       AttributePoint2DPtr anAttrA, anAttrB;
310       FeaturePtr anOwnerA, anOwnerB;
311       FeaturePtr aParentA, aParentB;
312       getPointOwnerAndParent(aRefAttrA, anAttrA, anOwnerA, aParentA);
313       getPointOwnerAndParent(aRefAttrB, anAttrB, anOwnerB, aParentB);
314
315       EntityWrapperPtr aPoint, anEntity;
316       if (aParentA == anOwnerB) {
317         aPoint = myStorage->entity(anAttrA);
318         anEntity = myStorage->entity(anOwnerB);
319       }
320       else if (aParentB == anOwnerA) {
321         aPoint = myStorage->entity(anAttrB);
322         anEntity = myStorage->entity(anOwnerA);
323       }
324       if (aPoint && anEntity)
325         anUpdater->addCoincidence(aPoint, anEntity);
326     }
327   }
328
329   // additionally check the point is coincident to extremity of coincident feature
330   if (myFeatureExtremities[0] && myFeatureExtremities[1]) {
331     EntityWrapperPtr aPoint =
332         myAttributes.front()->type() == ENTITY_POINT ? myAttributes.front() : myAttributes.back();
333
334     for (int i = 0; i < 2; ++i)
335       isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
336   }
337
338   if (isAccepted) {
339     if (!myInSolver) {
340       myInSolver = true;
341       myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
342     }
343   } else {
344     if (myInSolver) {
345       myInSolver = false;
346       myStorage->removeConstraint(myBaseConstraint);
347     }
348   }
349 }
350
351 void SketchSolver_ConstraintCoincidence::adjustConstraint()
352 {
353   if (myBaseConstraint->getKind() == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
354     AttributeIntegerPtr anIndexA = myBaseConstraint->integer(
355         SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_A());
356     AttributeIntegerPtr anIndexB = myBaseConstraint->integer(
357         SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B());
358     if ((anIndexA && anIndexA->isInitialized()) ||
359         (anIndexB && anIndexB->isInitialized())) {
360       remove();
361       process();
362     }
363   }
364 }