Skip to content

Commit 71ebd2d

Browse files
committed
tried to fix environmental shifting
1 parent 8f50108 commit 71ebd2d

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

test/unit/visual/visualTest.js

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export async function checkMatch(actual, expected, p5) {
135135
width,
136136
height,
137137
{
138-
threshold: 0.6,
138+
threshold: 0.35,
139139
includeAA: false,
140140
alpha: 0.1
141141
}
@@ -174,10 +174,15 @@ export async function checkMatch(actual, expected, p5) {
174174
// Define significance thresholds
175175
const MIN_CLUSTER_SIZE = 4; // Minimum pixels in a significant cluster
176176
const MAX_TOTAL_DIFF_PIXELS = 40; // Maximum total different pixels
177+
const MAX_LINE_SHIFT_PIXELS = 100;
177178

178179
// Determine if the differences are significant
179-
const significantClusters = clusterSizes.filter(size => size >= MIN_CLUSTER_SIZE);
180-
const significantDiffPixels = significantClusters.reduce((sum, size) => sum + size, 0);
180+
const lineShiftClusters = clusterSizes.filter(c => c.isLineShift && c.size > MIN_CLUSTER_SIZE);
181+
const nonLineShiftClusters = clusterSizes.filter(c => !c.isLineShift && c.size >= MIN_CLUSTER_SIZE);
182+
183+
// Calculate significant differences excluding line shifts
184+
const significantDiffPixels = nonLineShiftClusters.reduce((sum, c) => sum + c.size, 0);
185+
const lineShiftPixels = lineShiftClusters.reduce((sum, c) => sum + c.size, 0);
181186

182187
// Update the diff canvas
183188
diffCanvas.updatePixels();
@@ -188,12 +193,12 @@ export async function checkMatch(actual, expected, p5) {
188193

189194
// Determine test result
190195
const ok = (
191-
diffCount === 0 || // No differences at all
196+
diffCount === 0 ||
192197
(
193-
significantDiffPixels === 0 || // No significant clusters
198+
significantDiffPixels === 0 ||
194199
(
195-
significantDiffPixels <= MAX_TOTAL_DIFF_PIXELS && // Total different pixels within tolerance
196-
significantClusters.length <= 2 // Not too many significant clusters
200+
(significantDiffPixels <= MAX_TOTAL_DIFF_PIXELS) &&
201+
(nonLineShiftClusters.length <= 2) // Not too many significant clusters
197202
)
198203
)
199204
);
@@ -204,8 +209,7 @@ export async function checkMatch(actual, expected, p5) {
204209
details: {
205210
totalDiffPixels: diffCount,
206211
significantDiffPixels,
207-
clusters: clusterSizes,
208-
significantClusters
212+
clusters: clusterSizes
209213
}
210214
};
211215
}
@@ -216,6 +220,7 @@ export async function checkMatch(actual, expected, p5) {
216220
function findClusterSize(pixels, startX, startY, width, height, radius, visited) {
217221
const queue = [{x: startX, y: startY}];
218222
let size = 0;
223+
const clusterPixels = [];
219224

220225
while (queue.length > 0) {
221226
const {x, y} = queue.shift();
@@ -230,6 +235,7 @@ function findClusterSize(pixels, startX, startY, width, height, radius, visited)
230235
// Mark as visited
231236
visited.add(pos);
232237
size++;
238+
clusterPixels.push({x, y});
233239

234240
// Add neighbors to queue
235241
for (let dy = -radius; dy <= radius; dy++) {
@@ -248,8 +254,48 @@ function findClusterSize(pixels, startX, startY, width, height, radius, visited)
248254
}
249255
}
250256
}
257+
258+
let isLineShift = false;
259+
if (clusterPixels.length > 0) {
260+
// Count pixels with limited neighbors (line-like characteristic)
261+
let linelikePixels = 0;
262+
263+
for (const {x, y} of clusterPixels) {
264+
// Count neighbors
265+
let neighbors = 0;
266+
for (let dy = -1; dy <= 1; dy++) {
267+
for (let dx = -1; dx <= 1; dx++) {
268+
if (dx === 0 && dy === 0) continue; // Skip self
269+
270+
const nx = x + dx;
271+
const ny = y + dy;
272+
273+
// Skip if out of bounds
274+
if (nx < 0 || nx >= width || ny < 0 || ny >= height) continue;
275+
276+
const npos = (ny * width + nx) * 4;
277+
// Check if neighbor is a diff pixel
278+
if (pixels[npos] === 255 && pixels[npos + 1] === 0 && pixels[npos + 2] === 0) {
279+
neighbors++;
280+
}
281+
}
282+
}
283+
284+
// Line-like pixels typically have 1-2 neighbors
285+
if (neighbors <= 2) {
286+
linelikePixels++;
287+
}
288+
}
289+
290+
// If most pixels (>80%) in the cluster have ≤2 neighbors, it's likely a line shift
291+
isLineShift = linelikePixels / clusterPixels.length > 0.8;
292+
}
251293

252-
return size;
294+
return {
295+
size,
296+
pixels: clusterPixels,
297+
isLineShift
298+
};
253299
}
254300

255301
/**

0 commit comments

Comments
 (0)