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

问题描述 投票:2回答:1

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()
python pygame delaunay
1个回答
0
投票

在迭代同一列表时,您无法从列表中.remove()个元素。删除项目后,列表将更改:

.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

或创建一个新列表:

onSuper = lambda triangle : triangle.HasVertex(superTriangleA) or triangle.HasVertex(superTriangleB) or triangle.HasVertex(superTriangleC)

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.