def draw_all(screen, polylines, holes, triangles, emsize = 1024, zoom = 1.0, polylinecolor = green, holecolor = blue, trianglecolor = red): """This function takes the list of polylines and holes and the triangulation, and draws it in pygame. This function is pending deprecation.""" global args if trianglecolor is not None: for t in triangles: a = get_triangle_point(t, 0) b = get_triangle_point(t, 1) c = get_triangle_point(t, 2) x1 = int(ux(a) * zoom) y1 = int((emsize-uy(a)) * zoom) x2 = int(ux(b) * zoom) y2 = int((emsize-uy(b)) * zoom) x3 = int(ux(c) * zoom) y3 = int((emsize-uy(c)) * zoom) trigon(screen, x1, y1, x2, y2, x3, y3, trianglecolor) # Close the polylines loop again prior to drawing if polylinecolor is not None: for polyline in polylines: if hasattr(polyline, 'coords'): polyline = list(polyline.coords) polyline.append(polyline[0]) flipped = flip_polyline(polyline, emsize) for a, b in pairwise(flipped): x1 = int(a[0] * zoom) y1 = int(a[1] * zoom) x2 = int(b[0] * zoom) y2 = int(b[1] * zoom) line(screen, x1, y1, x2, y2, polylinecolor) # Same for holes if holecolor is not None: for hole in holes: if hasattr(hole, 'coords'): hole = list(hole.coords) hole.append(hole[0]) flipped = flip_polyline(hole, emsize) for a, b in pairwise(flipped): x1 = int(a[0] * zoom) y1 = int(a[1] * zoom) x2 = int(b[0] * zoom) y2 = int(b[1] * zoom) line(screen, x1, y1, x2, y2, holecolor) # Show result pygame.display.update()
def draw_mesh(self, params, mesh, fill=True): vert, tria = mesh.vertices, mesh.triangles vert_p = self.project_array(vert) surf = params['draw_surf'] dist = lambda x: np.linalg.norm(self.camera_position - vert[x[0]]) if fill: for i, t in enumerate(sorted(tria, key=dist, reverse=True)): # d = dist(t) c = (255, 255, 255) try: pgfx.trigon(surf, *vert_p[t[0]], *vert_p[t[1]], *vert_p[t[2]], c) except: pass
def draw_mesh(self, params, mesh, fill=True): # start = pc() vert, tria = mesh.vertices, mesh.triangles vert_p = [tuple(self.project(x)) for x in vert] # print('Project time (s):', pc()-start) # start = pc() surf = params['draw_surf'] dist = lambda x: np.linalg.norm(self.camera_position - vert[x[0]]) if fill: for i, t in enumerate(sorted(tria, key=dist, reverse=True)): d = dist(t) # c = (min(255,255//math.sqrt(d/3)),min(120,120//math.sqrt(d/3)),min(255,255//math.sqrt(d/3))) c = (255, 255, 255) pgfx.trigon(surf, *vert_p[t[0]], *vert_p[t[1]], *vert_p[t[2]], c)
def trigon(self, x1, y1, x2, y2, x3, y3, color): """三角形を描画します. Parameters ---------- x1 : int 三角形の1つめの頂点のx座標. y1 : int 三角形の1つめの頂点のy座標. x2 : int 三角形の2つめの頂点のx座標. y2 : int 三角形の2つめの頂点のy座標. x3 : int 三角形の3つめの頂点のx座標. y3 : int 三角形の3つめの頂点のy座標. color : tuple of int 描画に使用される色を指定します. """ return gfx.trigon(self.pg.screen, x1, y1, x2, y2, x3, y3, color)
def main(file_name_map, file_name_obs, translate, zoom, num_tasks): SCREEN_SIZE = 360,240 pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE,0,8) pygame.display.set_caption('poly2tri demo') pygame.mouse.set_visible(True) black = Color(0,0,0) red = Color(0, 0, 0) green = Color(255, 255, 255) yellow = Color(255, 255, 0) screen.fill(black) points = load_points(file_name_map) (holes,bbpath) = load_holes(file_name_obs) polyline = [] for p in points: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] polyline.append(Point(p[0],p[1])) # initialize clock t0 = clock() ## ## Step 1: Initialize ## NOTE: polyline must be a simple polygon. The polyline's points ## constitute constrained edges. No repeat points!!! ## cdt = CDT(polyline) ## ## Step 2: Add holes and interior Steiner points if necessary ## # xarr = [229, 236, 214, 285, 256, 265] # yarr = [193, 224, 246, 190, 260, 214] hole_arr = [] # obstacles = holes for h in holes: hole = [] for p in h: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0], p[1])) cdt.add_hole(hole) hole_arr.extend(hole) i = 0 xarr = [] yarr = [] while i<num_tasks: x = random.randint(1,119) y = random.randint(1, 79) if checkinPoly((x,y), bbpath): px = x*zoom + translate[0] py = y * zoom + translate[1] if(px in xarr and py in yarr): continue xarr.append(x*zoom +translate[0]) yarr.append(y*zoom +translate[1]) cdt.add_point(Point(x*zoom +translate[0],y*zoom +translate[1])) i = i + 1 ## Step 3: Triangulate ## triangles = cdt.triangulate() print "Elapsed time (ms) = " + str(clock()*1000.0) # The Main Event Loop done = False vertexArr = [] g = Graph(len(points) + len(hole_arr) + len(xarr)) for t in triangles: x1 = int(t.a.x) y1 = int(t.a.y) x2 = int(t.b.x) y2 = int(t.b.y) x3 = int(t.c.x) y3 = int(t.c.y) i1 = -1; i2 = -1; i3 = -1; for i in range(len(vertexArr)): if(vertexArr[i].x == x1 and vertexArr[i].y == y1): i1 = i elif(vertexArr[i].x == x2 and vertexArr[i].y == y2): i2 = i elif(vertexArr[i].x == x3 and vertexArr[i].y == y3): i3 = i if(i1 == -1): vertexArr.append(Point(x1, y1)) i1 = len(vertexArr) - 1 if(i2 == -1): vertexArr.append(Point(x2, y2)) i2 = len(vertexArr) - 1 if(i3 == -1): vertexArr.append(Point(x3, y3)) i3 = len(vertexArr) - 1 trigon(screen, x1, y1, x2, y2, x3, y3, red) g.addEdge(i1, i2, int(dist(Point(x1, y1), Point(x2, y2)))) g.addEdge(i3, i2, int(dist(Point(x2, y2), Point(x3, y3)))) g.addEdge(i1, i3, int(dist(Point(x3, y3), Point(x1, y1)))) g.FloydWarshall() guard = [] print "look " + str(len(xarr)) + str(len(vertexArr)) for i in range(g.sz): for j in range(len(xarr)): if(xarr[j] == int(vertexArr[i].x) and yarr[j] == int(vertexArr[i].y)): guard.append(i) g.guard = guard (rcdtgrid, parts, mapping) = g.GraphReduction() colors = [] for i in range(len(parts)): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) colors.append(Color(r, g, b)) flag = 0 while not done: # Draw outline for i in range(len(points)): j = i+1 if i < len(points) - 1 else 0 x1 = int(points[i][0]) y1 = int(points[i][1]) x2 = int(points[j][0]) y2 = int(points[j][1]) line(screen, x1, y1, x2, y2, green) # Draw holes if necessary for obs in holes: for i in range(len(obs)): j = i+1 if i < len(obs) - 1 else 0 x1 = int(obs[i][0]) y1 = int(obs[i][1]) x2 = int(obs[j][0]) y2 = int(obs[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(vertexArr)): for j in range(len(vertexArr)): if(i not in mapping): continue if(j not in mapping): continue if parts[mapping.index(i)] == 1: pygame.draw.circle(screen, (255, 0, 0), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) elif parts[mapping.index(i)] == 0: pygame.draw.circle(screen, (0, 255, 0), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) elif parts[mapping.index(i)] == 2: pygame.draw.circle(screen, (0, 0, 255), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) else: pygame.draw.circle(screen, (255, 255, 255), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) if(rcdtgrid[i][j] == 1000000007 or parts[mapping.index(i)] != parts[mapping.index(j)]): continue if not flag: print mapping.index(i) line(screen, int(vertexArr[i].x), int(vertexArr[i].y), int(vertexArr[j].x), int(vertexArr[j].y), colors[parts[mapping.index(i)]]) flag = 1 # Update the screen pygame.display.update() # Event Handling: events = pygame.event.get( ) for e in events: if( e.type == QUIT ): done = True break elif (e.type == KEYDOWN): if( e.key == K_ESCAPE ): done = True break if( e.key == K_f ): pygame.display.toggle_fullscreen() return
def main(file_name, translate, zoom): SCREEN_SIZE = 800, 600 pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE, 0, 8) pygame.display.set_caption('poly2tri demo') pygame.mouse.set_visible(True) black = Color(0, 0, 0) red = Color(255, 0, 0) green = Color(0, 255, 0) screen.fill(black) points = load_points(file_name) polyline = [] for p in points: p[0] = p[0] * zoom + translate[0] p[1] = p[1] * zoom + translate[1] polyline.append(Point(p[0], p[1])) # initialize clock t0 = clock() ## ## Step 1: Initialize ## NOTE: polyline must be a simple polygon. The polyline's points ## constitute constrained edges. No repeat points!!! ## cdt = CDT(polyline) ## ## Step 2: Add holes and interior Steiner points if necessary ## if file_name == "data/dude.dat": hole = [] for p in head_hole: p[0] = p[0] * zoom + translate[0] p[1] = p[1] * zoom + translate[1] hole.append(Point(p[0], p[1])) # Add a hole cdt.add_hole(hole) hole = [] for p in chest_hole: p[0] = p[0] * zoom + translate[0] p[1] = p[1] * zoom + translate[1] hole.append(Point(p[0], p[1])) # Add a hole cdt.add_hole(hole) # Add an interior Steiner point x = 361 * zoom + translate[0] y = 381 * zoom + translate[1] cdt.add_point(Point(x, y)) ## ## Step 3: Triangulate ## triangles = cdt.triangulate() print("Elapsed time (ms) = " + str(clock() * 1000.0)) # The Main Event Loop done = False while not done: # Draw triangles for t in triangles: x1 = int(t.a.x) y1 = int(t.a.y) x2 = int(t.b.x) y2 = int(t.b.y) x3 = int(t.c.x) y3 = int(t.c.y) trigon(screen, x1, y1, x2, y2, x3, y3, red) # Draw outline for i in range(len(points)): j = i + 1 if i < len(points) - 1 else 0 x1 = int(points[i][0]) y1 = int(points[i][1]) x2 = int(points[j][0]) y2 = int(points[j][1]) line(screen, x1, y1, x2, y2, green) # Draw holes if necessary if file_name == "data/dude.dat": for i in range(len(head_hole)): j = i + 1 if i < len(head_hole) - 1 else 0 x1 = int(head_hole[i][0]) y1 = int(head_hole[i][1]) x2 = int(head_hole[j][0]) y2 = int(head_hole[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(chest_hole)): j = i + 1 if i < len(chest_hole) - 1 else 0 x1 = int(chest_hole[i][0]) y1 = int(chest_hole[i][1]) x2 = int(chest_hole[j][0]) y2 = int(chest_hole[j][1]) line(screen, x1, y1, x2, y2, green) # Update the screen pygame.display.update() # Event Handling: events = pygame.event.get() for e in events: if (e.type == QUIT): done = True break elif (e.type == KEYDOWN): if (e.key == K_ESCAPE): done = True break if (e.key == K_f): pygame.display.toggle_fullscreen() return
def main(file_name, translate, zoom): SCREEN_SIZE = 800,600 pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE,0,8) pygame.display.set_caption('poly2tri demo') pygame.mouse.set_visible(True) black = Color(0,0,0) red = Color(255, 0, 0) green = Color(0, 255, 0) screen.fill(black) points = load_points(file_name) polyline = [] for p in points: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] polyline.append(Point(p[0],p[1])) # initialize clock t0 = clock() ## ## Step 1: Initialize ## NOTE: polyline must be a simple polygon. The polyline's points ## constitute constrained edges. No repeat points!!! ## cdt = CDT(polyline) ## ## Step 2: Add holes and interior Steiner points if necessary ## if file_name == "data/dude.dat": hole = [] for p in head_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) # Add a hole cdt.add_hole(hole) hole = [] for p in chest_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) # Add a hole cdt.add_hole(hole) # Add an interior Steiner point x = 361*zoom + translate[0] y = 381*zoom + translate[1] cdt.add_point(Point(x, y)) ## ## Step 3: Triangulate ## triangles = cdt.triangulate() print "Elapsed time (ms) = " + str(clock()*1000.0) # The Main Event Loop done = False while not done: # Draw triangles for t in triangles: x1 = int(t.a.x) y1 = int(t.a.y) x2 = int(t.b.x) y2 = int(t.b.y) x3 = int(t.c.x) y3 = int(t.c.y) trigon(screen, x1, y1, x2, y2, x3, y3, red) # Draw outline for i in range(len(points)): j = i+1 if i < len(points) - 1 else 0 x1 = int(points[i][0]) y1 = int(points[i][1]) x2 = int(points[j][0]) y2 = int(points[j][1]) line(screen, x1, y1, x2, y2, green) # Draw holes if necessary if file_name == "data/dude.dat": for i in range(len(head_hole)): j = i+1 if i < len(head_hole) - 1 else 0 x1 = int(head_hole[i][0]) y1 = int(head_hole[i][1]) x2 = int(head_hole[j][0]) y2 = int(head_hole[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(chest_hole)): j = i+1 if i < len(chest_hole) - 1 else 0 x1 = int(chest_hole[i][0]) y1 = int(chest_hole[i][1]) x2 = int(chest_hole[j][0]) y2 = int(chest_hole[j][1]) line(screen, x1, y1, x2, y2, green) # Update the screen pygame.display.update() # Event Handling: events = pygame.event.get( ) for e in events: if( e.type == QUIT ): done = True break elif (e.type == KEYDOWN): if( e.key == K_ESCAPE ): done = True break if( e.key == K_f ): pygame.display.toggle_fullscreen() return
def main(file_name, translate, zoom): SCREEN_SIZE = 800,600 pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE,0,8) pygame.display.set_caption('poly2tri demo') pygame.mouse.set_visible(True) black = Color(0,0,0) red = Color(0, 0, 0) green = Color(255, 255, 255) yellow = Color(255, 255, 0) screen.fill(black) points = load_points(file_name) polyline = [] for p in points: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] polyline.append(Point(p[0],p[1])) # initialize clock t0 = clock() ## ## Step 1: Initialize ## NOTE: polyline must be a simple polygon. The polyline's points ## constitute constrained edges. No repeat points!!! ## cdt = CDT(polyline) ## ## Step 2: Add holes and interior Steiner points if necessary ## if file_name == "data/dude.dat": hole = [] for p in head_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) # Add a hole cdt.add_hole(hole) hole = [] for p in chest_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) # Add a hole cdt.add_hole(hole) # Add an interior Steiner point x = 361*zoom + translate[0] y = 381*zoom + translate[1] cdt.add_point(Point(x, y)) # xarr = [323, 372, 334, 424, 342, 352, 100, 567] # yarr = [395, 410, 645, 546, 620, 534, 370, 387] xarr = [229, 236, 214, 285, 256, 265] yarr = [193, 224, 246, 190, 260, 214] if file_name == "data/test.dat": for i in range(len(xarr)): x = xarr[i]*zoom + translate[0] y = yarr[i]*zoom + translate[1] xarr[i] = x yarr[i] = y cdt.add_point(Point(x, y)) if file_name == "data/map.dat": for i in range(len(xarr)): x = xarr[i]*zoom + translate[0] y = yarr[i]*zoom + translate[1] xarr[i] = x yarr[i] = y cdt.add_point(Point(x, y)) hole_arr = [] if file_name == "data/map.dat": for obs_hole in obstacle: hole = [] for p in obs_hole: p[0] = p[0]*zoom + translate[0] p[1] = p[1]*zoom + translate[1] hole.append(Point(p[0],p[1])) cdt.add_hole(hole) hole_arr.extend(hole) ## ## Step 3: Triangulate ## triangles = cdt.triangulate() print "Elapsed time (ms) = " + str(clock()*1000.0) # The Main Event Loop done = False vertexArr = [] g = Graph(len(points) + len(hole_arr) + len(xarr)) for t in triangles: x1 = int(t.a.x) y1 = int(t.a.y) x2 = int(t.b.x) y2 = int(t.b.y) x3 = int(t.c.x) y3 = int(t.c.y) i1 = -1; i2 = -1; i3 = -1; for i in range(len(vertexArr)): if(vertexArr[i].x == x1 and vertexArr[i].y == y1): i1 = i elif(vertexArr[i].x == x2 and vertexArr[i].y == y2): i2 = i elif(vertexArr[i].x == x3 and vertexArr[i].y == y3): i3 = i if(i1 == -1): vertexArr.append(Point(x1, y1)) i1 = len(vertexArr) - 1 if(i2 == -1): vertexArr.append(Point(x2, y2)) i2 = len(vertexArr) - 1 if(i3 == -1): vertexArr.append(Point(x3, y3)) i3 = len(vertexArr) - 1 trigon(screen, x1, y1, x2, y2, x3, y3, red) g.addEdge(i1, i2, int(dist(Point(x1, y1), Point(x2, y2)))) g.addEdge(i3, i2, int(dist(Point(x2, y2), Point(x3, y3)))) g.addEdge(i1, i3, int(dist(Point(x3, y3), Point(x1, y1)))) g.FloydWarshall() guard = [] print "look " + str(len(xarr)) + str(len(vertexArr)) for i in range(g.sz): for j in range(len(xarr)): if(xarr[j] == int(vertexArr[i].x) and yarr[j] == int(vertexArr[i].y)): guard.append(i) g.guard = guard (rcdtgrid, parts, mapping) = g.GraphReduction() colors = [] for i in range(len(parts)): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) colors.append(Color(r, g, b)) flag = 0 while not done: # Draw outline for i in range(len(points)): j = i+1 if i < len(points) - 1 else 0 x1 = int(points[i][0]) y1 = int(points[i][1]) x2 = int(points[j][0]) y2 = int(points[j][1]) line(screen, x1, y1, x2, y2, green) # Draw holes if necessary if file_name == "data/dude.dat": for i in range(len(head_hole)): j = i+1 if i < len(head_hole) - 1 else 0 x1 = int(head_hole[i][0]) y1 = int(head_hole[i][1]) x2 = int(head_hole[j][0]) y2 = int(head_hole[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(chest_hole)): j = i+1 if i < len(chest_hole) - 1 else 0 x1 = int(chest_hole[i][0]) y1 = int(chest_hole[i][1]) x2 = int(chest_hole[j][0]) y2 = int(chest_hole[j][1]) line(screen, x1, y1, x2, y2, green) if file_name == "data/map.dat": for obs in obstacle: for i in range(len(obs)): j = i+1 if i < len(obs) - 1 else 0 x1 = int(obs[i][0]) y1 = int(obs[i][1]) x2 = int(obs[j][0]) y2 = int(obs[j][1]) line(screen, x1, y1, x2, y2, green) for i in range(len(vertexArr)): for j in range(len(vertexArr)): if(i not in mapping): continue if(j not in mapping): continue if parts[mapping.index(i)] == 1: pygame.draw.circle(screen, (255, 0, 0), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) elif parts[mapping.index(i)] == 0: pygame.draw.circle(screen, (0, 255, 0), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) else: pygame.draw.circle(screen, (0, 0, 255), (int(vertexArr[i].x), int(vertexArr[i].y)), 3, 0) if(rcdtgrid[i][j] == 1000000007):# or parts[mapping.index(i)] != parts[mapping.index(j)]): continue if not flag: print mapping.index(i) line(screen, int(vertexArr[i].x), int(vertexArr[i].y), int(vertexArr[j].x), int(vertexArr[j].y), colors[parts[mapping.index(i)]]) flag = 1 # Update the screen pygame.display.update() # Event Handling: events = pygame.event.get( ) for e in events: if( e.type == QUIT ): done = True break elif (e.type == KEYDOWN): if( e.key == K_ESCAPE ): done = True break if( e.key == K_f ): pygame.display.toggle_fullscreen() return