Delaunay三角剖分实现
参考文章:https://www.cnblogs.com/zhiyishou/p/4430017.html
本文使用逐点插入法进行剖分,并使用Unity3D实现。
通过阅读文章《Triangulate》给出的伪代码进行具体编写,我加了些注释:
subroutine triangulate
input : vertex list
output : triangle list
initialize the triangle list
determine the supertriangle
add supertriangle vertices to the end of the vertex list
add the supertriangle to the triangle list
for each sample point in the vertex list #遍历传入的每一个点 initialize the edge buffer #这里要重置边缓冲区 for each triangle currently in the triangle list
calculate the triangle circumcircle center and radius
if the point lies in the triangle circumcircle then #如果该点位于三角形外接圆内,则
add the three triangle edges to the edge buffer #将三个三角形边添加到边缓冲区
remove the triangle from the triangle list #从三角形列表中删除三角形
endif
endfor delete all doubly specified edges from the edge buffer #从边缓冲区中删除所有双重指定的边(例如线段AB和线段BA被当成两个线段存入)
this leaves the edges of the enclosing polygon only
add to the triangle list all triangles formed between the point #将三边和当前点进行组合成任意三角形保存到三角形列表
and the edges of the enclosing polygon endfor
remove any triangles from the triangle list that use the supertriangle vertices #从三角形列表中删除任何使用超三角形顶点的三角形
remove the supertriangle vertices from the vertex list #从顶点列表中删除超三角形顶点(如果不需要继续使用顶点数据,这一步也可以不做)
end
通过Unity的Gizmo和协程,下面是我制作的逐点插入法动图流程:

灰色空心圆 - 输入点
红色十字 - 循环当前点
黄色阶段 - 新的一轮操作开始
红色阶段 - 进行外接圆的筛选
绿色阶段 - 点在外接圆内,拆除三角形,并将边加入边缓冲(EdgeBuffer)
蓝色阶段 - 通过边缓冲的数据和当前顶点,重新组合新的三角形
关于外接圆的计算,取三角形任意两条边的垂线的交点即可得到圆心,圆心和三角形任意一点的线段长度即为半径。
注意:原文链接github的js代码,外接圆半径部分忘了开方(也可能是优化操作,但若参考其实现需要补上)。
更多的具体流程就不写了,理解还是要看伪代码,并且参考原文也很详细。
最后给出实现:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class DelaunayTriangle : MonoBehaviour
{
public struct EdgeBuffer
{
public Vector2 P0;
public Vector2 P1;
} public struct Triangle
{
public Vector2 A;
public Vector2 B;
public Vector2 C;
} public Transform[] points;//通过层级面板放入测试点
private Bounds mBounds;
private List<Triangle> mTriangleList = new List<Triangle>(); private void Update()
{
float kSuperTriangleScale = 5f;
float kStepDuration = 0.5f; Vector3 min = Vector3.one * 10000f;
Vector3 max = Vector3.one * -10000f; for (int i = 0; i < points.Length; i++)
{
Vector3 point = points[i].position;
if (point.x < min.x) min = new Vector3(point.x, min.y, min.z);
if (point.z < min.z) min = new Vector3(min.x, min.y, point.z); if (point.x > max.x) max = new Vector3(point.x, max.y, max.z);
if (point.z > max.z) max = new Vector3(max.x, max.y, point.z);
} mBounds = new Bounds() {min = min, max = max};
mBounds.size *= kSuperTriangleScale;//此处做法比较粗暴
mBounds.center += mBounds.extents * 0.5f; Vector2 superTriangleA = new Vector2(mBounds.min.x, mBounds.min.z);
Vector2 superTriangleB = new Vector2(mBounds.min.x, mBounds.max.z);
Vector2 superTriangleC = new Vector2(mBounds.max.x, mBounds.min.z); List<Vector2> vertexList = new List<Vector2>();
List<EdgeBuffer> edgeBufferList = new List<EdgeBuffer>();
mTriangleList.Clear();
mTriangleList.Add(new Triangle() {A = superTriangleA, B = superTriangleB, C = superTriangleC}); for (int i = 0; i < points.Length; i++)
{
Vector3 position = points[i].position;
vertexList.Add(new Vector2(position.x, position.z));
} vertexList.Add(superTriangleA);
vertexList.Add(superTriangleB);
vertexList.Add(superTriangleC); for (int i = 0; i < vertexList.Count; i++)//顶点遍历
{
Vector2 vertex = vertexList[i]; edgeBufferList.Clear(); for (int j = mTriangleList.Count - 1; j >= 0; j--)
{
Triangle triangle = mTriangleList[j]; (Vector2 center, float radius) = Circumcircle(triangle.A, triangle.B, triangle.C);
//外接圆计算 if (Vector2.Distance(vertex, center) <= radius)
{
edgeBufferList.Add(new EdgeBuffer() {P0 = triangle.A, P1 = triangle.B});
edgeBufferList.Add(new EdgeBuffer() {P0 = triangle.B, P1 = triangle.C});
edgeBufferList.Add(new EdgeBuffer() {P0 = triangle.C, P1 = triangle.A}); mTriangleList.RemoveAt(j);
}//若点在外接圆内则移除三角形,并将三角形三个边加入边缓冲
} Dedup(edgeBufferList);//边缓冲去重 for (int j = 0; j < edgeBufferList.Count; j++)
{
EdgeBuffer edgeBuffer = edgeBufferList[j];
Triangle triangle = new Triangle()
{
A = edgeBuffer.P0,
B = edgeBuffer.P1,
C = vertex
}; mTriangleList.Add(triangle);
}//重新组合三角形
} for (int j = mTriangleList.Count - 1; j >= 0; j--)
{
Triangle triangle = mTriangleList[j]; if (triangle.A == superTriangleA || triangle.B == superTriangleA || triangle.C == superTriangleA
|| triangle.A == superTriangleB || triangle.B == superTriangleB || triangle.C == superTriangleB
|| triangle.A == superTriangleC || triangle.B == superTriangleC || triangle.C == superTriangleC)
{
mTriangleList.RemoveAt(j);
}
}//移除连接超三角形的所有三角形
} private void Dedup(List<EdgeBuffer> edgeBufferList)
{
for (int i = edgeBufferList.Count - 1; i >= 0; i--)
{
for (int j = i - 1; j >= 0; j--)
{
EdgeBuffer x = edgeBufferList[i];
EdgeBuffer y = edgeBufferList[j]; if ((x.P0 == y.P0 && x.P1 == y.P1) || (x.P0 == y.P1 && x.P1 == y.P0))
{
edgeBufferList.RemoveAt(i);
edgeBufferList.RemoveAt(j); i = edgeBufferList.Count - 1;
break;
}
}
}
} private (Vector2 center, float radius) Circumcircle(Vector2 a, Vector2 b, Vector3 c)
{
float kEps = 0.000001f; float x1 = a.x;
float y1 = a.y;
float x2 = b.x;
float y2 = b.y;
float x3 = c.x;
float y3 = c.y;
float fabsy1y2 = Mathf.Abs(y1 - y2);
float fabsy2y3 = Mathf.Abs(y2 - y3);
float xc = 0f;
float yc = 0f;
float m1 = 0f;
float m2 = 0f;
float mx1 = 0f;
float mx2 = 0f;
float my1 = 0f;
float my2 = 0f;
float dx = 0f;
float dy = 0f; if (fabsy1y2 < kEps)
{
m2 = -((x3 - x2) / (y3 - y2));
mx2 = (x2 + x3) / 2.0f;
my2 = (y2 + y3) / 2.0f;
xc = (x2 + x1) / 2.0f;
yc = m2 * (xc - mx2) + my2;
} else if (fabsy2y3 < kEps)
{
m1 = -((x2 - x1) / (y2 - y1));
mx1 = (x1 + x2) / 2.0f;
my1 = (y1 + y2) / 2.0f;
xc = (x3 + x2) / 2.0f;
yc = m1 * (xc - mx1) + my1;
} else
{
m1 = -((x2 - x1) / (y2 - y1));
m2 = -((x3 - x2) / (y3 - y2));
mx1 = (x1 + x2) / 2.0f;
mx2 = (x2 + x3) / 2.0f;
my1 = (y1 + y2) / 2.0f;
my2 = (y2 + y3) / 2.0f;
xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2);
yc = (fabsy1y2 > fabsy2y3) ? m1 * (xc - mx1) + my1 : m2 * (xc - mx2) + my2;
} dx = x2 - xc;
dy = y2 - yc; return (center: new Vector2(xc, yc), radius: Mathf.Sqrt(dx * dx + dy * dy));
} private void OnDrawGizmos()
{
for (int i = 0; i < mTriangleList.Count; i++)
{
Triangle triangle = mTriangleList[i]; Gizmos.DrawLine(new Vector3(triangle.A.x, 0f, triangle.A.y), new Vector3(triangle.B.x, 0f, triangle.B.y));
Gizmos.DrawLine(new Vector3(triangle.B.x, 0f, triangle.B.y), new Vector3(triangle.C.x, 0f, triangle.C.y));
Gizmos.DrawLine(new Vector3(triangle.C.x, 0f, triangle.C.y), new Vector3(triangle.A.x, 0f, triangle.A.y));
}
}
}
将脚本挂载至场景,并配置。测试效果如下:

Delaunay三角剖分实现的更多相关文章
- Voronoi图和Delaunay三角剖分
刷题的时候发现了这么一个新的东西:Voronoi图和Delaunay三角剖分 发现这个东西可以$O(nlogn)$解决平面图最小生成树问题感觉非常棒 然后就去学了.. 看的n+e的blog,感谢n+e ...
- paper 153:Delaunay三角剖分算法--get 这个小技术吧!
直接摘自百度百科,希望大家能根据下面的介绍稍微理顺思路,按需使用,加油! 解释一下:点集的三角剖分(Triangulation),对数值分析(比如有限元分析)以及图形学来说,都是极为重要的一项预处理技 ...
- Delaunay三角剖分及MATLAB实例
https://blog.csdn.net/piaoxuezhong/article/details/68065170 一.原理部分 点集的三角剖分(Triangulation),对数值分析(如有限元 ...
- Delaunay三角剖分算法
在图像处理中,经常会使用到三角剖分算法: 具体定义及其算法可以参考:http://baike.so.com/doc/5447649.html 下面放出来代码: Delaunay接口为存C: 测试是使用 ...
- Delaunay三角剖分
Bowyer-Watson算法:1.假设已经生成了连接若干个顶点的Delaunay三角网格:2.加入一个新的节点,找出所有外接圆包含新加入节点的三角形,并将这些三角形删除形成一个空洞:3.空洞的节点与 ...
- 使用Delaunay三角剖分解决求多边形面积的问题
朋友那边最近有个需求,需要框选一个选区,然后根据选区中的点求出面积.并且让我尝试用Delaunay来解决 似乎音译过来应该是德诺类 大致如下: 我在github上找了一个可以用的Delaunay库 h ...
- Voronoi图与Delaunay三角剖分
详情请见[ZJOI2018]保镖 题解随笔 - 99 文章 - 0 评论 - 112
- 三角剖分算法(delaunay)
开篇 在做一个Low Poly的课题,而这种低多边形的成像效果在现在设计中越来越被喜欢,其中的低多边形都是由三角形组成的. 而如何自动生成这些看起来很特殊的三角形,就是本章要讨论的内容. 项目地址: ...
- Delaunay Triangulation in OpenCascade
Delaunay Triangulation in OpenCascade eryar@163.com 摘要:本文简要介绍了Delaunay三角剖分的基础理论,并使用OpenCascade的三角剖分算 ...
- Triangle - Delaunay Triangulator
Triangle - Delaunay Triangulator eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...
随机推荐
- #分治,决策单调性dp#CF868F Yet Another Minimization Problem
题目 给定一个序列 \(a\),要把它分成 \(k\) 个子段.(\(n\leq 10^5,k\leq 20\)) 每个子段的费用是其中相同元素的对数.求所有子段的费用之和的最小值. 分析 有一个很明 ...
- 深入理解java的泛型
目录 简介 泛型和协变 泛型在使用中会遇到的问题 类型擦除要注意的事项 总结 简介 泛型是JDK 5引入的概念,泛型的引入主要是为了保证java中类型的安全性,有点像C++中的模板. 但是Java为了 ...
- OpenHarmony应用全局的UI状态存储:AppStorage
AppStorage是应用全局的UI状态存储,是和应用的进程绑定的,由UI框架在应用程序启动时创建,为应用程序UI状态属性提供中央存储. 和AppStorage不同的是,LocalStorage是 ...
- Java 编程指南:入门,语法与学习方法
Java 是什么? Java 是一种流行的编程语言,诞生于 1995 年.由 Oracle 公司拥有,运行在超过 30 亿台设备上.Java 可以用于: 移动应用程序(尤其是 Android 应用) ...
- C# 虚方法virtual详解(转载)
C# 虚方法virtual详解 在C++.Java等众多OOP语言里都可以看到virtual的身影,而C#作为一个完全面向对象的语言当然也不例外. 虚拟函数从C#的程序编译的角度来看,它和其它一般的函 ...
- sql 语句系列(加减乘除与平均)[八百章之第十四章]
avg的注意事项 一张t2表: select * from t2 select AVG(sal) from t2 得到的结果是: 本来我们得到的结果应该是10的.但是得到的结果确实15. 这是因为忽略 ...
- c# 模拟web请求formdata webrequest
前言 在写代码中,我们常常需要去书写代码去请求一些东西,那么是不是可以模拟像web formdata一样请求. 正文 下面代码为模拟的: public string SendRequest(strin ...
- hibernate4升级5带来的一些参数变化
public String hqlToHibernate5(String hql) { String[] tmp = hql.split(" "); String hqlTmp = ...
- keycloak~jwt的rs256签名的验证方式
接口地址 keycloak开放接口地址:/auth/realms/fabao/.well-known/openid-configuration rsa算法相关术语 RSA算法是一种非对称加密算法,其安 ...
- 纯CSS实现带小三角提示框
要实现在页面上点击指定元素时,弹出一个信息提示框.在前面的文章中,我们已经简单介绍了如何使用纯 CSS 创建一个三角形.本文在此基础上,记录如何使用 CSS 创建带三角形的提示框. 实现的原理是创建一 ...