[graphics/krita] libs/image: Fix restore-shape centroid test

Dmitry Kazakov <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 83dba8971f28ac7ceaf1560422913032c497ab6f by Dmitry Kazakov, on behalf of Moritz Staudinger.
Committed on 07/08/2026 at 13:58.
Pushed by dkazakov into branch 'master'.

Fix restore-shape centroid test

M  +4    -0    libs/image/kis_liquify_transform_worker.cpp
M  +31   -10   libs/image/tests/kis_liquify_transform_worker_test.cpp

https://invent.kde.org/graphics/krita/-/commit/83dba8971f28ac7ceaf1560422913032c497ab6f

diff --git a/libs/image/kis_liquify_transform_worker.cpp b/libs/image/kis_liquify_transform_worker.cpp
index c9b06084297..0bbbf8aedc6 100644
--- a/libs/image/kis_liquify_transform_worker.cpp
+++ b/libs/image/kis_liquify_transform_worker.cpp
@@ -265,6 +265,10 @@ bool isFinite(const Eigen::Matrix2d &m)
 
 }
 
+// A restore-shape dab determines its samples and Gaussian falloff from the
+// points' positions at the beginning of the dab. The centroid correction keeps
+// this fixed weighted sample set centered; subsequent dabs query the moved
+// points and calculate their falloff again.
 void KisLiquifyTransformWorker::restoreShapePoints(const QPointF &base,
                                                    qreal amount,
                                                    qreal sigma,
diff --git a/libs/image/tests/kis_liquify_transform_worker_test.cpp b/libs/image/tests/kis_liquify_transform_worker_test.cpp
index 179e05f78e3..c70069b0dd6 100644
--- a/libs/image/tests/kis_liquify_transform_worker_test.cpp
+++ b/libs/image/tests/kis_liquify_transform_worker_test.cpp
@@ -133,22 +133,41 @@ qreal averageDistance(const QVector<QPointF> &lhs, const QVector<QPointF> &rhs)
     return distance / lhs.size();
 }
 
-QPointF weightedTransformedCentroid(KisLiquifyTransformWorker *worker,
-                                    const QPointF &base,
-                                    qreal sigma)
+struct WeightedPointSample
+{
+    int index;
+    qreal weight;
+};
+
+QVector<WeightedPointSample> transformedPointSamples(KisLiquifyTransformWorker *worker,
+                                                      const QPointF &base,
+                                                      qreal sigma)
 {
     const qreal maxDist = 3.0 * sigma;
-    QPointF centroid;
-    qreal weightSum = 0.0;
+    QVector<WeightedPointSample> samples;
 
-    Q_FOREACH (const QPointF &pt, worker->transformedPoints()) {
+    for (int i = 0; i < worker->transformedPoints().size(); i++) {
+        const QPointF &pt = worker->transformedPoints()[i];
         const QPointF diff = pt - base;
         const qreal dist = KisAlgebra2D::norm(diff);
         if (dist > maxDist) continue;
 
         const qreal weight = exp(-0.5 * pow2(dist / sigma));
-        centroid += pt * weight;
-        weightSum += weight;
+        samples << WeightedPointSample{i, weight};
+    }
+
+    return samples;
+}
+
+QPointF weightedTransformedCentroid(KisLiquifyTransformWorker *worker,
+                                    const QVector<WeightedPointSample> &samples)
+{
+    QPointF centroid;
+    qreal weightSum = 0.0;
+
+    Q_FOREACH (const WeightedPointSample &sample, samples) {
+        centroid += worker->transformedPoints()[sample.index] * sample.weight;
+        weightSum += sample.weight;
     }
 
     return centroid / weightSum;
@@ -331,11 +350,13 @@ void KisLiquifyTransformWorkerTest::testRestoreShapePreservesCentroid()
     QScopedPointer<KisLiquifyTransformWorker> indexedWorker(
         cloneWorkerWithRebuiltSpatialIndex(worker));
 
+    const QVector<WeightedPointSample> samples =
+        transformedPointSamples(indexedWorker.data(), base, sigma);
     const QPointF beforeCentroid =
-        weightedTransformedCentroid(indexedWorker.data(), base, sigma);
+        weightedTransformedCentroid(indexedWorker.data(), samples);
     indexedWorker->restoreShapePoints(base, 1.0, sigma, false, false, false);
     const QPointF afterCentroid =
-        weightedTransformedCentroid(indexedWorker.data(), base, sigma);
+        weightedTransformedCentroid(indexedWorker.data(), samples);
 
     QVERIFY(kisDistance(beforeCentroid, afterCentroid) < 1e-4);
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.