Skip to content

Commit 25d3960

Browse files
author
Matthias Koefferlein
committed
Refinement of snap, fixed tests
1 parent ea3e1e9 commit 25d3960

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/laybasic/laybasic/laySnap.cc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,18 @@ class ContourFinder
258258
public:
259259
/**
260260
* @brief Constructor
261+
*
262+
* "vertex_mode" is:
263+
* 0: no snapping to vertexes
264+
* 1: snapping to edge vertexes
265+
* 2: also snapping to centers
261266
*/
262-
ContourFinder (const db::DPoint &original, const db::DVector &grid, const std::vector <db::DEdge> &cutlines, bool with_vertex = true, bool directed = false)
267+
ContourFinder (const db::DPoint &original, const db::DVector &grid, const std::vector <db::DEdge> &cutlines, int vertex_mode = 2, bool directed = false)
263268
: m_any (false), m_any_exact (false),
264269
m_original (original), m_is_vertex (false), m_is_vertex_exact (false),
265270
m_tests (10000 /* max. number of tests, TODO: make variable? */),
266271
mp_layout (0), m_cutlines (cutlines), mp_prop_sel (0), m_inv_prop_sel (false),
267-
m_with_vertex (with_vertex), m_directed (directed)
272+
m_vertex_mode (vertex_mode), m_directed (directed)
268273
{
269274
m_projection_constraint = ! m_cutlines.empty ();
270275

@@ -491,7 +496,7 @@ class ContourFinder
491496

492497
void closest (const db::DPoint &p)
493498
{
494-
if (! m_with_vertex) {
499+
if (m_vertex_mode == 0) {
495500

496501
// Only edges are considered for snapping.
497502

@@ -668,7 +673,9 @@ class ContourFinder
668673
test_edge_with_center (t * db::Edge (db::Point (box.right (), box.bottom ()), box.p1 ()));
669674

670675
// test for box center
671-
test_edge (t * db::Edge (box.center (), box.center ()));
676+
if (m_vertex_mode > 1) {
677+
test_edge (t * db::Edge (box.center (), box.center ()));
678+
}
672679

673680
} else if (shape->is_point ()) {
674681

@@ -710,7 +717,7 @@ class ContourFinder
710717
void
711718
test_edge_with_center (const db::DEdge &edg)
712719
{
713-
if (m_with_vertex && ! edg.is_degenerate ()) {
720+
if (m_vertex_mode > 1 && ! edg.is_degenerate ()) {
714721

715722
db::DPoint c = edg.p1 () + (edg.p2 () - edg.p1 ()) * 0.5;
716723

@@ -726,7 +733,7 @@ class ContourFinder
726733
void
727734
test_edge (const db::DEdge &edg)
728735
{
729-
if (m_with_vertex) {
736+
if (m_vertex_mode > 0) {
730737

731738
// vertex snap is just annoying when trying to measure the width of simulation contours ..
732739
// But: when measuring corner-to-corner distances it is very valuable ..
@@ -761,7 +768,7 @@ class ContourFinder
761768
const std::set<db::properties_id_type> *mp_prop_sel;
762769
bool m_inv_prop_sel;
763770
bool m_projection_constraint;
764-
bool m_with_vertex;
771+
int m_vertex_mode;
765772
bool m_directed;
766773
};
767774

@@ -836,7 +843,7 @@ do_obj_snap2 (lay::LayoutViewBase *view, const db::DPoint &pt1, const db::DPoint
836843
db::DPoint dp1 (pt1);
837844
db::DPoint dp2 (pt2);
838845

839-
ContourFinder finder (dp1, grid, cutlines, cutlines.empty () /*vertex snap on "any direction"*/);
846+
ContourFinder finder (dp1, grid, cutlines, cutlines.empty () ? 1 : 0 /*vertex snap on "any direction", edge vertexes*/);
840847

841848
double sr = min_search_range;
842849
while (sr < max_search_range + 1e-6) {
@@ -880,7 +887,7 @@ do_obj_snap2 (lay::LayoutViewBase *view, const db::DPoint &pt1, const db::DPoint
880887

881888
}
882889

883-
ContourFinder finder2 (dp2, grid, cl, false /*no vertex snap*/, true /*directional cutlines*/);
890+
ContourFinder finder2 (dp2, grid, cl, 0 /*no vertex snap*/, true /*directional cutlines*/);
884891

885892
double sr2 = min_search_range;
886893
while (sr2 < max_search_range + 1e-6) {

src/laybasic/unit_tests/laySnapTests.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ TEST(1)
6666
EXPECT_EQ (res.object_snap, lay::PointSnapToObjectResult::ObjectUnspecific); // center point of edge
6767
EXPECT_EQ (res.snapped_point.to_string (), "0.5,0.5");
6868

69-
res = lay::obj_snap (&view, db::DPoint (0.485, 0.505), db::DVector (0.01, 0.01), 0.1);
69+
res = lay::obj_snap (&view, db::DPoint (0.385, 0.605), db::DVector (0.01, 0.01), 0.1);
7070
EXPECT_EQ (res.object_snap, lay::PointSnapToObjectResult::ObjectEdge);
71-
EXPECT_EQ (res.snapped_point.to_string (), "0.49,0.51");
71+
EXPECT_EQ (res.snapped_point.to_string (), "0.39,0.61");
7272
EXPECT_EQ (res.object_ref.to_string (), "(0,1;1,0)");
7373

7474
res = lay::obj_snap (&view, db::DPoint (0.205, 0.215), db::DVector (0.01, 0.025), 0.1);
@@ -87,7 +87,7 @@ TEST(1)
8787
EXPECT_EQ (res.object_snap, lay::PointSnapToObjectResult::ObjectVertex);
8888
EXPECT_EQ (res.snapped_point.to_string (), "0,1");
8989

90-
res = lay::obj_snap (&view, db::DPoint (1.000, 0.605), db::DPoint (0.405, 0.600), db::DVector (), lay::AC_Horizontal, 0.1);
90+
res = lay::obj_snap (&view, db::DPoint (1.000, 0.605), db::DPoint (0.405, 0.600), db::DVector (), lay::AC_Horizontal, 0.05);
9191
EXPECT_EQ (res.object_snap, lay::PointSnapToObjectResult::ObjectEdge);
9292
EXPECT_EQ (res.snapped_point.to_string (), "0.395,0.605");
9393

@@ -136,16 +136,20 @@ TEST(1)
136136
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0.355,0.645;0,0.29)");
137137

138138
res2 = lay::obj_snap2 (&view, db::DPoint (0.5, 0.5), db::DVector (), 0.005, 1.0);
139+
EXPECT_EQ (res2.any, false); // on edge -> measurement chooses wrong direction
140+
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0,0;0,0)");
141+
142+
res2 = lay::obj_snap2 (&view, db::DPoint (0.495, 0.495), db::DVector (), 0.005, 1.0);
139143
EXPECT_EQ (res2.any, true);
140-
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0.5,0.5;0.5,0.5)");
144+
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0.5,0.5;0,0)");
141145

142146
res2 = lay::obj_snap2 (&view, db::DPoint (0.6, 0.4), db::DVector (), 0.005, 1.0);
143147
EXPECT_EQ (res2.any, false);
144148
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0,0;0,0)");
145149

146150
res2 = lay::obj_snap2 (&view, db::DPoint (0.005, 0.5), db::DVector (), 0.005, 1.0);
147151
EXPECT_EQ (res2.any, true);
148-
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0,0.5;0,0.5)");
152+
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0,0.5;0.5,0.5)");
149153

150154
res2 = lay::obj_snap2 (&view, db::DPoint (0.005, 0.4), db::DVector (), 0.005, 1.0);
151155
EXPECT_EQ (res2.any, true);
@@ -156,10 +160,6 @@ TEST(1)
156160
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0,0;0,0)");
157161

158162
res2 = lay::obj_snap2 (&view, db::DPoint (-0.2, 0.5), db::DVector (), 0.005, 1.0);
159-
EXPECT_EQ (res2.any, true);
160-
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0,0.5;0,0.5)");
161-
162-
res2 = lay::obj_snap2 (&view, db::DPoint (-0.2, 0.4), db::DVector (), 0.005, 1.0);
163163
EXPECT_EQ (res2.any, false);
164164
EXPECT_EQ (db::DEdge (res2.first, res2.second).to_string (), "(0,0;0,0)");
165165
}

0 commit comments

Comments
 (0)