def test_triangulation(fn, extents):
tr = Triangulation()
color_change_grid_search(tr, *extents, 250,250*4//5)
grid_result = tr.p.copy()
retry = 10
nothing_added = False
while 1:
tr.p_array = np.array( tr.p )
print("points:", tr.p_array.shape)
tr.dela = scipy.spatial.Delaunay(tr.p_array)
if retry==0: break
if nothing_added: break
retry -= 1
nothing_added = True
for r in tr.dela.simplices:
centerx = 1.0/3*( tr.p[r[0]][0] + tr.p[r[1]][0] + tr.p[r[2]][0] )
centery = 1.0/3*( tr.p[r[0]][1] + tr.p[r[1]][1] + tr.p[r[2]][1] )
centercolor = color(centerx, centery)
for p in [0,1,2]:
worst = 0.02
fix_x, fix_y = None, None
for i in [5,0,1,2,3,4,6,7,8,9]: # start from middle
middle = i==5
p1share = (i+0.5)/10 # 0.05 0.15 .. 0.95
px = p1share*tr.p[r[p]][0] + (1-p1share)*tr.p[r[p-1]][0]
py = p1share*tr.p[r[p]][1] + (1-p1share)*tr.p[r[p-1]][1]
pcolor = color(px, py)
if pcolor==centercolor: continue
nx, ny = find_color_change(px,py,pcolor, centerx,centery,centercolor)
dist = np.sqrt( np.square(px-nx) + np.square(py-ny) )
if worst < dist:
worst = dist
fix_x, fix_y = nx, ny
else:
if middle: break # optimization
if fix_x != None:
tr.p.append( [fix_x, fix_y] )
nothing_added = False
obj = Obj(fn)
obj.v_idx = []
for v in tr.p:
obj.v_idx.append( obj.push_v( [v[0],v[1],0] ) )
obj.push_vn( [0,0,1] )
obj.push_vt( [v[0]*0.2+0.25, v[1]*0.2+0.25] )
for material_index,material in enumerate(tr.material_palette):
obj.out.write("\n\nusemtl %s\n" % material)
obj.out.write("o %s\n" % material)
for r in tr.dela.simplices:
rx = 1.0/3*( tr.p[r[0]][0] + tr.p[r[1]][0] + tr.p[r[2]][0] )
ry = 1.0/3*( tr.p[r[0]][1] + tr.p[r[1]][1] + tr.p[r[2]][1] )
c = color(rx, ry)
if c!=material_index: continue
obj.out.write("f %i/%i/%i %i/%i/%i %i/%i/%i\n" % tuple( [obj.v_idx[r[0]]]*3 + [obj.v_idx[r[1]]]*3 + [obj.v_idx[r[2]]]*3 ))
obj.out.close()
评论列表
文章目录