78 [view,
this](
bool aAnythingShown )
82 if( currentlyVisible && aAnythingShown )
91 m_toolMgr->GetToolHolder()->RefreshCanvas();
143 m_snapManager.GetSnapLineManager().SetDirections( aDirections );
149 m_snapManager.GetSnapLineManager().SetSnapLineOrigin( aOrigin );
166 double aSnapRange )
const
171 wxLogTrace(
traceSnap,
"SnapToConstructionLines: aPoint=(%d, %d), nearestGrid=(%d, %d), snapRange=%.1f",
172 aPoint.
x, aPoint.
y, aNearestGrid.
x, aNearestGrid.
y, aSnapRange );
176 wxLogTrace(
traceSnap,
" No snap origin or no directions, returning nullopt" );
180 const VECTOR2I& origin = *snapOrigin;
182 wxLogTrace(
traceSnap,
" snapOrigin=(%d, %d), directions count=%zu",
185 const std::vector<VECTOR2I>& directions = snapLineManager.
GetDirections();
188 if( activeDirection )
189 wxLogTrace(
traceSnap,
" activeDirection=%d", *activeDirection );
195 std::optional<VECTOR2I> bestPoint;
196 double bestPerp = std::numeric_limits<double>::max();
197 double bestDistance = std::numeric_limits<double>::max();
199 for(
size_t ii = 0; ii < directions.size(); ++ii )
201 const VECTOR2I& dir = directions[ii];
205 if( dirLength == 0.0 )
207 wxLogTrace(
traceSnap,
" Direction %zu: zero length, skipping", ii );
211 VECTOR2D dirUnit = dirVector / dirLength;
213 double distanceAlong =
delta.Dot( dirUnit );
214 VECTOR2D projection = originVec + dirUnit * distanceAlong;
218 double snapThreshold = aSnapRange;
220 if( activeDirection && *activeDirection ==
static_cast<int>( ii ) )
222 snapThreshold *= 1.5;
223 wxLogTrace(
traceSnap,
" Direction %zu: ACTIVE, increased snapThreshold=%.1f", ii, snapThreshold );
226 wxLogTrace(
traceSnap,
" Direction %zu: dir=(%d, %d), perpDist=%.1f, threshold=%.1f",
227 ii, dir.
x, dir.
y, perpDistance, snapThreshold );
229 if( perpDistance > snapThreshold )
231 wxLogTrace(
traceSnap,
" perpDistance > threshold, skipping" );
239 if( dir.
x == 0 && dir.
y != 0 )
242 candidate.
x = origin.
x;
243 candidate.
y = aNearestGrid.
y;
244 wxLogTrace(
traceSnap,
" Vertical snap: candidate=(%d, %d)",
245 (
int)candidate.
x, (
int)candidate.
y );
247 else if( dir.
y == 0 && dir.
x != 0 )
250 candidate.
x = aNearestGrid.
x;
251 candidate.
y = origin.
y;
252 wxLogTrace(
traceSnap,
" Horizontal snap: candidate=(%d, %d)",
253 (
int)candidate.
x, (
int)candidate.
y );
265 VECTOR2D relProjection = projection - gridOrigin;
268 std::vector<VECTOR2D> gridPoints;
269 for(
int dx = -1; dx <= 1; ++dx )
271 for(
int dy = -1; dy <= 1; ++dy )
273 double gridX = std::round( relProjection.
x / aGrid.
x ) * aGrid.
x + dx * aGrid.
x;
274 double gridY = std::round( relProjection.
y / aGrid.
y ) * aGrid.
y + dy * aGrid.
y;
275 gridPoints.push_back(
VECTOR2D( gridX + gridOrigin.
x, gridY + gridOrigin.
y ) );
280 double bestGridDist = std::numeric_limits<double>::max();
283 for(
const VECTOR2D& gridPt : gridPoints )
286 VECTOR2D gridDelta = gridPt - originVec;
287 double gridDistAlong = gridDelta.
Dot( dirUnit );
288 VECTOR2D gridProjection = originVec + dirUnit * gridDistAlong;
289 double gridPerpDist = ( gridPt - gridProjection ).EuclideanNorm();
292 double distFromCursor = ( gridPt - cursorVec ).EuclideanNorm();
295 double score = gridPerpDist + distFromCursor * 0.1;
297 if( score < bestGridDist )
299 bestGridDist = score;
304 candidate = bestGridPt;
305 wxLogTrace(
traceSnap,
" Diagonal snap: candidate=(%.1f, %.1f), perpDist=%.1f",
306 candidate.
x, candidate.
y, bestGridDist );
311 wxLogTrace(
traceSnap,
" Grid disabled, using projection candidate=(%.1f, %.1f)",
312 candidate.
x, candidate.
y );
319 wxLogTrace(
traceSnap,
" candidateInt matches m_skipPoint, skipping" );
323 VECTOR2D candidateDelta( candidateInt.
x - aPoint.
x, candidateInt.
y - aPoint.
y );
326 wxLogTrace(
traceSnap,
" candidateInt=(%d, %d), candidateDist=%.1f",
327 candidateInt.
x, candidateInt.
y, candidateDistance );
329 if( perpDistance < bestPerp
330 || (
std::abs( perpDistance - bestPerp ) < 1e-9 && candidateDistance < bestDistance ) )
332 wxLogTrace(
traceSnap,
" NEW BEST: perpDist=%.1f, candDist=%.1f", perpDistance, candidateDistance );
333 bestPerp = perpDistance;
334 bestDistance = candidateDistance;
335 bestPoint = candidateInt;
341 wxLogTrace(
traceSnap,
" RETURNING bestPoint=(%d, %d)", bestPoint->x, bestPoint->y );
345 wxLogTrace(
traceSnap,
" RETURNING nullopt (no valid snap found)" );
454 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.
A color representation with 4 components: red, green, blue, alpha.
const VECTOR2D & GetGridOrigin() const
bool GetGridSnapping() const
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
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.
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
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