KiCad PCB EDA Suite
graphics_cleaner.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) 2004-2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <reporter.h>
27 #include <board_commit.h>
28 #include <cleanup_item.h>
29 #include <pcb_shape.h>
30 #include <fp_shape.h>
31 #include <graphics_cleaner.h>
32 
33 
34 GRAPHICS_CLEANER::GRAPHICS_CLEANER( DRAWINGS& aDrawings, MODULE* aParentModule,
35  BOARD_COMMIT& aCommit ) :
36  m_drawings( aDrawings ),
37  m_parentModule( aParentModule ),
38  m_commit( aCommit ),
39  m_dryRun( true ),
40  m_itemsList( nullptr )
41 {
42 }
43 
44 
45 void GRAPHICS_CLEANER::CleanupBoard( bool aDryRun, std::vector<std::shared_ptr<CLEANUP_ITEM> >* aItemsList,
46  bool aMergeRects, bool aDeleteRedundant )
47 {
48  m_dryRun = aDryRun;
49  m_itemsList = aItemsList;
50 
51  // Clear the flag used to mark some segments as deleted, in dry run:
52  for( BOARD_ITEM* drawing : m_drawings )
53  drawing->ClearFlags( IS_DELETED );
54 
55  if( aDeleteRedundant )
57 
58  if( aMergeRects )
59  mergeRects();
60 
61  // Clear the flag used to mark some segments:
62  for( BOARD_ITEM* drawing : m_drawings )
63  drawing->ClearFlags( IS_DELETED );
64 }
65 
66 
68 {
69  switch( aSegment->GetShape() )
70  {
71  case S_SEGMENT:
72  case S_RECT:
73  return aSegment->GetStart() == aSegment->GetEnd();
74 
75  case S_CIRCLE:
76  return aSegment->GetRadius() == 0;
77 
78  case S_ARC:
79  return aSegment->GetCenter().x == aSegment->GetArcStart().x
80  && aSegment->GetCenter().y == aSegment->GetArcStart().y;
81 
82  case S_POLYGON:
83  return aSegment->GetPointCount() == 0;
84 
85  case S_CURVE:
86  aSegment->RebuildBezierToSegmentsPointsList( aSegment->GetWidth() );
87  return aSegment->GetBezierPoints().empty();
88 
89  default:
90  wxFAIL_MSG( "GRAPHICS_CLEANER::isNullSegment unsupported PCB_SHAPE shape: "
91  + PCB_SHAPE_TYPE_T_asString( aSegment->GetShape()) );
92  return false;
93  }
94 }
95 
96 
98 {
99  if( aShape1->GetShape() != aShape2->GetShape()
100  || aShape1->GetLayer() != aShape2->GetLayer()
101  || aShape1->GetWidth() != aShape2->GetWidth() )
102  {
103  return false;
104  }
105 
106  switch( aShape1->GetShape() )
107  {
108  case S_SEGMENT:
109  case S_RECT:
110  case S_CIRCLE:
111  return aShape1->GetStart() == aShape2->GetStart()
112  && aShape1->GetEnd() == aShape2->GetEnd();
113 
114  case S_ARC:
115  return aShape1->GetCenter() == aShape2->GetCenter()
116  && aShape1->GetArcStart() == aShape2->GetArcStart()
117  && aShape1->GetAngle() == aShape2->GetAngle();
118 
119  case S_POLYGON:
120  // TODO
121  return false;
122 
123  case S_CURVE:
124  return aShape1->GetBezControl1() == aShape2->GetBezControl1()
125  && aShape1->GetBezControl2() == aShape2->GetBezControl2()
126  && aShape1->GetBezierPoints() == aShape2->GetBezierPoints();
127 
128  default:
129  wxFAIL_MSG( "GRAPHICS_CLEANER::areEquivalent unsupported PCB_SHAPE shape: "
130  + PCB_SHAPE_TYPE_T_asString( aShape1->GetShape()) );
131  return false;
132  }
133 }
134 
135 
137 {
138  // Remove duplicate segments (2 superimposed identical segments):
139  for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
140  {
141  PCB_SHAPE* segment = dynamic_cast<PCB_SHAPE*>( *it );
142 
143  if( !segment || segment->GetShape() != S_SEGMENT || segment->HasFlag( IS_DELETED ) )
144  continue;
145 
146  if( isNullSegment( segment ) )
147  {
148  std::shared_ptr<CLEANUP_ITEM> item( new CLEANUP_ITEM( CLEANUP_NULL_GRAPHIC ) );
149  item->SetItems( segment );
150  m_itemsList->push_back( item );
151 
152  if( !m_dryRun )
153  m_commit.Removed( segment );
154 
155  continue;
156  }
157 
158  for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
159  {
160  PCB_SHAPE* segment2 = dynamic_cast<PCB_SHAPE*>( *it2 );
161 
162  if( !segment2 || segment2->HasFlag( IS_DELETED ) )
163  continue;
164 
165  if( areEquivalent( segment, segment2 ) )
166  {
167  std::shared_ptr<CLEANUP_ITEM> item( new CLEANUP_ITEM( CLEANUP_DUPLICATE_GRAPHIC ) );
168  item->SetItems( segment2 );
169  m_itemsList->push_back( item );
170 
171  segment2->SetFlags( IS_DELETED );
172 
173  if( !m_dryRun )
174  m_commit.Removed( segment2 );
175  }
176  }
177  }
178 }
179 
180 
182 {
183  struct SIDE_CANDIDATE
184  {
185  SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
186  start( aShape->GetStart() ),
187  end( aShape->GetEnd() ),
188  shape( aShape )
189  {
190  if( start.x > end.x || start.y > end.y )
191  std::swap( start, end );
192  }
193 
194  wxPoint start;
195  wxPoint end;
196  PCB_SHAPE* shape;
197  };
198 
199  std::vector<SIDE_CANDIDATE*> sides;
200  std::map<wxPoint, std::vector<SIDE_CANDIDATE*>> ptMap;
201 
202  // First load all the candidates into the side vector and layer maps
203  for( BOARD_ITEM* item : m_drawings )
204  {
205  PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
206 
207  if( !shape || shape->GetShape() != S_SEGMENT )
208  continue;
209 
210  if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
211  {
212  sides.emplace_back( new SIDE_CANDIDATE( shape ) );
213  ptMap[ sides.back()->start ].push_back( sides.back() );
214  }
215  }
216 
217  // Now go through the sides and try and match lines into rectangles
218  for( SIDE_CANDIDATE* side : sides )
219  {
220  if( side->shape->HasFlag( IS_DELETED ) )
221  continue;
222 
223  SIDE_CANDIDATE* left = nullptr;
224  SIDE_CANDIDATE* top = nullptr;
225  SIDE_CANDIDATE* right = nullptr;
226  SIDE_CANDIDATE* bottom = nullptr;
227 
228  auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
229  {
230  return aCandidate->shape->GetLayer() == side->shape->GetLayer()
231  && aCandidate->shape->GetWidth() == side->shape->GetWidth()
232  && !aCandidate->shape->HasFlag( IS_DELETED );
233  };
234 
235  if( side->start.x == side->end.x )
236  {
237  // We've found a possible left; see if we have a top
238  //
239  left = side;
240 
241  for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
242  {
243  if( candidate != left && viable( candidate ) )
244  {
245  top = candidate;
246  break;
247  }
248  }
249  }
250  else if( side->start.y == side->end.y )
251  {
252  // We've found a possible top; see if we have a left
253  //
254  top = side;
255 
256  for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
257  {
258  if( candidate != top && viable( candidate ) )
259  {
260  left = candidate;
261  break;
262  }
263  }
264  }
265 
266  if( top && left )
267  {
268  // See if we can fill in the other two sides
269  //
270  for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
271  {
272  if( candidate != top && viable( candidate ) )
273  {
274  right = candidate;
275  break;
276  }
277  }
278 
279  for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
280  {
281  if( candidate != left && viable( candidate ) )
282  {
283  bottom = candidate;
284  break;
285  }
286  }
287 
288  if( right && bottom && right->end == bottom->end )
289  {
290  left->shape->SetFlags( IS_DELETED );
291  top->shape->SetFlags( IS_DELETED );
292  right->shape->SetFlags( IS_DELETED );
293  bottom->shape->SetFlags( IS_DELETED );
294 
295  std::shared_ptr<CLEANUP_ITEM> item( new CLEANUP_ITEM( CLEANUP_LINES_TO_RECT ) );
296  item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
297  m_itemsList->push_back( item );
298 
299  if( !m_dryRun )
300  {
301  PCB_SHAPE* rect;
302 
303  if( m_parentModule )
304  rect = new FP_SHAPE( m_parentModule );
305  else
306  rect = new PCB_SHAPE();
307 
308  rect->SetShape( S_RECT );
309  rect->SetStart( top->start );
310  rect->SetEnd( bottom->end );
311  rect->SetLayer( top->shape->GetLayer() );
312  rect->SetWidth( top->shape->GetWidth() );
313 
314  m_commit.Add( rect );
315  m_commit.Remove( left->shape );
316  m_commit.Remove( top->shape );
317  m_commit.Remove( right->shape );
318  m_commit.Remove( bottom->shape );
319  }
320  }
321  }
322  }
323 
324  for( SIDE_CANDIDATE* side : sides )
325  delete side;
326 }
327 
usual segment : line with rounded ends
bool isNullSegment(PCB_SHAPE *aShape)
BOARD_COMMIT & m_commit
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Definition: pcb_shape.h:140
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
#define IS_DELETED
Definition: eda_item.h:108
COMMIT & Add(EDA_ITEM *aItem)
Adds a new item to the model
Definition: commit.h:78
int GetRadius() const
Function GetRadius returns the radius of this item Has meaning only for arc and circle.
Definition: pcb_shape.h:185
int GetWidth() const
Definition: pcb_shape.h:100
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
Bezier Curve.
Arcs (with rounded ends)
wxPoint GetArcStart() const
Definition: pcb_shape.h:163
COMMIT & Removed(EDA_ITEM *aItem)
Notifies observers that aItem has been removed
Definition: commit.h:96
segment with non rounded ends
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:220
void SetShape(PCB_SHAPE_TYPE_T aShape)
Definition: pcb_shape.h:113
GRAPHICS_CLEANER(DRAWINGS &aDrawings, MODULE *aParentModule, BOARD_COMMIT &aCommit)
wxPoint GetCenter() const override
Function GetCenter()
Definition: pcb_shape.cpp:334
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Definition: pcb_shape.h:129
void CleanupBoard(bool aDryRun, std::vector< std::shared_ptr< CLEANUP_ITEM > > *aItemsList, bool aMergeRects, bool aDeleteRedundant)
the cleanup function.
const std::vector< wxPoint > & GetBezierPoints() const
Definition: pcb_shape.h:229
COMMIT & Remove(EDA_ITEM *aItem)
Removes a new item from the model
Definition: commit.h:90
bool areEquivalent(PCB_SHAPE *aShape1, PCB_SHAPE *aShape2)
void SetStart(const wxPoint &aStart)
Definition: pcb_shape.h:132
polygon (not yet used for tracks, but could be in microwave apps)
const wxPoint & GetBezControl2() const
Definition: pcb_shape.h:120
double GetAngle() const
Definition: pcb_shape.h:108
void SetWidth(int aWidth)
Definition: pcb_shape.h:99
int GetPointCount() const
Definition: pcb_shape.cpp:1208
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_BezierPoints vertex list that approximate the Bezier curve by a list of segments Has me...
Definition: pcb_shape.cpp:308
PCB_SHAPE_TYPE_T GetShape() const
Definition: pcb_shape.h:114
bool HasFlag(STATUS_FLAGS aFlag)
Definition: eda_item.h:223
DRAWINGS & m_drawings
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
const wxPoint & GetBezControl1() const
Definition: pcb_shape.h:117
void SetEnd(const wxPoint &aEnd)
Definition: pcb_shape.h:143
static wxString PCB_SHAPE_TYPE_T_asString(PCB_SHAPE_TYPE_T a)