-
Notifications
You must be signed in to change notification settings - Fork 1
/
GJKExperiments.py
63 lines (43 loc) · 1.23 KB
/
GJKExperiments.py
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
###############################################################################
# Example of 3D intersection test
#
import sys
import numpy as np
from GJK import *
def make_cube(dim):
n = np.power(2, dim)
res = np.ones([n, dim])
for i in range(n):
for j in range(dim):
if i & (1 << j) == 0:
res[i, j] = -1
return res
def make_pyramid(dim):
t = make_cube(dim - 1)
n = t.shape[0]
new_col = np.zeros(n).reshape([-1, 1])
t = np.hstack([t, new_col])
new_row = np.zeros(dim)
new_row[dim - 1] = 1
res = np.vstack([new_row, t])
return res
def GJK_test(dim):
# 3D pyramid centered on the origin:
shape_A = make_pyramid(dim)
print(shape_A)
offset = np.zeros(dim)
offset[dim - 1] = 1
shape_B = shape_A - offset * 2
print(shape_B)
# Test non-intersection
print("Should not intersect:")
print(GJK(shape_A, shape_B))
# Test intersection
shape_B = shape_A - offset * .5
print("Should intersect:")
print(GJK(shape_A, shape_B))
dim = 3
if len(sys.argv) > 1:
dim = int(sys.argv[1])
print("Testing {0}-dimensional pyramids.".format(dim))
GJK_test(dim)