diff --git a/tests/js/zone-self-intersect.test.js b/tests/js/zone-self-intersect.test.js new file mode 100644 index 0000000000..71ab19e2d5 --- /dev/null +++ b/tests/js/zone-self-intersect.test.js @@ -0,0 +1,73 @@ +'use strict'; + +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const vm = require('vm'); + +const src = fs.readFileSync( + path.join(__dirname, '../../web/skins/classic/views/js/zone.js'), 'utf8'); + +let passed = 0; +let failed = 0; +function test(name, fn) { + try { + fn(); + console.log(' ok ' + name); + passed++; + } catch (e) { + console.error(' FAIL ' + name); + console.error(' ' + e.message); + failed++; + } +} + +function load(points) { + const alerts = []; + const chain = new Proxy(function() {}, { + get: (t, p) => (p === 'length' ? 0 : () => chain), + apply: () => chain, + }); + const ctx = { + console, Math, JSON, parseInt, parseFloat, Array, Object, + $j: chain, + alert: (m) => alerts.push(m), + window: {addEventListener() {}}, + document: {getElementById: () => null, querySelectorAll: () => []}, + selfIntersectingString: 'SELF', + }; + vm.createContext(ctx); + vm.runInContext(src, ctx); + ctx.zone = {Points: points}; + ctx.alerts = alerts; + return ctx; +} +const pts = (s) => s.split(' ').map((c) => ({x: c.split(',')[0], y: c.split(',')[1]})); +// An Inactive zone has no other fields to validate +const form = {elements: {'newZone[Type]': {value: 'Inactive'}}}; + +console.log('validateForm self intersection'); +test('a square is accepted', () => { + const ctx = load(pts('0,0 100,0 100,100 0,100')); + assert.strictEqual(ctx.validateForm(form), true); +}); +test('a bow tie is refused', () => { + const ctx = load(pts('0,0 100,100 100,0 0,100')); + assert.strictEqual(ctx.validateForm(form), false); + assert.deepStrictEqual(ctx.alerts, ['SELF']); +}); +test('a zone fixed after opening is accepted', () => { + const ctx = load(pts('0,0 100,100 100,0 0,100')); + ctx.zone.Points = pts('0,0 100,0 100,100 0,100'); + assert.strictEqual(ctx.validateForm(form), true); +}); +test('a concave zone and a midpoint on an edge are accepted', () => { + assert.strictEqual(load(pts('0,0 60,0 60,40 100,40 100,100 0,100')).validateForm(form), true); + assert.strictEqual(load(pts('0,0 50,0 100,0 100,100 0,100')).validateForm(form), true); +}); +test('a vertex touching another edge is refused', () => { + assert.strictEqual(load(pts('0,0 100,0 100,100 50,0 0,100')).validateForm(form), false); +}); + +console.log('\n' + passed + ' passed, ' + failed + ' failed'); +process.exit(failed ? 1 : 0); diff --git a/web/skins/classic/views/js/zone.js b/web/skins/classic/views/js/zone.js index cbb2f333c6..3f5dc1e2cf 100644 --- a/web/skins/classic/views/js/zone.js +++ b/web/skins/classic/views/js/zone.js @@ -13,9 +13,35 @@ var monitors = []; var analyse_frames = true; var TimerHideShow; +function segmentsIntersect(a, b, c, d) { + const cross = (p, q, r) => (q.x-p.x)*(r.y-p.y) - (q.y-p.y)*(r.x-p.x); + const within = (p, q, r) => Math.min(p.x, q.x) <= r.x && r.x <= Math.max(p.x, q.x) && + Math.min(p.y, q.y) <= r.y && r.y <= Math.max(p.y, q.y); + const d1 = cross(c, d, a); + const d2 = cross(c, d, b); + const d3 = cross(a, b, c); + const d4 = cross(a, b, d); + if ( ((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)) ) return true; + return (d1 == 0 && within(c, d, a)) || (d2 == 0 && within(c, d, b)) || + (d3 == 0 && within(a, b, c)) || (d4 == 0 && within(a, b, d)); +} + +/* The zone is edited in the page, so this has to look at the points as they are now */ +function pointsSelfIntersect(points) { + const n = points.length; + const pts = points.map((p) => ({x: parseFloat(p.x), y: parseFloat(p.y)})); + for ( let i = 0; i < n; i++ ) { + for ( let j = i+2; j < n; j++ ) { + if ( i == 0 && j == n-1 ) continue; // these edges share a vertex + if ( segmentsIntersect(pts[i], pts[(i+1)%n], pts[j], pts[(j+1)%n]) ) return true; + } + } + return false; +} + function validateForm( form ) { var errors = []; - if ( selfIntersecting ) { + if ( pointsSelfIntersect(zone['Points']) ) { errors[errors.length] = selfIntersectingString; } if ( form.elements['newZone[Type]'].value != 'Inactive' && form.elements['newZone[Type]'].value != 'Privacy' ) { diff --git a/web/skins/classic/views/js/zone.js.php b/web/skins/classic/views/js/zone.js.php index e180abec45..35282e605c 100644 --- a/web/skins/classic/views/js/zone.js.php +++ b/web/skins/classic/views/js/zone.js.php @@ -6,7 +6,6 @@ global $presets; global $zone; global $monitor; - global $selfIntersecting; global $streamMode; global $connkey; ?> @@ -83,8 +82,6 @@ monitorData[monitorData.length] = { 'janus_pin': 'Janus_Pin()) ?>' }; -var selfIntersecting = ; - var selfIntersectingString = ''; var alarmRGBUnsetString = ''; var minPixelThresUnsetString = '';