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