-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathicosahedron.py
154 lines (123 loc) · 3.91 KB
/
icosahedron.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Based roughly on the C++ code at
# http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html
import math
import numpy as np
class Point3D( object ):
def __init__( self, x, y, z ):
self.x, self.y, self.z = x, y, z
def distFrom( self, point ):
return math.sqrt( (self.x-point.x)**2
+ (self.y-point.y)**2
+ (self.z-point.z)**2 )
def normalize( self ):
length = math.sqrt( (self.x)**2
+ (self.y)**2
+ (self.z)**2 )
return Point3D(self.x/length, self.y/length, self.z/length)
def average( self, point ):
return Point3D((self.x + point.x)/2,
(self.y + point.y)/2,
(self.z + point.z)/2)
def exists( self, points, tolerence = 1.0e-6 ):
minIndex = -1
minDistance = 1.0
for i in range(len(points)):
if self.distFrom(points[i]) < tolerence:
minIndex = i
return minIndex
def make_icosahedron(order=3):
vertex = []
face = []
t = (1.0 + math.sqrt(5.0)) / 2.0;
vertex.append(Point3D(-1, t, 0).normalize())
vertex.append(Point3D( 1, t, 0).normalize())
vertex.append(Point3D(-1, -t, 0).normalize())
vertex.append(Point3D( 1, -t, 0).normalize())
vertex.append(Point3D( 0, -1, t).normalize())
vertex.append(Point3D( 0, 1, t).normalize())
vertex.append(Point3D( 0, -1, -t).normalize())
vertex.append(Point3D( 0, 1, -t).normalize())
vertex.append(Point3D( t, 0, -1).normalize())
vertex.append(Point3D( t, 0, 1).normalize())
vertex.append(Point3D(-t, 0, -1).normalize())
vertex.append(Point3D(-t, 0, 1).normalize())
#for i in range(len(vertex)):
# minDistance = 1.0e6
# for j in range(len(vertex)):
# if i != j:
# distance = vertex[i].distFrom(vertex[j])
# if distance < minDistance:
# minDistance = distance
# print(minDistance)
# 5 faces around point 0
face.append([0, 11, 5])
face.append([0, 5, 1])
face.append([0, 1, 7])
face.append([0, 7, 10])
face.append([0, 10, 11])
# 5 adjacent faces
face.append([1, 5, 9])
face.append([5, 11, 4])
face.append([11, 10, 2])
face.append([10, 7, 6])
face.append([7, 1, 8])
# 5 faces around point 3
face.append([3, 9, 4])
face.append([3, 4, 2])
face.append([3, 2, 6])
face.append([3, 6, 8])
face.append([3, 8, 9])
# 5 adjacent faces
face.append([4, 9, 5])
face.append([2, 4, 11])
face.append([6, 2, 10])
face.append([8, 6, 7])
face.append([9, 8, 1])
for ocount in range(order):
newface = []
for f in face:
vindex0 = f[0]
vindex1 = f[1]
vindex2 = f[2]
v0 = vertex[vindex0]
v1 = vertex[vindex1]
v2 = vertex[vindex2]
v3 = Point3D.average(v0, v1).normalize()
index = v3.exists(vertex)
if index >= 0:
vindex3 = index
else:
vertex.append(v3)
vindex3 = len(vertex)-1
v4 = Point3D.average(v1, v2).normalize()
index = v4.exists(vertex)
if index >= 0:
vindex4 = index
else:
vertex.append(v4)
vindex4 = len(vertex)-1
v5 = Point3D.average(v2, v0).normalize()
index = v5.exists(vertex)
if index >= 0:
vindex5 = index
else:
vertex.append(v5)
vindex5 = len(vertex)-1
newface.append([vindex0, vindex3, vindex5])
newface.append([vindex3, vindex1, vindex4])
newface.append([vindex4, vindex2, vindex5])
newface.append([vindex3, vindex4, vindex5])
face = newface
#print(len(face), len(vertex))
#for i in range(len(vertex)):
# minDistance = 1.0e6
# for j in range(len(vertex)):
# if i != j:
# distance = vertex[i].distFrom(vertex[j])
# if distance < minDistance:
# minDistance = distance
# print(minDistance)
npvertex = np.zeros(shape=(len(vertex),3))
for i in range(len(vertex)):
npvertex[i,:] = [vertex[i].x, vertex[i].y, vertex[i].z]
return npvertex