基于BowyerWatson的Delaunay三角化算法实现

2023-11-02

实现效果如下图所示:

代码:


#include<iostream>
using namespace std;

#include "bowyer_watson.h"

#include <sstream>
#include <fstream>
#include <chrono>
#include <armadillo>
using namespace std;

//function to provide a first-order test of whether BowyerWatson::Triangle::contains() works correctly
void test()
{
    typedef BowyerWatson::Triangle Triangle;
    //create initial polyhedron
    arma::dvec3 u({ 0,0,1 });
    arma::dvec3 e({ 1,0,0 });
    arma::dvec3 n({ 0,1,0 });
    arma::dvec3 w({ -1,0,0 });
    arma::dvec3 s({ 0,-1,0 });
    arma::dvec3 d({ 0,0,-1 });
    Triangle* uen = new Triangle(u, e, n, 0);
    Triangle* unw = new Triangle(u, n, w, 0);
    Triangle* uws = new Triangle(u, w, s, 0);
    Triangle* use = new Triangle(u, s, e, 0);
    Triangle* des = new Triangle(d, e, s, 0);
    Triangle* dsw = new Triangle(d, s, w, 0);
    Triangle* dwn = new Triangle(d, w, n, 0);
    Triangle* dne = new Triangle(d, n, e, 0);
    uen->neighbors[0] = dne;    uen->neighbors[1] = unw;    uen->neighbors[2] = use;
    unw->neighbors[0] = dwn;    unw->neighbors[1] = uws;    unw->neighbors[2] = uen;
    uws->neighbors[0] = dsw;    uws->neighbors[1] = use;    uws->neighbors[2] = unw;
    use->neighbors[0] = des;    use->neighbors[1] = uen;    use->neighbors[2] = uws;
    des->neighbors[0] = use;    des->neighbors[1] = dsw;    des->neighbors[2] = dne;
    dsw->neighbors[0] = uws;    dsw->neighbors[1] = dwn;    dsw->neighbors[2] = des;
    dwn->neighbors[0] = unw;    dwn->neighbors[1] = dne;    dwn->neighbors[2] = dsw;
    dne->neighbors[0] = uen;    dne->neighbors[1] = des;    dne->neighbors[2] = dwn;
    Triangle* tris[8] = { uen, unw, use, uws, dne, dwn, des, dsw };
    //test point containment
    cout << "Should be ones:" << endl;
    for (unsigned int i = 0; i < 8; ++i)
    {
        arma::dvec3 to_test(
            {
                 1.0f * (tris[i]->vertices[0][0] + tris[i]->vertices[1][0] + tris[i]->vertices[2][0]) / 3.0f,
            1.0f * (tris[i]->vertices[0][1] + tris[i]->vertices[1][1] + tris[i]->vertices[2][1]) / 3.0f,
            1.0f * (tris[i]->vertices[0][2] + tris[i]->vertices[1][2] + tris[i]->vertices[2][2]) / 3.0f
            }
        );
        cout << (tris[i]->contains(to_test) ? '1' : '0');
    }
    cout << "\nShould be zeroes:" << endl;
    for (unsigned int i = 0; i < 8; ++i)
    {
        arma::dvec3 to_test(
            {
            -1.0f * (tris[i]->vertices[0][0] + tris[i]->vertices[1][0] + tris[i]->vertices[2][0]) / 3.0f,
            -1.0f * (tris[i]->vertices[0][1] + tris[i]->vertices[1][1] + tris[i]->vertices[2][1]) / 3.0f,
            -1.0f * (tris[i]->vertices[0][2] + tris[i]->vertices[1][2] + tris[i]->vertices[2][2]) / 3.0f
            }
        );
        cout << (tris[i]->contains(to_test) ? '1' : '0');
    }
    for (unsigned int j = 1; j < 8; ++j)
        for (unsigned int i = 0; i < 8; ++i)
        {
            arma::dvec3 to_test(
                {
                    1.0f * (tris[i]->vertices[0][0] + tris[i]->vertices[1][0] + tris[i]->vertices[2][0]) / 3.0f,
                1.0f * (tris[i]->vertices[0][1] + tris[i]->vertices[1][1] + tris[i]->vertices[2][1]) / 3.0f,
                1.0f * (tris[i]->vertices[0][2] + tris[i]->vertices[1][2] + tris[i]->vertices[2][2]) / 3.0f
                }
            );
            cout << (tris[(i + j) % 8]->contains(to_test) ? '1' : '0');
        }
    cout << endl;
}



//arguments: (pointcount = 1000, relaxations = 5, filename = bwoutput.py, seed = 666)
int main(int argc, char* argv[])
{
    unsigned int pointcount = 20;
    unsigned int relaxations = 5;
    std::string output_filename("bwoutput.py");
    unsigned int seed = 666;

    istringstream ss;
    bool input_erroneous(false);
    bool display_help(false);
    switch (argc)
    {
    case(5):
        ss.clear();
        ss.str(argv[4]);
        if (!(ss >> seed))
        {
            cout << "Received invalid input \"" << argv[4] << "\" as argument 4; this should be an integer specifying the seed to use for the random number generator. Use argument \"--help\" to see help menu." << endl;
            input_erroneous = true;
        }
    case(4):
        output_filename = argv[3];
        if (output_filename.substr(output_filename.length() - 3) != ".py")
            output_filename += ".py";
    case(3):
        ss.clear();
        ss.str(argv[2]);
        if (!(ss >> relaxations))
        {
            cout << "Received invalid input \"" << argv[2] << "\" as argument 2; this should be a nonnegative integer specifying the number of times to perform modified Lloyd relaxation. Use argument \"--help\" to see help menu." << endl;
            input_erroneous = true;
        }
    case(2):
        if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "\"--help\"") == 0)
        {
            display_help = true;
            input_erroneous = true;
            break;
        }
        ss.clear();
        ss.str(argv[1]);
        if (!(ss >> pointcount) || pointcount == 0)
        {
            cout << "Received invalid input \"" << argv[1] << "\" as argument 1; this should be a positive integer specifying the number of points to generate. Use argument \"--help\" to see help menu." << endl;
            input_erroneous = true;
        }
    case(1):
        break;
    default:
        display_help = true; //the OS provides argv[0], so this line should never be reached
        input_erroneous = true;
    }
    if (display_help)
    {
        cout << "\tBowyer-Watson Algorithm: Generates random points on the unit sphere, constructs a triangulation thereof, and creates a python file for viewing the resuling mesh.\n";
        cout << "\tTo view this menu, pass \"--help\" as the first argument.\n";
        cout << "\tArguments expected, in order, and their defaults:\n";
        cout << "\t\tpointcount = 1000: a positive integer specifying the number of points to generate.\n";
        cout << "\t\trelaxations = 5: a nonnegative integer specifying the number of times to relax the points (modified Lloyd relaxation).\n";
        cout << "\t\tfilename = bwoutput.py: the filename for the output python file. If it does not end in \".py\", that extension will be appended to it.\n";
        cout << "\t\tseed = 666: a nonnegative integer specifying the seed to be used for the random number generator used in generating points.";
        cout << endl;
    }
    if (input_erroneous)
        return(0);

    srand(seed);
    chrono::high_resolution_clock::time_point starttime;
    chrono::high_resolution_clock::time_point endtime;
    chrono::high_resolution_clock::time_point meshendtime;

    BowyerWatson bw;
    cout << "\tBeginning spherical Bowyer-Watson test with " << pointcount << " points & " << relaxations << " iterations of the relaxation algorithm..." << endl;
    starttime = chrono::high_resolution_clock::now();

    std::vector<std::pair<arma::dvec3, unsigned int> > point_vector;
    bw.generate_points(pointcount, point_vector);
    BowyerWatson::Triangle* results = bw.perform(bw.create_initial_triangles(point_vector));
    for (unsigned int relaxations_performed = 0; relaxations_performed < relaxations; ++relaxations_performed)
    {
        bw.relax_points(results, point_vector);
        results = bw.perform(bw.create_initial_triangles(point_vector));
    }
    endtime = chrono::high_resolution_clock::now();

    cout << "\tRunning time: " << chrono::duration_cast<chrono::milliseconds>(endtime - starttime).count() << " milliseconds." << endl;
    cout << "\tCompleted calculations. Transforming to mesh..." << endl;
    BowyerWatson::Mesh mesh = bw.get_mesh(results, true);
    meshendtime = chrono::high_resolution_clock::now();
    cout << "\tMesh running time: " << chrono::duration_cast<chrono::milliseconds>(meshendtime - endtime).count() << " milliseconds." << endl;
    cout << "\tw00t, you got a mesh! Writing to file..." << endl;

    ///
    ofstream file;
    file.open(output_filename);
    file << "from mpl_toolkits.mplot3d import Axes3D\n";
    file << "from mpl_toolkits.mplot3d.art3d import Poly3DCollection\n";
    file << "import matplotlib.pyplot as plt\n";
    file << "fig = plt.figure()\n";
    file << "ax = Axes3D(fig)\n";
    for (unsigned int i = 0; i < mesh.triangles.size(); i += 3)
    {
        file << "ax.add_collection3d(Poly3DCollection([list(zip([" <<
            mesh.vertices[mesh.triangles[i]] << "," << mesh.vertices[mesh.triangles[i + 1]] << "," << mesh.vertices[mesh.triangles[i + 2]]
            << "],[" <<
            mesh.vertices[mesh.triangles[i] + 1] << "," << mesh.vertices[mesh.triangles[i + 1] + 1] << "," << mesh.vertices[mesh.triangles[i + 2] + 1]
            << "],[" <<
            mesh.vertices[mesh.triangles[i] + 2] << "," << mesh.vertices[mesh.triangles[i + 1] + 2] << "," << mesh.vertices[mesh.triangles[i + 2] + 2]
            << "]))], facecolors='w', edgecolors='b'))\n";
    }
    file << "ax.set_xlim(-1.4, 1.4)\n";
    file << "ax.set_ylim(-1.4, 1.4)\n";
    file << "ax.set_zlim(-1.4, 1.4)\n";
    file << "plt.show()\n";
    file.close();
    ///

    cout << "\tBowyer-Watson test complete! Wrote to: " << output_filename << endl;
    return(0);
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于BowyerWatson的Delaunay三角化算法实现 的相关文章

  • 为什么 C# Array.BinarySearch 这么快?

    我已经实施了一个很简单用于在整数数组中查找整数的 C 中的 binarySearch 实现 二分查找 static int binarySearch int arr int i int low 0 high arr Length 1 mid
  • WCF RIA 服务 - 加载多个实体

    我正在寻找一种模式来解决以下问题 我认为这很常见 我正在使用 WCF RIA 服务在初始加载时将多个实体返回给客户端 我希望两个实体异步加载 以免锁定 UI 并且我想利用 RIA 服务来执行此操作 我的解决方案如下 似乎有效 这种方法会遇到
  • GLKit的GLKMatrix“列专业”如何?

    前提A 当谈论线性存储器中的 列主 矩阵时 列被一个接一个地指定 使得存储器中的前 4 个条目对应于矩阵中的第一列 另一方面 行主 矩阵被理解为依次指定行 以便内存中的前 4 个条目指定矩阵的第一行 A GLKMatrix4看起来像这样 u
  • Web 客户端和 Expect100Continue

    使用 WebClient C NET 时设置 Expect100Continue 的最佳方法是什么 我有下面的代码 我仍然在标题中看到 100 continue 愚蠢的 apache 仍然抱怨 505 错误 string url http
  • 按成员序列化

    我已经实现了template
  • 秒表有最长运行时间吗?

    多久可以Stopwatch在 NET 中运行 如果达到该限制 它会回绕到负数还是从 0 重新开始 Stopwatch Elapsed返回一个TimeSpan From MSDN https learn microsoft com en us
  • 在哪里可以找到列出 SSE 内在函数操作的官方参考资料?

    是否有官方参考列出了 GCC 的 SSE 内部函数的操作 即 头文件中的函数 除了 Intel 的 vol 2 PDF 手册外 还有一个在线内在指南 https www intel com content www us en docs in
  • 不支持将数据直接绑定到存储查询(DbSet、DbQuery、DbSqlQuery)

    正在编码视觉工作室2012并使用实体模型作为我的数据层 但是 当页面尝试加载时 上面提到的标题 我使用 Linq 语句的下拉控件往往会引发未处理的异常 下面是我的代码 using AdventureWorksEntities dw new
  • 类模板参数推导 - clang 和 gcc 不同

    下面的代码使用 gcc 编译 但不使用 clang 编译 https godbolt org z ttqGuL template
  • BitTorrent 追踪器宣布问题

    我花了一点业余时间编写 BitTorrent 客户端 主要是出于好奇 但部分是出于提高我的 C 技能的愿望 我一直在使用理论维基 http wiki theory org BitTorrentSpecification作为我的向导 我已经建
  • 如何从 appsettings.json 文件中的对象数组读取值

    我的 appsettings json 文件 StudentBirthdays Anne 01 11 2000 Peter 29 07 2001 Jane 15 10 2001 John Not Mentioned 我有一个单独的配置类 p
  • 不同枚举类型的范围和可转换性

    在什么条件下可以从一种枚举类型转换为另一种枚举类型 让我们考虑以下代码 include
  • C# 中通过 Process.Kill() 终止的进程的退出代码

    如果在我的 C 应用程序中 我正在创建一个可以正常终止或开始行为异常的子进程 在这种情况下 我通过调用 Process Kill 来终止它 但是 我想知道该进程是否已退出通常情况下 我知道我可以获得终止进程的错误代码 但是正常的退出代码是什
  • while 循环中的 scanf

    在这段代码中 scanf只工作一次 我究竟做错了什么 include
  • 如何在 C 中调用采用匿名结构的函数?

    如何在 C 中调用采用匿名结构的函数 比如这个函数 void func struct int x p printf i n p x 当提供原型的函数声明在范围内时 调用该函数的参数必须具有与原型中声明的类型兼容的类型 其中 兼容 具有标准定
  • 覆盖子类中的字段或属性

    我有一个抽象基类 我想声明一个字段或属性 该字段或属性在从该父类继承的每个类中具有不同的值 我想在基类中定义它 以便我可以在基类方法中引用它 例如覆盖 ToString 来表示 此对象的类型为 property field 我有三种方法可以
  • 通过指向其基址的指针删除 POD 对象是否安全?

    事实上 我正在考虑那些微不足道的可破坏物体 而不仅仅是POD http en wikipedia org wiki Plain old data structure 我不确定 POD 是否可以有基类 当我读到这个解释时is triviall
  • 如何在Xamarin中删除ViewTreeObserver?

    假设我需要获取并设置视图的高度 在 Android 中 众所周知 只有在绘制视图之后才能获取视图高度 如果您使用 Java 有很多答案 最著名的方法之一如下 取自这个答案 https stackoverflow com a 24035591
  • C# 模拟VolumeMute按下

    我得到以下代码来模拟音量静音按键 DllImport coredll dll SetLastError true static extern void keybd event byte bVk byte bScan int dwFlags
  • Windows 和 Linux 上的线程

    我在互联网上看到过在 Windows 上使用 C 制作多线程应用程序的教程 以及在 Linux 上执行相同操作的其他教程 但不能同时用于两者 是否存在即使在 Linux 或 Windows 上编译也能工作的函数 您需要使用一个包含两者的实现

随机推荐

  • 再看中国互联网web2.0百强名单

    无意中翻看到一篇我在三年多前写的文章 我看中国互联网web2 0百强名单 读来颇有感概 2005 2006那两年 正是WEB2 0概念轰轰烈烈的时候 大大小小的新网站层出不穷 博客 视频 交友 评点 社区 聚合 不管自己的网站的UGC比例多
  • bootstrap table 表格支持shirt 多选_bootstrap-table 表格行内编辑实现

    这篇文章向大家介绍一下如何使用bootstrap table插件实现表格的行内编辑功能 我的web前端学习交流群点击进入1045267283 欢迎加入 先放一张效果图 应用场景 之前的项目也是采用bootstrap table 添加和修改数
  • 牛客——子序列(组合数学)

    子序列 题目描述 给定一个小写字母字符串 T T T 求有多少长度为 m m m的小写字母字符串 S
  • 端口相关知识总结

    端口相关知识总结 80是服务器上的一个软件 服务器软件 端口是软件的代号 3306是MySQL的端口 1521是Oracle的端口 80是外部服务器的通用端口 京东也是 不写也可以访问 80端口可以省略 文件下载端口 FTP 都是21 FT
  • Android 13 - Media框架(2)- Demo App与MediaPlayer Api了解

    尝试用MediaPlayer写了一个播放demo 实现了网络流和本地流的播放 由于本人对app开发一窍不通 所以demo中很多内容是边查资料边写的 写的也比较杂乱 能够帮助理解api就行 这一节主要会记录demo开发中学到的内容 以及了解M
  • LeetCode 312. Burst Balloons(戳气球)

    原题网址 https leetcode com problems burst balloons Given n balloons indexed from 0 to n 1 Each balloon is painted with a nu
  • 微信小程序云开发实现微信小程序订阅消息服务通知教程

    微信小程序云开发实现微信小程序订阅消息服务通知教程 申请模板 云函数 小程序页面 调试 我这里就直接真机测试了 申请模板 1 在这边菜单栏 找到 功能 里的 订阅消息 2 在 公共模板库 里面选取自己想要的模板 选取自己想要的消息即可 云函
  • 『学Vue2+Vue3』指令补充、computed计算属性、watch侦听器

    day02 一 今日学习目标 1 指令补充 指令修饰符 v bind对样式增强的操作 v model应用于其他表单元素 2 computed计算属性 基础语法 计算属性vs方法 计算属性的完整写法 成绩案例 3 watch侦听器 基础写法
  • linux文件基础-2_linux文件细节_lseek_文件指针

    一 linux管理文件 1 硬盘中的静态文件和inode i节点 1 静态文件 放在硬盘中 固定的形式 2 硬盘的两大区域 1 硬盘内容管理表项和储存内容区域 2 操作系统先去访问硬盘内容管理表项 gt 扇区级别的信息 gt 得到储存内容区
  • SAP 货币类型和公司代码的货币设置

    货币类型分为公司代码和集团货币 一般FI 10类型和集团货币 30 事务代码是8KEM 设置货币类型的事务代码是OB22 在S 4 1809版本里编辑功能统合到事务代码FINSC LEDGER 中了 这里集中了分类账和公司代码的设置 设置多
  • 使用正确的命令重启WSL子系统

    问题 大家都知道一般Linux系统重启非常简单 但是在WSL子系统中执行以下两个重启命令是完全无效的 reboot shutdown r 执行命令后提示如下 System has not been booted with systemd a
  • JavaWeb——Servlet

    目录 一 JavaWeb 二 servlet本质 三 Servlet对象生命周期 四 Servlet类的方法介绍 五 适配器思想 一 JavaWeb 对于一个web应用来说 涉及到的角色和规范协议 二 servlet本质 可以将servle
  • Over-smoothing issue in graph neural network(GNN中的过平滑问题)

    在这里转载只是为了让不能够科学搜索的同学们看到好文章而已 个人无收益只是分享知识 顺手做个记录罢了 原网址 https towardsdatascience com over smoothing issue in graph neural
  • 2011.11.24

    完成了刚体 并基本上封装好了
  • 小程序文章详情界面id传送问题

    今天在做由文章列表跳转至文章详情界面时发现不能正常获取文章ID 控制台显示未定义 经过询问他人与搜索资料终于找到了问题所在之处 心累 可以看到这里显示id未定义 错误中学到了什么 大家在发现错误时 一定要善于用console log 来看一
  • 解决创建Vue项目出现template下方有红色波浪错误

    问题 在创建完vue项目后每个点开的文件只要有template或const等单词都会出现红色波浪线报错提示 虽然不影响项目运行 但是看着还是非常碍眼 将鼠标一上去会显示 No Babel config file detected for 路
  • Linux中top命令参数详解

    因为面试经常会问top命令用法 以及各个参数的含义 因此转载补充了了一下 以便自己学习 top命令经常用来监控linux的系统状况 是常用的性能分析工具 能够实时显示系统中各个进程的资源占用情况 top的使用方式 top d number
  • 小程序引入vant-Weapp保姆级教程及安装过程的问题解决

    小知识 大挑战 本文正在参与 程序员必备小知识 创作活动 本文同时参与 掘力星计划 赢取创作大礼包 挑战创作激励金 当你想在小程序里引入vant时 第一步 打开官方文档 第二步 切到快速上手 然后开始步骤一 步骤二 步骤三 你只会看到 以下
  • Awesome IoT

    本文来自 https github com HQarroum awesome iot 中文可以参考 https yq aliyun com articles 54793 Inspired by the awesome list thing
  • 基于BowyerWatson的Delaunay三角化算法实现

    实现效果如下图所示 代码 include