From f321369818f31ae140d2ba2da6d79f13c9071711 Mon Sep 17 00:00:00 2001 From: "vilda.novak@gmail.com" Date: Wed, 28 Aug 2013 20:13:55 +0000 Subject: [PATCH] pencil strategy experiments with searching algo --- scripts/addons/cam/utils.py | 125 ++++++++++++++++++++++++++---------- 1 file changed, 91 insertions(+), 34 deletions(-) diff --git a/scripts/addons/cam/utils.py b/scripts/addons/cam/utils.py index 7f17bb37..a8f1fb1c 100644 --- a/scripts/addons/cam/utils.py +++ b/scripts/addons/cam/utils.py @@ -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 istartpix*0.01 and totaltestsr+1 and xsr+1 and yssatisfypix: 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.xmaxarx-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()