从使用matplotlib生成的delaunay三角网获取外接圆中心

k10s72fa  于 2023-06-23  发布在  其他
关注(0)|答案(3)|浏览(113)

如果我使用matplotlib为一组点生成一个delaunay三角剖分,那么得到三角形外接圆中心的最合适的方法是什么?我还没有设法找到一个明显的方法在三角图书馆做到这一点。

zbdgwd5y

zbdgwd5y1#

你应该可以使用matplotlib.delaunay.triangulate.Triangulation来计算它:
Triangulation(x,y)x,y --浮点数的一维数组形式的点的坐标
……
属性:(为了保持一致性,所有属性都应该被视为只读)x,y --作为浮点数的一维数组的点的坐标。

circumcenters -- (ntriangles, 2) array of floats giving the (x,y)
    coordinates of the circumcenters of each triangle (indexed by a triangle_id).

改编自matplotlib的一个例子(可能有一种更干净的方法来做到这一点,但它应该工作):

import matplotlib.pyplot as plt
import matplotlib.delaunay
import matplotlib.tri as tri
import numpy as np
import math

# Creating a Triangulation without specifying the triangles results in the
# Delaunay triangulation of the points.

# First create the x and y coordinates of the points.
n_angles = 36
n_radii = 8
min_radius = 0.25
radii = np.linspace(min_radius, 0.95, n_radii)

angles = np.linspace(0, 2*math.pi, n_angles, endpoint=False)
angles = np.repeat(angles[...,np.newaxis], n_radii, axis=1)
angles[:,1::2] += math.pi/n_angles

x = (radii*np.cos(angles)).flatten()
y = (radii*np.sin(angles)).flatten()

tt = matplotlib.delaunay.triangulate.Triangulation(x,y)
triang = tri.Triangulation(x, y)

# Plot the triangulation.
plt.figure()
plt.gca().set_aspect('equal')
plt.triplot(triang, 'bo-')

plt.plot(tt.circumcenters[:,0],tt.circumcenters[:,1],'r.')
plt.show()
uyhoqukh

uyhoqukh2#

这里有一个计算它们的函数。它也可以用于其他三角测量结构,例如scipyDelaunay triangulation(见下文)。

def compute_triangle_circumcenters(xy_pts, tri_arr):
    """
    Compute the centers of the circumscribing circle of each triangle in a triangulation.
    :param np.array xy_pts : points array of shape (n, 2)
    :param np.array tri_arr : triangles array of shape (m, 3), each row is a triple of indices in the xy_pts array

    :return: circumcenter points array of shape (m, 2)
    """
    tri_pts = xy_pts[tri_arr]  # (m, 3, 2) - triangles as points (not indices)

    # finding the circumcenter (x, y) of a triangle defined by three points:
    # (x-x0)**2 + (y-y0)**2 = (x-x1)**2 + (y-y1)**2
    # (x-x0)**2 + (y-y0)**2 = (x-x2)**2 + (y-y2)**2
    #
    # becomes two linear equations (squares are canceled):
    # 2(x1-x0)*x + 2(y1-y0)*y = (x1**2 + y1**2) - (x0**2 + y0**2)
    # 2(x2-x0)*x + 2(y2-y0)*y = (x2**2 + y2**2) - (x0**2 + y0**2)
    a = 2 * (tri_pts[:, 1, 0] - tri_pts[:, 0, 0])
    b = 2 * (tri_pts[:, 1, 1] - tri_pts[:, 0, 1])
    c = 2 * (tri_pts[:, 2, 0] - tri_pts[:, 0, 0])
    d = 2 * (tri_pts[:, 2, 1] - tri_pts[:, 0, 1])

    v1 = (tri_pts[:, 1, 0] ** 2 + tri_pts[:, 1, 1] ** 2) - (tri_pts[:, 0, 0] ** 2 + tri_pts[:, 0, 1] ** 2)
    v2 = (tri_pts[:, 2, 0] ** 2 + tri_pts[:, 2, 1] ** 2) - (tri_pts[:, 0, 0] ** 2 + tri_pts[:, 0, 1] ** 2)

    # solve 2x2 system (see https://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_2_%C3%97_2_matrices)
    det = (a * d - b * c)
    detx = (v1 * d - v2 * b)
    dety = (a * v2 - c * v1)

    x = detx / det
    y = dety / det

    return (np.vstack((x, y))).T

在@JoshAdel的answer above中的数据上,添加以下代码:

cc = compute_triangle_circumcenters(np.vstack([tt.x, tt.y]).T, tt.triangle_nodes)
plt.plot(cc[:, 0], cc[:, 1], ".k")

我会得到如下的图:

它也可以像这样在scipy.spatial.Delaunay上使用:

from scipy.spatial import Delaunay
xy_pts = np.vstack([x, y]).T
dt = Delaunay(xy_pts)
cc = compute_triangle_circumcenters(dt.points, dt.simplices)
fquxozlt

fquxozlt3#

我认为那个解决办法太过分了。你可以直接表示出每个三角形的顶点,比如:

mid_points = tri.points[tri.vertices].mean(axis=1)

相关问题