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 ...
随机推荐
- VS 2020制作安装包
VS制作安装包的一般步骤. 一·新建项目 (1)新建 (2)界面跳转 二·添加引用 (1)添加卸载程序 1.在'C:WINDOWSsystem32'路径下,找到msiexec.exe . 2.将msi ...
- 探索“智”感生活,HMS Core线上Codelabs挑战赛第4期开始!
HMS Core线上Codelabs挑战赛第4期正式开始!我们向所有实践力超强.创新力满满的开发者发出邀请,用你的超级"码"力,解锁更多应用价值! 生活里,我们被手机"秒 ...
- mysql 必知必会整理—sql 简单语句[二]
前言 简单整理一下sql 排序与过滤. 正文 在这里需要创建一下一个数据库实例. 为了方便直接用docker 创建一下啊,方便简洁. https://hub.docker.com/_/mysql 按照 ...
- redis 简单整理——内存的管理[二十六]
前言 redis 是一个内存型数据库,那么就需要重点关注一下内存了. 正文 理解Redis内存,首先需要掌握Redis内存消耗在哪些方面.有些内存消 耗是必不可少的,而有些可以通过参数调整和合理使用来 ...
- redis 简单整理——CEO[十五]
前文 简单介绍一下CEO. 正文 Redis3.2版本提供了GEO(地理信息定位)功能,支持存储地理位置信 息用来实现诸如附近位置.摇一摇这类依赖于地理位置信息的功能,对于需 要实现这些功能的开发者来 ...
- 实训篇-Html-超链接练习
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 实训篇-Html-表格练习1
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 实训篇-Html-超链接a标签使用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- WPF开发随笔收录-查看PDF文件
一.前言 在项目的开发过程中,涉及到查看服务器生成的pdf报告文件的查看,起初的方案是通过spire.pdf这个库来将pdf文件转换成图片,然后在进行查看.但是经常被吐槽预览不清晰,后面上网发现了一个 ...
- MD5前端vue加密
Vue 前端md5加密用户注册时将加密后的密码发送给后端存储当登陆的时候,再将加密后的密码和数据库中加密的密码相匹配.npm: https://www.npmjs.com/package/crypto ...