Salome HOME
Unit test for adding pole to B-spline curve
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_MacroBSpline.cpp
1 // Copyright (C) 2019-2020  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 <SketchPlugin_MacroBSpline.h>
21
22 #include <SketchPlugin_BSpline.h>
23 #include <SketchPlugin_BSplinePeriodic.h>
24 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Point.h>
27 #include <SketchPlugin_Tools.h>
28 #include <SketchPlugin_Sketch.h>
29
30 #include <ModelAPI_AttributeDoubleArray.h>
31 #include <ModelAPI_AttributeInteger.h>
32 #include <ModelAPI_AttributeRefAttrList.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_EventReentrantMessage.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Validator.h>
37
38 #include <GeomDataAPI_Point2DArray.h>
39
40 #include <GeomAlgoAPI_CompoundBuilder.h>
41 #include <GeomAlgoAPI_EdgeBuilder.h>
42 #include <GeomAlgoAPI_PointBuilder.h>
43
44 #include <GeomAPI_BSpline2d.h>
45
46 #include <sstream>
47
48 /// Create internal coincidence constraint with B-spline pole
49 static void createInternalConstraint(SketchPlugin_Sketch* theSketch,
50                                      AttributePtr thePoint,
51                                      AttributePtr theBSplinePoles,
52                                      const int thePoleIndex);
53
54
55 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline()
56   : SketchPlugin_SketchEntity(),
57     myDegree(3),
58     myIsPeriodic(false)
59 {
60 }
61
62 SketchPlugin_MacroBSpline::SketchPlugin_MacroBSpline(bool isPeriodic)
63   : SketchPlugin_SketchEntity(),
64     myDegree(3),
65     myIsPeriodic(isPeriodic)
66 {
67 }
68
69 void SketchPlugin_MacroBSpline::initAttributes()
70 {
71   data()->addAttribute(POLES_ID(), GeomDataAPI_Point2DArray::typeId());
72   data()->addAttribute(WEIGHTS_ID(), ModelAPI_AttributeDoubleArray::typeId());
73
74   data()->addAttribute(REF_POLES_ID(), ModelAPI_AttributeRefAttrList::typeId());
75   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), REF_POLES_ID());
76
77   data()->addAttribute(CONTROL_POLYGON_ID(), ModelAPI_AttributeBoolean::typeId());
78
79   data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
80 }
81
82 void SketchPlugin_MacroBSpline::execute()
83 {
84   FeaturePtr aBSpline = createBSplineFeature();
85
86   if (boolean(CONTROL_POLYGON_ID())->value()) {
87     std::list<FeaturePtr> aControlPoles;
88     createControlPolygon(aBSpline, aControlPoles);
89     constraintsForPoles(aControlPoles);
90
91     // message to init reentrant operation
92     static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
93     ReentrantMessagePtr aMessage(new ModelAPI_EventReentrantMessage(anId, this));
94     // set here the last pole to make coincidence with the start point of the next B-spline curve
95     aMessage->setCreatedFeature(aControlPoles.back());
96     Events_Loop::loop()->send(aMessage);
97   }
98 }
99
100 // LCOV_EXCL_START
101 std::string SketchPlugin_MacroBSpline::processEvent(
102                                               const std::shared_ptr<Events_Message>& theMessage)
103 {
104   ReentrantMessagePtr aReentrantMessage =
105       std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
106   if (aReentrantMessage) {
107     FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
108     ObjectPtr anObject = aReentrantMessage->selectedObject();
109     AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
110     std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
111
112     if (aClickedPoint) {
113       // fill points list (it consists of 2 points to make editable the second one)
114       AttributePoint2DArrayPtr aPointArrayAttr =
115           std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
116       aPointArrayAttr->setSize(2);
117       aPointArrayAttr->setPnt(0, aClickedPoint);
118       aPointArrayAttr->setPnt(1, aClickedPoint);
119
120       // fill weights
121       AttributeDoubleArrayPtr aWeightsArrayAttr = data()->realArray(WEIGHTS_ID());
122       aWeightsArrayAttr->setSize(2);
123       aWeightsArrayAttr->setValue(0, 1.0);
124       aWeightsArrayAttr->setValue(1, 1.0);
125
126       // fill reference attribute
127       AttributeRefAttrListPtr aRefAttrList =
128           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(attribute(REF_POLES_ID()));
129       if (anAttribute) {
130         if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
131           if (aCreatedFeature && anAttribute->id() == SketchPlugin_Point::COORD_ID())
132             anAttribute = aCreatedFeature->attribute(SketchPlugin_Point::COORD_ID());
133         }
134         aRefAttrList->append(anAttribute);
135       }
136     }
137     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
138   }
139   return std::string();
140 }
141 // LCOV_EXCL_STOP
142
143 FeaturePtr SketchPlugin_MacroBSpline::createBSplineFeature()
144 {
145   if (myKnots.empty() || myMultiplicities.empty())
146     getAISObject(AISObjectPtr()); // fill B-spline parameters
147
148   FeaturePtr aBSpline = sketch()->addFeature(
149       myIsPeriodic ? SketchPlugin_BSplinePeriodic::ID() : SketchPlugin_BSpline::ID());
150
151   aBSpline->integer(SketchPlugin_BSplineBase::DEGREE_ID())->setValue(myDegree);
152
153   AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
154       aBSpline->attribute(SketchPlugin_BSplineBase::POLES_ID()));
155   AttributePoint2DArrayPtr aPolesMacro =
156       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
157   aPoles->assign(aPolesMacro);
158
159   AttributeDoubleArrayPtr aWeights =
160       aBSpline->data()->realArray(SketchPlugin_BSplineBase::WEIGHTS_ID());
161   AttributeDoubleArrayPtr aWeightsMacro = data()->realArray(WEIGHTS_ID());
162   int aSize = aWeightsMacro->size();
163   aWeights->setSize(aSize);
164   for (int index = 0; index < aSize; ++index)
165     aWeights->setValue(index, aWeightsMacro->value(index));
166
167   AttributeDoubleArrayPtr aKnots =
168       aBSpline->data()->realArray(SketchPlugin_BSplineBase::KNOTS_ID());
169   aSize = (int)myKnots.size();
170   aKnots->setSize(aSize);
171   std::list<double>::iterator aKIt = myKnots.begin();
172   for (int index = 0; index < aSize; ++index, ++aKIt)
173     aKnots->setValue(index, *aKIt);
174
175   AttributeIntArrayPtr aMults = aBSpline->data()->intArray(SketchPlugin_BSplineBase::MULTS_ID());
176   aSize = (int)myMultiplicities.size();
177   aMults->setSize(aSize);
178   std::list<int>::iterator aMIt = myMultiplicities.begin();
179   for (int index = 0; index < aSize; ++index, ++aMIt)
180     aMults->setValue(index, *aMIt);
181
182   if (!myIsPeriodic) {
183     AttributePoint2DPtr aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184         aBSpline->attribute(SketchPlugin_BSpline::START_ID()));
185     aStartPoint->setValue(aPoles->pnt(0));
186
187     AttributePoint2DPtr aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188        aBSpline->attribute(SketchPlugin_BSpline::END_ID()));
189     aEndPoint->setValue(aPoles->pnt(aPoles->size() - 1));
190   }
191
192   aBSpline->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(
193       boolean(AUXILIARY_ID())->value());
194
195   aBSpline->execute();
196
197   return aBSpline;
198 }
199
200 void SketchPlugin_MacroBSpline::createControlPolygon(FeaturePtr theBSpline,
201                                                      std::list<FeaturePtr>& thePoles)
202 {
203   AttributePoint2DArrayPtr aPoles = std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(
204       theBSpline->attribute(SketchPlugin_BSpline::POLES_ID()));
205   int aSize = aPoles->size();
206   // poles
207   for (int index = 0; index < aSize; ++index)
208     thePoles.push_back(createAuxiliaryPole(aPoles, index));
209   // segments
210   for (int index = 1; index < aSize; ++index)
211     createAuxiliarySegment(aPoles, index - 1, index);
212   if (myIsPeriodic) {
213     // additional segment to close the control polygon
214     createAuxiliarySegment(aPoles, aSize - 1, 0);
215   }
216 }
217
218 void SketchPlugin_MacroBSpline::constraintsForPoles(const std::list<FeaturePtr>& thePoles)
219 {
220   AttributeRefAttrListPtr aRefAttrList = data()->refattrlist(REF_POLES_ID());
221   std::list<std::pair<ObjectPtr, AttributePtr> > aList;
222   if (aRefAttrList)
223     aList = aRefAttrList->list();
224
225   SketchPlugin_Sketch* aSketch = sketch();
226
227   std::list<std::pair<ObjectPtr, AttributePtr> >::iterator aLIt = aList.begin();
228   std::list<FeaturePtr>::const_iterator aPIt = thePoles.begin();
229   for (; aLIt != aList.end() && aPIt != thePoles.end(); ++aPIt, ++aLIt) {
230     // firstly, check the attribute (in this case the object will be not empty too)
231     if (aLIt->second) {
232       SketchPlugin_Tools::createConstraintAttrAttr(aSketch,
233           SketchPlugin_ConstraintCoincidence::ID(),
234           (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->second);
235     }
236     // now add coincidence with the result
237     else if (aLIt->first) {
238       SketchPlugin_Tools::createConstraintAttrObject(aSketch,
239           SketchPlugin_ConstraintCoincidence::ID(),
240           (*aPIt)->attribute(SketchPlugin_Point::COORD_ID()), aLIt->first);
241     }
242   }
243 }
244
245 AISObjectPtr SketchPlugin_MacroBSpline::getAISObject(AISObjectPtr thePrevious)
246 {
247   SketchPlugin_Sketch* aSketch = sketch();
248   if (!aSketch)
249     return AISObjectPtr();
250
251   AttributePoint2DArrayPtr aPolesArray =
252       std::dynamic_pointer_cast<GeomDataAPI_Point2DArray>(attribute(POLES_ID()));
253   AttributeDoubleArrayPtr aWeightsArray = data()->realArray(WEIGHTS_ID());
254
255   if (aPolesArray->size() < 2)
256     return AISObjectPtr();
257
258   std::list<GeomShapePtr> aShapes;
259
260   // convert poles to vertices and collect weights
261   std::list<GeomPnt2dPtr> aPoles2D;
262   std::list<double> aWeights;
263   for (int anIndex = 0; anIndex < aPolesArray->size(); ++anIndex) {
264     double aWeight = aWeightsArray->value(anIndex);
265     if (aWeight < 1.e-10)
266       continue; // skip poles with zero weights
267
268     aWeights.push_back(aWeight);
269
270     GeomPnt2dPtr aPole = aPolesArray->pnt(anIndex);
271     aPoles2D.push_back(aPole);
272     GeomPointPtr aPole3D = aSketch->to3D(aPole->x(), aPole->y());
273     aShapes.push_back(GeomAlgoAPI_PointBuilder::vertex(aPole3D));
274   }
275
276   // create result non-periodic B-spline curve
277   std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
278   try {
279     aBSplineCurve.reset(new GeomAPI_BSpline2d(aPoles2D, aWeights, myIsPeriodic));
280   } catch (...) {
281     // cannot build a B-spline curve
282     return AISObjectPtr();
283   }
284   GeomShapePtr anEdge =
285       GeomAlgoAPI_EdgeBuilder::bsplineOnPlane(aSketch->coordinatePlane(), aBSplineCurve);
286   if (!anEdge)
287     return AISObjectPtr();
288
289   // store transient parameters of B-spline curve
290   myDegree = aBSplineCurve->degree();
291   myKnots = aBSplineCurve->knots();
292   myMultiplicities = aBSplineCurve->mults();
293
294   aShapes.push_back(anEdge);
295   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
296
297   AISObjectPtr anAIS = thePrevious;
298   if (!anAIS)
299     anAIS.reset(new GeomAPI_AISObject());
300   anAIS->createShape(aCompound);
301
302   // Modify attributes
303   SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value());
304
305   return anAIS;
306 }
307
308
309
310 // ==========================     Auxiliary functions    ===========================================
311
312 void SketchPlugin_MacroBSpline::assignDefaultNameForAux(FeaturePtr theAuxFeature,
313                                                         AttributePoint2DArrayPtr theBSplinePoles,
314                                                         const int thePoleIndex1,
315                                                         const int thePoleIndex2)
316 {
317   FeaturePtr aBSpline = ModelAPI_Feature::feature(theBSplinePoles->owner());
318
319   std::ostringstream aName;
320   aName << aBSpline->name();
321   if (theAuxFeature->getKind() == SketchPlugin_Point::ID())
322     aName << "_" << theBSplinePoles->id() << "_" << thePoleIndex1;
323   else
324     aName << "_segment_" << thePoleIndex1 << "_" << thePoleIndex2;
325
326   theAuxFeature->data()->setName(aName.str());
327   theAuxFeature->lastResult()->data()->setName(aName.str());
328 }
329
330 FeaturePtr SketchPlugin_MacroBSpline::createAuxiliaryPole(AttributePoint2DArrayPtr theBSplinePoles,
331                                                           const int thePoleIndex)
332 {
333   FeaturePtr aBSpline = ModelAPI_Feature::feature(theBSplinePoles->owner());
334
335   SketchPlugin_Sketch* aSketch =
336       std::dynamic_pointer_cast<SketchPlugin_Feature>(aBSpline)->sketch();
337
338   // create child point equal to the B-spline's pole
339   FeaturePtr aPointFeature = aSketch->addFeature(SketchPlugin_Point::ID());
340   aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
341   aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(aBSpline);
342
343   GeomPnt2dPtr aPole = theBSplinePoles->pnt(thePoleIndex);
344
345   AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
346       aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
347   aCoord->setValue(aPole);
348
349   aPointFeature->execute();
350   assignDefaultNameForAux(aPointFeature, theBSplinePoles, thePoleIndex);
351
352   // internal constraint to keep position of the point
353   createInternalConstraint(aSketch, aCoord, theBSplinePoles, thePoleIndex);
354
355   return aPointFeature;
356 }
357
358 void SketchPlugin_MacroBSpline::createAuxiliarySegment(AttributePoint2DArrayPtr theBSplinePoles,
359                                                        const int thePoleIndex1,
360                                                        const int thePoleIndex2)
361 {
362   FeaturePtr aBSpline = ModelAPI_Feature::feature(theBSplinePoles->owner());
363
364   SketchPlugin_Sketch* aSketch =
365       std::dynamic_pointer_cast<SketchPlugin_Feature>(aBSpline)->sketch();
366
367   // create child segment between B-spline poles
368   FeaturePtr aLineFeature = aSketch->addFeature(SketchPlugin_Line::ID());
369   aLineFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true);
370   aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(aBSpline);
371
372   AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
373     aLineFeature->attribute(SketchPlugin_Line::START_ID()));
374   aLineStart->setValue(theBSplinePoles->pnt(thePoleIndex1));
375
376   AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
377     aLineFeature->attribute(SketchPlugin_Line::END_ID()));
378   aLineEnd->setValue(theBSplinePoles->pnt(thePoleIndex2));
379
380   aLineFeature->execute();
381   assignDefaultNameForAux(aLineFeature, theBSplinePoles, thePoleIndex1, thePoleIndex2);
382
383   // internal constraints to keep the segment position
384   createInternalConstraint(aSketch, aLineStart, theBSplinePoles, thePoleIndex1);
385   createInternalConstraint(aSketch, aLineEnd, theBSplinePoles, thePoleIndex2);
386 }
387
388 void createInternalConstraint(SketchPlugin_Sketch* theSketch,
389                               AttributePtr thePoint,
390                               AttributePtr theBSplinePoles,
391                               const int thePoleIndex)
392 {
393   std::shared_ptr<SketchPlugin_ConstraintCoincidenceInternal> aConstraint =
394       std::dynamic_pointer_cast<SketchPlugin_ConstraintCoincidenceInternal>(
395       theSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID()));
396   aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(thePoint);
397   aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(theBSplinePoles);
398   aConstraint->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B())
399       ->setValue(thePoleIndex);
400 }