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

67
tin.h
View File

@@ -11,6 +11,7 @@
#define _TIN_DELAUNAY_H
#include "cmath"
#include "vector"
#include "algorithm"
#define ZERO 1e-5
@@ -22,8 +23,8 @@ struct vertex2dc
double elev; // elevation at the vertex
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);}
void set(double inx, double iny, double inelev, unsigned int inid = 0)
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)
{
x = inx; y = iny; elev = inelev; id = inid;
return;
@@ -131,7 +132,7 @@ struct dem_point
{
double x, y; // position of 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
std::vector<triangle*> circum_host; // triangles which circumcircles include the location
@@ -144,55 +145,10 @@ struct dem_point
}
};
/**
* @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)
bool compare_dem_point(dem_point *a, dem_point *b)
{
int iMax = i, iLeft = 2 * i + 1, iRight = 2 * (i + 1);
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;
if (a->err > b->err) return true;
return false;
}
// 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;
// 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*>::iterator d_iter;
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;
tmp_vert = new vertex2dc(xmin, ymin, dem_grid[0]->elev, out_verts.size()); // lower left corner
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);
}
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;
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;
}
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)