KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vector_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 (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <geometry/seg.h>
23
24using namespace KIGEOM;
25
26bool KIGEOM::SegIsInDirection( const SEG& aSeg, const VECTOR2I& aDirection )
27{
28 return PointIsInDirection( aSeg.B, aDirection, aSeg.A );
29};
30
31
32bool KIGEOM::PointProjectsOntoSegment( const VECTOR2I& aPoint, const SEG& aSeg )
33{
34 // SEG::NearestPoint returns the end points if the projection is
35 // outside the segment
36 const VECTOR2I projected = aSeg.NearestPoint( aPoint );
37 return projected != aSeg.A && projected != aSeg.B;
38}
39
40
41double KIGEOM::GetLengthRatioFromStart( const VECTOR2I& aPoint, const SEG& aSeg )
42{
43 const double length = aSeg.Length();
44 const double projected_length = ( aPoint - aSeg.A ).EuclideanNorm();
45 return projected_length / length;
46}
47
48
49double KIGEOM::GetProjectedPointLengthRatio( const VECTOR2I& aPoint, const SEG& aSeg )
50{
51 const VECTOR2I projected = aSeg.NearestPoint( aPoint );
52
53 if( projected == aSeg.A )
54 return 0.0;
55 if( projected == aSeg.B )
56 return 1.0;
57
58 return GetLengthRatioFromStart( projected, aSeg );
59}
60
61
62const VECTOR2I& KIGEOM::GetNearestEndpoint( const SEG& aSeg, const VECTOR2I& aPoint )
63{
64 const double distToCBStart = aSeg.A.Distance( aPoint );
65 const double distToCBEnd = aSeg.B.Distance( aPoint );
66 return ( distToCBStart <= distToCBEnd ) ? aSeg.A : aSeg.B;
67}
68
69template <typename T>
70static constexpr T RoundNearest( T x, T g )
71{
72 return ( x + ( x < 0 ? -g / 2 : g / 2 ) ) / g * g;
73}
74
75template <typename T>
76static constexpr T RoundDown( T x, T g )
77{
78 return ( ( x < 0 ? ( x - g + 1 ) : x ) / g ) * g;
79}
80
81template <typename T>
82static constexpr T RoundUp( T x, T g )
83{
84 return ( ( x < 0 ? x : ( x + g - 1 ) ) / g ) * g;
85}
86
87VECTOR2I KIGEOM::RoundGrid( const VECTOR2I& aVec, int aGridSize )
88{
89 return VECTOR2I( RoundNearest( aVec.x, aGridSize ), RoundNearest( aVec.y, aGridSize ) );
90}
91
92VECTOR2I KIGEOM::RoundNW( const VECTOR2I& aVec, int aGridSize )
93{
94 return VECTOR2I( RoundDown( aVec.x, aGridSize ), RoundDown( aVec.y, aGridSize ) );
95}
96
97VECTOR2I KIGEOM::RoundSE( const VECTOR2I& aVec, int aGridSize )
98{
99 return VECTOR2I( RoundUp( aVec.x, aGridSize ), RoundUp( aVec.y, aGridSize ) );
100}
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.cpp:327
int Length() const
Return the length (this).
Definition: seg.h:333
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition: vector2d.h:557
Definition: oval.h:105
bool PointProjectsOntoSegment(const VECTOR2I &aPoint, const SEG &aSeg)
Determine if a point projects onto a segment.
double GetLengthRatioFromStart(const VECTOR2I &aPoint, const SEG &aSeg)
Get the ratio of the vector to a point from the segment's start, compared to the segment's length.
bool SegIsInDirection(const SEG &aSeg, const VECTOR2I &aDirection)
Determine if a segment's vector is within 90 degrees of a given direction.
VECTOR2I RoundNW(const VECTOR2I &aVec, int aGridSize)
Round a vector to the nearest grid point in the NW direction.
VECTOR2I RoundSE(const VECTOR2I &aVec, int aGridSize)
Round a vector to the nearest grid point in the SE direction.
const VECTOR2I & GetNearestEndpoint(const SEG &aSeg, const VECTOR2I &aPoint)
Get the nearest end of a segment to a point.
double GetProjectedPointLengthRatio(const VECTOR2I &aPoint, const SEG &aSeg)
Get the ratio of the vector to a point projected onto a segment from the start, relative to the segme...
VECTOR2I RoundGrid(const VECTOR2I &aVec, int aGridSize)
Round a vector to the nearest grid point in any direction.
bool PointIsInDirection(const VECTOR2< T > &aPoint, const VECTOR2< T > &aDirection, const VECTOR2< T > &aFrom)
Definition: vector_utils.h:57
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
static constexpr T RoundDown(T x, T g)
static constexpr T RoundUp(T x, T g)
static constexpr T RoundNearest(T x, T g)
Supplemental functions for working with vectors and simple objects that interact with vectors.