From 1379d305020d2a938cc9e1b3b0c72ff37dafa744 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sat, 19 Apr 2025 20:55:15 +0200 Subject: [PATCH] Debugging, Tests --- src/db/db/dbPLCConvexDecomposition.cc | 1 + src/pex/pex/pexSquareCountingRExtractor.cc | 55 +++--------- src/pex/pex/pexSquareCountingRExtractor.h | 37 ++++++++ .../pexSquareCountingRExtractorTests.cc | 90 ++++++++++++++++++- 4 files changed, 136 insertions(+), 47 deletions(-) diff --git a/src/db/db/dbPLCConvexDecomposition.cc b/src/db/db/dbPLCConvexDecomposition.cc index 2723eb796..426dfa06d 100644 --- a/src/db/db/dbPLCConvexDecomposition.cc +++ b/src/db/db/dbPLCConvexDecomposition.cc @@ -419,6 +419,7 @@ ConvexDecomposition::hertel_mehlhorn_decomposition (Triangulation &tris, const C for (auto i = iv->begin (); i != iv->end (); ++i) { poly->add_internal_vertex (*i); } + ++iv; } } diff --git a/src/pex/pex/pexSquareCountingRExtractor.cc b/src/pex/pex/pexSquareCountingRExtractor.cc index cdfc0d183..eaa865719 100644 --- a/src/pex/pex/pexSquareCountingRExtractor.cc +++ b/src/pex/pex/pexSquareCountingRExtractor.cc @@ -29,14 +29,6 @@ namespace pex { -SquareCountingRExtractor::SquareCountingRExtractor (double dbu) -{ - m_dbu = dbu; - - m_decomp_param.split_edges = true; - m_decomp_param.with_segments = false; -} - namespace { @@ -66,37 +58,6 @@ private: std::map > m_interactions; }; -struct PortDefinition -{ - PortDefinition () - : type (pex::RNode::Internal), port_index (0) - { } - - PortDefinition (pex::RNode::node_type _type, const db::Point &_location, unsigned int _port_index) - : type (_type), location (_location), port_index (_port_index) - { } - - bool operator< (const PortDefinition &other) const - { - if (type != other.type) { - return type < other.type; - } - if (port_index != other.port_index) { - return port_index < other.port_index; - } - return false; - } - - bool operator== (const PortDefinition &other) const - { - return type == other.type && port_index == other.port_index; - } - - pex::RNode::node_type type; - db::Point location; - unsigned int port_index; -}; - struct JoinEdgeSets { void operator() (std::set &a, const std::set &b) const @@ -107,6 +68,14 @@ struct JoinEdgeSets } +SquareCountingRExtractor::SquareCountingRExtractor (double dbu) +{ + m_dbu = dbu; + + m_decomp_param.split_edges = true; + m_decomp_param.with_segments = false; +} + static double yatx (const db::Edge &e, int x) { @@ -141,8 +110,8 @@ double calculate_squares (db::Coord x1, db::Coord x2, const std::set & } } -static -void rextract_square_counting (const db::Polygon &db_poly, const std::vector > &ports, pex::RNetwork &rnetwork, double /*dbu*/) +void +SquareCountingRExtractor::do_extract (const db::Polygon &db_poly, const std::vector > &ports, pex::RNetwork &rnetwork) { // "trans" will orient the polygon to be flat rather than tall db::Trans trans; @@ -189,7 +158,7 @@ void rextract_square_counting (const db::Polygon &db_poly, const std::vectorfirst.first < cc) { - r += calculate_squares (em->first.first, std::min (cc, em->first.second), em->second); + r += calculate_squares (std::max (c, em->first.first), std::min (cc, em->first.second), em->second); ++em; } @@ -320,7 +289,7 @@ SquareCountingRExtractor::extract (const db::Polygon &polygon, const std::vector p->second = n4p->second; } - rextract_square_counting (db_poly, ports, rnetwork, dbu ()); + do_extract (db_poly, ports, rnetwork); } diff --git a/src/pex/pex/pexSquareCountingRExtractor.h b/src/pex/pex/pexSquareCountingRExtractor.h index 99881099b..b498bb1b2 100644 --- a/src/pex/pex/pexSquareCountingRExtractor.h +++ b/src/pex/pex/pexSquareCountingRExtractor.h @@ -55,6 +55,43 @@ public: virtual void extract (const db::Polygon &polygon, const std::vector &vertex_ports, const std::vector &polygon_ports, RNetwork &rnetwork); +protected: + /** + * @brief A helper structure defining a port + */ + struct PortDefinition + { + PortDefinition () + : type (pex::RNode::Internal), port_index (0) + { } + + PortDefinition (pex::RNode::node_type _type, const db::Point &_location, unsigned int _port_index) + : type (_type), location (_location), port_index (_port_index) + { } + + bool operator< (const PortDefinition &other) const + { + if (type != other.type) { + return type < other.type; + } + if (port_index != other.port_index) { + return port_index < other.port_index; + } + return false; + } + + bool operator== (const PortDefinition &other) const + { + return type == other.type && port_index == other.port_index; + } + + pex::RNode::node_type type; + db::Point location; + unsigned int port_index; + }; + + void do_extract (const db::Polygon &db_poly, const std::vector > &ports, pex::RNetwork &rnetwork); + private: db::plc::ConvexDecompositionParameters m_decomp_param; double m_dbu; diff --git a/src/pex/unit_tests/pexSquareCountingRExtractorTests.cc b/src/pex/unit_tests/pexSquareCountingRExtractorTests.cc index 613d630b9..1e4d7e326 100644 --- a/src/pex/unit_tests/pexSquareCountingRExtractorTests.cc +++ b/src/pex/unit_tests/pexSquareCountingRExtractorTests.cc @@ -24,7 +24,87 @@ #include "pexSquareCountingRExtractor.h" #include "tlUnitTest.h" +namespace +{ + +class TestableSquareCountingRExtractor + : public pex::SquareCountingRExtractor +{ +public: + TestableSquareCountingRExtractor () + : pex::SquareCountingRExtractor (0.001) + { } + + using pex::SquareCountingRExtractor::PortDefinition; + using pex::SquareCountingRExtractor::do_extract; +}; + +} + TEST(basic) +{ + db::Point contour[] = { + db::Point (0, 0), + db::Point (0, 100), + db::Point (1000, 1000), + db::Point (2100, 1000), + db::Point (2100, 0) + }; + + db::Polygon poly; + poly.assign_hull (contour + 0, contour + sizeof (contour) / sizeof (contour[0])); + TestableSquareCountingRExtractor rex; + pex::RNetwork rn; + + TestableSquareCountingRExtractor::PortDefinition pd1 (pex::RNode::Internal, db::Point (-50, 50), 0); + TestableSquareCountingRExtractor::PortDefinition pd2 (pex::RNode::Internal, db::Point (1000, 100), 1); + TestableSquareCountingRExtractor::PortDefinition pd3 (pex::RNode::Internal, db::Point (1000, 500), 2); + TestableSquareCountingRExtractor::PortDefinition pd4 (pex::RNode::Internal, db::Point (2000, 500), 3); + + std::vector pds; + pds.push_back (TestableSquareCountingRExtractor::PortDefinition (pex::RNode::Internal, db::Point (0, 50), 0)); + pds.push_back (TestableSquareCountingRExtractor::PortDefinition (pex::RNode::Internal, db::Point (1000, 100), 1)); + pds.push_back (TestableSquareCountingRExtractor::PortDefinition (pex::RNode::Internal, db::Point (1000, 500), 2)); + pds.push_back (TestableSquareCountingRExtractor::PortDefinition (pex::RNode::Internal, db::Point (2000, 500), 3)); + + std::vector > ports; + for (auto pd = pds.begin (); pd != pds.end (); ++pd) { + ports.push_back (std::make_pair (*pd, rn.create_node (pd->type, pd->port_index))); + } + + rex.do_extract (poly, ports, rn); + + EXPECT_EQ (rn.to_string (), + "R $0 $1 0.390865\n" // w ramp w=100 to 1000 over x=0 to 1000 (squares = (x2-x1)/(w2-w1)*log(w2/w1) by integration) + "R $1 $2 0\n" // transition from y=50 to y=500 parallel to current direction + "R $2 $3 1" // 1 square between x=1000 and 2000 (w=1000) + ); + + // After rotation + + rn.clear (); + + db::Trans r90 (db::Trans::r90); + + poly.transform (r90); + + ports.clear (); + for (auto pd = pds.begin (); pd != pds.end (); ++pd) { + ports.push_back (std::make_pair (*pd, rn.create_node (pd->type, pd->port_index))); + ports.back ().first.location.transform (r90); + } + + rex.do_extract (poly, ports, rn); + + // Same network, but opposite order. $1 and $2 are shorted, hence can be swapped. + EXPECT_EQ (rn.to_string (), + "R $3 $1 1\n" + "R $1 $2 0\n" + "R $2 $0 0.390865" + ); +} + +TEST(extraction) { db::Point contour[] = { db::Point (0, 0), @@ -46,15 +126,17 @@ TEST(basic) pex::SquareCountingRExtractor rex (dbu); std::vector vertex_ports; - vertex_ports.push_back (db::Point (0, 50)); - vertex_ports.push_back (db::Point (1650, 50)); + vertex_ports.push_back (db::Point (0, 50)); // V0 + vertex_ports.push_back (db::Point (1650, 50)); // V1 std::vector polygon_ports; - polygon_ports.push_back (db::Polygon (db::Box (1000, 900, 1100, 1000))); + polygon_ports.push_back (db::Polygon (db::Box (1000, 900, 1100, 1000))); // P0 rex.extract (poly, vertex_ports, polygon_ports, rn); EXPECT_EQ (rn.to_string (), - "" + "R V0 $0 0.0952381\n" + "R $0 V1 0.166667\n" + "R P0 $0 0.117647" ) }