diff --git a/src/db/db/dbPLC.cc b/src/db/db/dbPLC.cc index e75673cf1..c44a96760 100644 --- a/src/db/db/dbPLC.cc +++ b/src/db/db/dbPLC.cc @@ -55,19 +55,19 @@ Vertex::Vertex (Graph *graph, const db::DPoint &p) } Vertex::Vertex (Graph *graph, const Vertex &v) - : DPoint (), mp_graph (graph), m_is_precious (false) + : DPoint (), mp_graph (graph), m_is_precious (false), m_id (0) { operator= (v); } Vertex::Vertex (Graph *graph, db::DCoord x, db::DCoord y) - : DPoint (x, y), mp_graph (graph), m_is_precious (false) + : DPoint (x, y), mp_graph (graph), m_is_precious (false), m_id (0) { // .. nothing yet .. } Vertex::Vertex (const Vertex &v) - : DPoint (v), mp_graph (v.mp_graph), m_is_precious (v.m_is_precious) + : DPoint (v), mp_graph (v.mp_graph), m_is_precious (v.m_is_precious), m_id (v.m_id) { // NOTE: edges are not copied! } @@ -83,6 +83,7 @@ Vertex &Vertex::operator= (const Vertex &v) // NOTE: edges are not copied! db::DPoint::operator= (v); m_is_precious = v.m_is_precious; + m_id = v.m_id; } return *this; } @@ -686,6 +687,17 @@ Polygon::contains (const db::DPoint &point) const return res; } +Edge * +Polygon::next_edge (const Edge *edge, const Vertex *vertex) const +{ + for (auto e = mp_e.begin (); e != mp_e.end (); ++e) { + if (*e != edge && ((*e)->v1 () == vertex || (*e)->v2 () == vertex)) { + return *e; + } + } + return 0; +} + double Polygon::min_edge_length () const { diff --git a/src/db/db/dbPLC.h b/src/db/db/dbPLC.h index c88c8a506..ea5ed4e38 100644 --- a/src/db/db/dbPLC.h +++ b/src/db/db/dbPLC.h @@ -127,17 +127,29 @@ public: bool has_edge (const Edge *edge) const; /** - * @brief Sets a valid indicating whether the vertex is precious + * @brief Sets a value indicating whether the vertex is precious * * "precious" vertexes are not removed during triangulation for example. */ - void set_is_precious (bool f) { m_is_precious = f; } + void set_is_precious (bool f, unsigned int id) + { + m_is_precious = f; + m_id = id; + } /** - * @brief Gets a valid indicating whether the vertex is precious + * @brief Gets a value indicating whether the vertex is precious */ bool is_precious () const { return m_is_precious; } + /** + * @brief Gets the ID passed to "set_is_precious" + * + * This ID can be used to identify the vertex in the context it came from (e.g. + * index in point vector). + */ + unsigned int id () const { return m_id; } + /** * @brief Returns a string representation of the vertex */ @@ -175,7 +187,8 @@ private: Graph *mp_graph; edges_type mp_edges; - bool m_is_precious; + bool m_is_precious : 1; + unsigned int m_id : 31; }; /** @@ -598,6 +611,14 @@ public: mp_v.push_back (v); } + /** + * @brief Reserves for n internal vertexes + */ + void reserve_internal_vertexes (size_t n) + { + mp_v.reserve (mp_v.size () + n); + } + /** * @brief Gets the nth vertex (n wraps around and can be negative) * The vertexes are oriented clockwise. @@ -716,6 +737,11 @@ public: return false; } + /** + * @brief Coming from an edge e and the vertex v, gets the next edge + */ + Edge *next_edge (const Edge *e, const Vertex *v) const; + /** * @brief Returns the minimum edge length */ diff --git a/src/db/db/dbPLCConvexDecomposition.cc b/src/db/db/dbPLCConvexDecomposition.cc index a39b5295d..45eda37ed 100644 --- a/src/db/db/dbPLCConvexDecomposition.cc +++ b/src/db/db/dbPLCConvexDecomposition.cc @@ -233,10 +233,6 @@ ConvexDecomposition::hertel_mehlhorn_decomposition (Triangulation &tris, const C std::vector concave_vertexes; collect_concave_vertexes (concave_vertexes); - // @@@ return if no concave corners - - // @@@ sort concave vertexes - std::vector new_points; if (with_segments) { @@ -306,15 +302,8 @@ ConvexDecomposition::hertel_mehlhorn_decomposition (Triangulation &tris, const C const Polygon *t = e->v2 () == v0 ? e->right () : e->left (); tl_assert (t != 0); - // @@@ make a method of Polygon -> next_edge(Edge *e, Vertex *at) - const Edge *en = 0; - for (unsigned int i = 0; i < 3; ++i) { - const Edge *ee = t->edge (i); - if (ee != e && (ee->v1 () == v0 || ee->v2 () == v0)) { - en = ee; - break; - } - } + const Edge *en = t->next_edge (e, v0); + tl_assert (en != 0); db::DVector v1 = e->edge ().d () * (e->v1 () == v0 ? 1.0 : -1.0); db::DVector v2 = en->edge ().d () * (en->v1 () == v0 ? 1.0 : -1.0); @@ -397,7 +386,7 @@ ConvexDecomposition::hertel_mehlhorn_decomposition (Triangulation &tris, const C } if (! qq || essential_edges.find (e) != essential_edges.end ()) { - edges.insert (const_cast (e)); // @@@ const_cast + edges.insert (const_cast (e)); // TODO: ugly const_cast } else if (left_triangles.find (qq) != left_triangles.end ()) { next_queue.push_back (qq); } @@ -422,8 +411,9 @@ ConvexDecomposition::hertel_mehlhorn_decomposition (Triangulation &tris, const C auto iv = internal_vertexes.begin (); for (auto p = polygons.begin (); p != polygons.end (); ++p) { Polygon *poly = mp_graph->create_polygon (p->begin (), p->end ()); + poly->reserve_internal_vertexes (iv->size ()); for (auto i = iv->begin (); i != iv->end (); ++i) { - poly->add_internal_vertex (*i); // @@@ reserve? + poly->add_internal_vertex (*i); } } } diff --git a/src/db/db/dbPLCTriangulation.cc b/src/db/db/dbPLCTriangulation.cc index 30757e249..90f68d296 100644 --- a/src/db/db/dbPLCTriangulation.cc +++ b/src/db/db/dbPLCTriangulation.cc @@ -1364,8 +1364,9 @@ Triangulation::create_constrained_delaunay (const db::Polygon &p, const std::vec { clear (); + unsigned int id = 0; for (auto v = vertexes.begin (); v != vertexes.end (); ++v) { - insert_point (trans * *v)->set_is_precious (true); + insert_point (trans * *v)->set_is_precious (true, id++); } std::vector > edge_contours; @@ -1379,8 +1380,9 @@ Triangulation::create_constrained_delaunay (const db::DPolygon &p, const std::ve { clear (); + unsigned int id = 0; for (auto v = vertexes.begin (); v != vertexes.end (); ++v) { - insert_point (trans * *v)->set_is_precious (true); + insert_point (trans * *v)->set_is_precious (true, id++); } std::vector > edge_contours; diff --git a/src/db/unit_tests/dbPLCConvexDecompositionTests.cc b/src/db/unit_tests/dbPLCConvexDecompositionTests.cc index fb6dcd245..2cb1e1a70 100644 --- a/src/db/unit_tests/dbPLCConvexDecompositionTests.cc +++ b/src/db/unit_tests/dbPLCConvexDecompositionTests.cc @@ -125,6 +125,7 @@ TEST(internal_vertex) std::vector vertexes; vertexes.push_back (db::Point (0, 50)); // on edge vertexes.push_back (db::Point (200, 70)); + vertexes.push_back (db::Point (0, 0)); // on vertex db::Polygon poly; poly.assign_hull (contour + 0, contour + sizeof (contour) / sizeof (contour[0])); @@ -144,10 +145,10 @@ TEST(internal_vertex) std::vector ip; for (size_t i = 0; i < p->internal_vertexes (); ++i) { - ip.push_back (p->internal_vertex (i)->to_string ()); + ip.push_back (p->internal_vertex (i)->to_string () + "#" + tl::to_string (p->internal_vertex (i)->id ())); } std::sort (ip.begin (), ip.end ()); - EXPECT_EQ (tl::join (ip, "/"), "(0, 0.05)/(0.2, 0.07)"); + EXPECT_EQ (tl::join (ip, "/"), "(0, 0)#2/(0, 0.05)#0/(0.2, 0.07)#1"); EXPECT_EQ (++p == plc.end (), true); } diff --git a/src/db/unit_tests/dbPLCGraphTests.cc b/src/db/unit_tests/dbPLCGraphTests.cc index 3536b5ba4..29b7d9597 100644 --- a/src/db/unit_tests/dbPLCGraphTests.cc +++ b/src/db/unit_tests/dbPLCGraphTests.cc @@ -54,8 +54,9 @@ TEST(basic) EXPECT_EQ (v1->to_string (), "(2, 1)"); EXPECT_EQ (v1->is_precious (), false); - v1->set_is_precious (true); + v1->set_is_precious (true, 17); EXPECT_EQ (v1->is_precious (), true); + EXPECT_EQ (v1->id (), 17u); } TEST(edge)