Skip to content

Commit

Permalink
Merge pull request #5 from oasis-engine/chore/workflow
Browse files Browse the repository at this point in the history
Chore/workflow
  • Loading branch information
singlecoder authored Jan 27, 2021
2 parents cdaf16f + c8c40be commit 65981e5
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 4 deletions.
116 changes: 116 additions & 0 deletions packages/math/tests/BoundingBox.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { BoundingBox } from "../src/BoundingBox";
import { BoundingSphere } from "../src/BoundingSphere";
import { Matrix } from "../src/Matrix";
import { Vector3 } from "../src/Vector3";

describe("BoundingBox test", () => {
it("Constructor", () => {
const box1 = new BoundingBox();
const box2 = new BoundingBox();
const box3 = new BoundingBox();

// Create a same box by diffrent param.
BoundingBox.fromCenterAndExtent(new Vector3(0, 0, 0), new Vector3(1, 1, 1), box1);

const points = [
new Vector3(0, 0, 0),
new Vector3(-1, 0, 0),
new Vector3(1, 0, 0),
new Vector3(0, 1, 0),
new Vector3(0, 1, 1),
new Vector3(1, 0, 1),
new Vector3(0, 0.5, 0.5),
new Vector3(0, -0.5, 0.5),
new Vector3(0, -1, 0.5),
new Vector3(0, 0, -1),
];
BoundingBox.fromPoints(points, box2);

const sphere = new BoundingSphere(new Vector3(0, 0, 0), 1);
BoundingBox.fromSphere(sphere, box3);

const { min: min1, max: max1 } = box1;
const { min: min2, max: max2 } = box2;
const { min: min3, max: max3 } = box3;

expect(Vector3.equals(min1, min2)).toEqual(true);
expect(Vector3.equals(max1, max2)).toEqual(true);
expect(Vector3.equals(min1, min3)).toEqual(true);
expect(Vector3.equals(max1, max3)).toEqual(true);
expect(Vector3.equals(min2, min3)).toEqual(true);
expect(Vector3.equals(max2, max3)).toEqual(true);
});

it("transform", () => {
const box = new BoundingBox(new Vector3(-1, -1, -1), new Vector3(1, 1, 1));
const matrix = new Matrix(
2, 0, 0, 0,
0, 2, 0, 0,
0, 0, 2, 0,
1, 0.5, -1, 1
);
const newBox = new BoundingBox();
BoundingBox.transform(box, matrix, newBox);

const newMin = new Vector3(-1, -1.5, -3);
const newMax = new Vector3(3, 2.5, 1);
expect(Vector3.equals(newBox.min, newMin)).toEqual(true);
expect(Vector3.equals(newBox.max, newMax)).toEqual(true);
});

it("merge", () => {
const box1 = new BoundingBox(new Vector3(-1, -1, -1), new Vector3(2, 2, 2));
const box2 = new BoundingBox(new Vector3(-2, -0.5, -2), new Vector3(3, 0, 3));
const box = new BoundingBox();

BoundingBox.merge(box1, box2, box);
expect(Vector3.equals(new Vector3(-2, -1, -2), box.min)).toEqual(true);
expect(Vector3.equals(new Vector3(3, 2, 3), box.max)).toEqual(true);
});

it("getCenter", () => {
const box = new BoundingBox(new Vector3(-1, -1, -1), new Vector3(3, 3, 3));
const center = new Vector3();

box.getCenter(center);
expect(Vector3.equals(new Vector3(1, 1, 1), center)).toEqual(true);
});

it("getExtent", () => {
const box = new BoundingBox(new Vector3(-1, -1, -1), new Vector3(3, 3, 3));
const extent = new Vector3();

box.getExtent(extent);
expect(Vector3.equals(new Vector3(2, 2, 2), extent)).toEqual(true);
});

it("getCorners", () => {
const min = new Vector3(-1, -1, -1);
const max = new Vector3(3, 3, 3);
const { x: minX, y: minY, z: minZ } = min;
const { x: maxX, y: maxY, z: maxZ } = max;
const expectedCorners = [
new Vector3(), new Vector3(), new Vector3(), new Vector3(),
new Vector3(), new Vector3(), new Vector3(), new Vector3()
];
expectedCorners[0].setValue(minX, maxY, maxZ);
expectedCorners[1].setValue(maxX, maxY, maxZ);
expectedCorners[2].setValue(maxX, minY, maxZ);
expectedCorners[3].setValue(minX, minY, maxZ);
expectedCorners[4].setValue(minX, maxY, minZ);
expectedCorners[5].setValue(maxX, maxY, minZ);
expectedCorners[6].setValue(maxX, minY, minZ);
expectedCorners[7].setValue(minX, minY, minZ);

const box = new BoundingBox(min, max);
const corners = [
new Vector3(), new Vector3(), new Vector3(), new Vector3(),
new Vector3(), new Vector3(), new Vector3(), new Vector3()
];

box.getCorners(corners);
for (let i = 0; i < 8; ++i) {
expect(Vector3.equals(corners[i], expectedCorners[i])).toEqual(true);
}
})
});
34 changes: 33 additions & 1 deletion packages/math/tests/BoundingFrustum.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import { BoundingBox } from "../src/BoundingBox";
import { BoundingFrustum } from "../src/BoundingFrustum";
import { BoundingSphere } from "../src/BoundingSphere";
import { Matrix } from "../src/Matrix";
import { Vector3 } from "../src/Vector3";

describe("MathUtil test", () => {
it("construtor", () => {});
const viewMatrix = new Matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -20, 1);
const projectionMatrix = new Matrix(0.03954802080988884, 0, 0, 0, 0, 0.10000000149011612, 0, 0, 0, 0, -0.0200200192630291, 0, -0, -0, -1.0020020008087158, 1);
const vpMatrix = new Matrix();
Matrix.multiply(projectionMatrix, viewMatrix, vpMatrix);
const frustum = new BoundingFrustum(vpMatrix);

it("intersectsBox", () => {
const box1 = new BoundingBox(new Vector3(-2, -2, -2), new Vector3(2, 2, 2));
const flag1 = frustum.intersectsBox(box1);
expect(flag1).toEqual(true);

const box2 = new BoundingBox(new Vector3(-32, -2, -2), new Vector3(-28, 2, 2));
const flag2 = frustum.intersectsBox(box2);
expect(flag2).toEqual(false);
});

it("intersectsSphere", () => {
const box1 = new BoundingBox(new Vector3(-2, -2, -2), new Vector3(2, 2, 2));
const sphere1 = new BoundingSphere();
BoundingSphere.fromBox(box1, sphere1);
const flag1 = frustum.intersectsSphere(sphere1);
expect(flag1).toEqual(true);

const box2 = new BoundingBox(new Vector3(-32, -2, -2), new Vector3(-28, 2, 2));
const sphere2 = new BoundingSphere();
BoundingSphere.fromBox(box2, sphere2);
const flag2 = frustum.intersectsSphere(sphere2);
expect(flag2).toEqual(false);
});
});
33 changes: 33 additions & 0 deletions packages/math/tests/BoundingSphere.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { BoundingBox } from "../src/BoundingBox";
import { BoundingSphere } from "../src/BoundingSphere";
import { Vector3 } from "../src/Vector3";

describe("BoundingSphere", () => {
it("Constructor", () => {
const sphere1 = new BoundingSphere();
const sphere2 = new BoundingSphere();

// Create a same sphere by diffrent param.
const points = [
new Vector3(0, 0, 0),
new Vector3(-1, 0, 0),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0),
new Vector3(1, 1, 1),
new Vector3(0, 0, 1),
new Vector3(-1, -0.5, -0.5),
new Vector3(0, -0.5, -0.5),
new Vector3(1, 0, -1),
new Vector3(0, -1, 0),
];
BoundingSphere.fromPoints(points, sphere1);

const box = new BoundingBox(new Vector3(-1, -1, -1), new Vector3(1, 1, 1));
BoundingSphere.fromBox(box, sphere2);

const { center: center1, radius: radius1 } = sphere1;
const { center: center2, radius: radius2 } = sphere2;
expect(Vector3.equals(center1, center2)).toEqual(true);
expect(radius1).toEqual(radius2);
});
});
139 changes: 139 additions & 0 deletions packages/math/tests/CollisionUtil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { PlaneIntersectionType } from "../src/enums/PlaneIntersectionType";
import { ContainmentType } from "../src/enums/ContainmentType";
import { CollisionUtil } from "../src/CollisionUtil";
import { Plane } from "../src/Plane";
import { Vector3 } from "../src/Vector3";
import { BoundingBox } from "../src/BoundingBox";
import { BoundingSphere } from "../src/BoundingSphere";
import { Ray } from "../src/Ray";
import { Matrix } from "../src/Matrix";
import { BoundingFrustum } from "../src/BoundingFrustum";

describe("CollisionUtil", () => {
const plane = new Plane(new Vector3(0, 1, 0), -5);
const viewMatrix = new Matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -20, 1);
const projectionMatrix = new Matrix(0.03954802080988884, 0, 0, 0, 0, 0.10000000149011612, 0, 0, 0, 0, -0.0200200192630291, 0, -0, -0, -1.0020020008087158, 1);
const vpMatrix = new Matrix();
Matrix.multiply(projectionMatrix, viewMatrix, vpMatrix);
const frustum = new BoundingFrustum(vpMatrix);

it("distancePlaneAndPoint", () => {
const point = new Vector3(0, 10, 0);

const distance = CollisionUtil.distancePlaneAndPoint(plane, point);
expect(distance).toEqual(5);
});

it("intersectsPlaneAndPoint", () => {
const point1 = new Vector3(0, 10, 0);
const point2 = new Vector3(2, 5, -9);
const point3 = new Vector3(0, 3, 0);

const intersection1 = CollisionUtil.intersectsPlaneAndPoint(plane, point1);
const intersection2 = CollisionUtil.intersectsPlaneAndPoint(plane, point2);
const intersection3 = CollisionUtil.intersectsPlaneAndPoint(plane, point3);
expect(intersection1).toEqual(PlaneIntersectionType.Front);
expect(intersection2).toEqual(PlaneIntersectionType.Intersecting);
expect(intersection3).toEqual(PlaneIntersectionType.Back);
});

it("intersectsPlaneAndBox", () => {
const box1 = new BoundingBox(new Vector3(-1, 6, -2), new Vector3(1, 10, 3));
const box2 = new BoundingBox(new Vector3(-1, 5, -2), new Vector3(1, 10, 3));
const box3 = new BoundingBox(new Vector3(-1, 4, -2), new Vector3(1, 5, 3));
const box4 = new BoundingBox(new Vector3(-1, -5, -2), new Vector3(1, 4.9, 3));

const intersection1 = CollisionUtil.intersectsPlaneAndBox(plane, box1);
const intersection2 = CollisionUtil.intersectsPlaneAndBox(plane, box2);
const intersection3 = CollisionUtil.intersectsPlaneAndBox(plane, box3);
const intersection4 = CollisionUtil.intersectsPlaneAndBox(plane, box4);
expect(intersection1).toEqual(PlaneIntersectionType.Front);
expect(intersection2).toEqual(PlaneIntersectionType.Intersecting);
expect(intersection3).toEqual(PlaneIntersectionType.Intersecting);
expect(intersection4).toEqual(PlaneIntersectionType.Back);
});

it("intersectsPlaneAndSphere", () => {
const sphere1 = new BoundingSphere(new Vector3(0, 8, 0), 2);
const sphere2 = new BoundingSphere(new Vector3(0, 8, 0), 3);
const sphere3 = new BoundingSphere(new Vector3(0, 3, 0), 2);
const sphere4 = new BoundingSphere(new Vector3(0, 0, 0), 2);

const intersection1 = CollisionUtil.intersectsPlaneAndSphere(plane, sphere1);
const intersection2 = CollisionUtil.intersectsPlaneAndSphere(plane, sphere2);
const intersection3 = CollisionUtil.intersectsPlaneAndSphere(plane, sphere3);
const intersection4 = CollisionUtil.intersectsPlaneAndSphere(plane, sphere4);
expect(intersection1).toEqual(PlaneIntersectionType.Front);
expect(intersection2).toEqual(PlaneIntersectionType.Intersecting);
expect(intersection3).toEqual(PlaneIntersectionType.Intersecting);
expect(intersection4).toEqual(PlaneIntersectionType.Back);
});

it("intersectsRayAndPlane", () => {
const ray1 = new Ray(new Vector3(0, 0, 0), new Vector3(0, 1, 0));
const ray2 = new Ray(new Vector3(0, 0, 0), new Vector3(0, -1, 0));

const distance1 = CollisionUtil.intersectsRayAndPlane(ray1, plane);
const distance2 = CollisionUtil.intersectsRayAndPlane(ray2, plane);
expect(distance1).toEqual(5);
expect(distance2).toEqual(-1);
});

it("intersectsRayAndBox", () => {
const ray = new Ray(new Vector3(0, 0, 0), new Vector3(0, 1, 0));
const box1 = new BoundingBox(new Vector3(-1, 3, -1), new Vector3(2, 8, 3));
const box2 = new BoundingBox(new Vector3(1, 1, 1), new Vector3(2, 2, 2));

const distance1 = CollisionUtil.intersectsRayAndBox(ray, box1);
const distance2 = CollisionUtil.intersectsRayAndBox(ray, box2);
expect(distance1).toEqual(3);
expect(distance2).toEqual(-1);
});

it("intersectsRayAndSphere", () => {
const ray = new Ray(new Vector3(0, 0, 0), new Vector3(0, 1, 0));
const sphere1 = new BoundingSphere(new Vector3(0, 4, 0), 3);
const sphere2 = new BoundingSphere(new Vector3(0, -5, 0), 4);

const distance1 = CollisionUtil.intersectsRayAndSphere(ray, sphere1);
const distance2 = CollisionUtil.intersectsRayAndSphere(ray, sphere2);
expect(distance1).toEqual(1);
expect(distance2).toEqual(-1);
});

it("intersectsFrustumAndBox", () => {
const box1 = new BoundingBox(new Vector3(-2, -2, -2), new Vector3(2, 2, 2));
const flag1 = frustum.intersectsBox(box1);
expect(flag1).toEqual(true);

const box2 = new BoundingBox(new Vector3(-32, -2, -2), new Vector3(-28, 2, 2));
const flag2 = frustum.intersectsBox(box2);
expect(flag2).toEqual(false);
});

it("frustumContainsBox", () => {
const box1 = new BoundingBox(new Vector3(-2, -2, -2), new Vector3(2, 2, 2));
const box2 = new BoundingBox(new Vector3(-32, -2, -2), new Vector3(-28, 2, 2));
const box3 = new BoundingBox(new Vector3(-35, -2, -2), new Vector3(-18, 2, 2));

const expected1 = CollisionUtil.frustumContainsBox(frustum, box1);
const expected2 = CollisionUtil.frustumContainsBox(frustum, box2);
const expected3 = CollisionUtil.frustumContainsBox(frustum, box3);
expect(expected1).toEqual(ContainmentType.Contains);
expect(expected2).toEqual(ContainmentType.Disjoint);
expect(expected3).toEqual(ContainmentType.Intersects);
});

it("frustumContainsSphere", () => {
const sphere1 = new BoundingSphere(new Vector3(0, 0, 0), 2);
const sphere2 = new BoundingSphere(new Vector3(-32, -2, -2), 1);
const sphere3 = new BoundingSphere(new Vector3(-32, -2, -2), 15);

const expected1 = CollisionUtil.frustumContainsSphere(frustum, sphere1);
const expected2 = CollisionUtil.frustumContainsSphere(frustum, sphere2);
const expected3 = CollisionUtil.frustumContainsSphere(frustum, sphere3);
expect(expected1).toEqual(ContainmentType.Contains);
expect(expected2).toEqual(ContainmentType.Disjoint);
expect(expected3).toEqual(ContainmentType.Intersects);
});
});
41 changes: 41 additions & 0 deletions packages/math/tests/Color.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Color } from "../src/Color";

describe("Color test", () => {
it("Constructor", () => {
const color1 = new Color(1, 0.5, 0.5, 1);
const color2 = new Color();
color2.r = 1;
color2.g = 0.5;
color2.b = 0.5;
color2.a = 1;

expect(Color.equals(color1, color2)).toEqual(true);
});

it("LinearAndGamma", () => {
const fixColor = (color: Color) => {
color.r = Math.floor(color.r * 1000) / 1000;
color.g = Math.floor(color.g * 1000) / 1000;
color.b = Math.floor(color.b * 1000) / 1000;
}

const colorLinear = new Color();
const colorGamma = new Color();
const colorNewLinear = new Color();

for (let i = 0; i < 100; ++i) {
colorLinear.r = Math.random();
colorLinear.g = Math.random();
colorLinear.b = Math.random();
fixColor(colorLinear);

colorLinear.toGamma(colorGamma);
colorGamma.toLinear(colorNewLinear);

fixColor(colorLinear);
fixColor(colorNewLinear);

expect(Color.equals(colorLinear, colorNewLinear)).toEqual(true);
}
});
});
18 changes: 18 additions & 0 deletions packages/math/tests/Plane.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Plane } from "../src/Plane";
import { Vector3 } from "../src/Vector3";

describe("Plane test", () => {
it("Constructor", () => {
const point1 = new Vector3(0, 1, 0);
const point2 = new Vector3(0, 1, 1);
const point3 = new Vector3(1, 1, 0);
const plane1 = new Plane();
Plane.fromPoints(point1, point2, point3, plane1)
const plane2 = new Plane(new Vector3(0, 1, 0), -1);

expect(plane1.distance - plane2.distance).toEqual(0);
plane1.normalize();
plane2.normalize();
expect(Vector3.equals(plane1.normal, plane2.normal)).toEqual(true);
});
});
Loading

0 comments on commit 65981e5

Please sign in to comment.