Debian Codegraph @

3rd_party/router/kimath/src/geometry/poly_grid_partition.cpp

raw
1 /*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2016-2017 CERN
5 * @author Tomasz Wlostowski <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25 #include <geometry/poly_grid_partition.h>
26 #include <math/util.h>
27
28 POLY_GRID_PARTITION::POLY_GRID_PARTITION( const SHAPE_LINE_CHAIN& aPolyOutline, int gridSize )
29 {
30 build( aPolyOutline, gridSize );
31 }
32
33
34 int POLY_GRID_PARTITION::ContainsPoint( const VECTOR2I& aP, int aClearance ) // const
35 {
36 if( containsPoint( aP ) )
37 return 1;
38
39 if( aClearance > 0 )
40 return checkClearance( aP, aClearance );
41
42 return 0;
43 }
44
45
46 int POLY_GRID_PARTITION::containsPoint( const VECTOR2I& aP, bool debug ) const
47 {
48 const auto gridPoint = poly2grid( aP );
49
50 if( !m_bbox.Contains( aP ) )
51 return 0;
52
53 SCAN_STATE state;
54 const EDGE_LIST& cell = m_grid[m_gridSize * gridPoint.y + gridPoint.x];
55
56 scanCell( state, cell, aP, gridPoint.x, gridPoint.y );
57
58 if( state.nearest < 0 )
59 {
60 state = SCAN_STATE();
61
62 for( int d = 1; d <= m_gridSize; d++ )
63 {
64 int xl = gridPoint.x - d;
65 int xh = gridPoint.x + d;
66
67 if( xl >= 0 )
68 {
69 const EDGE_LIST& cell2 = m_grid[m_gridSize * gridPoint.y + xl];
70 scanCell( state, cell2, aP, xl, gridPoint.y );
71
72 if( state.nearest >= 0 )
73 break;
74 }
75
76 if( xh < m_gridSize )
77 {
78 const EDGE_LIST& cell2 = m_grid[m_gridSize * gridPoint.y + xh];
79 scanCell( state, cell2, aP, xh, gridPoint.y );
80
81 if( state.nearest >= 0 )
82 break;
83 }
84 }
85 }
86
87 #ifdef TOM_EXTRA_VERBOSE
88 printf( "Nearest: %d prev: %d dmax %d\n", state.nearest, state.nearest_prev, state.dist_max );
89 #endif
90
91 if( state.nearest < 0 )
92 return 0;
93
94 if( state.dist_max == 0 )
95 return 1;
96
97
98 // special case for diagonal 'slits', e.g. two segments that partially overlap each other.
99 // Just love handling degeneracy... As I can't find any reliable way of fixing it for the moment,
100 // let's fall back to the good old O(N) point-in-polygon test
101 if( state.nearest_prev >= 0 && state.dist_max == state.dist_prev )
102 {
103 int d = std::abs( state.nearest_prev - state.nearest );
104
105 if( ( d == 1 ) && ( ( m_flags[state.nearest_prev] & m_flags[state.nearest] ) == 0 ) )
106 {
107 return m_outline.PointInside( aP );
108 }
109 }
110
111 if( state.dist_max > 0 )
112 {
113 return m_flags[state.nearest] & LEAD_EDGE ? 1 : 0;
114 }
115 else
116 {
117 return m_flags[state.nearest] & TRAIL_EDGE ? 1 : 0;
118 }
119 }
120
121
122 bool POLY_GRID_PARTITION::checkClearance( const VECTOR2I& aP, int aClearance )
123 {
124 int gx0 = poly2gridX( aP.x - aClearance - 1 );
125 int gx1 = poly2gridX( aP.x + aClearance + 1 );
126 int gy0 = poly2gridY( aP.y - aClearance - 1 );
127 int gy1 = poly2gridY( aP.y + aClearance + 1 );
128
129 using ecoord = VECTOR2I::extended_type;
130
131 ecoord dist = (ecoord) aClearance * aClearance;
132
133 for( int gx = gx0; gx <= gx1; gx++ )
134 {
135 for( int gy = gy0; gy <= gy1; gy++ )
136 {
137 const auto& cell = m_grid[m_gridSize * gy + gx];
138 for( auto index : cell )
139 {
140 const auto& seg = m_outline.Segment( index );
141
142 if( seg.SquaredDistance( aP ) <= dist )
143 return true;
144 }
145 }
146 }
147 return false;
148 }
149
150
151 int POLY_GRID_PARTITION::rescale_trunc( int aNumerator, int aValue, int aDenominator ) const
152 {
153 int64_t numerator = (int64_t) aNumerator * (int64_t) aValue;
154
155 wxASSERT( aDenominator != 0 );
156
157 if( aDenominator == 0 ) // Avoid crash when divide by 0
158 aDenominator = 1;
159
160 return numerator / aDenominator;
161 }
162
163
164 // convertes grid cell coordinates to the polygon coordinates
165 const VECTOR2I POLY_GRID_PARTITION::grid2poly( const VECTOR2I& p ) const
166 {
167 int px = rescale_trunc( p.x, m_bbox.GetWidth(), m_gridSize ) + m_bbox.GetPosition().x;
168 int py = rescale_trunc( p.y, m_bbox.GetHeight(), m_gridSize ) + m_bbox.GetPosition().y;
169 return VECTOR2I( px, py );
170 }
171
172
173 int POLY_GRID_PARTITION::grid2polyX( int x ) const
174 {
175 return rescale_trunc( x, m_bbox.GetWidth(), m_gridSize ) + m_bbox.GetPosition().x;
176 }
177
178
179 int POLY_GRID_PARTITION::grid2polyY( int y ) const
180 {
181 return rescale_trunc( y, m_bbox.GetHeight(), m_gridSize ) + m_bbox.GetPosition().y;
182 }
183
184
185 const VECTOR2I POLY_GRID_PARTITION::poly2grid( const VECTOR2I& p ) const
186 {
187 int px = rescale_trunc( p.x - m_bbox.GetPosition().x, m_gridSize, m_bbox.GetWidth() );
188 int py = rescale_trunc( p.y - m_bbox.GetPosition().y, m_gridSize, m_bbox.GetHeight() );
189
190 if( px < 0 )
191 px = 0;
192
193 if( px >= m_gridSize )
194 px = m_gridSize - 1;
195
196 if( py < 0 )
197 py = 0;
198
199 if( py >= m_gridSize )
200 py = m_gridSize - 1;
201
202 return VECTOR2I( px, py );
203 }
204
205
206 int POLY_GRID_PARTITION::poly2gridX( int x ) const
207 {
208 int px = rescale_trunc( x - m_bbox.GetPosition().x, m_gridSize, m_bbox.GetWidth() );
209
210 if( px < 0 )
211 px = 0;
212
213 if( px >= m_gridSize )
214 px = m_gridSize - 1;
215
216 return px;
217 }
218
219
220 int POLY_GRID_PARTITION::poly2gridY( int y ) const
221 {
222 int py = rescale_trunc( y - m_bbox.GetPosition().y, m_gridSize, m_bbox.GetHeight() );
223
224 if( py < 0 )
225 py = 0;
226
227 if( py >= m_gridSize )
228 py = m_gridSize - 1;
229
230 return py;
231 }
232
233
234 void POLY_GRID_PARTITION::build( const SHAPE_LINE_CHAIN& aPolyOutline, int gridSize )
235 {
236 m_outline = aPolyOutline;
237
238 //if (orientation(m_outline) < 0)
239 // m_outline = m_outline.Reverse();
240
241 m_bbox = m_outline.BBox();
242 m_gridSize = gridSize;
243
244 m_outline.SetClosed( true );
245
246 m_grid.reserve( gridSize * gridSize );
247
248 for( int y = 0; y < gridSize; y++ )
249 {
250 for( int x = 0; x < gridSize; x++ )
251 {
252 m_grid.emplace_back();
253 }
254 }
255
256 VECTOR2I ref_v( 0, 1 );
257 VECTOR2I ref_h( 0, 1 );
258
259 m_flags.reserve( m_outline.SegmentCount() );
260
261 std::unordered_map<SEG, int, segHash, segsEqual> edgeSet;
262
263 for( int i = 0; i < m_outline.SegmentCount(); i++ )
264 {
265 SEG edge = m_outline.Segment( i );
266
267 if( edgeSet.find( edge ) == edgeSet.end() )
268 {
269 edgeSet[edge] = 1;
270 }
271 else
272 {
273 edgeSet[edge]++;
274 }
275 }
276
277 for( int i = 0; i < m_outline.SegmentCount(); i++ )
278 {
279 auto edge = m_outline.Segment( i );
280 auto dir = edge.B - edge.A;
281 int flags = 0;
282
283
284 if( dir.y == 0 )
285 {
286 flags = 0;
287 }
288 else if( edgeSet[edge] == 1 )
289 {
290 if( dir.Dot( ref_h ) < 0 )
291 {
292 flags |= LEAD_EDGE;
293 }
294 else if( dir.Dot( ref_h ) > 0 )
295 {
296 flags |= TRAIL_EDGE;
297 }
298 }
299
300 m_flags.push_back( flags );
301
302 if( edge.A.y == edge.B.y )
303 continue;
304
305 std::set<int> indices;
306
307 indices.insert( m_gridSize * poly2gridY( edge.A.y ) + poly2gridX( edge.A.x ) );
308 indices.insert( m_gridSize * poly2gridY( edge.B.y ) + poly2gridX( edge.B.x ) );
309
310 if( edge.A.x > edge.B.x )
311 std::swap( edge.A, edge.B );
312
313 dir = edge.B - edge.A;
314
315 if( dir.x != 0 )
316 {
317 int gx0 = poly2gridX( edge.A.x );
318 int gx1 = poly2gridX( edge.B.x );
319
320 for( int x = gx0; x <= gx1; x++ )
321 {
322 int px = grid2polyX( x );
323 int py = ( edge.A.y + rescale_trunc( dir.y, px - edge.A.x, dir.x ) );
324 int yy = poly2gridY( py );
325
326 indices.insert( m_gridSize * yy + x );
327
328 if( x > 0 )
329 indices.insert( m_gridSize * yy + x - 1 );
330 }
331 }
332
333 if( edge.A.y > edge.B.y )
334 std::swap( edge.A, edge.B );
335
336 dir = edge.B - edge.A;
337
338 if( dir.y != 0 )
339 {
340 int gy0 = poly2gridY( edge.A.y );
341 int gy1 = poly2gridY( edge.B.y );
342
343 for( int y = gy0; y <= gy1; y++ )
344 {
345 int py = grid2polyY( y );
346 int px = ( edge.A.x + rescale_trunc( dir.x, py - edge.A.y, dir.y ) );
347 int xx = poly2gridX( px );
348
349 indices.insert( m_gridSize * y + xx );
350
351 if( y > 0 )
352 indices.insert( m_gridSize * ( y - 1 ) + xx );
353 }
354 }
355
356 for( auto idx : indices )
357 m_grid[idx].push_back( i );
358 }
359 }
360
361
362 void POLY_GRID_PARTITION::scanCell( SCAN_STATE& state, const EDGE_LIST& cell, const VECTOR2I& aP,
363 int cx, int cy ) const
364 {
365 int cx0 = grid2polyX( cx );
366 int cx1 = grid2polyX( cx + 1 );
367
368 #ifdef TOM_EXTRA_VERBOSE
369 printf( "Scan %d %d\n", cx, cy );
370 #endif
371
372 for( auto index : cell )
373 {
374 const SEG& edge = m_outline.CSegment( index );
375
376
377 if( m_flags[index] == 0 )
378 {
379 if( aP.y == edge.A.y
380 && inRange( edge.A.x, edge.B.x, aP.x ) ) // we belong to the outline
381 {
382 state.nearest = index;
383 state.dist_max = 0;
384 return;
385 }
386 else
387 {
388 continue;
389 }
390 }
391
392 if( inRange( edge.A.y, edge.B.y, aP.y ) )
393 {
394 #ifdef TOM_EXTRA_VERBOSE
395 printf( "Test edge: %d [%d %d %d %d] p %d %d flags %d\n", index, edge.A.x, edge.A.y,
396 edge.B.x, edge.B.y, aP.x, aP.y );
397 #endif
398 int dist = 0;
399 int x0;
400 if( edge.A.y == aP.y )
401 {
402 x0 = edge.A.x;
403 }
404 else if( edge.B.y == aP.y )
405 {
406 x0 = edge.B.x;
407 }
408 else
409 {
410 x0 = edge.A.x
411 + rescale( ( edge.B.x - edge.A.x ), ( aP.y - edge.A.y ),
412 ( edge.B.y - edge.A.y ) );
413 }
414
415
416 dist = aP.x - x0;
417
418 #ifdef TOM_EXTRA_VERBOSE
419 printf( " x0 %d dist %d [%s]\n", x0, dist,
420 x0 < cx0 || x0 > cx1 ? "outside" : "inside" );
421 #endif
422
423 if( x0 < cx0 || x0 > cx1 )
424 {
425 continue;
426 }
427
428
429 if( dist == 0 )
430 {
431 if( state.nearest_prev < 0 || state.nearest != index )
432 {
433 state.dist_prev = state.dist_max;
434 state.nearest_prev = state.nearest;
435 }
436
437 state.nearest = index;
438 state.dist_max = 0;
439 return;
440 }
441
442 if( dist != 0 && std::abs( dist ) <= std::abs( state.dist_max ) )
443 {
444 if( state.nearest_prev < 0 || state.nearest != index )
445 {
446 state.dist_prev = state.dist_max;
447 state.nearest_prev = state.nearest;
448 }
449
450 state.dist_max = dist;
451 state.nearest = index;
452 }
453 }
454 }
455 }
456
457
458 bool POLY_GRID_PARTITION::inRange( int v1, int v2, int x ) const
459 {
460 if( v1 < v2 )
461 {
462 return x >= v1 && x <= v2;
463 }
464
465 return x >= v2 && x <= v1;
466 }