KiCad PCB EDA Suite
Loading...
Searching...
No Matches
block_drag_dependents.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 2
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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <base_units.h>
23#include <board.h>
24#include <footprint.h>
25#include <pcb_shape.h>
27
28#include <optional>
29#include <set>
30
31
32namespace
33{
34
37std::optional<BLOCK_DRAG_FOLLOW> followKind( const PCB_SHAPE& aShape )
38{
39 switch( aShape.GetShape() )
40 {
43 default: return std::nullopt;
44 }
45}
46
47} // namespace
48
49
51BlockDragFindDependents( const std::vector<PCB_SHAPE*>& aCandidates,
52 const std::vector<BLOCK_DRAG_ENDPOINT>& aMovedEndpoints, bool aOverrideLocks,
53 std::vector<BLOCK_DRAG_ENDPOINT>& aDependents )
54{
55 aDependents.clear();
56
57 const int baseTol = pcbIUScale.mmToIU( 0.001 );
58 int64_t tolSq = (int64_t) baseTol * baseTol;
59
60 auto withinTol = [&]( const VECTOR2I& aFirst, const VECTOR2I& aSecond )
61 {
62 return aFirst.SquaredDistance( aSecond ) <= tolSq;
63 };
64
65 // Grows as each carried shape hands its far end on.
66 std::vector<BLOCK_DRAG_ENDPOINT> seeds( aMovedEndpoints.begin(), aMovedEndpoints.end() );
67
68 // Already moving, so their ends cannot be matched again.
69 std::set<const PCB_SHAPE*> carried;
70
71 auto isMoved = [&]( const PCB_SHAPE* aShape, bool aIsStart )
72 {
73 for( const BLOCK_DRAG_ENDPOINT& moved : seeds )
74 {
75 if( moved.shape == aShape && moved.isStart == aIsStart )
76 return true;
77 }
78
79 return false;
80 };
81
82 auto alreadyAccepted = [&]( const PCB_SHAPE* aShape, bool aIsStart )
83 {
84 for( const BLOCK_DRAG_ENDPOINT& dep : aDependents )
85 {
86 if( dep.shape == aShape && dep.isStart == aIsStart )
87 return true;
88 }
89
90 return false;
91 };
92
93 // Indexed, because a carried shape appends its far end to the seeds as this runs.
94 for( size_t si = 0; si < seeds.size(); ++si )
95 {
96 const BLOCK_DRAG_ENDPOINT moved = seeds[si]; // by value, the vector may reallocate
97 std::vector<BLOCK_DRAG_ENDPOINT> cluster;
98 int tol = baseTol;
99
100 // The board outline joins Edge.Cuts ends within its chaining epsilon, so match that
101 if( BOARD* board = moved.shape->GetBoard(); board && moved.shape->GetLayer() == Edge_Cuts )
102 tol = std::max( tol, board->GetOutlinesChainingEpsilon() );
103
104 tolSq = (int64_t) tol * tol;
105
106 for( PCB_SHAPE* candidate : aCandidates )
107 {
108 if( !candidate )
109 continue;
110
111 std::optional<BLOCK_DRAG_FOLLOW> follow = followKind( *candidate );
112
113 if( !follow || carried.count( candidate ) )
114 continue;
115
116 if( candidate->GetLayer() != moved.shape->GetLayer() )
117 continue;
118
119 for( bool isStart : { true, false } )
120 {
121 VECTOR2I pos = isStart ? candidate->GetStart() : candidate->GetEnd();
122
123 // Distance first, so the scan over moved endpoints only runs for the
124 // handful of candidates that sit on one.
125 if( !withinTol( pos, moved.position ) )
126 continue;
127
128 if( !isMoved( candidate, isStart ) )
129 cluster.push_back( { candidate, isStart, pos, *follow } );
130 }
131 }
132
133 // Every end meeting at this joint has to sit on every other one, or there is
134 // no single shape to follow.
135 bool ambiguous = false;
136
137 for( size_t ii = 0; ii < cluster.size() && !ambiguous; ++ii )
138 {
139 for( size_t jj = ii + 1; jj < cluster.size(); ++jj )
140 {
141 if( !withinTol( cluster[ii].position, cluster[jj].position ) )
142 {
143 ambiguous = true;
144 break;
145 }
146 }
147 }
148
149 if( ambiguous )
150 continue;
151
152 for( const BLOCK_DRAG_ENDPOINT& member : cluster )
153 {
154 if( alreadyAccepted( member.shape, member.isStart ) )
155 continue;
156
157 aDependents.push_back( member );
158
159 if( member.follow != BLOCK_DRAG_FOLLOW::WHOLE )
160 continue;
161
162 // Mark it carried before seeding, or its far end could match it again.
163 carried.insert( member.shape );
164
165 bool farIsStart = !member.isStart;
166 VECTOR2I farPos = farIsStart ? member.shape->GetStart() : member.shape->GetEnd();
167
168 seeds.push_back( { member.shape, farIsStart, farPos, BLOCK_DRAG_FOLLOW::WHOLE } );
169 }
170 }
171
172 if( !aOverrideLocks )
173 {
174 for( const BLOCK_DRAG_ENDPOINT& dep : aDependents )
175 {
176 // The same lock test the selected items get, which also covers a locked parent
177 if( ConstraintItemIsLocked( dep.shape ) )
178 {
179 aDependents.clear();
181 }
182 }
183 }
184
186}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
BLOCK_DRAG_DEPENDENT_RESULT BlockDragFindDependents(const std::vector< PCB_SHAPE * > &aCandidates, const std::vector< BLOCK_DRAG_ENDPOINT > &aMovedEndpoints, bool aOverrideLocks, std::vector< BLOCK_DRAG_ENDPOINT > &aDependents)
Find the joined endpoints that have to follow the ones the drag moves.
BLOCK_DRAG_DEPENDENT_RESULT
@ LOCKED
A shape that would have to follow is locked and Override Locks is off, so the drag has to stop.
@ STRETCH
Only this end moves, so the shape stretches.
@ WHOLE
The whole shape moves, for a shape that moving one end alone would distort.
bool ConstraintItemIsLocked(const BOARD_ITEM *aItem)
True when the solver must treat aItem as immovable, either locked itself or living inside a locked fo...
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
SHAPE_T GetShape() const
Definition eda_shape.h:178
constexpr extended_type SquaredDistance(const VECTOR2< T > &aVector) const
Compute the squared distance between two vectors.
Definition vector2d.h:582
@ SEGMENT
Definition eda_shape.h:57
@ Edge_Cuts
Definition layer_ids.h:108
One end of a graphic shape, used both for the ends that moved and for the ones found to follow them.
bool moved
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708