ObjToSchematic/tests/ray.test.ts

108 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Axes, Ray, rayIntersectTriangle } from '../src/ray';
2022-03-13 06:22:20 +08:00
import { Triangle } from '../src/triangle';
import { ASSERT } from '../src/util/error_util';
import { Vector3 } from '../src/vector';
import { TEST_PREAMBLE } from './preamble';
2022-03-13 06:22:20 +08:00
test('rayIntersectTriangle x-axis #1', () => {
TEST_PREAMBLE();
2022-03-13 06:22:20 +08:00
const ray: Ray = {
origin: new Vector3(-1, 0, 0),
axis: Axes.x,
};
const tri = new Triangle(
new Vector3(5, -1, -1),
new Vector3(5, 0, 1),
new Vector3(5, 1, -1),
2022-03-13 06:22:20 +08:00
);
const intersects = rayIntersectTriangle(ray, tri.v0, tri.v1, tri.v2);
expect(intersects).toBeDefined();
ASSERT(intersects);
expect(intersects.equals(new Vector3(5, 0, 0))).toEqual(true);
});
test('rayIntersectTriangle x-axis #2', () => {
TEST_PREAMBLE();
2022-03-13 06:22:20 +08:00
const ray: Ray = {
origin: new Vector3(1, 0, 0),
axis: Axes.x,
};
const tri = new Triangle(
new Vector3(0, -1, -1),
new Vector3(0, 0, 1),
new Vector3(0, 1, -1),
2022-03-13 06:22:20 +08:00
);
const intersects = rayIntersectTriangle(ray, tri.v0, tri.v1, tri.v2);
expect(intersects).toBeUndefined();
});
test('rayIntersectTriangle y-axis #1', () => {
TEST_PREAMBLE();
2022-03-13 06:22:20 +08:00
const ray: Ray = {
origin: new Vector3(0, -1, 0),
axis: Axes.y,
};
const tri = new Triangle(
new Vector3(-1, 6, -1),
new Vector3(0, 6, 1),
new Vector3(1, 6, -1),
2022-03-13 06:22:20 +08:00
);
const intersects = rayIntersectTriangle(ray, tri.v0, tri.v1, tri.v2);
expect(intersects).toBeDefined();
ASSERT(intersects);
expect(intersects.equals(new Vector3(0, 6, 0))).toEqual(true);
});
test('rayIntersectTriangle y-axis #2', () => {
TEST_PREAMBLE();
2022-03-13 06:22:20 +08:00
const ray: Ray = {
origin: new Vector3(0, 1, 0),
axis: Axes.y,
};
const tri = new Triangle(
new Vector3(-1, 0, -1),
new Vector3(0, 0, 1),
new Vector3(1, 0, -1),
2022-03-13 06:22:20 +08:00
);
const intersects = rayIntersectTriangle(ray, tri.v0, tri.v1, tri.v2);
expect(intersects).toBeUndefined();
});
test('rayIntersectTriangle z-axis #1', () => {
TEST_PREAMBLE();
2022-03-13 06:22:20 +08:00
const ray: Ray = {
origin: new Vector3(0, 0, -1),
axis: Axes.z,
};
const tri = new Triangle(
new Vector3(-1, -1, 7),
new Vector3(0, 1, 7),
new Vector3(1, -1, 7),
2022-03-13 06:22:20 +08:00
);
const intersects = rayIntersectTriangle(ray, tri.v0, tri.v1, tri.v2);
expect(intersects).toBeDefined();
ASSERT(intersects);
expect(intersects.equals(new Vector3(0, 0, 7))).toEqual(true);
});
test('rayIntersectTriangle z-axis #2', () => {
TEST_PREAMBLE();
2022-03-13 06:22:20 +08:00
const ray: Ray = {
origin: new Vector3(0, 0, 1),
axis: Axes.z,
};
const tri = new Triangle(
new Vector3(-1, -1, 0),
new Vector3(0, 1, 0),
new Vector3(1, -1, 0),
2022-03-13 06:22:20 +08:00
);
const intersects = rayIntersectTriangle(ray, tri.v0, tri.v1, tri.v2);
expect(intersects).toBeUndefined();
});