PlotDirector/PlotLine.Tests/Browser/maps-topology-engine.cjs

32 lines
8.2 KiB
JavaScript

const assert=require('node:assert/strict'),E=require('../../PlotLine/wwwroot/js/maps-engine.js'),T=require('../../PlotLine/wwwroot/js/maps-topology-engine.js');
const area=(id,x,y,w=200,h=200)=>({...E.makeFeature('County',x,y,id),geometryJson:JSON.stringify({points:[[-.5,-.5],[.5,-.5],[.5,.5],[-.5,.5]],coast:'Natural'}),width:w,height:h,isProvisional:false});
let fs=[area('A',0,0),area('B',200,0)],before=JSON.stringify(fs);
fs=T.join(fs,{areaID:'B',neighborID:'A',start:3,end:0,sourceDirection:'forward',neighborDirection:'backward'});
assert.equal(JSON.stringify([area('A',0,0),area('B',200,0)]),before);let c=T.context(fs),edge=[...c.edges.keys()][0];assert.equal(c.edges.size,1);assert.equal(c.users(edge).length,2);
const a=T.paths(fs[0],c)[1],b=T.paths(fs[1],c).find(cs=>Math.abs(cs[0][0][0]-100)<1e-5&&Math.abs(cs.at(-1)[3][0]-100)<1e-5);assert(b);assert.deepEqual(a.map(q=>q.map(p=>p)),[...b].reverse().map(q=>[...q].reverse()));
let vi=c.area('B').vertexRefs.findIndex(Boolean);fs=T.movePoint(fs,'B',vi,[110,100]);c=T.context(fs);assert(T.controls(fs[0],c).some(p=>p[0]===110&&p[1]===100));
const index=c.area('A').edgeRefs.indexOf(edge);fs=T.insert(fs,'A',index,0,.5);c=T.context(fs);assert.equal(c.edges.size,2);assert.equal(c.area('A').points.length,5);assert.equal(c.area('B').points.length,5);
const inserted=c.area('A').vertexRefs.findIndex(v=>v&&c.users(v,'vertex').length===2&&c.vertices.get(v)[1]>-90&&c.vertices.get(v)[1]<90);assert(inserted>=0);fs=T.removePoint(fs,'A',inserted);c=T.context(fs);assert.equal(c.edges.size,1);edge=[...c.edges.keys()][0];
const natural=T.sharedCurves(edge,c);fs=T.smooth(fs,edge,'Gentle');assert.notDeepEqual(T.sharedCurves(edge,fs),natural);fs=T.smooth(fs,edge,'Natural');assert.deepEqual(T.sharedCurves(edge,fs),natural);
const shapeBefore=fs.map(f=>T.outline(f,fs));const detached=T.unjoin(fs,'B',edge);detached.forEach((f,i)=>{const now=T.outline(f,detached);assert.equal(now.length,shapeBefore[i].length);now.forEach((p,j)=>assert(Math.hypot(p[0]-shapeBefore[i][j][0],p[1]-shapeBefore[i][j][1])<1e-7));});assert.equal(T.context(detached).edges.size,0);
const removed=T.remove(fs,new Set(['A']));assert.equal(removed.length,1);assert.equal(T.context(removed).edges.size,0);assert(T.simple(T.outline(removed[0],removed)));
assert.throws(()=>T.reposition(fs,fs.map((f,i)=>({...f,x:f.x+i*10}))),/incompatible/);const shifted=T.reposition(fs,fs.map(f=>({...f,x:f.x+30,y:f.y-10})));assert.equal(T.context(shifted).vertices.size,T.context(fs).vertices.size);
for(const scale of [.2,1,5]){const idx=T.snapIndex(detached,scale),p=T.controls(detached[0],detached)[0],snap=T.snap(idx,[p[0]+5*scale,p[1]],10*scale,{areaID:'B'});assert.equal(snap.type,'vertex');assert.deepEqual(snap.point,p);assert.equal(T.snap(idx,p,10*scale,{disabled:true}),null);}
const idx=T.snapIndex(detached,1),curve=T.edgeCurves(detached[0],1,detached),p=T.point(curve[0],.5),hit=T.snap(idx,[p[0]+2,p[1]],10,{areaID:'B',vertex:false});assert.equal(hit.type,'edge');assert(hit.distance<3);
const copy=T.detachCopies(fs,[{...E.copy(fs[0]),featureID:'copy',x:fs[0].x+50}],new Map([['A','copy']]))[0];assert(!T.linked(copy));assert(!T.hasStore(copy));
assert.deepEqual(T.context(JSON.parse(JSON.stringify(fs))).commit(),fs);
const withThird=T.movePoint([...fs,area('C',200,200)],'C',0,T.controls(fs[1],fs).find(p=>p[0]<150&&p[1]>0));const three=T.join(withThird,{areaID:'C',neighborID:'B',start:0,end:1,sourceDirection:'forward',neighborDirection:'backward'}),tc=T.context(three),junction=[...tc.vertices.keys()].find(id=>tc.users(id,'vertex').length===3);assert(junction,'three-way junction reuses one authoritative vertex');const ci=tc.area('C').vertexRefs.indexOf(junction),threeMoved=T.movePoint(three,'C',ci,[112,104]);assert.equal(T.context(threeMoved).users(junction,'vertex').length,3);
const reverseInserted=T.insert(fs,'B',T.context(fs).area('B').edgeRefs.indexOf(edge),0,.4);assert.equal(T.context(reverseInserted).edges.size,2);assert(T.simple(T.outline(reverseInserted[0],reverseInserted)));assert(T.simple(T.outline(reverseInserted[1],reverseInserted)));
let target=T.insert([area('D',0,0),area('E',200,0)],'D',1,0,.5);const different=T.join(target,{areaID:'E',neighborID:'D',start:3,end:0,sourceDirection:'forward',neighborDirection:'backward'});assert.equal(T.context(different).area('E').points.length,5,'joining adopts neighbour intermediate controls');
const invalidBefore=JSON.stringify(fs);assert.throws(()=>T.movePoint(fs,'A',T.context(fs).area('A').vertexRefs.findIndex(Boolean),T.controls(fs[0],fs)[0]),/self-intersect|collapse/);assert.equal(JSON.stringify(fs),invalidBefore);
const afterOwnerDelete=T.remove(threeMoved,new Set(['A']));assert.equal(T.context(afterOwnerDelete).edges.size,1);assert(T.context(afterOwnerDelete).vertices.has(junction),'deleting a definition owner transfers surviving junction authority');
const curvedTarget=[area('snapA',0,0),area('snapB',200,0)],cidx=T.snapIndex(curvedTarget,1),p1=T.point(T.edgeCurves(curvedTarget[0],1,curvedTarget)[0],.2),p2=T.point(T.edgeCurves(curvedTarget[0],1,curvedTarget)[0],.8);let snapped=T.movePoint(curvedTarget,'snapB',0,p1);snapped=T.movePoint(snapped,'snapB',3,p2);const edgeJoined=T.join(snapped,{areaID:'snapB',neighborID:'snapA',start:3,end:0,sourceDirection:'forward',neighborDirection:'backward'});assert.equal(T.context(edgeJoined).area('snapA').points.length,6,'joining inserts exact target curve endpoints');
const G=require('../../PlotLine/wwwroot/js/maps-geography-engine.js'),geoAreas=[{...area('ga',50,390,1040),locationID:1},{...area('gb',1090,390,1040),locationID:2}],geoJoined=T.join(geoAreas,{areaID:'gb',neighborID:'ga',start:3,end:0,sourceDirection:'forward',neighborDirection:'backward'}),states=[{locationID:1,status:'Resolved',matchJson:JSON.stringify({Latitude:0,Longitude:0})},{locationID:2,status:'Resolved',matchJson:JSON.stringify({Latitude:0,Longitude:.01})}],compatible=G.position(geoJoined,states,{x:0,y:0,w:1200,h:800});assert(compatible.canApply);for(const[id,p]of T.context(geoJoined).vertices){const q=T.context(compatible.features).vertices.get(id);assert(Math.hypot(q[0]-p[0]-30,q[1]-p[1]-10)<1e-6);}
const conflict=G.position(geoJoined,[states[0],{...states[1],matchJson:JSON.stringify({Latitude:.01,Longitude:.01})}],{x:0,y:0,w:1200,h:800});assert(!conflict.canApply);assert.deepEqual(conflict.features,geoJoined);
const retainedSource=[area('keepA',0,0),area('keepB',200,0)],keepCurve=T.edgeCurves(retainedSource[1],3,retainedSource),kept=T.join(retainedSource,{areaID:'keepB',neighborID:'keepA',start:3,end:0,sourceDirection:'forward',neighborDirection:'backward',retain:'selected'});assert.deepEqual(T.sharedCurves([...T.context(kept).edges.keys()][0],kept),keepCurve,'selected boundary can be retained explicitly');
assert.throws(()=>T.join([area('tieA',0,0),area('tieB',0,0)],{areaID:'tieA',neighborID:'tieB',start:0,end:2}),/equally|explicit/);
const lock=fs.map(f=>f.featureID==='B'?{...f,isLocked:true}:f);assert.throws(()=>T.smooth(lock,[...T.context(lock).edges.keys()][0],'Rugged'),/Unlock/);
const dash=[area('dashA',0,0),area('dashB',200,0)];E.updateArea(dash[0],g=>g.appearance={borderStyle:'Dashed'});const dashed=T.join(dash,{areaID:'dashB',neighborID:'dashA',start:3,end:0,sourceDirection:'forward',neighborDirection:'backward'}),dc=T.context(dashed),did=[...dc.edges.keys()][0],stroke=T.sharedCurves(did,dc),un=T.unjoin(dashed,'dashB');for(const f of un){const g=T.context(un).area(f.featureID),i=g.edgeStyles.findIndex(s=>s?.borderStyle==='Dashed'),cs=T.edgeCurves(f,i,un),oriented=g.edgeStyles[i].reverseStroke?T.reverse(cs):cs;oriented.forEach((q,j)=>q.forEach((p,k)=>assert(Math.hypot(p[0]-stroke[j][k][0],p[1]-stroke[j][k][1])<1e-7)));}
const many=Array.from({length:2000},(_,i)=>area('grid'+i,(i%50)*300,Math.floor(i/50)*300)),startTime=performance.now(),spatial=T.snapIndex(many,1),built=performance.now();for(let i=0;i<1000;i++){const p=[(i%50)*300-95,Math.floor(i/50)*300-100];assert(T.snap(spatial,p,10,{areaID:'outside'}));}const elapsed=performance.now()-built;assert(elapsed<1500);console.log('Snapping index: 2,000 areas in '+Math.round(built-startTime)+'ms; 1,000 queries in '+Math.round(elapsed)+'ms.');
console.log('PASS topology join, shared authority/edit/insert/remove, reversible smoothing, exact unjoin, deletion/duplication, compatible/conflicting movement, zoom-independent vertex/curved-edge snapping and persistence.');