KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_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
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
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 */
17
19
21#include <geometry/seg.h>
22
23
24BOOST_AUTO_TEST_SUITE( VectorUtils )
25
26BOOST_AUTO_TEST_CASE( PointIsInDirection )
27{
28 const VECTOR2I p0( 1000, 1000 );
29
30 const VECTOR2I ne( 100, 100 );
31 const VECTOR2I n( 0, 100 );
32 const VECTOR2I s( 0, -100 );
33
34 // To the east, so not directly inline with ne from p0
35 const VECTOR2I p1_east = p0 + VECTOR2I( 1000, 0 );
36 const VECTOR2I p1_west = p0 - VECTOR2I( 1000, 0 );
37
38 BOOST_TEST( KIGEOM::PointIsInDirection( p1_east, ne, p0 ) );
39 BOOST_TEST( !KIGEOM::PointIsInDirection( p1_west, ne, p0 ) );
40
41 // Test the perpendicular corner case
42
43 // Points on both sides are not in the direction
44 BOOST_TEST( !KIGEOM::PointIsInDirection( p1_east, n, p0 ) );
45 BOOST_TEST( !KIGEOM::PointIsInDirection( p1_west, n, p0 ) );
46
47 // And they're also not in the opposite direction
48 BOOST_TEST( !KIGEOM::PointIsInDirection( p1_east, s, p0 ) );
49 BOOST_TEST( !KIGEOM::PointIsInDirection( p1_west, s, p0 ) );
50}
51
52BOOST_AUTO_TEST_CASE( ProjectsOntoSeg )
53{
54 const SEG seg( VECTOR2I( 0, 0 ), VECTOR2I( 100, 0 ) );
55
56 BOOST_TEST( KIGEOM::PointProjectsOntoSegment( VECTOR2I( 50, 1000 ), seg ) );
57 BOOST_TEST( !KIGEOM::PointProjectsOntoSegment( VECTOR2I( 150, 1000 ), seg ) );
58}
59
61{
62 const SEG seg( VECTOR2I( 0, 0 ), VECTOR2I( 100, 0 ) );
63
64 BOOST_TEST( KIGEOM::GetLengthRatioFromStart( VECTOR2I( 0, 0 ), seg ) == 0.0 );
65 BOOST_TEST( KIGEOM::GetLengthRatioFromStart( VECTOR2I( 100, 0 ), seg ) == 1.0 );
66 BOOST_TEST( KIGEOM::GetLengthRatioFromStart( VECTOR2I( 50, 0 ), seg ) == 0.5 );
67
68 // Points not on the segment also work
69 BOOST_CHECK_CLOSE( KIGEOM::GetLengthRatioFromStart( VECTOR2I( 0, 100 ), seg ), 1.0, 0.0001 );
70 BOOST_CHECK_CLOSE( KIGEOM::GetLengthRatioFromStart( VECTOR2I( 0, 50 ), seg ), 0.5, 0.0001 );
71}
72
73BOOST_AUTO_TEST_CASE( NearestEndpoint )
74{
75 struct PtCase
76 {
77 VECTOR2I Point;
78 bool ExpectedStart;
79 };
80
81 const SEG seg( VECTOR2I( 0, 0 ), VECTOR2I( 100, 0 ) );
82
83 const std::vector<PtCase> cases{
84 { { -100, 0 }, true },
85 { { 0, 0 }, true },
86 { { 0, 50 }, true },
87 // Make sure the tie breaks predictably
88 // Equidistant points -> start
89 { { 50, 0 }, true },
90 { { 50, 50 }, true },
91 { { 50, -50 }, true },
92 // End points
93 { { 200, 0 }, false },
94 { { 100, 0 }, false },
95 { { 100, 50 }, false },
96 };
97
98 for( const PtCase& pc : cases )
99 {
100 BOOST_TEST_INFO( "Point: " << pc.Point );
101 BOOST_TEST( KIGEOM::GetNearestEndpoint( seg, pc.Point )
102 == ( pc.ExpectedStart ? seg.A : seg.B ) );
103 }
104}
105
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
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.
const VECTOR2I & GetNearestEndpoint(const SEG &aSeg, const VECTOR2I &aPoint)
Get the nearest end of a segment to a point.
bool PointIsInDirection(const VECTOR2< T > &aPoint, const VECTOR2< T > &aDirection, const VECTOR2< T > &aFrom)
Definition: vector_utils.h:57
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(PointIsInDirection)
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:691
Supplemental functions for working with vectors and simple objects that interact with vectors.