2*2网格划分为三角网
def get_triangle_bit(v1, v2, v3):
return v1 << 2 | v2 << 1 | v3
def get_triangle_shift(bitval):
if bitval == 0 or bitval == 7:
return (0,None,None,None,None)
if bitval == 1 or bitval == 6:
return (1,-0.5,0.5,0,1)
if bitval == 2 or bitval == 5:
return (1,0,1,0.5,0.5)
if bitval == 3 or bitval == 4:
return (1,-0.5,0.5,0.5,0.5)
class MarchSquareTriangleUtlis(MarchSquareUtlis):
def trancing_contours(self):
ret = []
width,height = self.net.net_info.shape
arr = self.net.net_info
for i in range(width-1):
for j in range(height-1):
v1 = int(arr[i][j])
v2 = int(arr[i + 1][j])
v3 = int(arr[i + 1][j + 1])
v4 = int(arr[i][j + 1])
bitv = get_triangle_bit(v1,v2,v4)
#net_shift = get_triangle_shift(bitv)
#ret.append(net_shift)
ret.append(bitv)
#again
bitv = get_triangle_bit(v2,v3,v4)
#net_shift = get_triangle_shift(bitv)
#ret.append(net_shift)
ret.append(bitv)
return ret
class PlotTriangleDemo(PlotDemo):
def show_contour(self):
self._set_default_figure(self.m, self.n)
net = self._net
utils = MarchSquareTriangleUtlis(net)
lines = utils.trancing_contours()
width,height = net.net_info.shape
arr = net.net_info
idx = 0
for i in range(width-1):
for j in range(height-1):
x,y = i,j
bitval = lines[idx]
idx = idx + 1
self._plot_by_bitval_left(bitval,x,y)
bitval = lines[idx]
idx = idx + 1
self._plot_by_bitval_right(bitval,x+1,y)
show()
def _plot_by_bitval_left(self, bitval, topleftx, toplefty):
if bitval == 0 or bitval == 7:
pass
if bitval == 1 or bitval == 6:
x1 = topleftx + 0
y1 = toplefty + 0.5
x2 = topleftx + 0.5
y2 = toplefty + 0.5
plot(x1,y1,x2,y2,'ro')
if bitval == 2 or bitval == 5:
x1 = topleftx + 0.5
y1 = toplefty + 0
x2 = topleftx + 0.5
y2 = toplefty + 0.5
plot(x1,y1,x2,y2,'ro')
if bitval == 3 or bitval == 4:
x1 = topleftx + 0
y1 = toplefty + 0.5
x2 = topleftx + 0.5
y2 = toplefty + 0.5
plot(x1,y1,x2,y2,'ro')
def _plot_by_bitval_right(self, bitval, topleftx, toplefty):
if bitval == 0 or bitval == 7:
pass
if bitval == 1 or bitval == 6:
x1 = topleftx - 0.5
y1 = toplefty + 0.5
x2 = topleftx + 0.5
y2 = toplefty + 1
plot(x1,y1,x2,y2,'ro')
if bitval == 2 or bitval == 5:
x1 = topleftx - 0.5
y1 = toplefty + 1
x2 = topleftx + 0
y2 = toplefty + 0.5
plot(x1,y1,x2,y2,'ro')
if bitval == 3 or bitval == 4:
x1 = topleftx - 0.5
y1 = toplefty + 0.5
x2 = topleftx + 0
y2 = toplefty + 0.5
plot(x1,y1,x2,y2,'ro')
demo = PlotTriangleDemo(100,100)
netinfo = RandomGenNet(100,100)
netinfo.add_circle(20,20,10,1)
netinfo.add_circle(70,50,20,1)
netinfo.add_retangle(20,45,40,20,1)
netinfo.add_retangle(70,50,10,10,1)
demo.set_net_info(netinfo)
demo.show_contour()