From bc6b04bb063a1ca65b2611dcc1ca8a522777fbac Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Wed, 15 Sep 2021 15:49:28 +0800 Subject: [PATCH] update src --- delaunay.h | 40 ++++++++++++++++++----------- demo.cpp | 33 ++++++++++++++++-------- demo.off | 74 ++++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 99 insertions(+), 48 deletions(-) diff --git a/delaunay.h b/delaunay.h index 3db46f2..a02f074 100644 --- a/delaunay.h +++ b/delaunay.h @@ -12,15 +12,13 @@ #include "cmath" #include "vector" -#include "iostream" - #define ZERO 1e-5 // Start vertex definition struct vertex2dc { - unsigned int id; - double x, y; + unsigned int id; // index of the vertex + double x, y; // position of the vertex vertex2dc() : x(NAN), y(NAN), id(0) {} vertex2dc(double inx, double iny, unsigned int inid = 0) {set(inx, iny, inid);} @@ -30,12 +28,21 @@ struct vertex2dc return; } }; + +bool operator ==(const vertex2dc &a, const vertex2dc &b) // overload the == operator for vertex2dc type +{ + if(fabs(a.x - b.x) <= ZERO && fabs(a.y - b.y) <= ZERO) + { + return true; + } + return false; +} // End vertex definition // Start edge definition struct edge { - vertex2dc *vert[2]; + vertex2dc *vert[2]; // vertex of the edge edge() {vert[0] = vert[1] = nullptr;} edge(vertex2dc *v0ptr, vertex2dc *v1ptr) {set(v0ptr, v1ptr);} @@ -46,7 +53,7 @@ struct edge } }; -bool operator ==(const edge &a, const edge &b) +bool operator ==(const edge &a, const edge &b) // overload the == operator for edge type { if((a.vert[0] == b.vert[0] && a.vert[1] == b.vert[1]) || (a.vert[0] == b.vert[1] && a.vert[1] == b.vert[0])) @@ -60,9 +67,9 @@ bool operator ==(const edge &a, const edge &b) // Start triangle definition struct triangle { - vertex2dc *vert[3]; - double cx, cy; // triangle's circular center - double cr; // triangle's circular radius + vertex2dc *vert[3]; // vertex of the triangle + double cx, cy; // center of the triangle's circumcircle + double cr; // radius of the circumcircle triangle() {vert[0] = vert[1] = vert[2] = nullptr;} triangle(vertex2dc *v0ptr, vertex2dc *v1ptr, vertex2dc *v2ptr) {set(v0ptr, v1ptr, v2ptr);} @@ -83,10 +90,10 @@ struct triangle // End triangle definition /** - * @brief 2D triangulation of given points using the Bowyer-Watson algorithm. + * @brief 2D Delaunay triangulation of some given points using the Bowyer-Watson algorithm. * - * @param in_verts Input vertexes - * @param out_tris Output triangles + * @param in_verts Input vertexes. Defined by the user. + * @param out_tris Output triangles. Compute by the function. */ void triangulation(std::vector &in_verts, std::vector &out_tris) { @@ -106,7 +113,7 @@ void triangulation(std::vector &in_verts, std::vector &out_ double midx = 0.5*(xmin + xmax); double midy = 0.5*(ymin + ymax); - double maxi_s = std::max(xmax - xmin, ymax - ymin); // use an extra wide rectangle to include all points + double maxi_s = std::max(xmax - xmin, ymax - ymin); // use an four times bigger rectangle to include all points vertex2dc *tmp_vert = nullptr; std::vector assit_vert; @@ -142,6 +149,8 @@ void triangulation(std::vector &in_verts, std::vector &out_ { // determine triangles that include the point and add the triangle to the cnst_tri // Otherwise, add the triangle to the remain_tri + // this is the part that could take a lot of time if we are working with a large amount of points + // We will fix this later cnst_tri.clear(); remain_tri.clear(); for (int t = 0; t < exist_tri.size(); ++t) @@ -149,7 +158,7 @@ void triangulation(std::vector &in_verts, std::vector &out_ dist = (exist_tri[t]->cx - in_verts[i].x) * (exist_tri[t]->cx - in_verts[i].x) + (exist_tri[t]->cy - in_verts[i].y) * (exist_tri[t]->cy - in_verts[i].y); - if ((dist - exist_tri[t]->cr) <= ZERO) // inside // think more later + if ((dist - exist_tri[t]->cr) <= ZERO) // Points on the circumcircle are also included { cnst_tri.push_back(exist_tri[t]); } @@ -168,7 +177,7 @@ void triangulation(std::vector &in_verts, std::vector &out_ tmp_edge.set(cnst_tri[c]->vert[e], cnst_tri[c]->vert[(e+1)%3]); removed = false; - for (e_iter = cnst_edge.begin(); e_iter != cnst_edge.end(); ++e_iter) + for (e_iter = cnst_edge.begin(); e_iter != cnst_edge.end(); ) { if (tmp_edge == *e_iter) // duplicate edge, remove from cnst_edge { @@ -176,6 +185,7 @@ void triangulation(std::vector &in_verts, std::vector &out_ removed = true; break; // no need to search more } + else e_iter++; } if (!removed) // not a duplicate edge, add to the cnst_edge diff --git a/demo.cpp b/demo.cpp index ff837b8..638a901 100644 --- a/demo.cpp +++ b/demo.cpp @@ -3,17 +3,28 @@ int main(int argc, char const *argv[]) { - std::vector points(10); - points[0].set(-0.7, -0.3, 0); - points[1].set(0.1, -0.4, 1); - points[2].set(-0.1, -0.1, 2); - points[3].set(-0.4, 0.0, 3); - points[4].set(-0.4, -0.3, 4); - points[5].set(-0.2, -0.1, 5); - points[6].set(-0.2, -0.6, 6); - points[7].set(-0.2, -0.4, 7); - points[8].set(-0.5, -0.5, 8); - points[9].set(-0.6, -0.2, 9); + std::vector points(21); + points[0].set(-0.8, -0.8, 0); + points[1].set(0.4, -1.2, 1); + points[2].set(1.2, -0.9, 2); + points[3].set(1.6, 0.1, 3); + points[4].set(2.5, 0.5, 4); + points[5].set(4.1, 0.7, 5); + points[6].set(5.7, 1.8, 6); + points[7].set(5.1, 3.4, 7); + points[8].set(2.5, 4.4, 8); + points[9].set(1.2, 3.7, 9); + points[10].set(-1.2, 3.9, 10); + points[11].set(-3.2, 5.1, 11); + points[12].set(-4.3, 2.9, 12); + points[13].set(-3.1, 0.7, 13); + points[14].set(-1.3, 0.6, 14); + points[15].set(-2.1, 2.9, 15); + points[16].set(0.6, 1.2, 16); + points[17].set(0.1, 2.4, 17); + points[18].set(2.4, 2.8, 18); + points[19].set(3.5, 1.8, 19); + points[20].set(3.6, 3.1, 20); std::vector elements; triangulation(points, elements); diff --git a/demo.off b/demo.off index 11b7d72..9f68ef3 100644 --- a/demo.off +++ b/demo.off @@ -1,23 +1,53 @@ OFF -10 11 0 --0.7 -0.3 0 -0.1 -0.4 0 --0.1 -0.1 0 --0.4 0 0 --0.4 -0.3 0 --0.2 -0.1 0 --0.2 -0.6 0 --0.2 -0.4 0 --0.5 -0.5 0 --0.6 -0.2 0 -3 2 3 5 -3 3 4 5 -3 1 2 7 -3 6 1 7 -3 2 5 7 -3 5 4 7 -3 6 7 8 -3 7 4 8 -3 4 3 9 -3 0 8 9 -3 8 4 9 +21 30 0 +-0.8 -0.8 0 +0.4 -1.2 0 +1.2 -0.9 0 +1.6 0.1 0 +2.5 0.5 0 +4.1 0.7 0 +5.7 1.8 0 +5.1 3.4 0 +2.5 4.4 0 +1.2 3.7 0 +-1.2 3.9 0 +-3.2 5.1 0 +-4.3 2.9 0 +-3.1 0.7 0 +-1.3 0.6 0 +-2.1 2.9 0 +0.6 1.2 0 +0.1 2.4 0 +2.4 2.8 0 +3.5 1.8 0 +3.6 3.1 0 +3 1 2 3 +3 3 2 4 +3 4 2 5 +3 9 8 10 +3 10 8 11 +3 13 0 14 +3 10 11 15 +3 11 12 15 +3 12 13 15 +3 13 14 15 +3 0 1 16 +3 1 3 16 +3 3 4 16 +3 14 0 16 +3 9 10 17 +3 10 15 17 +3 15 14 17 +3 14 16 17 +3 8 9 18 +3 16 4 18 +3 9 17 18 +3 17 16 18 +3 5 6 19 +3 6 7 19 +3 4 5 19 +3 18 4 19 +3 7 8 20 +3 8 18 20 +3 18 19 20 +3 19 7 20