KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_helper.h
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
20#ifndef GRID_HELPER_H
21#define GRID_HELPER_H
22
23#include <vector>
24#include <optional>
25
27#include <geometry/seg.h>
28#include <kiid.h>
29#include <math/vector2d.h>
33#include <snap/snap_resolver.h>
35#include <tool/selection.h>
36#include <origin_viewitem.h>
37
38class TOOL_MANAGER; // Forward declaration to avoid hard dependency in tests
39
40class EDA_ITEM;
41
49inline SNAP_TARGET_ID SnapTargetId( const KIID& aId )
50{
51 return aId.AsBytes();
52}
53
55{
56 // When the item doesn't match an override, use the current user grid
58
64};
65
67{
68 friend void TEST_CLEAR_ANCHORS( GRID_HELPER& helper );
69
70public:
72 GRID_HELPER( TOOL_MANAGER* aToolMgr, int aConstructionLayer );
73 virtual ~GRID_HELPER();
74
75 VECTOR2I GetGrid() const;
77 VECTOR2I GetOrigin() const;
78
83 virtual void FullReset()
84 {
85 m_constructionGeomPreview.ClearSnapLine();
86 m_snapManager.Clear();
87 m_anchors.clear();
88 m_snapItem.reset();
90 m_stickySnapIds.clear();
92 }
93
94 // Manual setters used when no TOOL_MANAGER/View is available (e.g. in tests)
95 void SetGridSize( const VECTOR2D& aGrid ) { m_manualGrid = aGrid; }
96 void SetVisibleGridSize( const VECTOR2D& aGrid ) { m_manualVisibleGrid = aGrid; }
97 void SetOrigin( const VECTOR2I& aOrigin ) { m_manualOrigin = aOrigin; }
98 void SetGridSnapping( bool aEnable ) { m_manualGridSnapping = aEnable; }
99
100 void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
101
102 virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
103 {
104 return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
105 }
106
107 virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
108 {
109 return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
110 }
111
112 virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
113 virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
114 const VECTOR2D& aOffset ) const;
115
116 VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
117 VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
118 const VECTOR2D& aOffset ) const;
119
123 virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
124
128 virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
129
133 virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
134
135 void SetSkipPoint( const VECTOR2I& aPoint )
136 {
137 m_skipPoint = aPoint;
138 }
139
144 {
145 m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
146 }
147
148 void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
149 bool GetSnap() const { return m_enableSnap; }
150
151 void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
152 bool GetUseGrid() const { return m_enableGrid; }
153
154 void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
155 void SetSnapLineDirections( const std::vector<VECTOR2I>& aDirections );
156 void SetSnapLineOrigin( const VECTOR2I& aOrigin );
157 void SetSnapLineEnd( const std::optional<VECTOR2I>& aEnd );
158 void ClearSnapLine();
159 std::optional<VECTOR2I> SnapToConstructionLines( const VECTOR2I& aPoint,
160 const VECTOR2I& aNearestGrid,
161 const VECTOR2D& aGrid,
162 double aSnapRange ) const;
163
164 void SetMask( int aMask ) { m_maskTypes = aMask; }
165 void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
166 void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
167
168 std::optional<VECTOR2I> GetSnappedPoint() const;
169
176 void SetAngleRestriction( std::optional<VECTOR2I> aOrigin, double aStepDegrees )
177 {
178 m_angleOrigin = aOrigin;
179 m_angleStepDegrees = aStepDegrees;
180
181 if( !m_angleOrigin )
182 m_retainedAngleBranch.reset();
183 }
184
185 void SetPointEditProfile( bool aEnabled ) { m_pointEditProfile = aEnabled; }
186
187 void SetStationarySelfGeometry( std::vector<VECTOR2I> aPoints, std::vector<SEG> aSegments )
188 {
189 m_stationarySelfPoints = std::move( aPoints );
190 m_stationarySelfSegments = std::move( aSegments );
191 }
192
194 {
195 m_feasibilityCallback = std::move( aCallback );
196 }
197
199 {
206
207 // This anchor comes from 'constructed' geometry (e.g. an intersection
208 // with something else), and not from some intrinsic point of an item
209 // (e.g. an endpoint)
212 };
213
214protected:
215 struct ANCHOR
216 {
225 ANCHOR( const VECTOR2I& aPos, int aFlags, int aPointTypes, std::vector<EDA_ITEM*> aItems ) :
226 pos( aPos ), flags( aFlags ), pointTypes( aPointTypes ),
227 items( std::move( aItems ) )
228 {
229 }
230
232 int flags;
234
237 std::vector<EDA_ITEM*> items;
238
239 double Distance( const VECTOR2I& aP ) const
240 {
241 return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
242 }
243
244 bool InvolvesItem( const EDA_ITEM& aItem ) const
245 {
246 return std::find( items.begin(), items.end(), &aItem ) != items.end();
247 }
248 };
249
250 void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem,
251 int aPointTypes = POINT_TYPE::PT_NONE )
252 {
253 addAnchor( aPos, aFlags, std::vector<EDA_ITEM*>{ aItem }, aPointTypes );
254 }
255
256 void addAnchor( const VECTOR2I& aPos, int aFlags, std::vector<EDA_ITEM*> aItems,
257 int aPointTypes )
258 {
259 if( ( aFlags & m_maskTypes ) == aFlags )
260 m_anchors.emplace_back( ANCHOR( aPos, aFlags, aPointTypes, std::move( aItems ) ) );
261 }
262
264 {
265 m_anchors.clear();
266 }
267
272 bool canUseGrid() const;
273
274 VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid, const VECTOR2I& aOffset ) const;
275
278 {
279 double scale;
280 int range;
281 int in;
282 int out;
284 };
285
294 SNAP_RANGES computeSnapRanges( bool aClampToVisibleGrid ) const;
295
301 void emitAngleBranchCandidates( std::vector<SNAP_CANDIDATE>& aCandidates, const VECTOR2I& aOrigin,
302 double aSnapScale ) const;
303
310 void emitSelfAndGridCandidates( std::vector<SNAP_CANDIDATE>& aCandidates, const SNAP_SOURCE_CONTEXT& aContext,
311 const VECTOR2I& aOrigin, const VECTOR2I& aNearestGrid, double aSnapScale,
312 int aSnapRange, bool aUseGrid ) const;
313
315
320 void retainAcceptedSnaps( const SNAP_RESULT& aResult );
321
322protected:
323 void applySnapResultGuides( const SNAP_RESULT& aResult );
324
325 void showConstructionGeometry( bool aShow );
326
328
329 void updateSnapPoint( const TYPED_POINT2I& aPoint );
330
337
338 std::vector<ANCHOR> m_anchors;
339
341 std::optional<VECTOR2I> m_auxAxis;
342
343 int m_maskTypes; // Mask of allowed snap types
344
345 bool m_enableSnap; // Allow snapping to other items on the layers
346 bool m_enableGrid; // If true, allow snapping to grid
347 bool m_enableSnapLine; // Allow drawing lines from snap points
348 std::optional<ANCHOR> m_snapItem; // Pointer to the currently snapped item in m_anchors
349 // (NULL if not snapped)
350 VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
351 // source point
354
355 // Manual grid parameters used when no TOOL_MANAGER is provided
360
361 // Snap-resolver integration state shared by the derived helpers
362 std::optional<VECTOR2I> m_angleOrigin;
363 double m_angleStepDegrees = 0.0;
364 std::optional<SNAP_STABLE_ID> m_retainedAngleBranch;
365 std::vector<SNAP_STABLE_ID> m_stickySnapIds;
367 std::vector<VECTOR2I> m_stationarySelfPoints;
368 std::vector<SEG> m_stationarySelfSegments;
369 bool m_pointEditProfile = false;
371
384 static SNAP_REFERENCE_PREFERENCE classifyReference( const VECTOR2I& aPoint, const BOX2I& aBounds,
385 bool aAnchorPoint );
386
388 void setLayoutReference( const VECTOR2I& aPoint, const std::optional<BOX2I>& aBounds, bool aAnchorPoint );
389
390private:
393
396
398 std::unique_ptr<KIGFX::ANCHOR_DEBUG> m_anchorDebug;
399};
400
401#endif
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
KIGFX::CONSTRUCTION_GEOM m_constructionGeomPreview
Show construction geometry (if any) on the canvas.
std::optional< VECTOR2I > m_auxAxis
VECTOR2I computeNearest(const VECTOR2I &aPoint, const VECTOR2I &aGrid, const VECTOR2I &aOffset) const
void retainAcceptedSnaps(const SNAP_RESULT &aResult)
Record which snaps the resolver accepted so they can be re-biased on the next resolve,...
std::optional< VECTOR2I > SnapToConstructionLines(const VECTOR2I &aPoint, const VECTOR2I &aNearestGrid, const VECTOR2D &aGrid, double aSnapRange) const
void SetSnapLineDirections(const std::vector< VECTOR2I > &aDirections)
void addAnchor(const VECTOR2I &aPos, int aFlags, EDA_ITEM *aItem, int aPointTypes=POINT_TYPE::PT_NONE)
SNAP_MANAGER & getSnapManager()
VECTOR2I m_skipPoint
bool GetSnap() const
bool m_enableGrid
SNAP_RANGES computeSnapRanges(bool aClampToVisibleGrid) const
Compute the snap thresholds.
void SetSnap(bool aSnap)
void emitAngleBranchCandidates(std::vector< SNAP_CANDIDATE > &aCandidates, const VECTOR2I &aOrigin, double aSnapScale) const
Emit the angle-restriction snap candidates (the two branches bracketing the cursor angle) into the cu...
virtual GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const
Get the coarsest grid that applies to an item.
void SetSkipPoint(const VECTOR2I &aPoint)
void showConstructionGeometry(bool aShow)
SNAP_RESOLVER::FEASIBILITY_CALLBACK m_feasibilityCallback
void SetAngleRestriction(std::optional< VECTOR2I > aOrigin, double aStepDegrees)
Restrict snapping to a set of angle branches emanating from an origin.
SNAP_MANAGER m_snapManager
Manage the construction geometry, snap lines, reference points, etc.
bool GetUseGrid() const
std::vector< SEG > m_stationarySelfSegments
virtual ~GRID_HELPER()
double m_angleStepDegrees
VECTOR2D m_manualVisibleGrid
void addAnchor(const VECTOR2I &aPos, int aFlags, std::vector< EDA_ITEM * > aItems, int aPointTypes)
void SetOrigin(const VECTOR2I &aOrigin)
Definition grid_helper.h:97
void SetSnapLineOrigin(const VECTOR2I &aOrigin)
void SetVisibleGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:96
void ClearSkipPoint()
Clear the skip point by setting it to an unreachable position, thereby preventing matching.
bool m_manualGridSnapping
void SetGridSnapping(bool aEnable)
Definition grid_helper.h:98
friend void TEST_CLEAR_ANCHORS(GRID_HELPER &helper)
VECTOR2I m_manualOrigin
virtual GRID_HELPER_GRIDS GetSelectionGrid(const SELECTION &aSelection) const
Gets the coarsest grid that applies to a selecion of items.
TOOL_MANAGER * m_toolMgr
void SetUseGrid(bool aSnapToGrid)
std::optional< VECTOR2I > GetSnappedPoint() const
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
VECTOR2D GetVisibleGrid() const
std::unique_ptr< KIGFX::ANCHOR_DEBUG > m_anchorDebug
KIGFX::VIEW_ITEM for visualising anchor points, if enabled.
std::vector< VECTOR2I > m_stationarySelfPoints
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
SNAP_RESOLVER::TRACE_CALLBACK snapTraceCallback(const SNAP_SOURCE_CONTEXT &aContext) const
VECTOR2I GetGrid() const
static SNAP_REFERENCE_PREFERENCE classifyReference(const VECTOR2I &aPoint, const BOX2I &aBounds, bool aAnchorPoint)
Classify the point a drag was started from against the moving object's bounds.
bool m_enableSnapLine
bool m_enableSnap
VECTOR2I GetOrigin() const
bool canUseGrid() const
Check whether it is possible to use the grid – this depends both on local grid helper settings and gl...
void ClearSnapLine()
void SetGridSize(const VECTOR2D &aGrid)
Definition grid_helper.h:95
std::optional< SNAP_STABLE_ID > m_retainedAngleBranch
void clearAnchors()
std::optional< ANCHOR > m_snapItem
SNAP_REFERENCE_PREFERENCE m_layoutReferencePreference
void SetFeasibilityCallback(SNAP_RESOLVER::FEASIBILITY_CALLBACK aCallback)
virtual void FullReset()
Reset all internal state.
Definition grid_helper.h:83
KIGFX::ANCHOR_DEBUG * enableAndGetAnchorDebug()
Enable the anchor debug if permitted and return it.
void SetMaskFlag(int aFlag)
void SetMask(int aMask)
void SetSnapLineEnd(const std::optional< VECTOR2I > &aEnd)
KIGFX::SNAP_INDICATOR m_viewSnapPoint
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
void updateSnapPoint(const TYPED_POINT2I &aPoint)
void SetStationarySelfGeometry(std::vector< VECTOR2I > aPoints, std::vector< SEG > aSegments)
KIGFX::ORIGIN_VIEWITEM m_viewAxis
std::optional< VECTOR2I > m_angleOrigin
bool m_pointEditProfile
void ClearMaskFlag(int aFlag)
std::vector< SNAP_STABLE_ID > m_stickySnapIds
void SetSnapLine(bool aSnap)
void setLayoutReference(const VECTOR2I &aPoint, const std::optional< BOX2I > &aBounds, bool aAnchorPoint)
Record the classified drag reference and trace it.
void emitSelfAndGridCandidates(std::vector< SNAP_CANDIDATE > &aCandidates, const SNAP_SOURCE_CONTEXT &aContext, const VECTOR2I &aOrigin, const VECTOR2I &aNearestGrid, double aSnapScale, int aSnapRange, bool aUseGrid) const
Emit the point editor's unchanged self-geometry and the independent grid axes.
void SetPointEditProfile(bool aEnabled)
VECTOR2D m_manualGrid
void applySnapResultGuides(const SNAP_RESULT &aResult)
std::vector< ANCHOR > m_anchors
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
View item to draw debug items for anchors.
Shows construction geometry for things like line extensions, arc centers, etc.
View item to draw an origin marker with an optional snap type indicator.
Definition kiid.h:46
std::array< uint8_t, 16 > AsBytes() const
Definition kiid.cpp:276
A SNAP_MANAGER glues together the snap line manager and construction manager., along with some other ...
std::function< SNAP_RESULT(const SNAP_SOURCE_CONTEXT &, const std::vector< SNAP_CANDIDATE > &)> FEASIBILITY_CALLBACK
std::function< void(const std::string &)> TRACE_CALLBACK
Master controller class:
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:281
SNAP_TARGET_ID SnapTargetId(const KIID &aId)
The snap identity of a document item.
Definition grid_helper.h:49
GRID_HELPER_GRIDS
Definition grid_helper.h:55
@ GRID_VIAS
Definition grid_helper.h:61
@ GRID_TEXT
Definition grid_helper.h:62
@ GRID_CURRENT
Definition grid_helper.h:57
@ GRID_GRAPHICS
Definition grid_helper.h:63
@ GRID_CONNECTABLE
Definition grid_helper.h:59
@ GRID_WIRES
Definition grid_helper.h:60
STL namespace.
@ PT_NONE
No specific point type.
Definition point_types.h:38
std::array< uint8_t, 16 > SNAP_TARGET_ID
ANCHOR(const VECTOR2I &aPos, int aFlags, int aPointTypes, std::vector< EDA_ITEM * > aItems)
std::vector< EDA_ITEM * > items
Items that are associated with this anchor (can be more than one, e.g.
double Distance(const VECTOR2I &aP) const
bool InvolvesItem(const EDA_ITEM &aItem) const
World-space snap thresholds derived from the screen-space snap radius.
int range
Snap radius, optionally clamped to the visible grid.
int in
Distance at which a candidate is picked up.
double scale
SNAP_SCREEN_RADIUS in world units.
int out
Distance at which a held candidate is released.
double rankingHysteresis
Resolver ranking stickiness, as a fraction of the radius.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707