mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
fix: check the current points for self intersection when saving a zone refs #5790
This commit is contained in:
1 parent
df46009630
commit
e6514ecd0a
3 files changed
+100
-4
No files matched your search
@@ -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);
|
||||
@@ -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' ) {
|
||||
|
||||
@@ -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': '<?php echo validJsStr($monitor->Janus_Pin()) ?>'
|
||||
};
|
||||
|
||||
var selfIntersecting = <?php echo $selfIntersecting ? 'true' : 'false' ?>;
|
||||
|
||||
var selfIntersectingString = '<?php echo addslashes(translate('SelfIntersecting')) ?>';
|
||||
var alarmRGBUnsetString = '<?php echo addslashes(translate('AlarmRGBUnset')) ?>';
|
||||
var minPixelThresUnsetString = '<?php echo addslashes(translate('MinPixelThresUnset')) ?>';
|
||||
|
||||
Reference in new issue
Block a user