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;
allowancePoints.reserve(points.size());
for(auto point : points)
for(auto &point : points)
{
allowancePoints.append(VSAPoint(point));
}
@ -206,8 +206,9 @@ QStringList PieceLabelText(const QVector<QPointF> &labelShape, const VTextManage
QStringList text;
if (labelShape.count() > 2)
{
text.reserve(tm.GetSourceLinesCount());
for (int i = 0; i < tm.GetSourceLinesCount(); ++i)
int sourceCount = tm.GetSourceLinesCount();
text.reserve(sourceCount);
for (int i = 0; i < sourceCount; ++i)
{
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> labels;
const QVector<quint32> placeLabels = piece.GetPlaceLabels();
const auto placeLabels = piece.GetPlaceLabels();
labels.reserve(placeLabels.size());
for(auto placeLabel : placeLabels)
for(auto &placeLabel : placeLabels)
{
const auto label = pattern->GeometricObject<VPlaceLabelItem>(placeLabel);
if (label->IsVisible())
@ -507,20 +508,11 @@ VLayoutPiece VLayoutPiece::Create(const VPiece &piece, vidtype id, const VContai
template <class T>
QVector<T> VLayoutPiece::Map(QVector<T> points) const
{
for (int i = 0; i < points.size(); ++i)
{
points[i] = d->matrix.map(points.at(i));
}
std::transform(points.begin(), points.end(), points.begin(),
[this](const auto &point) { return d->matrix.map(point); });
if (d->mirror)
{
QList<T> list = ConvertToList(points);
for (int k=0, s=list.size(), max=(s/2); k<max; k++)
{
SwapItemsAt(list, k, s-(1+k));
}
points = ConvertToVector(list);
std::reverse(points.begin(), points.end());
}
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
{
if (p1.isNull())
if (p1.isNull() || path.count() < 3)
{
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;
}
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 static_cast<int>(posIter - points.cbegin() + 1);
}
return 0; // Did not find edge
}

View File

@ -148,15 +148,16 @@ inline QVector<T> ConvertToVector(const QSet<T> &container)
}
//---------------------------------------------------------------------------------------------------------------------
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);
#else
container.swap(i, j);
#endif
}
// NOTE: Delete if not necessary anymore
//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);
//#else
// container.swap(i, j);
//#endif
//}
//---------------------------------------------------------------------------------------------------------------------
template <typename T>