KiCad PCB EDA Suite
Loading...
Searching...
No Matches
zone_utils.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "zone_utils.h"
25
26#include <zone.h>
28
29
30std::vector<std::unique_ptr<ZONE>> MergeZonesWithSameOutline( std::vector<std::unique_ptr<ZONE>>&& aZones )
31{
32 const auto polygonsAreMergeable = []( const SHAPE_POLY_SET::POLYGON& a, const SHAPE_POLY_SET::POLYGON& b ) -> bool
33 {
34 if( a.size() != b.size() )
35 return false;
36
37 // NOTE: this assumes the polygons have their line chains in the same order
38 // But that is not actually required for same geometry (i.e. mergeability)
39 for( size_t lineChainId = 0; lineChainId < a.size(); lineChainId++ )
40 {
41 const SHAPE_LINE_CHAIN& chainA = a[lineChainId];
42 const SHAPE_LINE_CHAIN& chainB = b[lineChainId];
43
44 // Note: this assumes the polygons are either already simplified or that it's
45 // OK to not merge even if they would be the same after simplification.
46 if( chainA.PointCount() != chainB.PointCount() || chainA.BBox() != chainB.BBox()
47 || !chainA.CompareGeometry( chainB ) )
48 {
49 // Different geometry, can't merge
50 return false;
51 }
52 }
53
54 return true;
55 };
56
57 const auto zonesAreMergeable = [&]( const ZONE& a, const ZONE& b ) -> bool
58 {
59 if( a.GetNetCode() != b.GetNetCode() )
60 return false;
61
62 // Can't merge rule areas with zone fills
63 if( a.GetIsRuleArea() != b.GetIsRuleArea() )
64 return false;
65
66 const SHAPE_POLY_SET* polySetA = a.Outline();
67 const SHAPE_POLY_SET* polySetB = b.Outline();
68
69 if( polySetA->OutlineCount() != polySetB->OutlineCount() )
70 return false;
71
72 if( polySetA->OutlineCount() == 0 )
73 {
74 // both have no outline, so they are the same, but we must not
75 // derefence them, as they are empty
76 return true;
77 }
78
79 // REVIEW: this assumes the zones only have a single polygon in the
80 const SHAPE_POLY_SET::POLYGON& polyA = polySetA->CPolygon( 0 );
81 const SHAPE_POLY_SET::POLYGON& polyB = polySetB->CPolygon( 0 );
82
83 return polygonsAreMergeable( polyA, polyB );
84 };
85
86 std::vector<std::unique_ptr<ZONE>> deduplicatedZones;
87 size_t mergedCount = 0;
88 // Map of zone indexes that we have already merged into a prior zone
89 std::vector<bool> merged( aZones.size(), false );
90
91 for( size_t i = 0; i < aZones.size(); i++ )
92 {
93 // This one has already been subsumed into a prior zone, so skip it
94 // and it will be dropped at the end.
95 if( merged[i] )
96 continue;
97
98 ZONE& primary = *aZones[i];
99 LSET layers = primary.GetLayerSet();
100 std::unordered_map<PCB_LAYER_ID, SHAPE_POLY_SET> mergedFills;
101
102 for( size_t j = i + 1; j < aZones.size(); j++ )
103 {
104 // This zone has already been subsumed by a prior zone, so it
105 // cannot be merged into another primary
106 if( merged[j] )
107 continue;
108
109 ZONE& candidate = *aZones[j];
110 bool canMerge = zonesAreMergeable( primary, candidate );
111
112 if( canMerge )
113 {
114 for( PCB_LAYER_ID layer : candidate.GetLayerSet() )
115 {
116 if( SHAPE_POLY_SET* fill = candidate.GetFill( layer ) )
117 mergedFills[layer] = *fill;
118 }
119
120 layers |= candidate.GetLayerSet();
121 merged[j] = true;
122 mergedCount++;
123 }
124 }
125
126 if( layers != primary.GetLayerSet() )
127 {
128 for( PCB_LAYER_ID layer : primary.GetLayerSet() )
129 {
130 if( SHAPE_POLY_SET* fill = primary.GetFill( layer ) )
131 mergedFills[layer] = *fill;
132 }
133
134 primary.SetLayerSet( layers );
135
136 for( const auto& [layer, fill] : mergedFills )
137 primary.SetFilledPolysList( layer, fill );
138
139 primary.SetNeedRefill( false );
140 primary.SetIsFilled( true );
141 }
142
143 // Keep this zone - it's a primary (may or may not have had other zones merged into it)
144 deduplicatedZones.push_back( std::move( aZones[i] ) );
145 }
146
147 return deduplicatedZones;
148}
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
bool CompareGeometry(const SHAPE_LINE_CHAIN &aOther, bool aCyclicalCompare=false, int aEpsilon=0) const
Compare this line chain with another one.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Represent a set of closed polygons.
std::vector< SHAPE_LINE_CHAIN > POLYGON
represents a single polygon outline with holes.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of outlines in the set.
const POLYGON & CPolygon(int aIndex) const
Handle a list of polygons defining a copper zone.
Definition zone.h:73
void SetNeedRefill(bool aNeedRefill)
Definition zone.h:301
SHAPE_POLY_SET * GetFill(PCB_LAYER_ID aLayer)
Definition zone.h:613
void SetFilledPolysList(PCB_LAYER_ID aLayer, const SHAPE_POLY_SET &aPolysList)
Set the list of filled polygons.
Definition zone.h:634
void SetIsFilled(bool isFilled)
Definition zone.h:298
void SetLayerSet(const LSET &aLayerSet) override
Definition zone.cpp:556
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:136
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
std::vector< std::unique_ptr< ZONE > > MergeZonesWithSameOutline(std::vector< std::unique_ptr< ZONE > > &&aZones)
Merges zones with identical outlines and nets on different layers into single multi-layer zones.