[Sketcher] Use std::ranges in SketchObject where possible

This commit is contained in:
Ajinkya Dahale
2025-04-01 17:12:47 +05:30
committed by Benjamin Nauck
parent 215fbc5187
commit 1cc0ca0a3b

View File

@@ -797,7 +797,7 @@ void SketchObject::generateId(const Part::Geometry* geo)
if (it != geoHistory->end()) {
// `find_if` avoids checking twice
auto iterOfId = std::find_if(it->begin(), it->end(), isNotInGeoMap);
auto iterOfId = std::ranges::find_if(*it, isNotInGeoMap);
if (iterOfId != it->end() && it2 == it) {
preReturn(*iterOfId);
return;
@@ -810,7 +810,7 @@ void SketchObject::generateId(const Part::Geometry* geo)
preReturn(++geoLastId);
return;
}
auto iterOfId = std::find_if(it->begin(), it->end(), isNotInGeoMap);
auto iterOfId = std::ranges::find_if(*it, isNotInGeoMap);
if (iterOfId != it->end()) {
preReturn(*iterOfId);
return;
@@ -830,7 +830,7 @@ void SketchObject::generateId(const Part::Geometry* geo)
// already some candidate exists, search for matching of both
// points
if (it2 != it) {
auto iterOfId = std::find_if(found.begin(), found.end(), isInIt2);
auto iterOfId = std::ranges::find_if(found, isInIt2);
if (iterOfId != found.end()) {
preReturn(*iterOfId);
return;
@@ -1356,9 +1356,8 @@ int SketchObject::diagnoseAdditionalConstraints(
std::vector<Sketcher::Constraint*> allconstraints;
allconstraints.reserve(objectconstraints.size() + additionalconstraints.size());
std::copy(objectconstraints.begin(), objectconstraints.end(), back_inserter(allconstraints));
std::copy(
additionalconstraints.begin(), additionalconstraints.end(), back_inserter(allconstraints));
std::ranges::copy(objectconstraints, back_inserter(allconstraints));
std::ranges::copy(additionalconstraints, back_inserter(allconstraints));
lastDoF =
solvedSketch.setUpSketch(getCompleteGeometry(), allconstraints, getExternalGeometryCount());
@@ -1953,7 +1952,7 @@ void SketchObject::replaceGeometries(std::vector<int> oldGeoIds,
auto& vals = getInternalGeometry();
auto newVals(vals);
if (std::any_of(oldGeoIds.begin(), oldGeoIds.end(), [](auto geoId) {
if (std::ranges::any_of(oldGeoIds, [](auto geoId) {
return geoId < 0;
})) {
THROWM(ValueError, "Cannot replace external geometries and axes.");
@@ -2377,7 +2376,7 @@ int SketchObject::delConstraintOnPoint(int geoId, PointPos posId, bool onlyCoinc
int replaceGeoId = GeoEnum::GeoUndef;
PointPos replacePosId = Sketcher::PointPos::none;
auto findReplacement = [geoId, posId, &replaceGeoId, &replacePosId, &vals]() {
auto it = std::find_if(vals.begin(), vals.end(), [geoId, posId](auto& constr) {
auto it = std::ranges::find_if(vals, [geoId, posId](auto& constr) {
return constr->Type == Sketcher::Coincident
&& constr->involvesGeoIdAndPosId(geoId, posId);
});
@@ -3185,7 +3184,7 @@ std::unique_ptr<Constraint> getNewConstraintAtTrimCut(const SketchObject* obj,
newConstr->SecondPos = PointPos::none;
}
return newConstr;
};
}
bool isGeoIdAllowedForTrim(const SketchObject* obj, int GeoId)
{
@@ -3495,8 +3494,7 @@ int SketchObject::trim(int GeoId, const Base::Vector3d& point)
// that happens, this causes redundant constraints, and in worse cases (incorrect)
// complaints of over-constraint and solver failures.
// if (std::none_of(newConstraints.begin(), newConstraints.end(), [](const auto& constr)
// {
// if (std::ranges::none_of(newConstraints, [](const auto& constr) {
// return constr->Type == ConstraintType::Equal;
// })) {
// constrainAsEqual(newIds.front(), newIds.back());
@@ -5938,7 +5936,7 @@ int SketchObject::exposeInternalGeometryForType<Part::GeomBSplineCurve>(const in
if (controlpointgeoids[0] != GeoEnum::GeoUndef) {
isfirstweightconstrained =
std::any_of(vals.begin(), vals.end(), [&controlpointgeoids](const auto& constr) {
std::ranges::any_of(vals, [&controlpointgeoids](const auto& constr) {
return (constr->Type == Sketcher::Weight && constr->First == controlpointgeoids[0]);
});
}
@@ -6551,6 +6549,7 @@ bool SketchObject::decreaseBSplineDegree(int GeoId, int degreedecrement /*= 1*/)
return true;
}
// clang-format on
bool SketchObject::modifyBSplineKnotMultiplicity(int GeoId, int knotIndex, int multiplicityincr)
{
// no need to check input data validity as this is an sketchobject managed operation.
@@ -6641,9 +6640,9 @@ bool SketchObject::modifyBSplineKnotMultiplicity(int GeoId, int knotIndex, int m
std::vector<double> knots = bsp->getKnots();
std::vector<double> newKnots = bspline->getKnots();
std::map<Sketcher::InternalAlignmentType, std::vector<int>>
indexInNew {{Sketcher::BSplineControlPoint, {}},
{Sketcher::BSplineKnotPoint, {}}};
std::map<Sketcher::InternalAlignmentType, std::vector<int>> indexInNew {
{Sketcher::BSplineControlPoint, {}},
{Sketcher::BSplineKnotPoint, {}}};
indexInNew[Sketcher::BSplineControlPoint].reserve(poles.size());
indexInNew[Sketcher::BSplineKnotPoint].reserve(knots.size());
@@ -6651,19 +6650,13 @@ bool SketchObject::modifyBSplineKnotMultiplicity(int GeoId, int knotIndex, int m
const auto it = std::ranges::find(newPoles, pole);
indexInNew[Sketcher::BSplineControlPoint].emplace_back(it - newPoles.begin());
}
std::replace(indexInNew[Sketcher::BSplineControlPoint].begin(),
indexInNew[Sketcher::BSplineControlPoint].end(),
int(newPoles.size()),
-1);
std::ranges::replace(indexInNew[Sketcher::BSplineControlPoint], int(newPoles.size()), -1);
for (const auto& knot : knots) {
const auto it = std::ranges::find(newKnots, knot);
indexInNew[Sketcher::BSplineKnotPoint].emplace_back(it - newKnots.begin());
}
std::replace(indexInNew[Sketcher::BSplineKnotPoint].begin(),
indexInNew[Sketcher::BSplineKnotPoint].end(),
int(newKnots.size()),
-1);
std::ranges::replace(indexInNew[Sketcher::BSplineKnotPoint], int(newKnots.size()), -1);
const std::vector<Sketcher::Constraint*>& cvals = Constraints.getValues();
@@ -6786,7 +6779,7 @@ bool SketchObject::insertBSplineKnot(int GeoId, double param, int multiplicity)
const auto it = std::ranges::find(newPoles, poles[j]);
poleIndexInNew[j] = it - newPoles.begin();
}
std::replace(poleIndexInNew.begin(), poleIndexInNew.end(), int(newPoles.size()), -1);
std::ranges::replace(poleIndexInNew, int(newPoles.size()), -1);
std::vector<double> knots = bsp->getKnots();
std::vector<double> newKnots = bspline->getKnots();
@@ -6796,7 +6789,7 @@ bool SketchObject::insertBSplineKnot(int GeoId, double param, int multiplicity)
const auto it = std::ranges::find(newKnots, knots[j]);
knotIndexInNew[j] = it - newKnots.begin();
}
std::replace(knotIndexInNew.begin(), knotIndexInNew.end(), int(newKnots.size()), -1);
std::ranges::replace(knotIndexInNew, int(newKnots.size()), -1);
const std::vector<Sketcher::Constraint*>& cvals = Constraints.getValues();
@@ -6870,6 +6863,7 @@ bool SketchObject::insertBSplineKnot(int GeoId, double param, int multiplicity)
return true;
}
// clang-format off
int SketchObject::carbonCopy(App::DocumentObject* pObj, bool construction)
{
@@ -9227,22 +9221,17 @@ const std::vector<std::map<int, Sketcher::PointPos>> SketchObject::getCoincidenc
int i = 0;
for (std::vector<std::map<int, Sketcher::PointPos>>::const_iterator iti =
coincidenttree.begin();
iti != coincidenttree.end();
++iti, ++i) {
for (auto iti = coincidenttree.begin(); iti != coincidenttree.end(); ++iti, ++i) {
// First
std::map<int, Sketcher::PointPos>::const_iterator filiterator;
filiterator = (*iti).find(constr->First);
if (filiterator != (*iti).end()) {
if (constr->FirstPos == (*filiterator).second)
firstpresentin = i;
if (filiterator != (*iti).end() && constr->FirstPos == (*filiterator).second) {
firstpresentin = i;
}
// Second
filiterator = (*iti).find(constr->Second);
if (filiterator != (*iti).end()) {
if (constr->SecondPos == (*filiterator).second)
secondpresentin = i;
if (filiterator != (*iti).end() && constr->SecondPos == (*filiterator).second) {
secondpresentin = i;
}
}
@@ -10444,7 +10433,6 @@ void SketchObject::migrateSketch()
if (noextensions) {
for (auto c : Constraints.getValues()) {
addGeometryState(c);
// Convert B-Spline controlpoints radius/diameter constraints to Weight constraints
@@ -10457,10 +10445,8 @@ void SketchObject::migrateSketch()
std::vector<double> weights = bsp->getWeights();
for (auto ccp : Constraints.getValues()) {
if ((ccp->Type == Radius || ccp->Type == Diameter)
&& ccp->First == circlegeoid) {
if (c->InternalAlignmentIndex < int(weights.size())) {
ccp->Type = Weight;
ccp->setValue(weights[c->InternalAlignmentIndex]);
@@ -10472,7 +10458,6 @@ void SketchObject::migrateSketch()
// Construction migration to extension
for (auto g : Geometry.getValues()) {
if (g->hasExtension(Part::GeometryMigrationExtension::getClassTypeId())) {
auto ext = std::static_pointer_cast<Part::GeometryMigrationExtension>(
g->getExtension(Part::GeometryMigrationExtension::getClassTypeId()).lock());
@@ -10483,8 +10468,7 @@ void SketchObject::migrateSketch()
bool oldconstr = ext->getConstruction();
if (g->is<Part::GeomPoint>()
&& !gf->isInternalAligned())
if (g->is<Part::GeomPoint>() && !gf->isInternalAligned())
oldconstr = true;
GeometryFacade::setConstruction(g, oldconstr);
@@ -10499,13 +10483,10 @@ void SketchObject::migrateSketch()
auto constraints = Constraints.getValues();
auto geometries = getInternalGeometry();
bool parabolaFound = std::any_of(geometries.begin(), geometries.end(), [](Part::Geometry* g) {
return g->is<Part::GeomArcOfParabola>();
});
bool parabolaFound = std::ranges::any_of(geometries, &Part::Geometry::is<Part::GeomArcOfParabola>);
if (parabolaFound) {
bool focalaxisfound = std::any_of(constraints.begin(), constraints.end(), [](auto c) {
bool focalaxisfound = std::ranges::any_of(constraints, [](auto& c) {
return c->Type == InternalAlignment && c->AlignmentType == ParabolaFocalAxis;
});
@@ -10561,9 +10542,7 @@ void SketchObject::migrateSketch()
newconstraints.push_back(c);
}
else {
auto axismajorcoincidentfound =
std::any_of(axisgeoid2parabolageoid.begin(),
axisgeoid2parabolageoid.end(),
auto axismajorcoincidentfound = std::ranges::any_of(axisgeoid2parabolageoid,
[&](const auto& pair) {
auto parabolageoid = pair.second;
auto axisgeoid = pair.first;
@@ -10580,8 +10559,7 @@ void SketchObject::migrateSketch()
}
auto focuscoincidentfound =
std::find_if(axisgeoid2parabolageoid.begin(),
axisgeoid2parabolageoid.end(),
std::ranges::find_if(axisgeoid2parabolageoid,
[&](const auto& pair) {
auto parabolageoid = pair.second;
auto axisgeoid = pair.first;