KiCad PCB EDA Suite
ar_autoplacer.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
7  *
8  * Copyright (C) 1992-2012 KiCad Developers, see change_log.txt for contributors.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #include <fctsys.h>
29 #include <confirm.h>
30 #include <pcbnew.h>
31 #include <pcb_edit_frame.h>
32 #include <gr_basic.h>
33 #include <macros.h>
34 #include <msgpanel.h>
35 #include <class_board.h>
36 #include <class_module.h>
37 #include <class_track.h>
38 #include <class_drawsegment.h>
39 #include <class_pad.h>
40 #include <board_commit.h>
42 #include <ratsnest_data.h>
44 
45 #include "ar_autoplacer.h"
46 #include "ar_cell.h"
47 #include "ar_matrix.h"
48 #include <memory>
49 
50 #define AR_GAIN 16
51 #define AR_KEEPOUT_MARGIN 500
52 #define AR_ABORT_PLACEMENT -1
53 
54 #define STEP_AR_MM 1.0
55 
56 /* Penalty (cost) for CntRot90 and CntRot180:
57  * CntRot90 and CntRot180 are from 0 (rotation allowed) to 10 (rotation not allowed)
58  */
59 static const double OrientationPenalty[11] =
60 {
61  2.0, // CntRot = 0 rotation prohibited
62  1.9, // CntRot = 1
63  1.8, // CntRot = 2
64  1.7, // CntRot = 3
65  1.6, // CntRot = 4
66  1.5, // CntRot = 5
67  1.4, // CntRot = 5
68  1.3, // CntRot = 7
69  1.2, // CntRot = 8
70  1.1, // CntRot = 9
71  1.0 // CntRot = 10 rotation authorized, no penalty
72 };
73 
74 
76 {
77  m_board = aBoard;
78  m_connectivity = std::make_unique<CONNECTIVITY_DATA>( );
79 
80  for( auto mod : m_board->Modules() )
81  m_connectivity->Add( mod );
82 
83  m_gridSize = Millimeter2iu( STEP_AR_MM );
84  m_progressReporter = nullptr;
85  m_refreshCallback = nullptr;
86  m_minCost = 0.0;
87 }
88 
89 
90 void AR_AUTOPLACER::placeModule( MODULE* aModule, bool aDoNotRecreateRatsnest, const wxPoint& aPos )
91 {
92  if( !aModule )
93  return;
94 
95  aModule->SetPosition( aPos );
96  m_connectivity->Update( aModule );
97 }
98 
99 
101 {
103 
105 
106  if( bbox.GetWidth() == 0 || bbox.GetHeight() == 0 )
107  return 0;
108 
109  // Build the board shape
110  m_board->GetBoardPolygonOutlines( m_boardShape /*, aErrorText, aErrorLocation*/ );
113 
114  m_matrix.ComputeMatrixSize( bbox );
115  int nbCells = m_matrix.m_Ncols * m_matrix.m_Nrows;
116 
117  // Choose the number of board sides.
122 
123  // Fill (mark) the cells inside the board:
124  fillMatrix();
125 
126  // Other obstacles can be added here:
127  for( auto drawing : m_board->Drawings() )
128  {
129  switch( drawing->Type() )
130  {
131  case PCB_LINE_T:
132  if( drawing->GetLayer() != Edge_Cuts )
133  {
136  }
137  break;
138 
139  default:
140  break;
141  }
142  }
143 
144  // Initialize top layer. to the same value as the bottom layer
147  nbCells * sizeof(AR_MATRIX::MATRIX_CELL) );
148 
149  return 1;
150 }
151 
152 
154 {
155  std::vector <int> x_coordinates;
156  bool success = true;
157  int step = m_matrix.m_GridRouting;
158  wxPoint coord_orgin = m_matrix.GetBrdCoordOrigin(); // Board coordinate of matruix cell (0,0)
159 
160  // Create a single board outline:
161  SHAPE_POLY_SET brd_shape = m_boardShape;
162  brd_shape.Fracture( SHAPE_POLY_SET::PM_FAST );
163  const SHAPE_LINE_CHAIN& outline = brd_shape.Outline(0);
164  const BOX2I& rect = outline.BBox();
165 
166  // Creates the horizontal segments
167  // Calculate the y limits of the area
168  for( int refy = rect.GetY(), endy = rect.GetBottom(); refy < endy; refy += step )
169  {
170  // The row index (vertical position) of current line scan inside the placement matrix
171  int idy = (refy - coord_orgin.y) / step;
172 
173  // Ensure we are still inside the placement matrix
174  if( idy >= m_matrix.m_Nrows )
175  break;
176 
177  // Ensure we are inside the placement matrix
178  if( idy <= 0 )
179  continue;
180 
181  // find all intersection points of an infinite line with polyline sides
182  x_coordinates.clear();
183 
184  for( int v = 0; v < outline.PointCount(); v++ )
185  {
186 
187  int seg_startX = outline.CPoint( v ).x;
188  int seg_startY = outline.CPoint( v ).y;
189  int seg_endX = outline.CPoint( v + 1 ).x;
190  int seg_endY = outline.CPoint( v + 1 ).y;
191 
192  /* Trivial cases: skip if ref above or below the segment to test */
193  if( ( seg_startY > refy ) && ( seg_endY > refy ) )
194  continue;
195 
196  // segment below ref point, or its Y end pos on Y coordinate ref point: skip
197  if( ( seg_startY <= refy ) && (seg_endY <= refy ) )
198  continue;
199 
200  /* at this point refy is between seg_startY and seg_endY
201  * see if an horizontal line at Y = refy is intersecting this segment
202  */
203  // calculate the x position of the intersection of this segment and the
204  // infinite line this is more easier if we move the X,Y axis origin to
205  // the segment start point:
206 
207  seg_endX -= seg_startX;
208  seg_endY -= seg_startY;
209  double newrefy = (double) ( refy - seg_startY );
210  double intersec_x;
211 
212  if ( seg_endY == 0 ) // horizontal segment on the same line: skip
213  continue;
214 
215  // Now calculate the x intersection coordinate of the horizontal line at
216  // y = newrefy and the segment from (0,0) to (seg_endX,seg_endY) with the
217  // horizontal line at the new refy position the line slope is:
218  // slope = seg_endY/seg_endX; and inv_slope = seg_endX/seg_endY
219  // and the x pos relative to the new origin is:
220  // intersec_x = refy/slope = refy * inv_slope
221  // Note: because horizontal segments are already tested and skipped, slope
222  // exists (seg_end_y not O)
223  double inv_slope = (double) seg_endX / seg_endY;
224  intersec_x = newrefy * inv_slope;
225  x_coordinates.push_back( (int) intersec_x + seg_startX );
226  }
227 
228  // A line scan is finished: build list of segments
229 
230  // Sort intersection points by increasing x value:
231  // So 2 consecutive points are the ends of a segment
232  std::sort( x_coordinates.begin(), x_coordinates.end() );
233 
234  // An even number of coordinates is expected, because a segment has 2 ends.
235  // An if this algorithm always works, it must always find an even count.
236  if( ( x_coordinates.size() & 1 ) != 0 )
237  {
238  success = false;
239  break;
240  }
241 
242  // Fill cells having the same Y coordinate
243  int iimax = x_coordinates.size() - 1;
244 
245  for( int ii = 0; ii < iimax; ii += 2 )
246  {
247  int seg_start_x = x_coordinates[ii] - coord_orgin.x;
248  int seg_end_x = x_coordinates[ii + 1] - coord_orgin.x;
249  // Fill cells at y coord = idy,
250  // and at x cood >= seg_start_x and <= seg_end_x
251 
252  for( int idx = seg_start_x / step; idx < m_matrix.m_Ncols; idx++ )
253  {
254  if( idx * step > seg_end_x )
255  break;
256 
257  if( idx * step >= seg_start_x )
259  }
260 
261  }
262  } // End examine segments in one area
263 
264  return success;
265 }
266 
267 
268 
269 void AR_AUTOPLACER::rotateModule( MODULE* module, double angle, bool incremental )
270 {
271  if( module == NULL )
272  return;
273 
274  if( incremental )
275  module->SetOrientation( module->GetOrientation() + angle );
276  else
277  module->SetOrientation( angle );
278 
279 
280  m_board->GetConnectivity()->Update( module );
281 }
282 
283 
284 void AR_AUTOPLACER::addFpBody( wxPoint aStart, wxPoint aEnd, LSET aLayerMask )
285 {
286  // Add a polygonal shape (rectangle) to m_fpAreaFront and/or m_fpAreaBack
287  if( aLayerMask[ F_Cu ] )
288  {
290  m_fpAreaTop.Append( aStart.x, aStart.y );
291  m_fpAreaTop.Append( aEnd.x, aStart.y );
292  m_fpAreaTop.Append( aEnd.x, aEnd.y );
293  m_fpAreaTop.Append( aStart.x, aEnd.y );
294  }
295  if( aLayerMask[ B_Cu ] )
296  {
298  m_fpAreaBottom.Append( aStart.x, aStart.y );
299  m_fpAreaBottom.Append( aEnd.x, aStart.y );
300  m_fpAreaBottom.Append( aEnd.x, aEnd.y );
301  m_fpAreaBottom.Append( aStart.x, aEnd.y );
302  }
303 }
304 
305 void AR_AUTOPLACER::addPad( D_PAD* aPad, int aClearance )
306 {
307  // Add a polygonal shape (rectangle) to m_fpAreaFront and/or m_fpAreaBack
308  EDA_RECT bbox = aPad->GetBoundingBox();
309  bbox.Inflate( aClearance );
310 
311  if( aPad->IsOnLayer( F_Cu ) )
312  {
314  m_fpAreaTop.Append( bbox.GetLeft(), bbox.GetTop() );
315  m_fpAreaTop.Append( bbox.GetRight(), bbox.GetTop() );
316  m_fpAreaTop.Append( bbox.GetRight(), bbox.GetBottom() );
317  m_fpAreaTop.Append( bbox.GetLeft(), bbox.GetBottom() );
318  }
319  if( aPad->IsOnLayer( B_Cu ) )
320  {
322  m_fpAreaBottom.Append( bbox.GetLeft(), bbox.GetTop() );
323  m_fpAreaBottom.Append( bbox.GetRight(), bbox.GetTop() );
324  m_fpAreaBottom.Append( bbox.GetRight(), bbox.GetBottom() );
325  m_fpAreaBottom.Append( bbox.GetLeft(), bbox.GetBottom() );
326  }
327 }
328 
329 
330 void AR_AUTOPLACER::buildFpAreas( MODULE* aFootprint, int aFpClearance )
331 {
334 
335  if( aFootprint->BuildPolyCourtyard() )
336  {
337  m_fpAreaTop = aFootprint->GetPolyCourtyardFront();
338  m_fpAreaBottom = aFootprint->GetPolyCourtyardBack();
339  }
340 
341  LSET layerMask;
342 
343  if( aFootprint->GetLayer() == F_Cu )
344  layerMask.set( F_Cu );
345 
346  if( aFootprint->GetLayer() == B_Cu )
347  layerMask.set( B_Cu );
348 
349  EDA_RECT fpBBox = aFootprint->GetBoundingBox();
350 
351  fpBBox.Inflate( ( m_matrix.m_GridRouting / 2 ) + aFpClearance );
352 
353  // Add a minimal area to the fp area:
354  addFpBody( fpBBox.GetOrigin(), fpBBox.GetEnd(), layerMask );
355 
356  // Trace pads + clearance areas.
357  for( auto pad : aFootprint->Pads() )
358  {
359  int margin = (m_matrix.m_GridRouting / 2) + pad->GetClearance();
360  addPad( pad, margin );
361  }
362 }
363 
364 
366 {
367  int ox, oy, fx, fy;
368  LSET layerMask;
369  EDA_RECT fpBBox = Module->GetBoundingBox();
370 
371  fpBBox.Inflate( m_matrix.m_GridRouting / 2 );
372  ox = fpBBox.GetX();
373  fx = fpBBox.GetRight();
374  oy = fpBBox.GetY();
375  fy = fpBBox.GetBottom();
376 
377  if( ox < m_matrix.m_BrdBox.GetX() )
378  ox = m_matrix.m_BrdBox.GetX();
379 
380  if( ox > m_matrix.m_BrdBox.GetRight() )
381  ox = m_matrix.m_BrdBox.GetRight();
382 
383  if( fx < m_matrix.m_BrdBox.GetX() )
384  fx = m_matrix.m_BrdBox.GetX();
385 
386  if( fx > m_matrix.m_BrdBox.GetRight() )
387  fx = m_matrix.m_BrdBox.GetRight();
388 
389  if( oy < m_matrix.m_BrdBox.GetY() )
390  oy = m_matrix.m_BrdBox.GetY();
391 
392  if( oy > m_matrix.m_BrdBox.GetBottom() )
393  oy = m_matrix.m_BrdBox.GetBottom();
394 
395  if( fy < m_matrix.m_BrdBox.GetY() )
396  fy = m_matrix.m_BrdBox.GetY();
397 
398  if( fy > m_matrix.m_BrdBox.GetBottom() )
399  fy = m_matrix.m_BrdBox.GetBottom();
400 
401  if( Module->GetLayer() == F_Cu )
402  layerMask.set( F_Cu );
403 
404  if( Module->GetLayer() == B_Cu )
405  layerMask.set( B_Cu );
406 
407  m_matrix.TraceFilledRectangle( ox, oy, fx, fy, layerMask,
409 
410  // Trace pads + clearance areas.
411  for( auto pad : Module->Pads() )
412  {
413  int margin = (m_matrix.m_GridRouting / 2) + pad->GetClearance();
415  }
416 
417  // Trace clearance.
418  int margin = ( m_matrix.m_GridRouting * Module->GetPadCount() ) / AR_GAIN;
419  m_matrix.CreateKeepOutRectangle( ox, oy, fx, fy, margin, AR_KEEPOUT_MARGIN , layerMask );
420 
421  // Build the footprint courtyard
422  buildFpAreas( Module, margin );
423 
424  // Substract the shape to free areas
427 }
428 
429 
430 /* Test if the rectangular area (ux, ux .. y0, y1):
431  * - is a free zone (except OCCUPED_By_MODULE returns)
432  * - is on the working surface of the board (otherwise returns OUT_OF_BOARD)
433  *
434  * Returns OUT_OF_BOARD, or OCCUPED_By_MODULE or FREE_CELL if OK
435  */
436 int AR_AUTOPLACER::testRectangle( const EDA_RECT& aRect, int side )
437 {
438  EDA_RECT rect = aRect;
439 
440  rect.Inflate( m_matrix.m_GridRouting / 2 );
441 
442  wxPoint start = rect.GetOrigin();
443  wxPoint end = rect.GetEnd();
444 
445  start -= m_matrix.m_BrdBox.GetOrigin();
446  end -= m_matrix.m_BrdBox.GetOrigin();
447 
448  int row_min = start.y / m_matrix.m_GridRouting;
449  int row_max = end.y / m_matrix.m_GridRouting;
450  int col_min = start.x / m_matrix.m_GridRouting;
451  int col_max = end.x / m_matrix.m_GridRouting;
452 
453  if( start.y > row_min * m_matrix.m_GridRouting )
454  row_min++;
455 
456  if( start.x > col_min * m_matrix.m_GridRouting )
457  col_min++;
458 
459  if( row_min < 0 )
460  row_min = 0;
461 
462  if( row_max >= ( m_matrix.m_Nrows - 1 ) )
463  row_max = m_matrix.m_Nrows - 1;
464 
465  if( col_min < 0 )
466  col_min = 0;
467 
468  if( col_max >= ( m_matrix.m_Ncols - 1 ) )
469  col_max = m_matrix.m_Ncols - 1;
470 
471  for( int row = row_min; row <= row_max; row++ )
472  {
473  for( int col = col_min; col <= col_max; col++ )
474  {
475  unsigned int data = m_matrix.GetCell( row, col, side );
476 
477  if( ( data & CELL_IS_ZONE ) == 0 )
478  return AR_OUT_OF_BOARD;
479 
480  if( (data & CELL_IS_MODULE) )
481  return AR_OCCUIPED_BY_MODULE;
482  }
483  }
484 
485  return AR_FREE_CELL;
486 }
487 
488 int AR_AUTOPLACER::testModuleByPolygon( MODULE* aModule, int aSide, const wxPoint& aOffset )
489 {
490  // Test for footprint out of board:
491  // If a footprint is not fully inside the board, substract board polygon
492  // to the footprint polygon gives a non null area.
493  SHAPE_POLY_SET fp_area = m_fpAreaTop;
494  fp_area.Move( -aOffset );
495  SHAPE_POLY_SET out_of_board_area;
496  out_of_board_area.BooleanSubtract( fp_area, m_topFreeArea, SHAPE_POLY_SET::PM_FAST );
497 
498  if( out_of_board_area.OutlineCount() )
499  return AR_OCCUIPED_BY_MODULE;
500 
501  return AR_FREE_CELL;
502 }
503 
504 
505 /* Calculates and returns the clearance area of the rectangular surface
506  * aRect):
507  * (Sum of cells in terms of distance)
508  */
509 unsigned int AR_AUTOPLACER::calculateKeepOutArea( const EDA_RECT& aRect, int side )
510 {
511  wxPoint start = aRect.GetOrigin();
512  wxPoint end = aRect.GetEnd();
513 
514  start -= m_matrix.m_BrdBox.GetOrigin();
515  end -= m_matrix.m_BrdBox.GetOrigin();
516 
517  int row_min = start.y / m_matrix.m_GridRouting;
518  int row_max = end.y / m_matrix.m_GridRouting;
519  int col_min = start.x / m_matrix.m_GridRouting;
520  int col_max = end.x / m_matrix.m_GridRouting;
521 
522  if( start.y > row_min * m_matrix.m_GridRouting )
523  row_min++;
524 
525  if( start.x > col_min * m_matrix.m_GridRouting )
526  col_min++;
527 
528  if( row_min < 0 )
529  row_min = 0;
530 
531  if( row_max >= ( m_matrix.m_Nrows - 1 ) )
532  row_max = m_matrix.m_Nrows - 1;
533 
534  if( col_min < 0 )
535  col_min = 0;
536 
537  if( col_max >= ( m_matrix.m_Ncols - 1 ) )
538  col_max = m_matrix.m_Ncols - 1;
539 
540  unsigned int keepOutCost = 0;
541 
542  for( int row = row_min; row <= row_max; row++ )
543  {
544  for( int col = col_min; col <= col_max; col++ )
545  {
546  // m_matrix.GetDist returns the "cost" of the cell
547  // at position (row, col)
548  // in autoplace this is the cost of the cell, if it is
549  // inside aRect
550  keepOutCost += m_matrix.GetDist( row, col, side );
551  }
552  }
553 
554  return keepOutCost;
555 }
556 
557 
558 /* Test if the module can be placed on the board.
559  * Returns the value TstRectangle().
560  * Module is known by its bounding box
561  */
562 int AR_AUTOPLACER::testModuleOnBoard( MODULE* aModule, bool TstOtherSide, const wxPoint& aOffset )
563 {
564  int side = AR_SIDE_TOP;
565  int otherside = AR_SIDE_BOTTOM;
566 
567  if( aModule->GetLayer() == B_Cu )
568  {
569  side = AR_SIDE_BOTTOM; otherside = AR_SIDE_TOP;
570  }
571 
572  EDA_RECT fpBBox = aModule->GetFootprintRect();
573  fpBBox.Move( -aOffset );
574 
575  buildFpAreas( aModule, 0 );
576 
577  int diag = //testModuleByPolygon( aModule, side, aOffset );
578  testRectangle( fpBBox, side );
579 //printf("test %p diag %d\n", aModule, diag);fflush(0);
580  if( diag != AR_FREE_CELL )
581  return diag;
582 
583  if( TstOtherSide )
584  {
585  diag = //testModuleByPolygon( aModule, otherside, aOffset );
586  testRectangle( fpBBox, otherside );
587 
588  if( diag != AR_FREE_CELL )
589  return diag;
590  }
591 
592  int marge = ( m_matrix.m_GridRouting * aModule->GetPadCount() ) / AR_GAIN;
593 
594  fpBBox.Inflate( marge );
595  return calculateKeepOutArea( fpBBox, side );
596 }
597 
598 
600 {
601  int error = 1;
602  wxPoint LastPosOK;
603  double min_cost, curr_cost, Score;
604  bool TstOtherSide;
605 
606  aModule->CalculateBoundingBox();
607 
608  LastPosOK = m_matrix.m_BrdBox.GetOrigin();
609 
610  wxPoint mod_pos = aModule->GetPosition();
611  EDA_RECT fpBBox = aModule->GetFootprintRect();
612 
613  // Move fpBBox to have the footprint position at (0,0)
614  fpBBox.Move( -mod_pos );
615  wxPoint fpBBoxOrg = fpBBox.GetOrigin();
616 
617  // Calculate the limit of the footprint position, relative
618  // to the routing matrix area
619  wxPoint xylimit = m_matrix.m_BrdBox.GetEnd() - fpBBox.GetEnd();
620 
621  wxPoint initialPos = m_matrix.m_BrdBox.GetOrigin() - fpBBoxOrg;
622 
623  // Stay on grid.
624  initialPos.x -= initialPos.x % m_matrix.m_GridRouting;
625  initialPos.y -= initialPos.y % m_matrix.m_GridRouting;
626 
627  m_curPosition = initialPos;
628  auto moduleOffset = mod_pos - m_curPosition;
629 
630  /* Examine pads, and set TstOtherSide to true if a footprint
631  * has at least 1 pad through.
632  */
633  TstOtherSide = false;
634 
636  {
637  LSET other( aModule->GetLayer() == B_Cu ? F_Cu : B_Cu );
638 
639  for( auto pad : aModule->Pads() )
640  {
641  if( !( pad->GetLayerSet() & other ).any() )
642  continue;
643 
644  TstOtherSide = true;
645  break;
646  }
647  }
648 
649  fpBBox.SetOrigin( fpBBoxOrg + m_curPosition );
650 
651  min_cost = -1.0;
652 // m_frame->SetStatusText( wxT( "Score ??, pos ??" ) );
653 
654 
655  for( ; m_curPosition.x < xylimit.x; m_curPosition.x += m_matrix.m_GridRouting )
656  {
657  m_curPosition.y = initialPos.y;
658 
659  for( ; m_curPosition.y < xylimit.y; m_curPosition.y += m_matrix.m_GridRouting )
660  {
661 
662  fpBBox.SetOrigin( fpBBoxOrg + m_curPosition );
663  moduleOffset = mod_pos - m_curPosition;
664  int keepOutCost = testModuleOnBoard( aModule, TstOtherSide, moduleOffset );
665 
666  if( keepOutCost >= 0 ) // i.e. if the module can be put here
667  {
668  error = 0;
669  // m_frame->build_ratsnest_module( aModule ); // fixme
670  curr_cost = computePlacementRatsnestCost( aModule, moduleOffset );
671  Score = curr_cost + keepOutCost;
672 
673  if( (min_cost >= Score ) || (min_cost < 0 ) )
674  {
675  LastPosOK = m_curPosition;
676  min_cost = Score;
677  wxString msg;
678 /* msg.Printf( wxT( "Score %g, pos %s, %s" ),
679  min_cost,
680  GetChars( ::CoordinateToString( LastPosOK.x ) ),
681  GetChars( ::CoordinateToString( LastPosOK.y ) ) );
682  m_frame->SetStatusText( msg );*/
683  }
684  }
685  }
686  }
687 
688  // Regeneration of the modified variable.
689  m_curPosition = LastPosOK;
690 
691  m_minCost = min_cost;
692  return error;
693 }
694 
695 
696 const D_PAD* AR_AUTOPLACER::nearestPad( MODULE *aRefModule, D_PAD* aRefPad, const wxPoint& aOffset)
697 {
698  const D_PAD* nearest = nullptr;
699  int64_t nearestDist = INT64_MAX;
700 
701  for ( auto mod : m_board->Modules() )
702  {
703  if ( mod == aRefModule )
704  continue;
705 
706  if( !m_matrix.m_BrdBox.Contains( mod->GetPosition() ) )
707  continue;
708 
709  for ( auto pad: mod->Pads() )
710  {
711  if ( pad->GetNetCode() != aRefPad->GetNetCode() || pad->GetNetCode() <= 0 )
712  continue;
713 
714  auto dist = (VECTOR2I( aRefPad->GetPosition() - aOffset ) - VECTOR2I( pad->GetPosition() ) ).EuclideanNorm();
715 
716  if ( dist < nearestDist )
717  {
718  nearestDist = dist;
719  nearest = pad;
720  }
721  }
722  }
723 
724  return nearest;
725 }
726 
727 
728 double AR_AUTOPLACER::computePlacementRatsnestCost( MODULE *aModule, const wxPoint& aOffset )
729 {
730  double curr_cost;
731  VECTOR2I start; // start point of a ratsnest
732  VECTOR2I end; // end point of a ratsnest
733  int dx, dy;
734 
735  curr_cost = 0;
736 
737  for ( auto pad : aModule->Pads() )
738  {
739  auto nearest = nearestPad( aModule, pad, aOffset );
740 
741  if( !nearest )
742  continue;
743 
744  //printf("pad %s nearest %s\n", (const char *)aModule->GetReference().c_str(), (const char *)nearest->GetParent()->GetReference().c_str());
745 
746  start = VECTOR2I( pad->GetPosition() ) - VECTOR2I(aOffset);
747  end = VECTOR2I( nearest->GetPosition() );
748 
749  //m_overlay->SetIsStroke( true );
750  //m_overlay->SetStrokeColor( COLOR4D(0.0, 1.0, 0.0, 1.0) );
751  //m_overlay->Line( start, end );
752 
753  // Cost of the ratsnest.
754  dx = end.x - start.x;
755  dy = end.y - start.y;
756 
757  dx = abs( dx );
758  dy = abs( dy );
759 
760  // ttry to have always dx >= dy to calculate the cost of the rastsnet
761  if( dx < dy )
762  std::swap( dx, dy );
763 
764  // Cost of the connection = length + penalty due to the slope
765  // dx is the biggest length relative to the X or Y axis
766  // the penalty is max for 45 degrees ratsnests,
767  // and 0 for horizontal or vertical ratsnests.
768  // For Horizontal and Vertical ratsnests, dy = 0;
769  double conn_cost = hypot( dx, dy * 2.0 );
770  curr_cost += conn_cost; // Total cost = sum of costs of each connection
771  }
772 
773  return curr_cost;
774 }
775 
776 
777 // Sort routines
778 static bool sortFootprintsByComplexity( MODULE* ref, MODULE* compare )
779 {
780  double ff1, ff2;
781 
782  ff1 = ref->GetArea() * ref->GetPadCount();
783  ff2 = compare->GetArea() * compare->GetPadCount();
784 
785  return ff2 < ff1;
786 }
787 
788 
789 static bool sortFootprintsByRatsnestSize( MODULE* ref, MODULE* compare )
790 {
791  double ff1, ff2;
792 
793  ff1 = ref->GetArea() * ref->GetFlag();
794  ff2 = compare->GetArea() * compare->GetFlag();
795  return ff2 < ff1;
796 }
797 
798 
800 {
801  MODULE* module;
802  std::vector <MODULE*> moduleList;
803 
804 
805  for( auto m : m_board->Modules() )
806  {
808  moduleList.push_back( m );
809  }
810 
811  sort( moduleList.begin(), moduleList.end(), sortFootprintsByComplexity );
812 
813  for( unsigned kk = 0; kk < moduleList.size(); kk++ )
814  {
815  module = moduleList[kk];
816  module->SetFlag( 0 );
817 
818  if( !module->NeedsPlaced() )
819  continue;
820 
821  m_connectivity->Update( module );
822  }
823 
824  m_connectivity->RecalculateRatsnest();
825 
826  for( unsigned kk = 0; kk < moduleList.size(); kk++ )
827  {
828  module = moduleList[kk];
829 
830  auto edges = m_connectivity->GetRatsnestForComponent( module, true );
831 
832  module->SetFlag( edges.size() ) ;
833  }
834 
835  sort( moduleList.begin(), moduleList.end(), sortFootprintsByRatsnestSize );
836 
837  // Search for "best" module.
838  MODULE* bestModule = nullptr;
839  MODULE* altModule = nullptr;
840 
841  for( unsigned ii = 0; ii < moduleList.size(); ii++ )
842  {
843  module = moduleList[ii];
844 
845  if( !module->NeedsPlaced() )
846  continue;
847 
848  altModule = module;
849 
850  if( module->GetFlag() == 0 )
851  continue;
852 
853  bestModule = module;
854  break;
855  }
856 
857  if( bestModule )
858  return bestModule;
859  else
860  return altModule;
861 }
862 
863 
865 {
866  // Draw the board free area
867  m_overlay->Clear();
868  m_overlay->SetIsFill( true );
869  m_overlay->SetIsStroke( false );
870 
871  SHAPE_POLY_SET freeArea = m_topFreeArea;
872  freeArea.Fracture( SHAPE_POLY_SET::PM_FAST );
873 
874  // Draw the free polygon areas, top side:
875  if( freeArea.OutlineCount() > 0 )
876  {
877  m_overlay->SetIsFill( true );
878  m_overlay->SetIsStroke( false );
879  m_overlay->SetFillColor( COLOR4D(0.7, 0.0, 0.1, 0.2) );
880  m_overlay->Polygon( freeArea );
881  }
882 
883  freeArea = m_bottomFreeArea;
884  freeArea.Fracture( SHAPE_POLY_SET::PM_FAST );
885 
886  // Draw the free polygon areas, bottom side:
887  if( freeArea.OutlineCount() > 0 )
888  {
889  m_overlay->SetFillColor( COLOR4D(0.0, 0.7, 0.0, 0.2) );
890  m_overlay->Polygon( freeArea );
891  }
892 }
893 
894 
895 AR_RESULT AR_AUTOPLACER::AutoplaceModules( std::vector<MODULE*> aModules,
896  BOARD_COMMIT* aCommit, bool aPlaceOffboardModules )
897 {
898  wxPoint PosOK;
899  wxPoint memopos;
900  int error;
901  MODULE* module = nullptr;
902  bool cancelled = false;
903 
904  memopos = m_curPosition;
905 
906  //printf("set grid: %d\n", m_gridSize);
907 
908  m_matrix.m_GridRouting = m_gridSize; //(int) m_frame->GetScreen()->GetGridSize().x;
909 
910  // Ensure Board.m_GridRouting has a reasonable value:
911  if( m_matrix.m_GridRouting < Millimeter2iu( 0.25 ) )
912  m_matrix.m_GridRouting = Millimeter2iu( 0.25 );
913 
914  // Compute module parameters used in auto place
915  if( genPlacementRoutingMatrix( ) == 0 )
916  return AR_FAILURE;
917 
918  int moduleCount = 0;
919 
920  for ( auto m : m_board->Modules() )
921  {
922  m->SetNeedsPlaced( false );
923  }
924 
925  std::vector<MODULE *> offboardMods;
926 
927  if( aPlaceOffboardModules )
928  {
929  for ( auto m : m_board->Modules() )
930  {
931  if( !m_matrix.m_BrdBox.Contains( m->GetPosition() ) )
932  {
933  offboardMods.push_back( m );
934  }
935  }
936  }
937 
938  for ( auto m : aModules )
939  {
940  m->SetNeedsPlaced( true );
941  aCommit->Modify(m);
942  }
943 
944  for ( auto m : offboardMods )
945  {
946  m->SetNeedsPlaced( true );
947  aCommit->Modify(m);
948  }
949 
950  for ( auto m : m_board->Modules() )
951  {
952  if( m->NeedsPlaced() ) // Erase from screen
953  moduleCount++;
954  else
956  }
957 
958 
959  int cnt = 0;
960  wxString msg;
961 
962  if( m_progressReporter )
963  {
964  m_progressReporter->Report( _( "Autoplacing components..." ) );
965  m_progressReporter->SetMaxProgress( moduleCount );
966  }
967 
969 
970  if( m_refreshCallback )
971  m_refreshCallback( nullptr );
972 
973 
974  while( ( module = pickModule( ) ) != nullptr )
975  {
976  // Display some info about activity, module placement can take a while:
977  //m_frame->SetStatusText( msg );
978 
979  if( m_progressReporter )
981  _( "Autoplacing %s" ), module->GetReference() ) );
982 
983  double initialOrient = module->GetOrientation();
984 
985  error = getOptimalModulePlacement( module );
986  double bestScore = m_minCost;
987  double bestRotation = 0.0;
988  int rotAllowed;
989  PosOK = m_curPosition;
990 
991  if( error == AR_ABORT_PLACEMENT )
992  goto end_of_tst;
993 
994  // Try orientations 90, 180, 270 degrees from initial orientation
995  rotAllowed = module->GetPlacementCost180();
996 
997  //printf("rotAllowed %d\n", rotAllowed);
998 
999  if( rotAllowed != 0 )
1000  {
1001  rotateModule( module, 1800.0, true );
1002  error = getOptimalModulePlacement( module );
1003  m_minCost *= OrientationPenalty[rotAllowed];
1004 
1005  if( bestScore > m_minCost ) // This orientation is better.
1006  {
1007  PosOK = m_curPosition;
1008  bestScore = m_minCost;
1009  bestRotation = 1800.0;
1010  }
1011  else
1012  {
1013  rotateModule( module, initialOrient, false );
1014  }
1015 
1016  if( error == AR_ABORT_PLACEMENT )
1017  goto end_of_tst;
1018  }
1019 
1020  // Determine if the best orientation of a module is 90.
1021  rotAllowed = module->GetPlacementCost90();
1022 
1023  if( rotAllowed != 0 )
1024  {
1025  rotateModule( module, 900.0, true );
1026  error = getOptimalModulePlacement( module );
1027  m_minCost *= OrientationPenalty[rotAllowed];
1028 
1029  if( bestScore > m_minCost ) // This orientation is better.
1030  {
1031  PosOK = m_curPosition;
1032  bestScore = m_minCost;
1033  bestRotation = 900.0;
1034  }
1035  else
1036  {
1037  rotateModule( module, initialOrient, false );
1038  }
1039 
1040  if( error == AR_ABORT_PLACEMENT )
1041  goto end_of_tst;
1042  }
1043 
1044  // Determine if the best orientation of a module is -90.
1045  if( rotAllowed != 0 )
1046  {
1047  rotateModule( module, 2700.0, true );
1048  error = getOptimalModulePlacement( module );
1049  m_minCost *= OrientationPenalty[rotAllowed];
1050 
1051  if( bestScore > m_minCost ) // This orientation is better.
1052  {
1053  PosOK = m_curPosition;
1054  bestScore = m_minCost;
1055  bestRotation = 2700.0;
1056  }
1057  else
1058  {
1059  rotateModule( module, initialOrient, false );
1060  }
1061 
1062  if( error == AR_ABORT_PLACEMENT )
1063  goto end_of_tst;
1064  }
1065 
1066 end_of_tst:
1067 
1068  if( error == AR_ABORT_PLACEMENT )
1069  break;
1070 
1071 
1072  bestRotation += initialOrient;
1073 
1074  if( bestRotation != module->GetOrientation() )
1075  {
1076  //printf("best rotation %d\n", bestRotation );
1077  rotateModule( module, bestRotation, false );
1078  }
1079 
1080  // Place module.
1081  placeModule( module, true, m_curPosition );
1082 
1083  module->CalculateBoundingBox();
1084  genModuleOnRoutingMatrix( module );
1085  module->SetIsPlaced( true );
1086  module->SetNeedsPlaced( false );
1088 
1089  if( m_refreshCallback )
1090  m_refreshCallback( module );
1091 
1092 
1093  if( m_progressReporter )
1094  {
1096 
1097  if ( !m_progressReporter->KeepRefreshing( false ) )
1098  {
1099  cancelled = true;
1100  break;
1101  }
1102  }
1103  cnt++;
1104  }
1105 
1106  m_curPosition = memopos;
1107 
1109 
1110  for ( auto m : m_board->Modules() )
1111  {
1112  m->CalculateBoundingBox();
1113  }
1114 
1115  return cancelled ? AR_CANCELLED : AR_COMPLETED;
1116 }
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
GetPadCount returns the number of pads.
SHAPE_POLY_SET & GetPolyCourtyardFront()
Used in DRC to test the courtyard area (a complex polygon)
Definition: class_module.h:662
SHAPE_POLY_SET m_topFreeArea
bool BuildPolyCourtyard()
Used in DRC to build the courtyard area (a complex polygon) from graphic items put on the courtyard.
#define AR_SIDE_BOTTOM
Definition: ar_matrix.h:43
void rotateModule(MODULE *module, double angle, bool incremental)
double GetOrientation() const
Definition: class_module.h:218
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
int InitRoutingMatrix()
Function InitBoard initializes the data structures.
Definition: ar_matrix.cpp:92
int GetPlacementCost90() const
Definition: class_module.h:551
double GetArea(int aPadding=0) const
void Move(const wxPoint &aMoveVector)
Function Move moves the rectangle by the aMoveVector.
int GetNetCode() const
Function GetNetCode.
int OutlineCount() const
Returns the number of outlines in the set
PROGRESS_REPORTER * m_progressReporter
static const int dist[10][10]
Definition: ar_matrix.cpp:320
#define AR_SIDE_TOP
Definition: ar_matrix.h:42
void placeModule(MODULE *aModule, bool aDoNotRecreateRatsnest, const wxPoint &aPos)
This file is part of the common library.
int m_Ncols
Definition: ar_matrix.h:65
#define CELL_IS_MODULE
Definition: ar_cell.h:37
int GetX() const
Definition: eda_rect.h:111
int GetTop() const
Definition: eda_rect.h:123
const EDA_RECT GetBoardEdgesBoundingBox() const
Function GetBoardEdgesBoundingBox Returns the board bounding box calculated using exclusively the boa...
Definition: class_board.h:792
void CalculateBoundingBox()
Function CalculateBoundingBox calculates the bounding box in board coordinates.
int m_GridRouting
Definition: ar_matrix.h:63
int GetLeft() const
Definition: eda_rect.h:122
Class that computes missing connections on a PCB.
void TraceFilledRectangle(int ux0, int uy0, int ux1, int uy1, double angle, LSET aLayerMask, int color, AR_MATRIX::CELL_OP op_logic)
Definition: ar_matrix.cpp:794
#define CELL_IS_ZONE
Definition: ar_cell.h:40
EDA_RECT m_BrdBox
Definition: ar_matrix.h:64
int GetWidth() const
Definition: eda_rect.h:119
PADS & Pads()
Definition: class_module.h:176
void SetNeedsPlaced(bool needsPlaced)
Definition: class_module.h:325
void Report(const wxString &aMessage)
Display aMessage in the progress bar dialog.
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:131
AR_RESULT AutoplaceModules(std::vector< MODULE * > aModules, BOARD_COMMIT *aCommit, bool aPlaceOffboardModules=false)
void CreateKeepOutRectangle(int ux0, int uy0, int ux1, int uy1, int marge, int aKeepOut, LSET aLayerMask)
Function CreateKeepOutRectangle builds the cost map: Cells ( in Dist map ) inside the rect x0,...
Definition: ar_matrix.cpp:1175
void PlacePad(D_PAD *aPad, int color, int marge, AR_MATRIX::CELL_OP op_logic)
Definition: ar_matrix.cpp:1280
EDA_RECT GetFootprintRect() const
Function GetFootprintRect() Returns the area of the module footprint excluding any text.
void buildFpAreas(MODULE *aFootprint, int aFpClearance)
void TraceSegmentPcb(DRAWSEGMENT *pt_segm, int color, int marge, AR_MATRIX::CELL_OP op_logic)
Definition: ar_matrix.cpp:1085
coord_type GetBottom() const
Definition: box2.h:198
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
#define AR_GAIN
int PointCount() const
Function PointCount()
void addPad(D_PAD *aPad, int aClearance)
#define AR_KEEPOUT_MARGIN
#define abs(a)
Definition: auxiliary.h:84
bool Contains(const wxPoint &aPoint) const
Function Contains.
Definitions for tracks, vias and zones.
This file contains miscellaneous commonly used macros and functions.
int GetBottom() const
Definition: eda_rect.h:124
const D_PAD * nearestPad(MODULE *aRefModule, D_PAD *aRefPad, const wxPoint &aOffset)
const wxString GetReference() const
Function GetReference.
Definition: class_module.h:432
void genModuleOnRoutingMatrix(MODULE *Module)
AR_MATRIX m_matrix
const VECTOR2I & CPoint(int aIndex) const
Function Point()
const wxPoint GetEnd() const
Definition: eda_rect.h:116
SHAPE_POLY_SET m_fpAreaBottom
void drawPlacementRoutingMatrix()
PCB_LAYER_ID m_routeLayerTop
Definition: ar_matrix.h:69
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Class LSET is a set of PCB_LAYER_IDs.
const BOX2I BBox(int aClearance=0) const override
Function BBox()
int getOptimalModulePlacement(MODULE *aModule)
SHAPE_POLY_SET m_boardShape
static bool sortFootprintsByComplexity(MODULE *ref, MODULE *compare)
void Move(const VECTOR2I &aVector) override
MODULES & Modules()
Definition: class_board.h:227
double computePlacementRatsnestCost(MODULE *aModule, const wxPoint &aOffset)
int genPlacementRoutingMatrix()
static bool sortFootprintsByRatsnestSize(MODULE *ref, MODULE *compare)
Class SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
const wxPoint GetOrigin() const
Definition: eda_rect.h:114
MATRIX_CELL GetCell(int aRow, int aCol, int aSide)
Definition: ar_matrix.cpp:194
int GetRight() const
Definition: eda_rect.h:121
#define STEP_AR_MM
#define CELL_IS_HOLE
Definition: ar_cell.h:36
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:301
#define AR_ABORT_PLACEMENT
int m_Nrows
Definition: ar_matrix.h:65
MODULE * pickModule()
Find the "best" module place.
MATRIX_CELL * m_BoardSide[AR_MAX_ROUTING_LAYERS_COUNT]
Definition: ar_matrix.h:56
void SetCell(int aRow, int aCol, int aSide, MATRIX_CELL aCell)
Definition: ar_matrix.cpp:205
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, wxString *aErrorText=nullptr, wxPoint *aErrorLocation=nullptr)
Function GetBoardPolygonOutlines Extracts the board outlines and build a closed polygon from lines,...
SHAPE_POLY_SET m_bottomFreeArea
void SetIsPlaced(bool isPlaced)
Definition: class_module.h:316
AR_RESULT
Definition: ar_autoplacer.h:49
SHAPE_POLY_SET m_fpAreaTop
void SetPosition(const wxPoint &aPos) override
bool NeedsPlaced() const
Definition: class_module.h:324
unsigned int calculateKeepOutArea(const EDA_RECT &aRect, int side)
int testModuleOnBoard(MODULE *aModule, bool TstOtherSide, const wxPoint &aOffset)
int NewOutline()
Creates a new empty polygon in the set and returns its index
#define _(s)
void Fracture(POLYGON_MODE aFastMode)
Converts a set of polygons with holes to a singe outline with "slits"/"fractures" connecting the oute...
int GetHeight() const
Definition: eda_rect.h:120
Pad object description.
bool fillMatrix()
fills m_matrix cells from m_boardShape.
int GetPlacementCost180() const
Definition: class_module.h:548
void UnInitRoutingMatrix()
Definition: ar_matrix.cpp:140
coord_type GetY() const
Definition: box2.h:189
int testRectangle(const EDA_RECT &aRect, int side)
int testModuleByPolygon(MODULE *aModule, int aSide, const wxPoint &aOffset)
void addFpBody(wxPoint aStart, wxPoint aEnd, LSET aLayerMask)
AR_AUTOPLACER(BOARD *aBoard)
Class to handle a graphic segment.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
int m_RoutingLayersCount
Definition: ar_matrix.h:62
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
int GetFlag() const
Definition: class_module.h:260
Class SHAPE_LINE_CHAIN.
void SetOrientation(double newangle)
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
wxPoint GetBrdCoordOrigin()
function GetBrdCoordOrigin
Definition: ar_matrix.h:99
void RemoveAllContours()
Removes all outlines & holes (clears) the polygon set.
#define CELL_IS_EDGE
Definition: ar_cell.h:38
bool KeepRefreshing(bool aWait=false)
Update the UI dialog.
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
std::function< int(MODULE *aModule)> m_refreshCallback
int GetY() const
Definition: eda_rect.h:112
static const double OrientationPenalty[11]
unsigned char MATRIX_CELL
Definition: ar_matrix.h:52
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Performs boolean polyset difference For aFastMode meaning, see function booleanOp
SHAPE_POLY_SET & GetPolyCourtyardBack()
Definition: class_module.h:663
Module description (excepted pads)
wxPoint m_curPosition
bool ComputeMatrixSize(const EDA_RECT &aBoundingBox)
Function ComputeMatrixSize calculates the number of rows and columns of dimensions of aPcb for routin...
Definition: ar_matrix.cpp:62
bool IsOnLayer(PCB_LAYER_ID aLayer) const override
Function IsOnLayer tests to see if this object is on the given layer.
Definition: class_pad.h:731
const wxPoint GetPosition() const override
Definition: class_pad.h:241
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
void SetMaxProgress(int aMaxProgress)
Fix the value thar gives the 100 precent progress bar length (inside the current virtual zone)
Message panel definition file.
virtual void SetTitle(const wxString &aTitle)
change the title displayed on the window caption MUST only be called from the main thread.
DIST_CELL GetDist(int aRow, int aCol, int aSide)
Definition: ar_matrix.cpp:259
void AdvanceProgress()
Increment the progress bar length (inside the current virtual zone)
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_overlay
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
const wxPoint GetPosition() const override
Definition: class_module.h:213
DRAWINGS & Drawings()
Definition: class_board.h:236
#define mod(a, n)
Definition: greymap.cpp:24
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: class_pad.cpp:227
void SetFlag(int aFlag)
Definition: class_module.h:258
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
PCB_LAYER_ID m_routeLayerBottom
Definition: ar_matrix.h:70
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
std::unique_ptr< CONNECTIVITY_DATA > m_connectivity