Skip to content

Commit

Permalink
tests ported
Browse files Browse the repository at this point in the history
  • Loading branch information
sraimund committed Jan 6, 2025
1 parent ae5cef2 commit 0f89190
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 2 deletions.
79 changes: 77 additions & 2 deletions modules/core/test/lib/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import test, {Test} from 'tape-promise/tape';
import {Vector2, Vector3, Pose, _MathUtils} from '@math.gl/core';
import {config, configure, isArray, clone, equals, exactEquals, formatValue} from '@math.gl/core';
import {toRadians, toDegrees} from '@math.gl/core';
import {radians, degrees, sin, cos, tan, asin, acos, atan, clamp, lerp} from '@math.gl/core';
import {radians, degrees, mod, zeroToTwoPi, negativePiToPi, sin, cos, tan, asin, acos, atan, clamp, lerp} from '@math.gl/core';
import {tapeEquals} from 'test/utils/tape-assertions';

test('math.gl#tests', (t) => {
Expand Down Expand Up @@ -164,7 +164,7 @@ function runTests(t: Test, functionUnderTest: Function, testCases: any[]): void
for (const testCase of testCases) {
tapeEquals(
t,
functionUnderTest(testCase.input),
testCase.hasOwnProperty("input") ? functionUnderTest(testCase.input) : functionUnderTest(...testCase.inputs),
testCase.result,
`should return a value of ${JSON.stringify(testCase.result)}`
);
Expand Down Expand Up @@ -205,6 +205,81 @@ test('math.gl#degrees', (t) => {
t.end();
});

test('math.gl#mod', (t) => {
runTests(t, mod, [
{ inputs: [0.0, 1.0], result: 0.0 },
{ inputs: [0.1, 1.0], result: 0.1 },
{ inputs: [0.5, 1.0], result: 0.5 },
{ inputs: [1.0, 1.0], result: 0.0 },
{ inputs: [1.1, 1.0], result: 0.1 },
{ inputs: [-0.0, 1.0], result: 0.0 },
{ inputs: [-0.1, 1.0], result: 0.9 },
{ inputs: [-0.5, 1.0], result: 0.5 },
{ inputs: [-1.0, 1.0], result: 0.0 },
{ inputs: [-1.1, 1.0], result: 0.9 },
{ inputs: [0.0, -1.0], result: -0.0 },
{ inputs: [0.1, -1.0], result: -0.9 },
{ inputs: [0.5, -1.0], result: -0.5 },
{ inputs: [1.0, -1.0], result: -0.0 },
{ inputs: [1.1, -1.0], result: -0.9 },
{ inputs: [-0.0, -1.0], result: -0.0 },
{ inputs: [-0.1, -1.0], result: -0.1 },
{ inputs: [-0.5, -1.0], result: -0.5 },
{ inputs: [-1.0, -1.0], result: -0.0 },
{ inputs: [-1.1, -1.0], result: -0.1 },
]);
t.end();
})

test('math.gl#zeroToTwoPi', (t) => {
runTests(t, zeroToTwoPi, [
{ input: 0.0, result: 0.0 },
{ input: +Math.PI, result: +Math.PI },
{ input: -Math.PI, result: +Math.PI },
{ input: +Math.PI - 1.0, result: +Math.PI - 1.0 },
{ input: -Math.PI + 1.0, result: +Math.PI + 1.0 },
{ input: +Math.PI - 0.1, result: +Math.PI - 0.1 },
{ input: -Math.PI + 0.1, result: +Math.PI + 0.1 },
{ input: +Math.PI + 0.1, result: +Math.PI + 0.1 },
{ input: -Math.PI - 0.1, result: +Math.PI - 0.1 },
{ input: +2.0 * Math.PI, result: 2.0 * Math.PI },
{ input: -2.0 * Math.PI, result: 2.0 * Math.PI },
{ input: +3.0 * Math.PI, result: Math.PI },
{ input: -3.0 * Math.PI, result: Math.PI },
{ input: +4.0 * Math.PI, result: 2.0 * Math.PI },
{ input: -4.0 * Math.PI, result: 2.0 * Math.PI },
{ input: +5.0 * Math.PI, result: Math.PI },
{ input: -5.0 * Math.PI, result: Math.PI },
{ input: +6.0 * Math.PI, result: 2.0 * Math.PI },
{ input: -6.0 * Math.PI, result: 2.0 * Math.PI },
]);
t.end();
})

test('math.gl#negativePiToPi', (t) => {
runTests(t, negativePiToPi, [
{ input: 0.0, result: 0.0 },
{ input: +Math.PI, result: +Math.PI },
{ input: -Math.PI, result: -Math.PI },
{ input: +Math.PI - 1.0, result: +Math.PI - 1.0 },
{ input: -Math.PI + 1.0, result: -Math.PI + 1.0 },
{ input: +Math.PI - 0.1, result: +Math.PI - 0.1 },
{ input: -Math.PI + 0.1, result: -Math.PI + 0.1 },
{ input: +2.0 * Math.PI, result: 0.0 },
{ input: -2.0 * Math.PI, result: 0.0 },
{ input: +3.0 * Math.PI, result: Math.PI },
{ input: -3.0 * Math.PI, result: Math.PI },
{ input: +4.0 * Math.PI, result: 0.0 },
{ input: -4.0 * Math.PI, result: 0.0 },
{ input: +5.0 * Math.PI, result: Math.PI },
{ input: -5.0 * Math.PI, result: Math.PI },
{ input: +6.0 * Math.PI, result: 0.0 },
{ input: -6.0 * Math.PI, result: 0.0 },
]);
t.end();
})


test('math.gl#sin', (t) => {
runTests(t, sin, [
{input: 0, result: 0},
Expand Down
3 changes: 3 additions & 0 deletions modules/culling/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import './lib/algorithms/compute-eigen-decomposition.spec';
import './lib/algorithms/bounding-sphere-from-points.spec';
import './lib/algorithms/bounding-box-from-points.spec';
import './lib/algorithms/plane-ray-intersection.spec';

import './lib/bounding-volumes/axis-aligned-bounding-box.spec';
import './lib/bounding-volumes/bounding-sphere.spec';
import './lib/bounding-volumes/oriented-bounding-box.spec';

import './lib/plane.spec';
import './lib/ray.spec';
import './lib/culling-volume.spec';
import './lib/ellipsoid-tangent-plane.spec';

import './lib/perspective-off-center-frustum.spec';
import './lib/perspective-frustum.spec';
49 changes: 49 additions & 0 deletions modules/culling/test/lib/algorithms/plane-ray-intersection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// math.gl
// SPDX-License-Identifier: MIT and Apache-2.0
// Copyright (c) vis.gl contributors

// This file is derived from the Cesium math library under Apache 2 license
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md

import test from 'tape-promise/tape';
import {tapeEquals} from 'test/utils/tape-assertions';
import {intersectPlaneWithRay, Plane, Ray} from '@math.gl/culling';
import {Vector3} from '@math.gl/core';

const VECTOR3_UNIT_X = new Vector3(1.0, 0.0, 0.0);

test('rayPlane intersects', (t) => {
const ray = new Ray(
new Vector3(2.0, 0.0, 0.0),
new Vector3(-1.0, 0.0, 0.0),
);
const plane = new Plane(VECTOR3_UNIT_X, -1.0);

const intersectionPoint = intersectPlaneWithRay(plane, ray);
tapeEquals(t, intersectionPoint, new Vector3(1.0, 0.0, 0.0));
t.end();
});

test('rayPlane misses', (t) => {
const ray = new Ray(
new Vector3(2.0, 0.0, 0.0),
new Vector3(1.0, 0.0, 0.0),
);
const plane = new Plane(VECTOR3_UNIT_X, -1.0);

const intersectionPoint = intersectPlaneWithRay(plane, ray);
t.equals(intersectionPoint, undefined);
t.end();
});

test('rayPlane misses (parallel)', (t) => {
const ray = new Ray(
new Vector3(2.0, 0.0, 0.0),
new Vector3(0.0, 1.0, 0.0),
);
const plane = new Plane(VECTOR3_UNIT_X, -1.0);

const intersectionPoint = intersectPlaneWithRay(plane, ray);
t.equals(intersectionPoint, undefined);
t.end();
});
38 changes: 38 additions & 0 deletions modules/culling/test/lib/ellipsoid-tangent-plane.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// math.gl
// SPDX-License-Identifier: MIT and Apache-2.0
// Copyright (c) vis.gl contributors

// This file is derived from the Cesium math library under Apache 2 license
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md

import test from 'tape-promise/tape';
import {tapeEquals} from 'test/utils/tape-assertions';
import {EllipsoidTangentPlane} from '@math.gl/culling';
import {Vector2, Vector3} from '@math.gl/core';

const VECTOR3_UNIT_X = new Vector3(1.0, 0.0, 0.0);

test('EllipsoidTangentPlane#constructor sets expected values', (t) => {
const tangentPlane = new EllipsoidTangentPlane(VECTOR3_UNIT_X);
tapeEquals(t, tangentPlane.origin, new Vector3(6378137.0, 0.0, 0.0));
t.end();
});

test('EllipsoidTangentPlane#projectPointToNearestOnPlane works', (t) => {
const tangentPlane = new EllipsoidTangentPlane(VECTOR3_UNIT_X);

const inputAndResults = [
{input: new Vector3(1.0, 0.0, 0.0), result: new Vector2(0.0, 0.0)},
{input: new Vector3(0.0, 0.0, 0.0), result: new Vector2(0.0, 0.0)},
{input: new Vector3(-1.0, 0.0, 0.0), result: new Vector2(0.0, 0.0)},
{input: new Vector3(1.0, 0.0, 1.0), result: new Vector2(0.0, 1.0)},
{input: new Vector3(0.0, 1.0, 0.0), result: new Vector2(1.0, 0.0)},
{input: new Vector3(0.0, 1.0, 1.0), result: new Vector2(1.0, 1.0)},
];

for (const {input, result} of inputAndResults) {
const output = tangentPlane.projectPointToNearestOnPlane(input);
tapeEquals(t, output, result);
}
t.end();
});
40 changes: 40 additions & 0 deletions modules/culling/test/lib/ray.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// math.gl
// SPDX-License-Identifier: MIT and Apache-2.0
// Copyright (c) vis.gl contributors

// This file is derived from the Cesium math library under Apache 2 license
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md

import test from 'tape-promise/tape';
import {tapeEquals} from 'test/utils/tape-assertions';
import {Ray} from "@math.gl/culling";
import {Vector3} from "@math.gl/core";

const VECTOR3_ZERO = new Vector3(0.0, 0.0, 0.0);
const VECTOR3_UNIT_X = new Vector3(1.0, 0.0, 0.0);
const VECTOR3_UNIT_Y = new Vector3(0.0, 1.0, 0.0);

test("Ray#default constructor create zero valued Ray", (t) => {
const ray = new Ray();
tapeEquals(t, ray.origin, VECTOR3_ZERO);
tapeEquals(t, ray.direction, VECTOR3_ZERO);
t.end();
});

test("Ray#constructor sets expected properties", (t) => {
const origin = VECTOR3_UNIT_Y;
const direction = VECTOR3_UNIT_X;
const ray = new Ray(origin, direction);
tapeEquals(t, ray.origin, VECTOR3_UNIT_Y);
tapeEquals(t, ray.direction, VECTOR3_UNIT_X);
t.end();
});

test("Ray#constructor normalizes direction", (t) => {
const origin = VECTOR3_UNIT_Y;
const direction = new Vector3(18., 0, 0);
const ray = new Ray(origin, direction);
tapeEquals(t, ray.origin, VECTOR3_UNIT_Y);
tapeEquals(t, ray.direction, VECTOR3_UNIT_X);
t.end();
});
68 changes: 68 additions & 0 deletions modules/geospatial/test/geo-rectangle.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// math.gl
// SPDX-License-Identifier: MIT and Apache-2.0
// Copyright (c) vis.gl contributors

// This file is derived from the Cesium math library under Apache 2 license
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md

import test from 'tape-promise/tape';
import {tapeEquals} from 'test/utils/tape-assertions';
import {Vector3, radians, _MathUtils} from '@math.gl/core';
import {GeoRectangle} from '@math.gl/geospatial';

const west = -0.9;
const south = 0.5;
const east = 1.4;
const north = 1.0;
const center = new Vector3((west + east) / 2.0, (south + north) / 2.0, 0.0);

test('GeoRectangle#default constructor sets expected values.', (t) => {
const rectangle = new GeoRectangle();
t.equals(rectangle.west, 0.0);
t.equals(rectangle.south, 0.0);
t.equals(rectangle.north, 0.0);
t.equals(rectangle.east, 0.0);
t.end();
});

test('GeoRectangle#constructor sets expected parameter values.', (t) => {
const rectangle = new GeoRectangle(west, south, east, north);
t.equals(rectangle.west, west);
t.equals(rectangle.south, south);
t.equals(rectangle.north, north);
t.equals(rectangle.east, east);
t.end();
});

test('GeoRectangle#width works', (t) => {
let rectangle = new GeoRectangle(west, south, east, north);
t.equals(rectangle.width, east - west);

rectangle = new GeoRectangle(2.0, -1.0, -2.0, 1.0);
t.equals(rectangle.width, rectangle.east - rectangle.west + _MathUtils.TWO_PI);
t.end();
});

test('GeoRectangle#center works', (t) => {
const rectangle = new GeoRectangle(west, south, east, north);
const returnedResult = GeoRectangle.center(rectangle);
tapeEquals(t, returnedResult, center);
t.end();
});

test('GeoRectangle#center works across IDL', (t) => {
const inputAndResults = [
{input: [170, 0, -170, 0], result: [180, 0]},
{input: [160, 0, -170, 0], result: [175, 0]},
{input: [170, 0, -160, 0], result: [-175, 0]},
{input: [160, 0, 140, 0], result: [-30, 0]},
]

for (const {input, result} of inputAndResults) {
const rectangle = new GeoRectangle(...radians(input));
const returnedResult = GeoRectangle.center(rectangle);
tapeEquals(t, returnedResult, new Vector3(...radians(result), 0));
}

t.end();
});
1 change: 1 addition & 0 deletions modules/geospatial/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
// Copyright (c) vis.gl contributors

import './type-utils.spec';
import './geo-rectangle.spec';
import './ellipsoid/ellipsoid.spec';
import './ellipsoid/ellipsoid-transform.spec';

0 comments on commit 0f89190

Please sign in to comment.