Files
planet/frontend/public/earth/js/surface-line-simplification.test.js
rayd1o 05377bf5c6
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
ci / delivery (push) Has been cancelled
release / images (push) Has been cancelled
release: bump version to 0.76.0
2026-09-30 17:49:29 +08:00

42 lines
2.4 KiB
JavaScript

import {expect,test} from "bun:test";
import * as THREE from "three";
import {simplifySurfaceLines,surfaceLineTolerance} from "./surface-line-simplification.js";
import {appendSurfaceSegments} from "./earth-surface.js";
function segments(points) {return new Float32Array(points.slice(1).flatMap((point,i)=>[...points[i],...point]));}
test("collinear same-face detail merges without mutating the source",()=>{
const source=segments([[0,0,0],[1,.00001,0],[2,0,0],[3,0,0]]),copy=source.slice();
const reduced=simplifySurfaceLines(source,new Uint32Array(6).fill(7),.001);
expect([...reduced]).toEqual([0,0,0,3,0,0]);expect([...source]).toEqual([...copy]);
});
test("face boundaries and disconnected geometry cannot be bridged",()=>{
const source=segments([[0,0,0],[1,0,0],[2,0,0],[3,1,0]]);
const faces=new Uint32Array([1,1,1,2,2,2]);
expect([...simplifySurfaceLines(source,faces,100)]).toEqual([...source]);
const separate=new Float32Array([0,0,0,1,0,0,10,0,0,11,0,0]);
expect([...simplifySurfaceLines(separate,new Uint32Array(4).fill(1),100)]).toEqual([...separate]);
});
test("small closed islands retain every original edge even with a large tolerance",()=>{
const source=segments([[0,0,0],[1,0,0],[1,1,0],[0,0,0]]);
expect([...simplifySurfaceLines(source,new Uint32Array(6).fill(1),100)]).toEqual([...source]);
});
test("surface tags do not change the existing draped positions or cross triangle planes",()=>{
const ring=Array.from({length:100},(_,i)=>[10+i*.0001,20+i*.0001]);
const points=[],faces=[],reference=[];
appendSurfaceSegments(ring,100.48,points,faces);appendSurfaceSegments(ring,100.48,reference);
expect(points.map(p=>p.toArray())).toEqual(reference.map(p=>p.toArray()));
expect(faces.length).toBe(points.length);
const source=new Float32Array(points.flatMap(p=>p.toArray()));
const reduced=simplifySurfaceLines(source,new Uint32Array(faces),.01);
expect(reduced.length).toBeLessThan(source.length);
for(let i=0;i<reduced.length;i+=3) {
const point=new THREE.Vector3().fromArray(reduced,i);
expect(points.some(p=>p.distanceTo(point)<.00001)).toBe(true);
}
});
test("quantized tolerances never exceed the screen-space budget and tighten on zoom",()=>{
const far=surfaceLineTolerance(300,100.48,2000),near=surfaceLineTolerance(110,100.48,2000);
expect(far).toBeLessThanOrEqual(.15*(300-100.48)/2000);
expect(near).toBeLessThan(far);
expect(surfaceLineTolerance(100,100.48,2000)).toBe(0);
});