This commit is contained in:
2025-05-16 08:49:00 +08:00
parent 5f4d6267b6
commit 3789f373ce
2 changed files with 364 additions and 0 deletions

110
src/mesh/create_tin_ex.cpp Normal file
View File

@@ -0,0 +1,110 @@
// File: create_tin_ex.cpp
// Author: Dr. Yi Zhang (yizhang-geo@zju.edu.cn)
// Date: 2025-05-15
#include "gctl/io/file_io.h"
#include "gctl/io/gmsh_io.h"
#include "gctl/io/dsv_io.h"
#include "gctl/mesh/tin.h"
using namespace gctl;
int main(int argc, char *argv[]) try
{
// read dem grid
std::vector<double> topo(10201);
std::ifstream infile("data/mesh/topo");
for (int i = 0; i < 10201; ++i)
{
infile >> topo[i];
}
infile.close();
std::vector<tin_vertex2dc> box(4);
box[0].id = 0; box[0].x = 500; box[0].y = 250;
box[1].id = 1; box[1].x = 750; box[1].y = 250;
box[2].id = 2; box[2].x = 750; box[2].y = 500;
box[3].id = 3; box[3].x = 500; box[3].y = 500;
std::vector<region> box_region(1);
box_region[0].set(box, 5.0);
std::vector<double> err_records;
std::vector<tin_vertex2dc*> tin_vert;
std::vector<tin_triangle*> tin_ele;
//grd2tin(topo, 0, 1000, 0, 1000, 10, 10, tin_vert, tin_ele, 1.0, nullptr, &err_records, nullptr);
grd2tin(topo, 0, 1000, 0, 1000, 10, 10, tin_vert, tin_ele, 1.0, nullptr, &err_records, &box_region);
int times = err_records.size();
_1d_array errs;
errs.input(err_records);
destroy_vector(err_records);
dsv_io log_out;
log_out.init_table(err_records.size(), 2);
log_out.column_names({"Times", "Maxi-Error"});
log_out.fill_column(array<int>(err_records.size(), 1, 1), "Times");
log_out.fill_column(errs, "Maxi-Error");
log_out.save_csv("data/mesh/topo_TIN.log");
// Write a Gmsh's .msh file
std::ofstream outfile("data/mesh/topo_TIN.msh");
outfile << "$MeshFormat" << std::endl << "2.2 0 8" << std::endl << "$EndMeshFormat "<<std::endl;
outfile << "$Nodes" << std::endl << tin_vert.size() << std::endl;
for (int i = 0; i < tin_vert.size(); i++)
{
outfile << tin_vert[i]->id + 1 << " " << std::setprecision(16)
<< tin_vert[i]->x << " " << tin_vert[i]->y << " " << tin_vert[i]->elev << std::endl;
}
outfile<<"$EndNodes"<<std::endl;
outfile << "$Elements" << std::endl << tin_ele.size() <<std::endl;
for (int i = 0; i < tin_ele.size(); i++)
{
outfile << i + 1 << " 2 0";
for (int j = 0; j < 3; j++)
{
outfile << " " << tin_ele[i]->vert[j]->id + 1;
}
outfile << std::endl;
}
outfile << "$EndElements"<< std::endl;
outfile<<"$NodeData"<<std::endl;
outfile<<1<<std::endl
<<"\"Topography (m)\"" <<std::endl
<< 1 <<std::endl<< 0.0 <<std::endl
<< 3 <<std::endl<< 0<<std::endl
<< 1 <<std::endl<< tin_vert.size() <<std::endl;
for (int i = 0; i < tin_vert.size(); i++)
{
outfile << tin_vert[i]->id + 1 << " " << std::setprecision(16) << tin_vert[i]->elev << std::endl;
}
outfile << "$EndNodeData" << std::endl;
outfile.close();
// write a neighbor file
outfile.open("data/mesh/topo_TIN.neigh");
outfile << tin_ele.size() << std::endl;
for (int i = 0; i < tin_ele.size(); i++)
{
outfile << i + 1;
for (int j = 0; j < 3; j++)
{
if (tin_ele[i]->neigh[j] != nullptr)
{
outfile << " " << tin_ele[i]->neigh[j]->id + 1;
}
else outfile << " -1";
}
outfile << std::endl;
}
outfile.close();
// Destroy memories allocated by the dem2tin function
destroy_vector(tin_vert);
destroy_vector(tin_ele);
return 0;
}
catch (std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}