基于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三角化算法实现 的相关文章

随机推荐

  • 再看中国互联网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