Marching squares (triangle)

来源:互联网 发布:安米app源码 编辑:程序博客网 时间:2024/05/01 08:08

2*2网格划分为三角网

def get_triangle_bit(v1, v2, v3):return v1 << 2 | v2 << 1 | v3def 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.shapearr = self.net.net_infofor 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)#againbitv = get_triangle_bit(v2,v3,v4)#net_shift = get_triangle_shift(bitv)#ret.append(net_shift)ret.append(bitv)return retclass PlotTriangleDemo(PlotDemo):def show_contour(self):self._set_default_figure(self.m, self.n)net = self._netutils = MarchSquareTriangleUtlis(net)lines = utils.trancing_contours()width,height = net.net_info.shapearr = net.net_infoidx = 0for i in range(width-1):for j in range(height-1):x,y = i,jbitval = lines[idx]idx = idx + 1self._plot_by_bitval_left(bitval,x,y)bitval = lines[idx]idx = idx + 1self._plot_by_bitval_right(bitval,x+1,y)show()def _plot_by_bitval_left(self, bitval, topleftx, toplefty):if bitval == 0 or bitval == 7:passif bitval == 1 or bitval == 6:x1 = topleftx + 0y1 = toplefty + 0.5x2 = topleftx + 0.5y2 = toplefty + 0.5plot(x1,y1,x2,y2,'ro')if bitval == 2 or bitval == 5:x1 = topleftx + 0.5y1 = toplefty + 0x2 = topleftx + 0.5y2 = toplefty + 0.5plot(x1,y1,x2,y2,'ro')if bitval == 3 or bitval == 4:x1 = topleftx + 0y1 = toplefty + 0.5x2 = topleftx + 0.5y2 = toplefty + 0.5plot(x1,y1,x2,y2,'ro')def _plot_by_bitval_right(self, bitval, topleftx, toplefty):if bitval == 0 or bitval == 7:passif bitval == 1 or bitval == 6:x1 = topleftx - 0.5y1 = toplefty + 0.5x2 = topleftx + 0.5y2 = toplefty + 1plot(x1,y1,x2,y2,'ro')if bitval == 2 or bitval == 5:x1 = topleftx - 0.5y1 = toplefty + 1x2 = topleftx + 0y2 = toplefty + 0.5plot(x1,y1,x2,y2,'ro')if bitval == 3 or bitval == 4:x1 = topleftx - 0.5y1 = toplefty + 0.5x2 = topleftx + 0y2 = toplefty + 0.5plot(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()