Merge branch 'develop' into 'develop'

Optimized some algorithms

Refactoring of some algorithms, which should increase the performance of at least the ::Map method significantly. 

Labeled as Draft, because I didn't test the code apart from running the test cases. However, I believe the test cases don't cover these code sections, because the tests still pass, even if I manipulate the algorithms deliberately to fail.

See merge request smart-pattern/valentina!9
This commit is contained in:
Roman Telezhynskyi 2020-08-29 10:59:29 +00:00
commit 08e2ee59b3
2 changed files with 26 additions and 43 deletions

View file

@ -175,7 +175,7 @@ QVector<VSAPoint> PrepareAllowance(const QVector<QPointF> &points)
{ {
QVector<VSAPoint> allowancePoints; QVector<VSAPoint> allowancePoints;
allowancePoints.reserve(points.size()); allowancePoints.reserve(points.size());
for(auto point : points) for(auto &point : points)
{ {
allowancePoints.append(VSAPoint(point)); allowancePoints.append(VSAPoint(point));
} }
@ -206,8 +206,9 @@ QStringList PieceLabelText(const QVector<QPointF> &labelShape, const VTextManage
QStringList text; QStringList text;
if (labelShape.count() > 2) if (labelShape.count() > 2)
{ {
text.reserve(tm.GetSourceLinesCount()); int sourceCount = tm.GetSourceLinesCount();
for (int i = 0; i < tm.GetSourceLinesCount(); ++i) text.reserve(sourceCount);
for (int i = 0; i < sourceCount; ++i)
{ {
text.append(tm.GetSourceLine(i).m_qsText); text.append(tm.GetSourceLine(i).m_qsText);
} }
@ -219,9 +220,9 @@ QStringList PieceLabelText(const QVector<QPointF> &labelShape, const VTextManage
QVector<VLayoutPlaceLabel> ConvertPlaceLabels(const VPiece &piece, const VContainer *pattern) QVector<VLayoutPlaceLabel> ConvertPlaceLabels(const VPiece &piece, const VContainer *pattern)
{ {
QVector<VLayoutPlaceLabel> labels; QVector<VLayoutPlaceLabel> labels;
const QVector<quint32> placeLabels = piece.GetPlaceLabels(); const auto placeLabels = piece.GetPlaceLabels();
labels.reserve(placeLabels.size()); labels.reserve(placeLabels.size());
for(auto placeLabel : placeLabels) for(auto &placeLabel : placeLabels)
{ {
const auto label = pattern->GeometricObject<VPlaceLabelItem>(placeLabel); const auto label = pattern->GeometricObject<VPlaceLabelItem>(placeLabel);
if (label->IsVisible()) if (label->IsVisible())
@ -507,20 +508,11 @@ VLayoutPiece VLayoutPiece::Create(const VPiece &piece, vidtype id, const VContai
template <class T> template <class T>
QVector<T> VLayoutPiece::Map(QVector<T> points) const QVector<T> VLayoutPiece::Map(QVector<T> points) const
{ {
for (int i = 0; i < points.size(); ++i) std::transform(points.begin(), points.end(), points.begin(),
{ [this](const auto &point) { return d->matrix.map(point); });
points[i] = d->matrix.map(points.at(i));
}
if (d->mirror) if (d->mirror)
{ {
QList<T> list = ConvertToList(points); std::reverse(points.begin(), points.end());
for (int k=0, s=list.size(), max=(s/2); k<max; k++)
{
SwapItemsAt(list, k, s-(1+k));
}
points = ConvertToVector(list);
} }
return points; return points;
} }
@ -1407,30 +1399,20 @@ QLineF VLayoutPiece::Edge(const QVector<QPointF> &path, int i) const
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
// NOTE: Once C++17 is made mandatory, this method can further be refactored with std::optional<int>
int VLayoutPiece::EdgeByPoint(const QVector<QPointF> &path, const QPointF &p1) const int VLayoutPiece::EdgeByPoint(const QVector<QPointF> &path, const QPointF &p1) const
{ {
if (p1.isNull()) if (p1.isNull() || path.count() < 3)
{ {
return 0; return 0;
} }
if (path.count() < 3) const auto points = Map(path);
const auto posIter = std::find_if(points.cbegin(), points.cend(),
[&p1](const auto &point){ return VFuzzyComparePoints(point, p1); });
if (posIter != points.cend())
{ {
return 0; return static_cast<int>(posIter - points.cbegin() + 1);
}
const QVector<QPointF> points = Map(path);
for (int i=0; i < points.size(); i++)
{
if (VFuzzyComparePoints(points.at(i), p1))
{
int pos = i+1;
if (pos > points.size())
{
pos = 1;
}
return pos;
}
} }
return 0; // Did not find edge return 0; // Did not find edge
} }

View file

@ -148,15 +148,16 @@ inline QVector<T> ConvertToVector(const QSet<T> &container)
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template <typename T> // NOTE: Delete if not necessary anymore
inline void SwapItemsAt(T &container, int i, int j) //template <typename T>
{ //inline void SwapItemsAt(T &container, int i, int j)
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) //{
container.swapItemsAt(i, j); //#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
#else // container.swapItemsAt(i, j);
container.swap(i, j); //#else
#endif // container.swap(i, j);
} //#endif
//}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
template <typename T> template <typename T>