Repository: NASA-AMMOS/3DTilesRendererJS
Pull request #1199 (2025-06-29 01:44:39)

Add tests for TilingScheme
Related to #1155 


Patch:
diff --git a/src/plugins/three/images/utils/TilingScheme.js b/src/plugins/three/images/utils/TilingScheme.js
index 4196a75..275dee9 100644
--- a/src/plugins/three/images/utils/TilingScheme.js
+++ b/src/plugins/three/images/utils/TilingScheme.js
@@ -165,7 +165,7 @@ export class TilingScheme {
 	}
 
 	// query functions
-	getTileAtPoint( bx, by, level, normalized = false, clampTiles = true ) {
+	getTileAtPoint( bx, by, level, normalized = false ) {
 
 		const { projection, flipY } = this;
 		const { tileCountX, tileCountY } = this.getLevel( level );
@@ -179,7 +179,7 @@ export class TilingScheme {
 
 		}
 
-		let tx = Math.floor( bx / xStride );
+		const tx = Math.floor( bx / xStride );
 		let ty = Math.floor( by / yStride );
 
 		if ( flipY ) {
@@ -188,13 +188,6 @@ export class TilingScheme {
 
 		}
 
-		if ( clampTiles ) {
-
-			tx = clamp( tx, 0, tileCountX - 1 );
-			ty = clamp( ty, 0, tileCountY - 1 );
-
-		}
-
 		return [ tx, ty ];
 
 	}
@@ -235,6 +228,7 @@ export class TilingScheme {
 		const [ tminx, tminy, tmaxx, tmaxy ] = this.getTileBounds( x, y, level );
 		const isDegenerate = tminx >= tmaxx || tminy >= tmaxy;
 
+		// TODO: is supporting "just touch" correct?
 		return ! isDegenerate && tminx <= rmaxx && tminy <= rmaxy && tmaxx >= rminx && tmaxy >= rminy;
 
 	}


Test patch:
diff --git a/test/TilingScheme.test.js b/test/TilingScheme.test.js
new file mode 100644
index 0000000..fec2d2f
--- /dev/null
+++ b/test/TilingScheme.test.js
@@ -0,0 +1,104 @@
+import { TilingScheme } from '../src/plugins/three/images/utils/TilingScheme.js';
+import { ProjectionScheme } from '../src/plugins/three/images/utils/ProjectionScheme.js';
+
+describe( 'TiltingScheme', () => {
+
+	it( 'should allow for automatically building levels.', () => {
+
+		const scheme = new TilingScheme();
+		scheme.generateLevels( 10, 1, 2, {
+			minLevel: 5,
+			tilePixelWidth: 128,
+			tilePixelHeight: 128,
+		} );
+
+		expect( scheme.minLevel ).toBe( 5 );
+		expect( scheme.maxLevel ).toBe( 9 );
+		expect( scheme.levelCount ).toBe( 10 );
+		expect( scheme.aspect ).toBe( 0.5 );
+		expect( scheme.rootOrigin ).toEqual( [ 0, 0 ] );
+		expect( scheme.rootBounds ).toEqual( [ 0, 0, 1, 1 ] );
+		expect( scheme.getLevel( 4 ) ).toEqual( null );
+
+		const maxLevel = scheme.getLevel( 9 );
+		expect( maxLevel.tilePixelWidth ).toEqual( 128 );
+		expect( maxLevel.tilePixelHeight ).toEqual( 128 );
+		expect( maxLevel.pixelWidth ).toEqual( 128 * 2 ** 9 );
+		expect( maxLevel.pixelHeight ).toEqual( 128 * 2 * 2 ** 9 );
+		expect( maxLevel.tileCountX ).toEqual( 2 ** 9 );
+		expect( maxLevel.tileCountY ).toEqual( 2 * 2 ** 9 );
+
+	} );
+
+	it( 'should report the bounds of a specific tile correctly.', () => {
+
+		const scheme = new TilingScheme();
+		scheme.generateLevels( 3, 2, 1 );
+
+		expect( scheme.getFullBounds() ).toEqual( [ 0, 0, 1, 1 ] );
+		expect( scheme.getFullBounds( true ) ).toEqual( [ 0, 0, 1, 1 ] );
+		expect( scheme.getTileBounds( 0, 0, 0 ) ).toEqual( [ 0, 0, 0.5, 1 ] );
+		expect( scheme.getTileBounds( 0, 0, 1 ) ).toEqual( [ 0, 0, 0.25, 0.5 ] );
+
+	} );
+
+	it( 'should limit the loadable tiles by the bounds.', () => {
+
+		const scheme = new TilingScheme();
+		scheme.generateLevels( 3, 2, 1 );
+		scheme.setBounds( 0, 0, 0.7, 0.7 );
+
+		expect( scheme.getTileExists( 0, 0, 0 ) ).toBe( true );
+		expect( scheme.getTileExists( 1, 0, 0 ) ).toBe( true );
+
+		expect( scheme.getTileExists( 0, 0, 1 ) ).toBe( true );
+		expect( scheme.getTileExists( 3, 0, 1 ) ).toBe( false );
+
+	} );
+
+	it( 'should report all the tiles in a given range even if they don\'t exist.', () => {
+
+		const scheme = new TilingScheme();
+		scheme.generateLevels( 3, 2, 1 );
+
+		expect( scheme.getTilesInRange( 0, 0, 0.5, 0.5, 2 ) ).toEqual( [ 0, 0, 4, 2 ] );
+
+		scheme.setBounds( 0.3, 0.3, 0.7, 0.7 );
+		expect( scheme.getTilesInRange( 0, 0, 0.5, 0.5, 2 ) ).toEqual( [ 0, 0, 4, 2 ] );
+
+	} );
+
+	it( 'should support inferring tile bounds by projection scheme.', () => {
+
+		const scheme = new TilingScheme();
+		scheme.generateLevels( 3, 2, 1 );
+		scheme.setProjection( new ProjectionScheme() );
+
+		expect( scheme.getFullBounds() ).toEqual( [ - Math.PI, - Math.PI / 2, Math.PI, Math.PI / 2 ] );
+		expect( scheme.getFullBounds( true ) ).toEqual( [ 0, 0, 1, 1 ] );
+		expect( scheme.toNormalizedPoint( - Math.PI, - Math.PI / 2 ) ).toEqual( [ 0, 0 ] );
+		expect( scheme.toNormalizedPoint( 0, 0 ) ).toEqual( [ 0.5, 0.5 ] );
+
+	} );
+
+	it( 'should correctly support tiling sampling outside the valid range.', () => {
+
+		const scheme = new TilingScheme();
+		scheme.generateLevels( 3, 1, 1 );
+
+		expect( scheme.getTileAtPoint( - 0.25, - 0.25, 1, true ) ).toEqual( [ - 1, - 1 ] );
+
+	} );
+
+	it( 'should correctly support flipY variable.', () => {
+
+		const scheme = new TilingScheme();
+		scheme.generateLevels( 3, 1, 1 );
+		scheme.flipY = true;
+
+		expect( scheme.getTileAtPoint( - 0.25, - 0.25, 1, true ) ).toEqual( [ - 1, 2 ] );
+		expect( scheme.getTileAtPoint( 0, 0, 1, true ) ).toEqual( [ 0, 1 ] );
+
+	} );
+
+} );