summaryrefslogtreecommitdiff
path: root/modules/Geometry.test.js
blob: 45ce39716e4e302984442c182ed58eb9814987c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { test, assert } from './test-assert.js';

import { AABB, QTNode } from './Geometry.js';


test('AABB correctly contains/excludes points', () => {
	const box = new AABB(0, 0, 1, 1);

	// interior
	assert.ok(box.contains({ x: 0.5, y: 0.5 }));

	// upper left
	assert.ok(!box.contains({ x: -1, y: -1 }));

	// above
	assert.ok(!box.contains({ x: 0.5, y: -1}));

	// upper right
	assert.ok(!box.contains({ x: 2, y: -1 }));

	// left
	assert.ok(!box.contains({ x: -1, y: 0.5 }));

	// right
	assert.ok(!box.contains({ x: 2, y: 0.5}));

	// lower left
	assert.ok(!box.contains({ x: -1, y: 2 }));

	// below 
	assert.ok(!box.contains({ x: 0.5, y: 2}));

	// lower right
	assert.ok(!box.contains({ x: 2, y: 2 }));
});


test('AABB correctly intersects other AABBs', () => {
	const box = new AABB(1, 1, 4, 4);

	// interior
	assert.ok(box.intersects(new AABB(2, 2, 2, 2,)));

	// upper left
	assert.ok(box.intersects(new AABB(0, 0, 4, 4)));
	assert.ok(!box.intersects(new AABB(0, 0, 0.5, 0.5)));

	// above
	assert.ok(box.intersects(new AABB(2, 0, 2, 2)));
	assert.ok(!box.intersects(new AABB(2, 0, 2, 0.5)));

	// upper right
	assert.ok(box.intersects(new AABB(2, 0, 4, 2)));
	assert.ok(!box.intersects(new AABB(6, 0, 4, 2)));

	// left
	assert.ok(box.intersects(new AABB(0, 2, 2, 2)));
	assert.ok(!box.intersects(new AABB(0, 2, 0.5, 2)));

	// right
	assert.ok(box.intersects(new AABB(4, 2, 2, 2)));
	assert.ok(!box.intersects(new AABB(6, 2, 2, 2)));

	// lower left
	assert.ok(box.intersects(new AABB(0, 4, 4, 4)));
	assert.ok(!box.intersects(new AABB(0, 6, 0.5, 0.5)));

	// below
	assert.ok(box.intersects(new AABB(2, 4, 2, 2)));
	assert.ok(!box.intersects(new AABB(2, 6, 2, 0.5)));

	// lower right
	assert.ok(box.intersects(new AABB(2, 4, 4, 2)));
	assert.ok(!box.intersects(new AABB(6, 6, 4, 2)));
});


test('QTNode correctly inserts points', () => {
	const node = new QTNode(0, 0, 1, 1);
	assert.equal(node.type.toString(), 'QTNode.Empty');

	let result = node.insert({ x: -1, y: -1 }); // out of range, should not insert
	assert.ok(!result);
	assert.equal(node.type.toString(), 'QTNode.Empty');

	result = node.insert({ x: 0.5, y: 0.5 }); // in range
	assert.ok(result);
	assert.equal(node.type.toString(), 'QTNode.Leaf');
	assert.deepEqual(node.point, { x: 0.5, y: 0.5 });
});