67 wxUnusedVar( aConstructionLayer );
73 [view,
this](
bool aAnythingShown )
77 if( currentlyVisible && aAnythingShown )
138 m_snapManager.GetSnapLineManager().SetDirections( aDirections );
144 m_snapManager.GetSnapLineManager().SetSnapLineOrigin( aOrigin );
161 double aSnapRange )
const
166 wxLogTrace(
traceSnap,
"SnapToConstructionLines: aPoint=(%d, %d), nearestGrid=(%d, %d), snapRange=%.1f",
167 aPoint.
x, aPoint.
y, aNearestGrid.
x, aNearestGrid.
y, aSnapRange );
171 wxLogTrace(
traceSnap,
" No snap origin or no directions, returning nullopt" );
175 const VECTOR2I& origin = *snapOrigin;
177 wxLogTrace(
traceSnap,
" snapOrigin=(%d, %d), directions count=%zu",
180 const std::vector<VECTOR2I>& directions = snapLineManager.
GetDirections();
183 if( activeDirection )
184 wxLogTrace(
traceSnap,
" activeDirection=%d", *activeDirection );
190 std::optional<VECTOR2I> bestPoint;
191 double bestPerp = std::numeric_limits<double>::max();
192 double bestDistance = std::numeric_limits<double>::max();
194 for(
size_t ii = 0; ii < directions.size(); ++ii )
196 const VECTOR2I& dir = directions[ii];
200 if( dirLength == 0.0 )
202 wxLogTrace(
traceSnap,
" Direction %zu: zero length, skipping", ii );
206 VECTOR2D dirUnit = dirVector / dirLength;
208 double distanceAlong =
delta.Dot( dirUnit );
209 VECTOR2D projection = originVec + dirUnit * distanceAlong;
213 double snapThreshold = aSnapRange;
215 if( activeDirection && *activeDirection ==
static_cast<int>( ii ) )
217 snapThreshold *= 1.5;
218 wxLogTrace(
traceSnap,
" Direction %zu: ACTIVE, increased snapThreshold=%.1f", ii, snapThreshold );
221 wxLogTrace(
traceSnap,
" Direction %zu: dir=(%d, %d), perpDist=%.1f, threshold=%.1f",
222 ii, dir.
x, dir.
y, perpDistance, snapThreshold );
224 if( perpDistance > snapThreshold )
226 wxLogTrace(
traceSnap,
" perpDistance > threshold, skipping" );
234 if( dir.
x == 0 && dir.
y != 0 )
237 candidate.
x = origin.
x;
238 candidate.
y = aNearestGrid.
y;
239 wxLogTrace(
traceSnap,
" Vertical snap: candidate=(%d, %d)",
240 (
int)candidate.
x, (
int)candidate.
y );
242 else if( dir.
y == 0 && dir.
x != 0 )
245 candidate.
x = aNearestGrid.
x;
246 candidate.
y = origin.
y;
247 wxLogTrace(
traceSnap,
" Horizontal snap: candidate=(%d, %d)",
248 (
int)candidate.
x, (
int)candidate.
y );
260 VECTOR2D relProjection = projection - gridOrigin;
263 std::vector<VECTOR2D> gridPoints;
264 for(
int dx = -1; dx <= 1; ++dx )
266 for(
int dy = -1; dy <= 1; ++dy )
268 double gridX = std::round( relProjection.
x / aGrid.
x ) * aGrid.
x + dx * aGrid.
x;
269 double gridY = std::round( relProjection.
y / aGrid.
y ) * aGrid.
y + dy * aGrid.
y;
270 gridPoints.push_back(
VECTOR2D( gridX + gridOrigin.
x, gridY + gridOrigin.
y ) );
275 double bestGridDist = std::numeric_limits<double>::max();
278 for(
const VECTOR2D& gridPt : gridPoints )
281 VECTOR2D gridDelta = gridPt - originVec;
282 double gridDistAlong = gridDelta.
Dot( dirUnit );
283 VECTOR2D gridProjection = originVec + dirUnit * gridDistAlong;
284 double gridPerpDist = ( gridPt - gridProjection ).EuclideanNorm();
287 double distFromCursor = ( gridPt - cursorVec ).EuclideanNorm();
290 double score = gridPerpDist + distFromCursor * 0.1;
292 if( score < bestGridDist )
294 bestGridDist = score;
299 candidate = bestGridPt;
300 wxLogTrace(
traceSnap,
" Diagonal snap: candidate=(%.1f, %.1f), perpDist=%.1f",
301 candidate.
x, candidate.
y, bestGridDist );
306 wxLogTrace(
traceSnap,
" Grid disabled, using projection candidate=(%.1f, %.1f)",
307 candidate.
x, candidate.
y );
314 wxLogTrace(
traceSnap,
" candidateInt matches m_skipPoint, skipping" );
318 VECTOR2D candidateDelta( candidateInt.
x - aPoint.
x, candidateInt.
y - aPoint.
y );
321 wxLogTrace(
traceSnap,
" candidateInt=(%d, %d), candidateDist=%.1f",
322 candidateInt.
x, candidateInt.
y, candidateDistance );
324 if( perpDistance < bestPerp
325 || (
std::abs( perpDistance - bestPerp ) < 1e-9 && candidateDistance < bestDistance ) )
327 wxLogTrace(
traceSnap,
" NEW BEST: perpDist=%.1f, candDist=%.1f", perpDistance, candidateDistance );
328 bestPerp = perpDistance;
329 bestDistance = candidateDistance;
330 bestPoint = candidateInt;
336 wxLogTrace(
traceSnap,
" RETURNING bestPoint=(%d, %d)", bestPoint->x, bestPoint->y );
340 wxLogTrace(
traceSnap,
" RETURNING nullopt (no valid snap found)" );
445 KiROUND( (
double) ( aPoint.
y - aOffset.
y ) / aGrid.
y ) * aGrid.
y + aOffset.
y );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
A base class for most all the KiCad significant classes used in schematics and boards.
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
std::optional< VECTOR2I > SnapToConstructionLines(const VECTOR2I &aPoint, const VECTOR2I &aNearestGrid, const VECTOR2D &aGrid, double aSnapRange) const
void SetSnapLineDirections(const std::vector< VECTOR2I > &aDirections)
virtual GRID_HELPER_GRIDS GetItemGrid(const EDA_ITEM *aItem) const
Get the coarsest grid that applies to an item.
void showConstructionGeometry(bool aShow)
SNAP_MANAGER m_snapManager
Manage the construction geometry, snap lines, reference points, etc.
VECTOR2D m_manualVisibleGrid
void SetSnapLineOrigin(const VECTOR2I &aOrigin)
bool m_manualGridSnapping
virtual GRID_HELPER_GRIDS GetSelectionGrid(const SELECTION &aSelection) const
Gets the coarsest grid that applies to a selecion of items.
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
#VIEW_ITEM for visualising anchor points, if enabled.
virtual VECTOR2D GetGridSize(GRID_HELPER_GRIDS aGrid) const
Return the size of the specified grid.
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...
std::optional< ANCHOR > m_snapItem
KIGFX::ANCHOR_DEBUG * enableAndGetAnchorDebug()
Enable the anchor debug if permitted and return it.
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)
KIGFX::ORIGIN_VIEWITEM m_viewAxis
virtual VECTOR2I AlignGrid(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
View item to draw debug items for anchors.
const VECTOR2D & GetGridOrigin() const
VECTOR2D GetVisibleGridSize() const
Return the visible grid size in x and y directions.
const VECTOR2D & GetGridSize() const
Return the grid size.
bool GetGridSnapping() const
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
const std::vector< VECTOR2I > & GetDirections() const
std::optional< int > GetActiveDirection() const
const OPT_VECTOR2I & GetSnapLineOrigin() const
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
bool m_EnableSnapAnchorsDebug
Enable snap anchors debug visualization.
const wxChar *const traceSnap
Flag to enable snap/grid helper debug tracing.
@ GEOMETRY
Position or shape has changed.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
std::optional< VECTOR2I > OPT_VECTOR2I
wxLogTrace helper definitions.
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D