参考文章: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三角剖分实现的更多相关文章

  1. Voronoi图和Delaunay三角剖分

    刷题的时候发现了这么一个新的东西:Voronoi图和Delaunay三角剖分 发现这个东西可以$O(nlogn)$解决平面图最小生成树问题感觉非常棒 然后就去学了.. 看的n+e的blog,感谢n+e ...

  2. paper 153:Delaunay三角剖分算法--get 这个小技术吧!

    直接摘自百度百科,希望大家能根据下面的介绍稍微理顺思路,按需使用,加油! 解释一下:点集的三角剖分(Triangulation),对数值分析(比如有限元分析)以及图形学来说,都是极为重要的一项预处理技 ...

  3. Delaunay三角剖分及MATLAB实例

    https://blog.csdn.net/piaoxuezhong/article/details/68065170 一.原理部分 点集的三角剖分(Triangulation),对数值分析(如有限元 ...

  4. Delaunay三角剖分算法

    在图像处理中,经常会使用到三角剖分算法: 具体定义及其算法可以参考:http://baike.so.com/doc/5447649.html 下面放出来代码: Delaunay接口为存C: 测试是使用 ...

  5. Delaunay三角剖分

    Bowyer-Watson算法:1.假设已经生成了连接若干个顶点的Delaunay三角网格:2.加入一个新的节点,找出所有外接圆包含新加入节点的三角形,并将这些三角形删除形成一个空洞:3.空洞的节点与 ...

  6. 使用Delaunay三角剖分解决求多边形面积的问题

    朋友那边最近有个需求,需要框选一个选区,然后根据选区中的点求出面积.并且让我尝试用Delaunay来解决 似乎音译过来应该是德诺类 大致如下: 我在github上找了一个可以用的Delaunay库 h ...

  7. Voronoi图与Delaunay三角剖分

    详情请见[ZJOI2018]保镖 题解随笔 - 99 文章 - 0 评论 - 112

  8. 三角剖分算法(delaunay)

    开篇 在做一个Low Poly的课题,而这种低多边形的成像效果在现在设计中越来越被喜欢,其中的低多边形都是由三角形组成的. 而如何自动生成这些看起来很特殊的三角形,就是本章要讨论的内容. 项目地址:  ...

  9. Delaunay Triangulation in OpenCascade

    Delaunay Triangulation in OpenCascade eryar@163.com 摘要:本文简要介绍了Delaunay三角剖分的基础理论,并使用OpenCascade的三角剖分算 ...

  10. Triangle - Delaunay Triangulator

    Triangle - Delaunay Triangulator  eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...

随机推荐

  1. #笛卡尔树,dp#洛谷 7244 章节划分

    题目 分析 考虑段数受到答案限制,而答案为最大值的约数,那么枚举答案, 设\(dp[i]\)表示前\(i\)个位置分完最多可以分多少段只要\(dp[n]\geq k\)即合法. 那么\(dp[i]=\ ...

  2. Windows Terminal的资料

    Windows Terminal是微软Windows平台难得好用的工具. 由于工作内容的原因,需要打开多个CMD窗口.多个git bash窗口,并且在多个窗口间切换,因此命令行窗口支持多TAB的特性, ...

  3. Stage模型深入解读

     原文链接:https://mp.weixin.qq.com/s/4Mb5BMw1IgKvqE0Ff9Ts-w,点击链接查看更多技术内容:   HarmonyOS 3.1版本(API 9)推出了全新应 ...

  4. How to install Django-Install Python Django | Django 安装指南【官方版】

    How to install Django¶ This document will get you up and running with Django. Install Python--Linux ...

  5. 淘宝二面:MySQL里有2000万条数据,但是Redis中只存20万的数据,如何保证redis中的数据都是热点数据?

    引言 在当今互联网领域,尤其在大型电商平台如淘宝这样的复杂分布式系统中,数据的高效管理和快速访问至关重要.面对数以千万计的商品.交易记录以及其他各类业务数据,如何在MySQL等传统关系型数据库之外,借 ...

  6. c# apollo订阅与发布(一)(迁)

    前言 apollo 翻译过来是阿波罗的意思,准确的说是:apache apollo,看了apache基本可以放心,因为它不像微软. 安装 下面我以windows 为例. https://activem ...

  7. 实训篇-Html-表格练习1

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Effective Python:简介

    作者:布雷特·斯拉特金 本书的大部分范例代码都遵循Python 3.7版本的语法规范,Python 3.7发布于2018年6月.另外,书里还会给出一些采用Python 3.8语法规范所写的范例,让大家 ...

  9. 力扣594(java&python)-最长和谐子序列(简单)

    题目: 和谐数组是指一个数组里元素的最大值和最小值之间的差别 正好是 1 . 现在,给你一个整数数组 nums ,请你在所有可能的子序列中找到最长的和谐子序列的长度. 数组的子序列是一个由数组派生出来 ...

  10. 终于要跟大家见面了,Flink 面试指南

    面试,一个令人大多数同学头疼的问题,要么成功进入心仪公司,要么沮丧与其失之交臂.但是,如果能在面试前就能知道面试官将会问的问题,然后可以好好提前准备,这种感觉是不是特别棒? 之前社区帮大家汇总了目前 ...