-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_optimize_rand.py
209 lines (160 loc) · 4.83 KB
/
cloud_optimize_rand.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from digifab import *
import math
import numpy
import random
# Writing bounds #
bounds= numpy.asarray([[10,10,25],[140,140,140]])
def cloud_optimize(cloud, edges):
while(optimize(cloud, edges)):
pass
return
# Attempts ten times to improve the worst point in the cloud
# ALMOST DONE, BOUNDING BOX
def optimize(cloud, edges):
# find the bounding box of the sculpture
# finish
i = 0
while (i < 1500):
print i
before = hCloud(cloud, edges)
#worstPT = findWorstPoint(cloud, edges)
worstPT = random.randint(0, 5)
#if worstPT == None:
# print "Sculpture is awesome!"
# return False
originalPos = randomMove(cloud, worstPT, 75, bounds)
# Failure, try again
if originalPos == None:
print 'failed move'
return False
after = hCloud(cloud,edges)
# Success!
if after > before:
print "CHANGED A POINT"
return True
# Failure, try again
else:
#print 'failed move: ' + str(after) + ": " + str(before)
cloud[worstPT][0] = originalPos[0]
cloud[worstPT][1] = originalPos[1]
cloud[worstPT][2] = originalPos[2]
i += 1
# Out of tries!
return False
# Returns a number for goodness of fit given a point, higher = better
# DONE
def hPoint(cloud, edges, index):
return worst_angle(cloud, edges, index)
def randPoint(cloud, edges):
badPts = []
for i in range(len(cloud)):
if joint_volume(cloud, edges, i) != 125.0:
#print joint_volume(cloud, edges, i)
badPts.append(i)
if badPts == []:
return None
else:
return random.choice(badPts)
#perfectPts.add(worstPT)
#if len(perfectPts) == len(cloud):
# return False
#worstPT = randint(0,5)
# Returns a number for goodness of fit for the cloud, higher = better
# DONE
def hCloud(cloud, edges):
worst = 0
for i in range(len(cloud)):
h = hPoint(cloud, edges, i)
worst += h
return worst
# Finds the point with the most acute diahedral angle
# DONE
def findWorstPoint(cloud, edges):
worstPT = 0
worstVal = None
for i in range(len(cloud)):
h = hPoint(cloud, edges, i)
if worstPT == None:
worstVal = h
worstPT = i
elif h < worstVal:
worstVal = h
worstPT = i
return worstPT
# Moves the point within radius sphere randomly
# DONE
def randomMove(cloud, index, radius, bounds):
def dist(a,b):
return numpy.linalg.norm(pts_to_vec(a,b))
def off_bounds(pts,bds):
return (bds[0][0] <= pts[0] <= bds[1][0]) and (bds[0][1] <= pts[1] <= bds[1][1]) and (bds[0][2] <= pts[2] <= bds[1][2])
t = 0
while t < 1000:
rand_vec = numpy.array([random.uniform(-radius, radius), random.uniform(-radius, radius), random.uniform(-radius, radius)]) # just to give a specific arbitrary direction
#rand_radius = numpy.random.uniform(0.0, radius, 1)[0]
#rand_norm = numpy.array([rand_vec[0]*rand_radius, rand_vec[1]*rand_radius, rand_vec[2]*rand_radius]) # normalize the vector to radius distance
# Moving the point #
mv_cloud = cloud[index].copy()
og = cloud[index].copy()
mv_cloud = mv_cloud + rand_vec
failure = False
for i in range(len(cloud)):
if i != index and dist(mv_cloud,cloud[i]) < 30.0 or not off_bounds(mv_cloud, bounds):
#cloud[i] = mv_cloud
failure = True
if not failure:
cloud[index][0] = mv_cloud[0]
cloud[index][1] = mv_cloud[1]
cloud[index][2] = mv_cloud[2]
return og
t += 1
return None
# Vector constructor #
# DONE
def pts_to_vec (pt_a, pt_b):
start = numpy.asarray(pt_a)
end = numpy.asarray(pt_b)
vec = end - start
return vec
# Find the diahedral angle given two vectors #
# DONE
def vectors_angle (vec_0, vec_1):
dot_product = numpy.dot(vec_0, vec_1)
vec_0_norm = numpy.linalg.norm(vec_0)
vec_1_norm = numpy.linalg.norm(vec_1)
return math.acos(dot_product / (vec_0_norm * vec_1_norm))
# Find the worst angle given a point #
#DONE
def worst_angle(cloud, edges, p_i):
neighbors_pts = neighbors(edges, p_i)
vector_lst = []
angle_lst = []
for i in range(len(neighbors_pts)):
vector_lst.append(pts_to_vec(cloud[p_i], cloud[neighbors_pts[i]]))
prev = vector_lst[-1]
for i in (range(len(vector_lst))):
angle_lst.append(vectors_angle(vector_lst[i], prev))
prev = vector_lst[i]
return min(angle_lst)
def joint_volume(cloud, edges, p_i):
# Calculating the angles of all pair of axes in a connector
the_angle= worst_angle(cloud, edges, p_i)
#print the_angle
r = (3.0/2.0) + 0.4
if round(math.sin(the_angle),2)== 0.0:
radii = 5.0
else:
radii= (r/(math.sin(the_angle)))
max_radii= max(radii , 5.0)
#print max_radii
return max_radii**3
# Find the points a given point is connected to
# DONE
def neighbors(edges, index):
n = []
for edge in edges:
if edge[0] == index:
n.append(edge[1])
elif edge[1] == index:
n.append(edge[0])
return n