1 // Copyright (C) 2019-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchAPI_BSpline.h"
22 #include <GeomAPI_BSpline2d.h>
23 #include <GeomAPI_Pnt2d.h>
25 #include <GeomAlgoAPI_EdgeBuilder.h>
27 #include <ModelHighAPI_Double.h>
28 #include <ModelHighAPI_Dumper.h>
29 #include <ModelHighAPI_Integer.h>
30 #include <ModelHighAPI_Selection.h>
31 #include <ModelHighAPI_Tools.h>
33 #include <ModelAPI_Tools.h>
35 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
36 #include <SketchPlugin_Line.h>
37 #include <SketchPlugin_Point.h>
42 SketchAPI_BSpline::SketchAPI_BSpline(const std::shared_ptr<ModelAPI_Feature> & theFeature)
43 : SketchAPI_SketchEntity(theFeature)
48 SketchAPI_BSpline::SketchAPI_BSpline(const std::shared_ptr<ModelAPI_Feature>& theFeature,
50 : SketchAPI_SketchEntity(theFeature)
56 SketchAPI_BSpline::~SketchAPI_BSpline()
60 void SketchAPI_BSpline::setByDegreePolesAndWeights(const ModelHighAPI_Integer& theDegree,
61 const std::list<GeomPnt2dPtr>& thePoles,
62 const std::list<ModelHighAPI_Double>& theWeights)
64 std::list<ModelHighAPI_Double> aWeights;
65 if (theWeights.size() <= 1) {
66 // prepare array of equal weights
67 aWeights.assign(thePoles.size(),
68 theWeights.empty() ? ModelHighAPI_Double(1.0) : theWeights.front());
71 aWeights = theWeights;
73 ModelHighAPI_Integer aDegree = theDegree;
74 std::list<ModelHighAPI_Double> aKnots;
75 std::list<ModelHighAPI_Integer> aMults;
76 getDefaultParameters(thePoles, aWeights, aDegree, aKnots, aMults);
78 setByParameters(aDegree, thePoles, aWeights, aKnots, aMults);
81 void SketchAPI_BSpline::setByParameters(const ModelHighAPI_Integer& theDegree,
82 const std::list<GeomPnt2dPtr>& thePoles,
83 const std::list<ModelHighAPI_Double>& theWeights,
84 const std::list<ModelHighAPI_Double>& theKnots,
85 const std::list<ModelHighAPI_Integer>& theMults)
87 fillAttribute(theDegree, degree());
89 fillAttribute(thePoles, poles());
90 if (theWeights.size() <= 1) {
91 // prepare array of equal weights
92 std::list<ModelHighAPI_Double> aWeights(thePoles.size(),
93 theWeights.empty() ? ModelHighAPI_Double(1.0) : theWeights.front());
94 fillAttribute(aWeights, weights());
97 fillAttribute(theWeights, weights());
99 fillAttribute(theKnots, knots());
100 fillAttribute(theMults, multiplicities());
102 if (feature()->getKind() != SketchPlugin_BSplinePeriodic::ID())
103 setStartAndEndPoints();
107 void SketchAPI_BSpline::setStartAndEndPoints()
109 fillAttribute(poles()->pnt(0), startPoint());
110 fillAttribute(poles()->pnt(poles()->size() - 1), endPoint());
113 void SketchAPI_BSpline::setByExternal(const ModelHighAPI_Selection & theExternal)
115 fillAttribute(theExternal, external());
119 static CompositeFeaturePtr sketchForFeature(FeaturePtr theFeature)
121 const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
122 for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt)
123 if ((*anIt)->id() == SketchPlugin_Sketch::FEATURES_ID())
124 return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
125 return CompositeFeaturePtr();
128 static void createInternalConstraint(const CompositeFeaturePtr& theSketch,
129 const AttributePoint2DPtr& thePoint,
130 const AttributePoint2DArrayPtr& thePoles,
131 const int thePoleIndex)
133 FeaturePtr aConstraint = theSketch->addFeature(SketchPlugin_ConstraintCoincidenceInternal::ID());
134 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A())->setAttr(thePoint);
135 aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B())->setAttr(thePoles);
136 aConstraint->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B())
137 ->setValue(thePoleIndex);
138 aConstraint->execute();
141 static void createPole(const CompositeFeaturePtr& theSketch,
142 const FeaturePtr& theBSpline,
143 const AttributePoint2DArrayPtr& thePoles,
144 const int thePoleIndex,
145 const bool theAuxiliary,
146 std::list<FeaturePtr>& theEntities)
148 GeomPnt2dPtr aPole = thePoles->pnt(thePoleIndex);
150 FeaturePtr aPointFeature = theSketch->addFeature(SketchPlugin_Point::ID());
151 AttributePoint2DPtr aCoord = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
152 aPointFeature->attribute(SketchPlugin_Point::COORD_ID()));
153 aCoord->setValue(aPole);
154 aPointFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
155 aPointFeature->execute();
157 std::wostringstream aName;
158 aName << theBSpline->name() << "_" << ModelAPI_Tools::toWString(thePoles->id()) << "_" << thePoleIndex;
159 aPointFeature->data()->setName(aName.str());
160 aPointFeature->lastResult()->data()->setName(aName.str());
162 aPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(theAuxiliary);
164 createInternalConstraint(theSketch, aCoord, thePoles, thePoleIndex);
166 theEntities.push_back(aPointFeature);
169 static void createSegment(const CompositeFeaturePtr& theSketch,
170 const FeaturePtr& theBSpline,
171 const AttributePoint2DArrayPtr& thePoles,
172 const int theStartPoleIndex,
173 const bool theAuxiliary,
174 std::list<FeaturePtr>& theEntities)
176 int aEndPoleIndex = (theStartPoleIndex + 1) % thePoles->size();
177 GeomPnt2dPtr aStartPoint = thePoles->pnt(theStartPoleIndex);
178 GeomPnt2dPtr aEndPoint = thePoles->pnt(aEndPoleIndex);
180 FeaturePtr aLineFeature = theSketch->addFeature(SketchPlugin_Line::ID());
181 AttributePoint2DPtr aLineStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
182 aLineFeature->attribute(SketchPlugin_Line::START_ID()));
183 aLineStart->setValue(aStartPoint);
184 AttributePoint2DPtr aLineEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185 aLineFeature->attribute(SketchPlugin_Line::END_ID()));
186 aLineEnd->setValue(aEndPoint);
187 aLineFeature->reference(SketchPlugin_Point::PARENT_ID())->setValue(theBSpline);
188 aLineFeature->execute();
190 std::wostringstream aName;
191 aName << theBSpline->name() << "_segment_" << theStartPoleIndex << "_" << aEndPoleIndex;
192 aLineFeature->data()->setName(aName.str());
193 aLineFeature->lastResult()->data()->setName(aName.str());
195 aLineFeature->boolean(SketchPlugin_Line::AUXILIARY_ID())->setValue(theAuxiliary);
197 createInternalConstraint(theSketch, aLineStart, thePoles, theStartPoleIndex);
198 createInternalConstraint(theSketch, aLineEnd, thePoles, aEndPoleIndex);
200 theEntities.push_back(aLineFeature);
203 static void toMapOfAuxIndices(const std::list<int>& theRegular,
204 const std::list<int>& theAuxiliary,
205 std::map<int, bool>& theIndices)
207 for (auto it = theRegular.begin(); it != theRegular.end(); ++it)
208 theIndices[*it] = false;
209 for (auto it = theAuxiliary.begin(); it != theAuxiliary.end(); ++it)
210 theIndices[*it] = true;
213 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_BSpline::controlPoles(
214 const std::list<int>& regular,
215 const std::list<int>& auxiliary) const
217 std::map<int, bool> anAux;
218 toMapOfAuxIndices(regular, auxiliary, anAux);
220 std::list<FeaturePtr> anEntities;
222 FeaturePtr aBSpline = feature();
223 CompositeFeaturePtr aSketch = sketchForFeature(aBSpline);
224 AttributePoint2DArrayPtr aPoles = poles();
226 for (auto it = anAux.begin(); it != anAux.end(); ++it)
227 createPole(aSketch, aBSpline, aPoles, it->first, it->second, anEntities);
229 return SketchAPI_SketchEntity::wrap(anEntities);
232 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_BSpline::controlPolygon(
233 const std::list<int>& regular,
234 const std::list<int>& auxiliary) const
236 std::map<int, bool> anAux;
237 toMapOfAuxIndices(regular, auxiliary, anAux);
239 std::list<FeaturePtr> anEntities;
241 FeaturePtr aBSpline = feature();
242 CompositeFeaturePtr aSketch = sketchForFeature(aBSpline);
243 AttributePoint2DArrayPtr aPoles = poles();
245 for (auto it = anAux.begin(); it != anAux.end(); ++it)
246 createSegment(aSketch, aBSpline, aPoles, it->first, it->second, anEntities);
248 return SketchAPI_SketchEntity::wrap(anEntities);
252 void SketchAPI_BSpline::getDefaultParameters(
253 const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
254 const std::list<ModelHighAPI_Double>& theWeights,
255 ModelHighAPI_Integer& theDegree,
256 std::list<ModelHighAPI_Double>& theKnots,
257 std::list<ModelHighAPI_Integer>& theMults) const
259 std::shared_ptr<GeomAPI_BSpline2d> aBSplineCurve;
261 std::list<double> aWeights;
262 for (std::list<ModelHighAPI_Double>::const_iterator it = theWeights.begin();
263 it != theWeights.end(); ++it)
264 aWeights.push_back(it->value());
266 bool isPeriodic = feature()->getKind() == SketchPlugin_BSplinePeriodic::ID();
267 if (theDegree.intValue() < 0)
268 aBSplineCurve.reset(new GeomAPI_BSpline2d(thePoles, aWeights, isPeriodic));
270 aBSplineCurve.reset(new GeomAPI_BSpline2d(theDegree.intValue(), thePoles, aWeights,
271 std::list<double>(), std::list<int>(), isPeriodic));
275 // cannot build a B-spline curve
279 theDegree = aBSplineCurve->degree();
280 std::list<double> aKnots = aBSplineCurve->knots();
281 std::list<int> aMults = aBSplineCurve->mults();
282 theKnots.assign(aKnots.begin(), aKnots.end());
283 theMults.assign(aMults.begin(), aMults.end());
286 void SketchAPI_BSpline::checkDefaultParameters(bool& isDefaultDegree,
287 bool& isDefaultWeights,
288 bool& isDefaultKnotsMults) const
290 static const double TOLERANCE = 1.e-7;
292 AttributePoint2DArrayPtr aPolesAttr = poles();
293 AttributeDoubleArrayPtr aWeightsAttr = weights();
294 AttributeDoubleArrayPtr aKnotsAttr = knots();
295 AttributeIntArrayPtr aMultsAttr = multiplicities();
297 std::list<GeomPnt2dPtr> aPoles;
298 std::list<ModelHighAPI_Double> aWeights;
299 isDefaultWeights = true;
300 for (int anIndex = 0; anIndex < aPolesAttr->size(); ++anIndex) {
301 aPoles.push_back(aPolesAttr->pnt(anIndex));
302 double aCurWeight = aWeightsAttr->value(anIndex);
303 isDefaultWeights = isDefaultWeights && fabs(aCurWeight - 1.0) < TOLERANCE;
304 aWeights.push_back(aCurWeight);
307 ModelHighAPI_Integer aDegree(-1);
308 std::list<ModelHighAPI_Double> aKnots;
309 std::list<ModelHighAPI_Integer> aMults;
310 getDefaultParameters(aPoles, aWeights, aDegree, aKnots, aMults);
311 isDefaultDegree = aDegree.intValue() == degree()->value();
312 if (!isDefaultDegree) {
313 // recalculate knots and multiplicities with the actual degree
314 aDegree = degree()->value();
315 getDefaultParameters(aPoles, aWeights, aDegree, aKnots, aMults);
318 isDefaultKnotsMults = aKnotsAttr->size() == (int)aKnots.size()
319 && aMultsAttr->size() == (int)aMults.size();
320 if (isDefaultKnotsMults) {
321 std::list<ModelHighAPI_Double>::iterator anIt = aKnots.begin();
322 for (int anIndex = 0; isDefaultKnotsMults && anIt != aKnots.end(); ++anIt, ++anIndex)
323 isDefaultKnotsMults = fabs(anIt->value() - aKnotsAttr->value(anIndex)) < TOLERANCE;
325 if (isDefaultKnotsMults) {
326 std::list<ModelHighAPI_Integer>::iterator anIt = aMults.begin();
327 for (int anIndex = 0; isDefaultKnotsMults && anIt != aMults.end(); ++anIt, ++anIndex)
328 isDefaultKnotsMults = anIt->intValue() == aMultsAttr->value(anIndex);
331 isDefaultDegree = isDefaultDegree && isDefaultKnotsMults;
332 isDefaultWeights = isDefaultWeights && isDefaultKnotsMults;
336 static void bsplineAuxiliaryFeature(const AttributeRefAttrPtr& theReference,
337 FeaturePtr& thePoint,
338 FeaturePtr& theSegment)
340 ObjectPtr anAuxObject;
341 if (theReference->isObject())
342 anAuxObject = theReference->object();
344 anAuxObject = theReference->attr()->owner();
346 FeaturePtr anAuxFeature = ModelAPI_Feature::feature(anAuxObject);
347 if (anAuxFeature->getKind() == SketchPlugin_Point::ID())
348 thePoint = anAuxFeature;
349 else if (anAuxFeature->getKind() == SketchPlugin_Line::ID() &&
350 theReference->attr()->id() == SketchPlugin_Line::START_ID()) {
351 // process only coincidence with start point
352 theSegment = anAuxFeature;
356 static void collectAuxiliaryFeatures(FeaturePtr theBSpline,
357 std::map<int, FeaturePtr>& thePoints,
358 std::map<int, FeaturePtr>& theSegments)
360 const std::set<AttributePtr>& aRefs = theBSpline->data()->refsToMe();
361 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
362 aRefIt != aRefs.end(); ++aRefIt) {
363 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
364 if (anOwner->getKind() == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
365 // process internal constraints only
366 AttributeRefAttrPtr aRefAttrA = anOwner->refattr(SketchPlugin_Constraint::ENTITY_A());
367 AttributeRefAttrPtr aRefAttrB = anOwner->refattr(SketchPlugin_Constraint::ENTITY_B());
368 AttributePtr anAttrA = aRefAttrA->attr();
369 AttributePtr anAttrB = aRefAttrB->attr();
371 AttributeIntegerPtr aPoleIndex;
372 FeaturePtr aPoint, aLine;
373 if (anAttrA && anAttrA->attributeType() == GeomDataAPI_Point2DArray::typeId()) {
374 aPoleIndex = anOwner->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_A());
375 bsplineAuxiliaryFeature(aRefAttrB, aPoint, aLine);
377 else if (anAttrB && anAttrB->attributeType() == GeomDataAPI_Point2DArray::typeId()) {
378 aPoleIndex = anOwner->integer(SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B());
379 bsplineAuxiliaryFeature(aRefAttrA, aPoint, aLine);
383 thePoints[aPoleIndex->value()] = aPoint;
385 theSegments[aPoleIndex->value()] = aLine;
390 void SketchAPI_BSpline::dump(ModelHighAPI_Dumper& theDumper) const
393 return; // no need to dump copied feature
395 FeaturePtr aBase = feature();
396 const std::string& aSketchName = theDumper.parentName(aBase);
398 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
399 if (anExternal->context()) {
400 // B-spline is external
401 theDumper << aBase << " = " << aSketchName << ".addSpline(" << anExternal << ")" << std::endl;
403 // check if some B-spline parameters are default and should not be dumped
404 bool isDefaultDegree, isDefaultWeights, isDefaultKnotsMults;
405 checkDefaultParameters(isDefaultDegree, isDefaultWeights, isDefaultKnotsMults);
407 theDumper << aBase << " = " << aSketchName << ".addSpline(";
408 if (!isDefaultDegree)
409 theDumper << "degree = " << degree() << ", ";
410 theDumper << "poles = " << poles();
411 if (!isDefaultWeights)
412 theDumper << ", weights = " << weights();
413 if (!isDefaultKnotsMults)
414 theDumper << ", knots = " << knots() << ", multiplicities = " << multiplicities();
415 if (aBase->getKind() == SketchPlugin_BSplinePeriodic::ID())
416 theDumper << ", periodic = True";
417 theDumper << ")" << std::endl;
419 // dump "auxiliary" flag if necessary
420 SketchAPI_SketchEntity::dump(theDumper);
422 // dump control polygon
423 std::map<int, FeaturePtr> anAuxPoles, anAuxSegments;
424 collectAuxiliaryFeatures(aBase, anAuxPoles, anAuxSegments);
426 if (!anAuxPoles.empty())
427 dumpControlPolygon(theDumper, aBase, "controlPoles", anAuxPoles);
428 if (!anAuxSegments.empty())
429 dumpControlPolygon(theDumper, aBase, "controlPolygon", anAuxSegments);
432 static void dumpList(ModelHighAPI_Dumper& theDumper,
433 const std::string& theAttrName,
434 const std::set<int>& theIndices)
436 theDumper << theAttrName << " = [";
437 std::set<int>::const_iterator it = theIndices.begin();
439 for (++it; it != theIndices.end(); ++it)
440 theDumper << ", " << *it;
444 void SketchAPI_BSpline::dumpControlPolygon(
445 ModelHighAPI_Dumper& theDumper,
446 const FeaturePtr& theBSpline,
447 const std::string& theMethod,
448 const std::map<int, FeaturePtr>& theAuxFeatures) const
452 // dump features and split them to auxiliary and regular
453 std::set<int> aRegular, anAuxiliary;
454 for (std::map<int, FeaturePtr>::const_iterator it = theAuxFeatures.begin();
455 it != theAuxFeatures.end(); ++it) {
458 theDumper << theDumper.name(it->second, false);
459 theDumper.doNotDumpFeature(it->second);
462 if (it->second->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
463 anAuxiliary.insert(it->first);
465 aRegular.insert(it->first);
467 theDumper << "] = " << theDumper.name(theBSpline) << "." << theMethod << "(";
468 if (!aRegular.empty()) {
469 dumpList(theDumper, "regular", aRegular);
470 if (!anAuxiliary.empty())
473 if (!anAuxiliary.empty())
474 dumpList(theDumper, "auxiliary", anAuxiliary);
475 theDumper << ")" << std::endl;
478 static void setCoordinates(const FeaturePtr& theFeature,
479 const std::string& theAttrName,
480 const GeomPnt2dPtr& theCoordinates)
482 AttributePoint2DPtr aPoint =
483 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->attribute(theAttrName));
484 aPoint->setValue(theCoordinates);
487 bool SketchAPI_BSpline::insertPole(const int theIndex,
488 const GeomPnt2dPtr& theCoordinates,
489 const ModelHighAPI_Double& theWeight)
491 std::ostringstream anActionName;
492 anActionName << SketchPlugin_BSplineBase::ADD_POLE_ACTION_ID() << "#" << theIndex;
493 bool isOk = feature()->customAction(anActionName.str());
495 int anIndex = theIndex + 1;
496 if (feature()->getKind() == SketchPlugin_BSpline::ID() && anIndex + 1 >= poles()->size())
497 anIndex = poles()->size() - 2;
498 // initialize coordinates and weight of new pole
499 poles()->setPnt(anIndex, theCoordinates);
500 weights()->setValue(anIndex, theWeight.value());
502 // update coordinates of points of control polygon
503 std::map<int, FeaturePtr> aPoints, aLines;
504 collectAuxiliaryFeatures(feature(), aPoints, aLines);
505 std::map<int, FeaturePtr>::iterator aFound = aPoints.find(anIndex);
506 if (aFound != aPoints.end())
507 setCoordinates(aFound->second, SketchPlugin_Point::COORD_ID(), theCoordinates);
508 aFound = aLines.find(anIndex);
509 if (aFound != aLines.end())
510 setCoordinates(aFound->second, SketchPlugin_Line::START_ID(), theCoordinates);
511 aFound = aLines.find(anIndex - 1);
512 if (aFound != aLines.end())
513 setCoordinates(aFound->second, SketchPlugin_Line::END_ID(), theCoordinates);
520 // =================================================================================================
521 SketchAPI_BSplinePeriodic::SketchAPI_BSplinePeriodic(const FeaturePtr& theFeature)
522 : SketchAPI_BSpline(theFeature, false)