gpt4 book ai didi

python - 我实现的 Bowyer-Watson Delaunay 三角剖分不会删除包含超三角形点的三角形

转载 作者:太空狗 更新时间:2023-10-30 01:04:09 28 4
gpt4 key购买 nike

Python 3.7.2,尽管我怀疑这条信息是否非常有用。

Pygame 1.7.2。我主要用它来绘制三角剖分。所有计算均使用普通公式完成。

根据维基百科,Bowyer-Watson 算法的伪代码如下所示:

function BowyerWatson (pointList)
// pointList is a set of coordinates defining the points to be triangulated
triangulation := empty triangle mesh data structure
add super-triangle to triangulation // must be large enough to completely contain all the points in pointList
for each point in pointList do // add all the points one at a time to the triangulation
badTriangles := empty set
for each triangle in triangulation do // first find all the triangles that are no longer valid due to the insertion
if point is inside circumcircle of triangle
add triangle to badTriangles
polygon := empty set
for each triangle in badTriangles do // find the boundary of the polygonal hole
for each edge in triangle do
if edge is not shared by any other triangles in badTriangles
add edge to polygon
for each triangle in badTriangles do // remove them from the data structure
remove triangle from triangulation
for each edge in polygon do // re-triangulate the polygonal hole
newTri := form a triangle from edge to point
add newTri to triangulation
for each triangle in triangulation // done inserting points, now clean up
if triangle contains a vertex from original super-triangle
remove triangle from triangulation
return triangulation

但是,当我运行我的代码时,虽然它从最终三角剖分中删除了一些具有超三角形顶点的三角形,但它并没有删除所有这些三角形。

这是我的代码:

import pygame
import pygame.gfxdraw
import math
import random

pygame.init()

def circumcenter(a, b, c):
ad = a[0] * a[0] + a[1] * a[1]
bd = b[0] * b[0] + b[1] * b[1]
cd = c[0] * c[0] + c[1] * c[1]
D = 2 * (a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]))
return pygame.Vector2((1 / D * (ad * (b[1] - c[1]) + bd * (c[1] - a[1]) + cd * (a[1] - b[1])),
1 / D * (ad * (c[0] - b[0]) + bd * (a[0] - c[0]) + cd * (b[0] - a[0]))))

def LineIsEqual(line1,line2):
if (line1[0] == line2[0] and line1[1] == line2[1]) or (line1[0] == line2[1] and line1[1] == line2[0]):
return True
return False

def distance(point1,point2):
return math.sqrt((point1[0]-point2[0])**2 + (point1[1]-point2[1])**2)

class Triangle:
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
self.edges = [[self.a,self.b],
[self.b,self.c],
[self.c,self.a]]
self.circumcenter = circumcenter(a,b,c)
def IsPointInCircumcircle(self,point):
if (self.a.distance_to(self.circumcenter) > point.distance_to(self.circumcenter)):
return True
return False
def HasVertex(self,point):
if (self.a == point) or (self.b == point) or (self.c == point):
return True
return False
def Show(self,screen,colour):
for edge in self.edges:
pygame.draw.aaline(screen,colour,edge[0],edge[1])


def DelaunayTriangulation(points,width,height):

triangulation = []
superTriangleA = pygame.Vector2(-100,-100)
superTriangleB = pygame.Vector2(2*width+100,-100)
superTriangleC = pygame.Vector2(-100,2*height+100)
superTriangle = Triangle(superTriangleA,superTriangleB,superTriangleC)
triangulation.append(superTriangle)

for point in points:

badTriangles = []
for triangle in triangulation:
if triangle.IsPointInCircumcircle(point):
badTriangles.append(triangle)

polygon = []
for triangle in badTriangles:
for triangleEdge in triangle.edges:
isShared = False
for other in badTriangles:
if triangle == other:
continue
for otherEdge in other.edges:
if LineIsEqual(triangleEdge,otherEdge):
isShared = True
if isShared == False:
polygon.append(triangleEdge)

for badTriangle in badTriangles:
triangulation.remove(badTriangle)

for edge in polygon:
newTriangle = Triangle(edge[0],edge[1],point)
triangulation.append(newTriangle)

for triangle in triangulation:
if triangle.HasVertex(superTriangleA) and triangle in triangulation:
triangulation.remove(triangle)
if triangle.HasVertex(superTriangleB) and triangle in triangulation:
triangulation.remove(triangle)
if triangle.HasVertex(superTriangleC) and triangle in triangulation:
triangulation.remove(triangle)

return triangulation

background = 20,40,100
white = 255,255,255
width = int(500)
height = int(500)
amount = int(100)

screen = pygame.display.set_mode((width,height))
screen.fill(background)

points = []
for i in range(amount):
x = random.randint(1,width-1)
y = random.randint(1,height-1)
points.append(pygame.Vector2(x,y))

delaunay = DelaunayTriangulation(points,width,height)
for triangle in delaunay:
triangle.Show(screen,white)

pygame.display.update()

最佳答案

你不能.remove()列表中的元素,同时迭代相同的列表。当一个项目被删除时,列表会发生变化:

for triangle in triangulation:
if triangle.HasVertex(superTriangleA) and triangle in triangulation:
triangulation.remove(triangle)

有一些解决方案可以解决这个问题。

一个解决方案是创建一个 shallow copy triangulation[:] 的列表:

onSuper = lambda triangle : triangle.HasVertex(superTriangleA) or triangle.HasVertex(superTriangleB) or triangle.HasVertex(superTriangleC)
for triangle in triangulation[:]:
if onSuper(triangle):
triangulation.remove(triangle)

或者创建一个要删除的三角形列表(to_be_removed)并将它们从列表中删除:

to_be_removed = []
for triangle in triangulation:
if onSuper(triangle):
to_be_removed.append(triangle)
for triangle in to_be_removed:
triangulation.remove(triangle)

或者创建一个新列表:

triangulation = [triangle for triangle in triangulation if not onSuper(triangle)]

所有上述解决方案都导致相同的三角剖分。

关于python - 我实现的 Bowyer-Watson Delaunay 三角剖分不会删除包含超三角形点的三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58116412/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com