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