x5-geometry
Version:
Geometry and word processing utilities for XNet
95 lines (94 loc) • 4.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const x5sub_1 = require("../x5sub");
const testPoint1 = { x: 0.01, y: 0.01 };
const testPoint2 = { x: 0.5, y: 0.5 };
describe('extended geometry tests', () => {
test('testing reordering hex vertices', () => {
expect(typeof x5sub_1.reorderVerticies).toBe('function');
let vertices = [
{ x: 0, y: 2 },
{ x: 1.732, y: 1 },
{ x: 1.732, y: -1 },
{ x: 0, y: -2 },
{ x: -1.732, y: -1 },
{ x: -1.732, y: 1 }
];
vertices.reverse();
let result = (0, x5sub_1.reorderVerticies)(vertices);
expect(result).toHaveLength(6);
expect(result[0].x).toEqual(1.732);
expect(result[0].y).toEqual(-1);
});
test('testing hexIndexToIndexArray', () => {
expect(typeof x5sub_1.hexIndexToIndexArray).toBe('function');
let hexIndex = 62;
let result = (0, x5sub_1.hexIndexToIndexArray)(hexIndex);
expect(result).toHaveLength(4);
expect(result[0]).toEqual(0);
expect(result[1]).toEqual(2);
expect(result[2]).toEqual(14);
expect(result[3]).toEqual(62);
});
test('testing isPointInsideTriangle', () => {
expect(typeof x5sub_1.isPointInsideTriangle).toBe('function');
let triangle = [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 0, y: 1 }
];
const p1 = { x: 0.35, y: 0.25 };
const p2 = { x: 1.5, y: 0.5 };
const p3 = { x: 0.5, y: 0.5 };
console.log("point triangle test" + (0, x5sub_1.isPointInsideTriangle)(p1, triangle[0], triangle[1], triangle[2]));
expect((0, x5sub_1.isPointInsideTriangle)(p1, triangle[0], triangle[1], triangle[2])).toBe(true);
expect((0, x5sub_1.isPointInsideTriangle)(p2, triangle[0], triangle[1], triangle[2])).toBe(false);
expect((0, x5sub_1.isPointInsideTriangle)(p3, triangle[0], triangle[1], triangle[2], true)).toBe(true);
expect((0, x5sub_1.isPointInsideTriangle)(p3, triangle[0], triangle[1], triangle[2], false)).toBe(false);
});
test('subdivideTriangle', () => {
expect(typeof x5sub_1.subdivideTriangle).toBe('function');
let triangle = [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 0, y: 1 }
];
let { vertices, triangles } = (0, x5sub_1.subdivideTriangle)(triangle);
expect(vertices).toHaveLength(6);
expect(triangles).toHaveLength(4);
expect(vertices[0].x).toEqual(1);
expect(vertices[0].y).toEqual(0);
expect(vertices[1].x).toEqual(0);
expect(vertices[1].y).toEqual(1);
expect(vertices[2].x).toEqual(0);
expect(vertices[2].y).toEqual(0);
expect(vertices[3].x).toEqual(0.5);
expect(vertices[3].y).toEqual(0.5);
expect(vertices[4].x).toEqual(0);
expect(vertices[4].y).toEqual(0.5);
expect(vertices[5].x).toEqual(0.5);
expect(vertices[5].y).toEqual(0);
expect(triangles[0]).toEqual([3, 4, 5]);
expect(triangles[1]).toEqual([0, 3, 5]);
expect(triangles[2]).toEqual([3, 1, 4]);
expect(triangles[3]).toEqual([5, 4, 2]);
});
test('getHexSubdivisionIndices', () => {
expect(typeof x5sub_1.getHexSubdivisionIndices).toBe('function');
let hexVertices = [
{ x: 0, y: 2 },
{ x: 1.732, y: 1 },
{ x: 1.732, y: -1 },
{ x: 0, y: -2 },
{ x: -1.732, y: -1 },
{ x: -1.732, y: 1 }
];
hexVertices.reverse();
console.log(hexVertices);
console.log((0, x5sub_1.getHexSubdivisionIndices)(testPoint1, hexVertices));
expect(Array.isArray((0, x5sub_1.getHexSubdivisionIndices)(testPoint1, hexVertices))).toBe(true);
expect((0, x5sub_1.getHexSubdivisionIndices)(testPoint1, hexVertices)).toHaveLength(4);
expect((0, x5sub_1.getHexSubdivisionIndices)(testPoint1, hexVertices)).toEqual([0, 2, 14, 62]);
expect((0, x5sub_1.getHexSubdivisionIndices)({ x: 5, y: 5 }, hexVertices)).toBe(false);
});
});