update to std::sort

This commit is contained in:
张壹 2021-09-17 11:05:59 +08:00
parent d6984a3270
commit f08a549746
5 changed files with 175 additions and 136 deletions

82
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,82 @@
{
"files.associations": {
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__functional_base": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tuple": "cpp",
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"locale": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"queue": "cpp",
"random": "cpp",
"ratio": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"vector": "cpp",
"*.tcc": "cpp",
"memory_resource": "cpp",
"stop_token": "cpp"
}
}

View File

@ -6,10 +6,10 @@
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
{ {
// read dem grid // read dem grid
std::vector<double> topo(1002001); std::vector<double> topo(10201);
std::ifstream infile("topo2.txt"); std::ifstream infile("topo.txt");
for (int i = 0; i < 1002001; ++i) for (int i = 0; i < 10201; ++i)
{ {
infile >> topo[i]; infile >> topo[i];
} }
@ -18,10 +18,10 @@ int main(int argc, char const *argv[])
std::vector<double> err_records; std::vector<double> err_records;
std::vector<vertex2dc*> tin_vert; std::vector<vertex2dc*> tin_vert;
std::vector<triangle*> tin_ele; std::vector<triangle*> tin_ele;
dem2tin(topo, 0, 1000, 0, 1000, 1, 1, tin_vert, tin_ele, 10.0, &err_records); dem2tin(topo, 0, 1000, 0, 1000, 10, 10, tin_vert, tin_ele, 1.0, &err_records);
// Write a log file // Write a log file
std::ofstream logfile("topo2_TIN.log"); std::ofstream logfile("topo_TIN.log");
logfile << "# Insertion Maxi-Error\n"; logfile << "# Insertion Maxi-Error\n";
for (int i = 0; i < err_records.size(); ++i) for (int i = 0; i < err_records.size(); ++i)
{ {
@ -30,7 +30,7 @@ int main(int argc, char const *argv[])
logfile.close(); logfile.close();
// Write a Gmsh's .msh file // Write a Gmsh's .msh file
std::ofstream outfile("topo2_TIN.msh"); std::ofstream outfile("topo_TIN.msh");
outfile << "$MeshFormat" << std::endl << "2.2 0 8" << std::endl << "$EndMeshFormat "<<std::endl; outfile << "$MeshFormat" << std::endl << "2.2 0 8" << std::endl << "$EndMeshFormat "<<std::endl;
outfile << "$Nodes" << std::endl << tin_vert.size() << std::endl; outfile << "$Nodes" << std::endl << tin_vert.size() << std::endl;
for (int i = 0; i < tin_vert.size(); i++) for (int i = 0; i < tin_vert.size(); i++)

67
tin.h
View File

@ -11,6 +11,7 @@
#define _TIN_DELAUNAY_H #define _TIN_DELAUNAY_H
#include "cmath" #include "cmath"
#include "vector" #include "vector"
#include "algorithm"
#define ZERO 1e-5 #define ZERO 1e-5
@ -22,8 +23,8 @@ struct vertex2dc
double elev; // elevation at the vertex double elev; // elevation at the vertex
vertex2dc() : x(NAN), y(NAN), elev(NAN), id(0) {} vertex2dc() : x(NAN), y(NAN), elev(NAN), id(0) {}
vertex2dc(double inx, double iny, double inelev, unsigned int inid = 0) {set(inx, iny, inelev, inid);} vertex2dc(double inx, double iny, double inelev, unsigned int inid) {set(inx, iny, inelev, inid);}
void set(double inx, double iny, double inelev, unsigned int inid = 0) void set(double inx, double iny, double inelev, unsigned int inid)
{ {
x = inx; y = iny; elev = inelev; id = inid; x = inx; y = iny; elev = inelev; id = inid;
return; return;
@ -131,7 +132,7 @@ struct dem_point
{ {
double x, y; // position of the DEM location double x, y; // position of the DEM location
double elev; // elevation at the DEM location double elev; // elevation at the DEM location
double err; double err; // error of the TIN with respect to the elevation
triangle *host; // host triangle of the DEM location triangle *host; // host triangle of the DEM location
std::vector<triangle*> circum_host; // triangles which circumcircles include the location std::vector<triangle*> circum_host; // triangles which circumcircles include the location
@ -144,55 +145,10 @@ struct dem_point
} }
}; };
/** bool compare_dem_point(dem_point *a, dem_point *b)
* @brief Utility function of the heap_sort function.
*
* @param a Input vector
* @param[in] i vector's index i
* @param[in] n vector's index n
*/
void update_heap(std::vector<dem_point*> &a, int i, int n)
{ {
int iMax = i, iLeft = 2 * i + 1, iRight = 2 * (i + 1); if (a->err > b->err) return true;
return false;
if (iLeft < n && a[iMax]->err > a[iLeft]->err)
{
iMax = iLeft;
}
if (iRight < n && a[iMax]->err > a[iRight]->err)
{
iMax = iRight;
}
if (iMax != i)
{
dem_point *tmp = a[iMax]; a[iMax] = a[i]; a[i] = tmp;
update_heap(a, iMax, n);
}
return;
}
/**
* @brief Heap sort of the dem_point vector in a descending order with respect to the error values
*
* @param a Input vector
*/
void heap_sort(std::vector<dem_point*> &a)
{
int n = a.size();
for (int i = (n - 1) / 2; i >= 0; i--)
{
update_heap(a, i, n);
}
dem_point *tmp;
for (int i = n - 1; i > 0; --i)
{
tmp = a[i]; a[i] = a[0]; a[0] = tmp;
update_heap(a, 0, i);
}
return;
} }
// End DEM definition // End DEM definition
@ -228,7 +184,7 @@ void dem2tin(const std::vector<double> &dem, double xmin, double xmax, double ym
if (dem.size() != xnum*ynum) return; if (dem.size() != xnum*ynum) return;
// Prepare the DEM points // Prepare the DEM points
dem_point *tmp_dem; dem_point *tmp_dem = nullptr;;
std::vector<dem_point*> dem_grid(xnum*ynum); std::vector<dem_point*> dem_grid(xnum*ynum);
std::vector<dem_point*>::iterator d_iter; std::vector<dem_point*>::iterator d_iter;
for (int i = 0; i < ynum; ++i) for (int i = 0; i < ynum; ++i)
@ -240,7 +196,6 @@ void dem2tin(const std::vector<double> &dem, double xmin, double xmax, double ym
} }
vertex2dc *tmp_vert = nullptr; vertex2dc *tmp_vert = nullptr;
tmp_vert = new vertex2dc(xmin, ymin, dem_grid[0]->elev, out_verts.size()); // lower left corner tmp_vert = new vertex2dc(xmin, ymin, dem_grid[0]->elev, out_verts.size()); // lower left corner
out_verts.push_back(tmp_vert); out_verts.push_back(tmp_vert);
@ -320,7 +275,8 @@ void dem2tin(const std::vector<double> &dem, double xmin, double xmax, double ym
dem_grid[i]->err = fabs(dem_grid[i]->host->interpolate(dem_grid[i]->x, dem_grid[i]->y) - dem_grid[i]->elev); dem_grid[i]->err = fabs(dem_grid[i]->host->interpolate(dem_grid[i]->x, dem_grid[i]->y) - dem_grid[i]->elev);
} }
heap_sort(dem_grid); // Sort dem_grid in the desceding order with respect to the error
std::sort(dem_grid.begin(), dem_grid.end(), compare_dem_point);
bool removed; bool removed;
edge tmp_edge; edge tmp_edge;
@ -457,7 +413,8 @@ void dem2tin(const std::vector<double> &dem, double xmin, double xmax, double ym
delete tmp_tri; tmp_tri = nullptr; delete tmp_tri; tmp_tri = nullptr;
} }
heap_sort(dem_grid); // Sort dem_grid in the desceding order with respect to the error
std::sort(dem_grid.begin(), dem_grid.end(), compare_dem_point);
} }
if (err_records != nullptr) if (err_records != nullptr)

View File

@ -1276,9 +1276,9 @@
1275 1.87107 1275 1.87107
1276 1.86875 1276 1.86875
1277 1.86767 1277 1.86767
1278 2.15267 1278 1.86767
1279 2.1165 1279 2.15267
1280 1.86767 1280 2.1165
1281 1.86755 1281 1.86755
1282 1.90845 1282 1.90845
1283 1.8675 1283 1.8675
@ -2086,10 +2086,10 @@
2085 1.4206 2085 1.4206
2086 1.1363 2086 1.1363
2087 1.136 2087 1.136
2088 1.4285 2088 2.002
2089 3.5135 2089 1.136
2090 1.136 2090 1.4285
2091 2.002 2091 3.5135
2092 1.1352 2092 1.1352
2093 1.69885 2093 1.69885
2094 1.1346 2094 1.1346

View File

@ -1283,10 +1283,10 @@ $Nodes
1278 820 40 19.5174 1278 820 40 19.5174
1279 450 650 108.683 1279 450 650 108.683
1280 410 660 96.7007 1280 410 660 96.7007
1281 600 310 141.07 1281 680 390 143.568
1282 590 310 132.237 1282 600 310 141.07
1283 610 310 148.977 1283 590 310 132.237
1284 680 390 143.568 1284 610 310 148.977
1285 640 250 70.0052 1285 640 250 70.0052
1286 650 250 72.3381 1286 650 250 72.3381
1287 700 400 119.085 1287 700 400 119.085
@ -2093,11 +2093,11 @@ $Nodes
2088 350 750 84.5986 2088 350 750 84.5986
2089 330 750 89.6465 2089 330 750 89.6465
2090 240 570 45.0428 2090 240 570 45.0428
2091 520 470 168.821 2091 700 420 115.744
2092 510 470 168.277 2092 690 420 125.532
2093 510 460 176.628 2093 520 470 168.821
2094 700 420 115.744 2094 510 470 168.277
2095 690 420 125.532 2095 510 460 176.628
2096 910 280 26.9883 2096 910 280 26.9883
2097 900 260 26.7625 2097 900 260 26.7625
2098 750 460 65.73990000000001 2098 750 460 65.73990000000001
@ -3294,21 +3294,21 @@ $Elements
941 2 0 627 437 1280 941 2 0 627 437 1280
942 2 0 520 1055 1280 942 2 0 520 1055 1280
943 2 0 1055 112 1280 943 2 0 1055 112 1280
944 2 0 210 1281 1282 944 2 0 511 915 1281
945 2 0 784 1143 1282 945 2 0 914 512 1281
946 2 0 1143 1145 1282 946 2 0 915 914 1281
947 2 0 156 741 1283 947 2 0 512 761 1281
948 2 0 741 489 1283 948 2 0 761 1020 1281
949 2 0 489 300 1283 949 2 0 1020 838 1281
950 2 0 300 1281 1283 950 2 0 838 1113 1281
951 2 0 511 915 1284 951 2 0 1113 511 1281
952 2 0 914 512 1284 952 2 0 210 1282 1283
953 2 0 915 914 1284 953 2 0 784 1143 1283
954 2 0 512 761 1284 954 2 0 1143 1145 1283
955 2 0 761 1020 1284 955 2 0 156 741 1284
956 2 0 1020 838 1284 956 2 0 741 489 1284
957 2 0 838 1113 1284 957 2 0 489 300 1284
958 2 0 1113 511 1284 958 2 0 300 1282 1284
959 2 0 921 272 1285 959 2 0 921 272 1285
960 2 0 272 1012 1285 960 2 0 272 1012 1285
961 2 0 1238 921 1286 961 2 0 1238 921 1286
@ -4250,7 +4250,7 @@ $Elements
1897 2 0 1145 1144 1653 1897 2 0 1145 1144 1653
1898 2 0 97 1064 1653 1898 2 0 97 1064 1653
1899 2 0 1064 1652 1653 1899 2 0 1064 1652 1653
1900 2 0 1282 1145 1654 1900 2 0 1283 1145 1654
1901 2 0 1145 1653 1654 1901 2 0 1145 1653 1654
1902 2 0 1652 87 1654 1902 2 0 1652 87 1654
1903 2 0 1653 1652 1654 1903 2 0 1653 1652 1654
@ -4986,8 +4986,8 @@ $Elements
2633 2 0 1409 448 1881 2633 2 0 1409 448 1881
2634 2 0 1483 1409 1881 2634 2 0 1483 1409 1881
2635 2 0 1666 1483 1881 2635 2 0 1666 1483 1881
2636 2 0 210 1282 1883 2636 2 0 210 1283 1883
2637 2 0 1282 1654 1883 2637 2 0 1283 1654 1883
2638 2 0 87 1173 1883 2638 2 0 87 1173 1883
2639 2 0 1654 87 1883 2639 2 0 1654 87 1883
2640 2 0 1882 210 1883 2640 2 0 1882 210 1883
@ -5299,9 +5299,9 @@ $Elements
2946 2 0 56 616 1962 2946 2 0 56 616 1962
2947 2 0 616 928 1962 2947 2 0 616 928 1962
2948 2 0 928 784 1962 2948 2 0 928 784 1962
2949 2 0 784 1282 1962 2949 2 0 784 1283 1962
2950 2 0 1281 300 1962 2950 2 0 1282 300 1962
2951 2 0 1282 1281 1962 2951 2 0 1283 1282 1962
2952 2 0 368 794 1963 2952 2 0 368 794 1963
2953 2 0 794 1230 1963 2953 2 0 794 1230 1963
2954 2 0 906 368 1964 2954 2 0 906 368 1964
@ -5446,10 +5446,10 @@ $Elements
3093 2 0 759 830 2001 3093 2 0 759 830 2001
3094 2 0 2000 758 2001 3094 2 0 2000 758 2001
3095 2 0 1276 759 2001 3095 2 0 1276 759 2001
3096 2 0 1281 210 2002 3096 2 0 1282 210 2002
3097 2 0 1283 1281 2002 3097 2 0 1284 1282 2002
3098 2 0 376 156 2002 3098 2 0 376 156 2002
3099 2 0 156 1283 2002 3099 2 0 156 1284 2002
3100 2 0 860 1668 2002 3100 2 0 860 1668 2002
3101 2 0 1668 376 2002 3101 2 0 1668 376 2002
3102 2 0 210 1882 2002 3102 2 0 210 1882 2002
@ -5810,28 +5810,28 @@ $Elements
3457 2 0 1553 1399 2090 3457 2 0 1553 1399 2090
3458 2 0 1399 1992 2090 3458 2 0 1399 1992 2090
3459 2 0 1992 962 2090 3459 2 0 1992 962 2090
3460 2 0 47 463 2091 3460 2 0 564 303 2091
3461 2 0 463 467 2091 3461 2 0 249 916 2092
3462 2 0 467 884 2091 3462 2 0 916 762 2092
3463 2 0 884 468 2091 3463 2 0 303 1796 2092
3464 2 0 1867 1869 2092 3464 2 0 1796 249 2092
3465 2 0 1869 546 2092 3465 2 0 2091 303 2092
3466 2 0 468 1867 2092 3466 2 0 762 1112 2092
3467 2 0 2091 468 2092 3467 2 0 1112 2091 2092
3468 2 0 464 47 2093 3468 2 0 47 463 2093
3469 2 0 532 464 2093 3469 2 0 463 467 2093
3470 2 0 546 547 2093 3470 2 0 467 884 2093
3471 2 0 2092 546 2093 3471 2 0 884 468 2093
3472 2 0 47 2091 2093 3472 2 0 1867 1869 2094
3473 2 0 2091 2092 2093 3473 2 0 1869 546 2094
3474 2 0 564 303 2094 3474 2 0 468 1867 2094
3475 2 0 249 916 2095 3475 2 0 2093 468 2094
3476 2 0 916 762 2095 3476 2 0 464 47 2095
3477 2 0 303 1796 2095 3477 2 0 532 464 2095
3478 2 0 1796 249 2095 3478 2 0 546 547 2095
3479 2 0 2094 303 2095 3479 2 0 2094 546 2095
3480 2 0 762 1112 2095 3480 2 0 47 2093 2095
3481 2 0 1112 2094 2095 3481 2 0 2093 2094 2095
3482 2 0 1535 2051 2096 3482 2 0 1535 2051 2096
3483 2 0 399 400 2097 3483 2 0 399 400 2097
3484 2 0 2096 399 2097 3484 2 0 2096 399 2097
@ -5986,10 +5986,10 @@ $Elements
3633 2 0 1511 563 2134 3633 2 0 1511 563 2134
3634 2 0 2133 1511 2134 3634 2 0 2133 1511 2134
3635 2 0 1112 1873 2135 3635 2 0 1112 1873 2135
3636 2 0 2094 1112 2135 3636 2 0 2091 1112 2135
3637 2 0 1873 1111 2135 3637 2 0 1873 1111 2135
3638 2 0 1111 2133 2135 3638 2 0 1111 2133 2135
3639 2 0 564 2094 2135 3639 2 0 564 2091 2135
3640 2 0 2133 2134 2135 3640 2 0 2133 2134 2135
3641 2 0 2134 564 2135 3641 2 0 2134 564 2135
3642 2 0 1208 84 2136 3642 2 0 1208 84 2136
@ -6984,8 +6984,8 @@ $Elements
4631 2 0 226 532 2345 4631 2 0 226 532 2345
4632 2 0 651 226 2345 4632 2 0 651 226 2345
4633 2 0 1690 651 2345 4633 2 0 1690 651 2345
4634 2 0 532 2093 2345 4634 2 0 532 2095 2345
4635 2 0 2093 547 2345 4635 2 0 2095 547 2345
$EndElements $EndElements
$NodeData $NodeData
1 1
@ -8276,10 +8276,10 @@ $NodeData
1278 19.5174 1278 19.5174
1279 108.683 1279 108.683
1280 96.7007 1280 96.7007
1281 141.07 1281 143.568
1282 132.237 1282 141.07
1283 148.977 1283 132.237
1284 143.568 1284 148.977
1285 70.0052 1285 70.0052
1286 72.3381 1286 72.3381
1287 119.085 1287 119.085
@ -9086,11 +9086,11 @@ $NodeData
2088 84.5986 2088 84.5986
2089 89.6465 2089 89.6465
2090 45.0428 2090 45.0428
2091 168.821 2091 115.744
2092 168.277 2092 125.532
2093 176.628 2093 168.821
2094 115.744 2094 168.277
2095 125.532 2095 176.628
2096 26.9883 2096 26.9883
2097 26.7625 2097 26.7625
2098 65.73990000000001 2098 65.73990000000001