From 1925913eead890e257249130d5cbc2880ec1c05f Mon Sep 17 00:00:00 2001 From: singlecoder Date: Tue, 26 Jan 2021 21:21:57 +0800 Subject: [PATCH 1/7] chore: add color test --- packages/math/tests/Color.test.ts | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 packages/math/tests/Color.test.ts diff --git a/packages/math/tests/Color.test.ts b/packages/math/tests/Color.test.ts new file mode 100644 index 0000000000..b21e43a752 --- /dev/null +++ b/packages/math/tests/Color.test.ts @@ -0,0 +1,42 @@ +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); + } + }); + console.log(`hehehe`) +}); From 5f6744fb534e64569db91d696760aec148fb934d Mon Sep 17 00:00:00 2001 From: singlecoder Date: Tue, 26 Jan 2021 21:39:29 +0800 Subject: [PATCH 2/7] chore: add plane test --- packages/math/tests/Color.test.ts | 1 - packages/math/tests/Plane.test.ts | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 packages/math/tests/Plane.test.ts diff --git a/packages/math/tests/Color.test.ts b/packages/math/tests/Color.test.ts index b21e43a752..08b92a4ce2 100644 --- a/packages/math/tests/Color.test.ts +++ b/packages/math/tests/Color.test.ts @@ -38,5 +38,4 @@ describe("Color test", () => { expect(Color.equals(colorLinear, colorNewLinear)).toEqual(true); } }); - console.log(`hehehe`) }); diff --git a/packages/math/tests/Plane.test.ts b/packages/math/tests/Plane.test.ts new file mode 100644 index 0000000000..113f24490f --- /dev/null +++ b/packages/math/tests/Plane.test.ts @@ -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); + }); +}); From 6dcb1412ebec5b08995993f5e0ea2ea08dcd303e Mon Sep 17 00:00:00 2001 From: singlecoder Date: Tue, 26 Jan 2021 21:53:36 +0800 Subject: [PATCH 3/7] chore: add ray test --- packages/math/tests/Ray.test.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/math/tests/Ray.test.ts b/packages/math/tests/Ray.test.ts index 02f274accc..bae7845c94 100644 --- a/packages/math/tests/Ray.test.ts +++ b/packages/math/tests/Ray.test.ts @@ -1,12 +1,37 @@ +import { BoundingBox } from "../src/BoundingBox"; +import { BoundingSphere } from "../src/BoundingSphere"; import { Plane } from "../src/Plane"; import { Ray } from "../src/Ray"; import { Vector3 } from "../src/Vector3"; describe("Ray test", () => { it("ray-plane", () => { - const ray = new Ray(new Vector3(0, 0, 0), new Vector3(1, 0.5, 0.3)); - const plane = new Plane(new Vector3(0.4, 0.5, 1), 5); + const ray = new Ray(new Vector3(0, 0, 0), new Vector3(0, 1, 0)); + const plane = new Plane(new Vector3(0, 1, 0), -3); - // ray.intersectPlane(plane.) + expect(ray.intersectPlane(plane)).toEqual(-plane.distance); + }); + + it("ray-sphere", () => { + const ray = new Ray(new Vector3(0, 0, 0), new Vector3(0, 1, 0)); + const sphere = new BoundingSphere(new Vector3(0, 5, 0), 1); + + expect(ray.intersectSphere(sphere)).toEqual(4); + }); + + it("ray-box", () => { + const ray = new Ray(new Vector3(0, 0, 0), new Vector3(0, 1, 0)); + const box = new BoundingBox(); + BoundingBox.fromCenterAndExtent(new Vector3(0, 20, 0), new Vector3(5, 5, 5), box); + + expect(ray.intersectBox(box)).toEqual(15); + }); + + it("ray-getPoint", () => { + const ray = new Ray(new Vector3(0, 0, 0), new Vector3(0, 1, 0)); + const out = new Vector3(); + ray.getPoint(10, out); + + expect(Vector3.equals(out, new Vector3(0, 10, 0))).toEqual(true); }); }); From 6bea9f4eb96da38855b2ef029538c01506294781 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Wed, 27 Jan 2021 00:48:53 +0800 Subject: [PATCH 4/7] chore: add bounding box test --- packages/math/tests/BoundingBox.test.ts | 116 ++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 packages/math/tests/BoundingBox.test.ts diff --git a/packages/math/tests/BoundingBox.test.ts b/packages/math/tests/BoundingBox.test.ts new file mode 100644 index 0000000000..d7fd9467fa --- /dev/null +++ b/packages/math/tests/BoundingBox.test.ts @@ -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); + } + }) +}); From ccb1880757dedf2d8497629dbfda8dbfd75dd0a1 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Wed, 27 Jan 2021 01:05:46 +0800 Subject: [PATCH 5/7] chore: add bounding sphere test --- packages/math/tests/BoundingSphere.test.ts | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 packages/math/tests/BoundingSphere.test.ts diff --git a/packages/math/tests/BoundingSphere.test.ts b/packages/math/tests/BoundingSphere.test.ts new file mode 100644 index 0000000000..aca28fed71 --- /dev/null +++ b/packages/math/tests/BoundingSphere.test.ts @@ -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); + }); +}); From e4a4898a1f41f615b5c441a962dc84fb5a30ca72 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Wed, 27 Jan 2021 01:31:00 +0800 Subject: [PATCH 6/7] chore: add bounding frustum test --- packages/math/tests/BoundingFrustum.test.ts | 34 ++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/math/tests/BoundingFrustum.test.ts b/packages/math/tests/BoundingFrustum.test.ts index 0ee382f3c8..8e6b3b8eef 100644 --- a/packages/math/tests/BoundingFrustum.test.ts +++ b/packages/math/tests/BoundingFrustum.test.ts @@ -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); + }); }); From 3f02729385bf206d3928dae77c360d95a097c984 Mon Sep 17 00:00:00 2001 From: singlecoder Date: Wed, 27 Jan 2021 02:07:24 +0800 Subject: [PATCH 7/7] chore: add collision util test --- packages/math/tests/CollisionUtil.test.ts | 139 ++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 packages/math/tests/CollisionUtil.test.ts diff --git a/packages/math/tests/CollisionUtil.test.ts b/packages/math/tests/CollisionUtil.test.ts new file mode 100644 index 0000000000..78993c948d --- /dev/null +++ b/packages/math/tests/CollisionUtil.test.ts @@ -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); + }); +});