Internal vertex ID added, cleanup

This commit is contained in:
Matthias Koefferlein 2025-04-17 23:40:58 +02:00
parent a2ef7a28f8
commit 3ed39e8a4a
6 changed files with 59 additions and 27 deletions

View File

@ -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
{

View File

@ -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
*/

View File

@ -233,10 +233,6 @@ ConvexDecomposition::hertel_mehlhorn_decomposition (Triangulation &tris, const C
std::vector<ConcaveCorner> concave_vertexes;
collect_concave_vertexes (concave_vertexes);
// @@@ return if no concave corners
// @@@ sort concave vertexes
std::vector<db::DPoint> 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<Edge *> (e)); // @@@ const_cast
edges.insert (const_cast<Edge *> (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);
}
}
}

View File

@ -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<std::vector<Vertex *> > 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<std::vector<Vertex *> > edge_contours;

View File

@ -125,6 +125,7 @@ TEST(internal_vertex)
std::vector<db::Point> 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<std::string> 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);
}

View File

@ -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)