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

实现效果如下图所示:

代码:


#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);
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Delaunay三角算法是一种用于将给定的点集进行三角划分的方法。它的目标是使得生成的三角形的外接圆不包含任何其他点。 具体实现Delaunay三角算法的方法可以是Bowyer-Watson算法。该算法的起始思想是先生成一个超级三角形,该超级三角形包含了整个地图中的所有点。然后,通过逐步添加地图中的点,并不断调整现有的三角形,最终得到一个满足Delaunay条件的三角划分。 具体步骤如下: 1. 首先,生成一个包含所有地图点的超级三角形。 2. 将随机点集中的第一个点添加到超级三角形中。 3. 对于随机点集中的每个点,按照以下步骤进行处理: a. 找到当前点所在的三角形。 b. 检查该三角形的外接圆是否包含其他任何点。如果包含,则需要调整该三角形。 c. 将该点与三角形的三个顶点连接,形成新的三个三角形。 d. 对于每个新形成的三角形,检查其外接圆是否包含其他任何点。如果包含,则需要调整该三角形。 4. 当所有的点都被处理完毕后,删除超级三角形和任何与超级三角形相关的三角形。 总结起来,Delaunay三角算法实现策略是通过逐步添加地图中的点,并根据Delaunay条件对现有的三角形进行调整,最终得到一个满足条件的三角划分。 参考文献: Delaunay 三角,访问链接:https://zh.wikipedia.org/wiki/Delaunay%E4%B8%89%E8%A7%92%E5%8C%96 Delaunay 三角 -- 实现策略,Bowyer-Watson算法,见引用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CppBlock

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值