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 <[email protected]>
6  * Copyright (C) 1992-2022 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 <macros.h>
28 #include <board_commit.h>
29 #include <cleanup_item.h>
30 #include <pcb_shape.h>
31 #include <fp_shape.h>
32 #include <graphics_cleaner.h>
33 #include <board_design_settings.h>
34 
35 
36 GRAPHICS_CLEANER::GRAPHICS_CLEANER( DRAWINGS& aDrawings, FOOTPRINT* aParentFootprint,
37  BOARD_COMMIT& aCommit ) :
38  m_drawings( aDrawings ),
39  m_parentFootprint( aParentFootprint ),
40  m_commit( aCommit ),
41  m_dryRun( true ),
42  m_itemsList( nullptr )
43 {
44 }
45 
46 
47 void GRAPHICS_CLEANER::CleanupBoard( bool aDryRun,
48  std::vector<std::shared_ptr<CLEANUP_ITEM>>* aItemsList,
49  bool aMergeRects, bool aDeleteRedundant )
50 {
51  m_dryRun = aDryRun;
52  m_itemsList = aItemsList;
53 
55 
56  // Clear the flag used to mark some shapes as deleted, in dry run:
57  for( BOARD_ITEM* drawing : m_drawings )
58  drawing->ClearFlags( IS_DELETED );
59 
60  if( aDeleteRedundant )
61  cleanupShapes();
62 
63  if( aMergeRects )
64  mergeRects();
65 
66  // Clear the flag used to mark some shapes:
67  for( BOARD_ITEM* drawing : m_drawings )
68  drawing->ClearFlags( IS_DELETED );
69 }
70 
71 
72 bool equivalent( const VECTOR2I& a, const VECTOR2I& b, int epsilon )
73 {
74  return abs( a.x - b.x ) < epsilon && abs( a.y - b.y ) < epsilon;
75 };
76 
77 
79 {
80  switch( aShape->GetShape() )
81  {
82  case SHAPE_T::SEGMENT:
83  case SHAPE_T::RECT:
84  case SHAPE_T::ARC:
85  return equivalent( aShape->GetStart(), aShape->GetEnd(), m_epsilon );
86 
87  case SHAPE_T::CIRCLE:
88  return aShape->GetRadius() == 0;
89 
90  case SHAPE_T::POLY:
91  return aShape->GetPointCount() == 0;
92 
93  case SHAPE_T::BEZIER:
94  aShape->RebuildBezierToSegmentsPointsList( aShape->GetWidth() );
95  return aShape->GetBezierPoints().empty();
96 
97  default:
99  return false;
100  }
101 }
102 
103 
105 {
106  if( aShape1->GetShape() != aShape2->GetShape()
107  || aShape1->GetLayer() != aShape2->GetLayer()
108  || aShape1->GetWidth() != aShape2->GetWidth() )
109  {
110  return false;
111  }
112 
113  switch( aShape1->GetShape() )
114  {
115  case SHAPE_T::SEGMENT:
116  case SHAPE_T::RECT:
117  case SHAPE_T::CIRCLE:
118  return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
119  && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
120 
121  case SHAPE_T::ARC:
122  return equivalent( aShape1->GetCenter(), aShape2->GetCenter(), m_epsilon )
123  && equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
124  && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
125 
126  case SHAPE_T::POLY:
127  // TODO
128  return false;
129 
130  case SHAPE_T::BEZIER:
131  return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
132  && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon )
133  && equivalent( aShape1->GetBezierC1(), aShape2->GetBezierC1(), m_epsilon )
134  && equivalent( aShape1->GetBezierC2(), aShape2->GetBezierC2(), m_epsilon );
135 
136  default:
137  wxFAIL_MSG( wxT( "GRAPHICS_CLEANER::areEquivalent unimplemented for " )
138  + aShape1->SHAPE_T_asString() );
139  return false;
140  }
141 }
142 
143 
145 {
146  // Remove duplicate shapes (2 superimposed identical shapes):
147  for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
148  {
149  PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( *it );
150 
151  if( !shape || shape->HasFlag( IS_DELETED ) )
152  continue;
153 
154  if( isNullShape( shape ) )
155  {
156  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_NULL_GRAPHIC );
157  item->SetItems( shape );
158  m_itemsList->push_back( item );
159 
160  if( !m_dryRun )
161  m_commit.Remove( shape );
162 
163  continue;
164  }
165 
166  for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
167  {
168  PCB_SHAPE* shape2 = dynamic_cast<PCB_SHAPE*>( *it2 );
169 
170  if( !shape2 || shape2->HasFlag( IS_DELETED ) )
171  continue;
172 
173  if( areEquivalent( shape, shape2 ) )
174  {
175  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_GRAPHIC );
176  item->SetItems( shape2 );
177  m_itemsList->push_back( item );
178 
179  shape2->SetFlags(IS_DELETED );
180 
181  if( !m_dryRun )
182  m_commit.Remove( shape2 );
183  }
184  }
185  }
186 }
187 
188 
190 {
191  struct SIDE_CANDIDATE
192  {
193  SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
194  start( aShape->GetStart() ),
195  end( aShape->GetEnd() ),
196  shape( aShape )
197  {
198  if( start.x > end.x || start.y > end.y )
199  std::swap( start, end );
200  }
201 
202  wxPoint start;
203  wxPoint end;
204  PCB_SHAPE* shape;
205  };
206 
207  std::vector<SIDE_CANDIDATE*> sides;
208  std::map<wxPoint, std::vector<SIDE_CANDIDATE*>> ptMap;
209 
210  // First load all the candidates into the side vector and layer maps
211  for( BOARD_ITEM* item : m_drawings )
212  {
213  PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
214 
215  if( !shape || isNullShape( shape ) || shape->GetShape() != SHAPE_T::SEGMENT )
216  continue;
217 
218  if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
219  {
220  sides.emplace_back( new SIDE_CANDIDATE( shape ) );
221  ptMap[ sides.back()->start ].push_back( sides.back() );
222  }
223  }
224 
225  // Now go through the sides and try and match lines into rectangles
226  for( SIDE_CANDIDATE* side : sides )
227  {
228  if( side->shape->HasFlag( IS_DELETED ) )
229  continue;
230 
231  SIDE_CANDIDATE* left = nullptr;
232  SIDE_CANDIDATE* top = nullptr;
233  SIDE_CANDIDATE* right = nullptr;
234  SIDE_CANDIDATE* bottom = nullptr;
235 
236  auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
237  {
238  return aCandidate->shape->GetLayer() == side->shape->GetLayer()
239  && aCandidate->shape->GetWidth() == side->shape->GetWidth()
240  && !aCandidate->shape->HasFlag( IS_DELETED );
241  };
242 
243  if( side->start.x == side->end.x )
244  {
245  // We've found a possible left; see if we have a top
246  //
247  left = side;
248 
249  for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
250  {
251  if( candidate != left && viable( candidate ) )
252  {
253  top = candidate;
254  break;
255  }
256  }
257  }
258  else if( side->start.y == side->end.y )
259  {
260  // We've found a possible top; see if we have a left
261  //
262  top = side;
263 
264  for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
265  {
266  if( candidate != top && viable( candidate ) )
267  {
268  left = candidate;
269  break;
270  }
271  }
272  }
273 
274  if( top && left )
275  {
276  // See if we can fill in the other two sides
277  //
278  for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
279  {
280  if( candidate != top && candidate != left && viable( candidate ) )
281  {
282  right = candidate;
283  break;
284  }
285  }
286 
287  for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
288  {
289  if( candidate != top && candidate != left && viable( candidate ) )
290  {
291  bottom = candidate;
292  break;
293  }
294  }
295 
296  if( right && bottom && right->end == bottom->end )
297  {
298  left->shape->SetFlags( IS_DELETED );
299  top->shape->SetFlags( IS_DELETED );
300  right->shape->SetFlags( IS_DELETED );
301  bottom->shape->SetFlags( IS_DELETED );
302 
303  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_LINES_TO_RECT );
304  item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
305  m_itemsList->push_back( item );
306 
307  if( !m_dryRun )
308  {
309  PCB_SHAPE* rect;
310  FP_SHAPE* fp_rect = nullptr;
311 
312  if( m_parentFootprint )
313  rect = fp_rect = new FP_SHAPE( m_parentFootprint );
314  else
315  rect = new PCB_SHAPE();
316 
317  rect->SetShape( SHAPE_T::RECT );
318  rect->SetFilled( false );
319  rect->SetStart( top->start );
320  rect->SetEnd( bottom->end );
321  rect->SetLayer( top->shape->GetLayer() );
322  rect->SetWidth( top->shape->GetWidth() );
323 
324  if( fp_rect )
325  fp_rect->SetLocalCoord();
326 
327  m_commit.Add( rect );
328  m_commit.Remove( left->shape );
329  m_commit.Remove( top->shape );
330  m_commit.Remove( right->shape );
331  m_commit.Remove( bottom->shape );
332  }
333  }
334  }
335  }
336 
337  for( SIDE_CANDIDATE* side : sides )
338  delete side;
339 }
340 
const std::vector< wxPoint > & GetBezierPoints() const
Definition: eda_shape.h:210
int GetWidth() const
Definition: eda_shape.h:98
BOARD_COMMIT & m_commit
bool isNullShape(PCB_SHAPE *aShape)
void SetFilled(bool aFlag)
Definition: eda_shape.h:92
void SetEnd(const wxPoint &aEnd)
Definition: eda_shape.h:135
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:164
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:106
COMMIT & Add(EDA_ITEM *aItem)
Notify observers that aItem has been added.
Definition: commit.h:78
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:155
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:152
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:590
GRAPHICS_CLEANER(DRAWINGS &aDrawings, FOOTPRINT *aParentFootprint, BOARD_COMMIT &aCommit)
This file contains miscellaneous commonly used macros and functions.
void SetStart(const wxPoint &aStart)
Definition: eda_shape.h:110
const wxPoint & GetBezierC1() const
Definition: eda_shape.h:154
int GetPointCount() const
Definition: eda_shape.cpp:1222
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:131
#define IS_DELETED
wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:79
const wxPoint & GetBezierC2() const
Definition: eda_shape.h:157
void CleanupBoard(bool aDryRun, std::vector< std::shared_ptr< CLEANUP_ITEM > > *aItemsList, bool aMergeRects, bool aDeleteRedundant)
the cleanup function.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
void SetWidth(int aWidth)
Definition: eda_shape.h:97
COMMIT & Remove(EDA_ITEM *aItem)
Notify observers that aItem has been removed.
Definition: commit.h:90
bool areEquivalent(PCB_SHAPE *aShape1, PCB_SHAPE *aShape2)
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:71
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:366
BOARD * GetBoard() const
void SetShape(SHAPE_T aShape)
Definition: eda_shape.h:100
void SetLocalCoord()
Set relative coordinates from draw coordinates.
Definition: fp_shape.cpp:52
SHAPE_T GetShape() const
Definition: eda_shape.h:101
int GetRadius() const
Definition: eda_shape.cpp:472
DRAWINGS & m_drawings
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:143
FOOTPRINT * m_parentFootprint
bool equivalent(const VECTOR2I &a, const VECTOR2I &b, int epsilon)