diff --git a/delaunay.h b/delaunay.h index a02f074..a6513b4 100644 --- a/delaunay.h +++ b/delaunay.h @@ -131,7 +131,8 @@ void triangulation(std::vector &in_verts, std::vector &out_ assit_vert.push_back(tmp_vert); triangle *tmp_tri = nullptr; - std::vector exist_tri, cnst_tri, remain_tri; + std::vector exist_tri, cnst_tri; + std::vector::iterator t_iter; tmp_tri = new triangle(assit_vert[0], assit_vert[1], assit_vert[2]); // order the vertex anti-clock wise exist_tri.push_back(tmp_tri); @@ -147,25 +148,22 @@ void triangulation(std::vector &in_verts, std::vector &out_ std::vector::iterator e_iter; for (int i = 0; i < in_verts.size(); ++i) { - // 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 + // determine triangles that include the point and add the triangle to the cnst_tri and remove the triangle from exist_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) + for (t_iter = exist_tri.begin(); t_iter != exist_tri.end(); ) { - 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); + tmp_tri = *t_iter; - if ((dist - exist_tri[t]->cr) <= ZERO) // Points on the circumcircle are also included + dist = (tmp_tri->cx - in_verts[i].x) * (tmp_tri->cx - in_verts[i].x) + + (tmp_tri->cy - in_verts[i].y) * (tmp_tri->cy - in_verts[i].y); + + if ((dist - tmp_tri->cr) <= ZERO) // Points on the circumcircle are also included { - cnst_tri.push_back(exist_tri[t]); - } - else - { - remain_tri.push_back(exist_tri[t]); + t_iter = exist_tri.erase(t_iter); + cnst_tri.push_back(tmp_tri); } + else t_iter++; } // loop to remove duplicate edges @@ -195,24 +193,22 @@ void triangulation(std::vector &in_verts, std::vector &out_ } } - exist_tri.clear(); - exist_tri.reserve(remain_tri.size() + cnst_edge.size()); - // copy remain_tri to exist_tri - for (int r = 0; r < remain_tri.size(); ++r) - { - exist_tri.push_back(remain_tri[r]); - } - // construct new triangles and add to exist_tri for (int c = 0; c < cnst_edge.size(); ++c) { tmp_tri = new triangle(cnst_edge[c].vert[0], cnst_edge[c].vert[1], &in_verts[i]); // order the vertex anti-clock wise exist_tri.push_back(tmp_tri); } + + // destroy memories used by cnst_edge + for (int c = 0; c < cnst_tri.size(); ++c) + { + tmp_tri = cnst_tri[c]; + delete tmp_tri; tmp_tri = nullptr; + } } // remove any triangles has an assistant vertex from exist_tri - std::vector::iterator t_iter; for (t_iter = exist_tri.begin(); t_iter != exist_tri.end(); ) { tmp_tri = *t_iter; @@ -220,14 +216,14 @@ void triangulation(std::vector &in_verts, std::vector &out_ tmp_tri->vert[1] == assit_vert[0] || tmp_tri->vert[1] == assit_vert[1] || tmp_tri->vert[1] == assit_vert[2] || tmp_tri->vert[1] == assit_vert[3] || tmp_tri->vert[2] == assit_vert[0] || tmp_tri->vert[2] == assit_vert[1] || tmp_tri->vert[2] == assit_vert[2] || tmp_tri->vert[2] == assit_vert[3]) { - // destroy the memory located and remove from the vector + // destroy the memories located and remove from the vector t_iter = exist_tri.erase(t_iter); delete tmp_tri; tmp_tri = nullptr; } else t_iter++; } - // copy exist_tri to out_tris and destroy memory located + // copy exist_tri to out_tris and destroy memories located out_tris.resize(exist_tri.size()); for (int i = 0; i < exist_tri.size(); ++i) { @@ -235,7 +231,7 @@ void triangulation(std::vector &in_verts, std::vector &out_ delete exist_tri[i]; exist_tri[i] = nullptr; } - // destroy memory located for assit_vert + // destroy memories located for assit_vert for (int i = 0; i < 4; ++i) { delete assit_vert[i]; assit_vert[i] = nullptr;