Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript: unit tests Frustum issues #238

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/util/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import {mat4, vec3, vec4} from 'gl-matrix';
import assert from 'assert';

class Frustum {
constructor(public points: vec4[], public planes: vec4[]) { }
points: vec4[];
planes: vec4[];

constructor(points: vec4[], planes: vec4[]) {
this.points = points;
this.planes = planes;
}

public static fromInvProjectionMatrix(invProj: mat4, worldSize: number, zoom: number): Frustum {
const clipSpaceCorners = [
Expand All @@ -24,15 +30,15 @@ class Frustum {
.map(v => vec4.scale(vec4.create(), v, 1.0 / v[3] / worldSize * scale));

const frustumPlanePointIndices = [
vec3.fromValues(0, 1, 2), // near
vec3.fromValues(6, 5, 4), // far
vec3.fromValues(0, 3, 7), // left
vec3.fromValues(2, 1, 5), // right
vec3.fromValues(3, 2, 6), // bottom
vec3.fromValues(0, 4, 5) // top
[0, 1, 2], // near
[6, 5, 4], // far
[0, 3, 7], // left
[2, 1, 5], // right
[3, 2, 6], // bottom
[0, 4, 5] // top
];

const frustumPlanes = frustumPlanePointIndices.map((p: vec3) => {
const frustumPlanes = frustumPlanePointIndices.map((p: number[]) => {
const a = vec3.sub(vec3.create(), frustumCoords[p[0]] as vec3, frustumCoords[p[1]] as vec3);
const b = vec3.sub(vec3.create(), frustumCoords[p[2]] as vec3, frustumCoords[p[1]] as vec3);
const n = vec3.normalize(vec3.create(), vec3.cross(vec3.create(), a, b));
Expand Down
30 changes: 16 additions & 14 deletions test/stub_loader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import gl from 'gl';
import { JSDOM, VirtualConsole } from "jsdom";
import { PNG } from 'pngjs';
import {JSDOM, VirtualConsole} from "jsdom";
import {PNG} from 'pngjs';
import sinon from 'sinon';

let lastDataFroUrl = null;
let lastDataFromUrl = null;

// The following is the mocking of what's needed in window and global for the tests to run.
const { window } = new JSDOM('', {
const {window} = new JSDOM('', {
// Send jsdom console output to the node console object.
virtualConsole: new VirtualConsole().sendTo(console)
});
Expand All @@ -23,20 +23,22 @@ global.Image = window.Image;
global.navigator = window.navigator;
global.WheelEvent = window.WheelEvent;
// stubbing image load as it is not implemented in jsdom
// eslint-disable-next-line accessor-pairs
Object.defineProperty(global.Image.prototype, 'src', {
set(src) {
if (lastDataFroUrl) {
if (lastDataFromUrl) {
const reader = new window.FileReader();
reader.onload = (_) => {
const dataUrl = reader.result;
new PNG().parse(dataUrl, (err, png) => {
if (err) throw new Error("Couldn't parse PNG");
this.data = png.data;
this.height = png.height;
this.width = png.width;
setTimeout(() => this.onload());
});
}
reader.readAsArrayBuffer(lastDataFroUrl);
};
reader.readAsArrayBuffer(lastDataFromUrl);
}
}
});
Expand Down Expand Up @@ -82,7 +84,7 @@ window.useFakeHTMLCanvasGetContext = function () {

window.clearFakeHTMLCanvasGetContext = () => {
global.HTMLCanvasElement.prototype.getContext = imitateWebGlGetContext;
}
};

// HM TODO: move this to the relevat test...
window.useFakeXMLHttpRequest = function () {
Expand All @@ -94,14 +96,15 @@ window.useFakeXMLHttpRequest = function () {
window.clearFakeXMLHttpRequest = () => {
window.server = null;
global.XMLHttpRequest = null;
}
};

global.URL.createObjectURL = (blob) => {
lastDataFroUrl = blob;
lastDataFromUrl = blob;
return 'blob:';
}
};

global.URL.revokeObjectURL = function () {
lastDataFroUrl = null;
lastDataFromUrl = null;
};

window.useFakeWorkerPresence = function () {
Expand All @@ -113,9 +116,8 @@ window.clearFakeWorkerPresence = function () {
global.self = undefined;
};


window.performance.getEntriesByName = function () { };
window.performance.mark = function () { };
window.performance.measure = function () { };
window.performance.clearMarks = function () { };
window.performance.clearMeasures = function () { };
window.performance.clearMeasures = function () { };
17 changes: 11 additions & 6 deletions test/unit/util/primitives.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ test('primitives', (t) => {
const createTestCameraFrustum = (fovy, aspectRatio, zNear, zFar, elevation, rotation) => {
const proj = new Float64Array(16);
const invProj = new Float64Array(16);

// Note that left handed coordinate space is used where z goes towards the sky.
// Y has to be flipped as well because it's part of the projection/camera matrix used in transform.js
mat4.perspective(proj, fovy, aspectRatio, zNear, zFar);
Expand Down Expand Up @@ -92,7 +93,7 @@ test('primitives', (t) => {
});

t.test('No intersection between aabb and frustum', (t) => {
const frustum = createTestCameraFrustum(Math.PI / 2, 1.0, 0.1, 100.0, -5);
const frustum = createTestCameraFrustum(Math.PI / 2, 1.0, 0.1, 100.0, -5, 0);

const aabbList = [
new Aabb(vec3.fromValues(-6, 0, 0), vec3.fromValues(-5.5, 0, 0)),
Expand All @@ -111,13 +112,14 @@ test('primitives', (t) => {

t.test('frustum', (t) => {
t.test('Create a frustum from inverse projection matrix', (t) => {
const proj = new Float64Array(16);
const invProj = new Float64Array(16);
const proj = new Float64Array(16); // [] also passes test
const invProj = new Float64Array(16); // [] also passes test
// FAIL const proj = mat4.create(); // Float32Array also fails
// FAIL const invProj = mat4.create(); // Float32Array also fails
mat4.perspective(proj, Math.PI / 2, 1.0, 0.1, 100.0);
mat4.invert(invProj, proj);

const frustum = Frustum.fromInvProjectionMatrix(invProj, 1.0, 0.0);

// mat4.perspective generates a projection matrix for right handed coordinate space.
// This means that forward direction will be -z
const expectedFrustumPoints = [
Expand All @@ -132,8 +134,11 @@ test('primitives', (t) => {
];

// Round numbers to mitigate the precision loss
frustum.points = frustum.points.map(array => array.map(n => Math.round(n * 10) / 10));
frustum.planes = frustum.planes.map(array => array.map(n => Math.round(n * 1000) / 1000));
// this is just for test so convert to regular non typed Array for test comparison
// frustum.points = frustum.points.map(array => array.map(n => Math.round(n * 10) / 10));
// frustum.planes = frustum.planes.map(array => array.map(n => Math.round(n * 1000) / 1000));
frustum.points = frustum.points.map(array => Array.from(array).map(n => Math.round(n * 10) / 10));
frustum.planes = frustum.planes.map(array => Array.from(array).map(n => Math.round(n * 1000) / 1000));

const expectedFrustumPlanes = [
[0, 0, 1.0, 0.1],
Expand Down