pencil strategy experiments with searching algo

pull/4/head
vilda.novak@gmail.com 2013-08-28 20:13:55 +00:00
rodzic 05262301a6
commit f321369818
1 zmienionych plików z 91 dodań i 34 usunięć

Wyświetl plik

@ -1025,11 +1025,15 @@ def getOffsetImageEdges(o,i):#for pencil operation mainly
#if bpy.app.debug_value==2:
ar=numpy.logical_or(vertical,horizontal)
dilateAr(ar,1)
#dilateAr(ar,0)
iname=getCachePath(o)+'_pencilthres.exr'
numpysave(ar,iname)
numpysave(ar,iname)#save for comparison before
chunks = crazyStrokeImage(o,ar)
iname=getCachePath(o)+'_pencilthres_comp.exr'
numpysave(ar,iname)#and after
#chunks=imageToChunks(o,ar)
#chunks=imageToChunksPencil(o,ar)
for ch in chunks:#convert 2d chunks to 3d
@ -1058,31 +1062,44 @@ def crazyStrokeImage(o,ar):
pixsize=o.pixsize
edges=[]
r=ceil((o.cutter_diameter/2)/o.pixsize)
r=3#ceil((o.cutter_diameter/12)/o.pixsize)
d=2*r
coef=0.75
#sx=o.max.x-o.min.x
#sy=o.max.y-o.min.y
#size=ar.shape[0]
maxarx=ar.shape[0]
maxary=ar.shape[1]
cutterArray=getCircleBinary(r)
cutterArray=1-cutterArray
cutterArrayNegative=-cutterArray
#cutterArray=1-cutterArray
cutterimagepix=cutterArray.sum()
satisfypix=cutterimagepix/10#a threshold which says if it is valuable to cut in a direction
indices1=ar.nonzero()#first get white pixels
#ar.fill(True)
satisfypix=3#cutterimagepix/10#a threshold which says if it is valuable to cut in a direction
indices=ar.nonzero()#first get white pixels
startpix=ar.sum()#
totpix=startpix
chunks=[]
nchunk=[indices1[0]]#startposition
nchunk=camPathChunk([(indices[0][0],indices[1][0])])#startposition
print(indices)
print (indices[0][0],indices[1][0])
lastvect=Vector((r,0,0))#vector is 3d, blender somehow doesn't rotate 2d vectors with angles.
testvect=lastvect.normalized()*2#multiply *2 not to get values <1 pixel
rot=Euler((0,0,1))
i=0
perc=0
itests=0
totaltests=0
maxtests=500
while totpix>startpix/3 or i<maxtests:#a ratio when the algorithm is allowed to end
maxtotaltests=300000
xs=nchunk.points[-1][0]
ys=nchunk.points[-1][1]
ar[xs-r:xs-r+d,ys-r:ys-r+d]=ar[xs-r:xs-r+d,ys-r:ys-r+d]*cutterArrayNegative
while totpix>startpix*0.01 and totaltests<maxtotaltests:#a ratio when the algorithm is allowed to end
#if perc!=int(100*totpix/startpix):
# perc=int(100*totpix/startpix)
@ -1095,34 +1112,52 @@ def crazyStrokeImage(o,ar):
testlength=r
while not success:
xs=lastvect.x+int(testvect.x)
ys=lastvect.y+int(testvect.y)
xs=nchunk.points[-1][0]+int(testvect.x)
ys=nchunk.points[-1][1]+int(testvect.y)
if xs>r+1 and xs<ar.shape[0]-r-1 and ys>r+1 and ys<ar.shape[1]-r-1 :
testar=ar[xs-r:xs-r+d,ys-r:ys-r+d]*cutterArray
if 0:
print('test')
print(testar.sum(),satisfypix)
print(xs,ys,testlength,testangle)
print(lastvect)
print(testvect)
print(totpix)
if testar.sum()>satisfypix:
success=True
if success:
nchunk.append([xs,ys])
lastvect=Vector((xs,ys,0))
ar[xs-r:xs-r+d,ys-r:ys-r+d]=ar[xs-r:xs-r+d,ys-r:ys-r+d]*cutterArray
print('success')
print(xs,ys,testlength,testangle)
print(lastvect)
print(testvect)
nchunk.points.append([xs,ys])
lastvect=testvect
ar[xs-r:xs-r+d,ys-r:ys-r+d]=ar[xs-r:xs-r+d,ys-r:ys-r+d]*(-cutterArray)
if 0:
print('success')
print(xs,ys,testlength,testangle)
print(lastvect)
print(testvect)
print(itests)
else:
nchunk.append([xs,ys])#for debugging purpose
#nchunk.append([xs,ys])#for debugging purpose
#ar.shape[0]
testvect=lastvect.normalized()*testlength
if testleftright:
testangle=-testangle
testleftright=False
else:
testangle=abs(testangle)+0.005#increment angle
testangle=abs(testangle)+0.05#increment angle
testleftright=True
if testangle>1:#/testlength
if abs(testangle)>1:#/testlength
testangle=0
testlength+=2
testlength+=r/2
if nchunk.points[-1][0]+testvect.x<r:
testvect.x=r
if nchunk.points[-1][1]+testvect.y<r:
testvect.y=r
if nchunk.points[-1][0]+testvect.x>maxarx-r:
testvect.x=maxarx-r
if nchunk.points[-1][1]+testvect.y>maxary-r:
testvect.y=maxary-r
'''
if testlength>10:#weird test
indices1=ar.nonzero()
@ -1135,23 +1170,45 @@ def crazyStrokeImage(o,ar):
rot.z=testangle
testvect.rotate(rot)
#print(xs,ys,testlength,testangle)
#print(lastvect)
#print(testvect)
if 0:
print(xs,ys,testlength,testangle)
print(lastvect)
print(testvect)
print(totpix)
itests+=1
i+=1
totaltests+=1
#achjo
if i>maxtests:
if itests>maxtests:
#print('resetting location')
indices=ar.nonzero()
chunks.append(nchunk)
nchunk=camPathChunk([(indices[0][0],indices[1][0])])#startposition
xs=nchunk.points[-1][0]
ys=nchunk.points[-1][1]
ar[xs-r:xs-r+d,ys-r:ys-r+d]=ar[xs-r:xs-r+d,ys-r:ys-r+d]*cutterArrayNegative
#lastvect=Vector((r,0,0))#vector is 3d, blender somehow doesn't rotate 2d vectors with angles.
testvect=lastvect.normalized()*2#multiply *2 not to get values <1 pixel
lastvect=testvect.copy()
success=True
i=0
itests=0
#xs=(s.x-o.min.x)/o.simulation_detail+o.borderwidth+o.simulation_detail/2#-m
#ys=(s.y-o.min.y)/o.simulation_detail+o.borderwidth+o.simulation_detail/2#-m
i+=1
if i==10:
print('10 tests done')
if i%10==0:
print('10 succesfull tests done')
totpix=ar.sum()
return [nchunk]
print(totpix)
print(totaltests)
i=0
chunks.append(nchunk)
for ch in chunks:
#vecchunk=[]
#vecchunks.append(vecchunk)
ch=ch.points
for i in range(0,len(ch)):
ch[i]=((ch[i][0]+coef-o.borderwidth)*o.pixsize+minx,(ch[i][1]+coef-o.borderwidth)*o.pixsize+miny,0)
#vecchunk.append(Vector(ch[i]))
return chunks
def imageToChunks(o,image):
t=time.time()