KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_sch_api_hittest.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 The 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
20
#include <boost/test/unit_test.hpp>
21
22
#include <
api/api_utils.h
>
23
#include <api/common/types/base_types.pb.h>
24
#include <
base_units.h
>
25
#include <
sch_line.h
>
26
35
36
BOOST_AUTO_TEST_SUITE
( ApiSchHitTest )
37
38
39
BOOST_AUTO_TEST_CASE
( UnpackVector2RoundTripsThroughSchIuScale )
40
{
41
const
VECTOR2I
nativeSchIu( 50000, 75000 );
// 5 mm x 7.5 mm in schematic IU
42
43
kiapi::common::types::Vector2 wire;
44
kiapi::common::PackVector2
( wire, nativeSchIu,
schIUScale
);
45
46
BOOST_CHECK_EQUAL
( wire.x_nm(),
static_cast<
int64_t
>
( 5'000'000 ) );
47
BOOST_CHECK_EQUAL
( wire.y_nm(),
static_cast<
int64_t
>
( 7'500'000 ) );
48
49
VECTOR2I
roundTrip =
kiapi::common::UnpackVector2
( wire,
schIUScale
);
50
BOOST_CHECK_EQUAL
( roundTrip.
x
, nativeSchIu.
x
);
51
BOOST_CHECK_EQUAL
( roundTrip.
y
, nativeSchIu.
y
);
52
}
53
54
55
BOOST_AUTO_TEST_CASE
( UnpackVector2WithWrongScaleYieldsWrongPosition )
56
{
57
const
VECTOR2I
nativeSchIu( 50000, 75000 );
58
59
kiapi::common::types::Vector2 wire;
60
kiapi::common::PackVector2
( wire, nativeSchIu,
schIUScale
);
61
62
VECTOR2I
asPcb =
kiapi::common::UnpackVector2
( wire,
pcbIUScale
);
63
64
BOOST_CHECK_EQUAL
( asPcb.
x
, 5'000'000 );
65
BOOST_CHECK_EQUAL
( asPcb.
y
, 7'500'000 );
66
BOOST_CHECK_NE( asPcb.
x
, nativeSchIu.
x
);
67
}
68
69
70
BOOST_AUTO_TEST_CASE
( HitTestHitsWhenPositionUnpackedWithSchIuScale )
71
{
72
SCH_LINE
line;
73
line.
SetStartPoint
(
VECTOR2I
( 100000, 100000 ) );
74
line.
SetEndPoint
(
VECTOR2I
( 200000, 100000 ) );
75
76
VECTOR2I
onLine( 150000, 100000 );
77
78
kiapi::common::types::Vector2 wire;
79
kiapi::common::PackVector2
( wire, onLine,
schIUScale
);
80
81
VECTOR2I
correct =
kiapi::common::UnpackVector2
( wire,
schIUScale
);
82
BOOST_CHECK( line.
HitTest
( correct, 0 ) );
83
84
VECTOR2I
broken =
kiapi::common::UnpackVector2
( wire,
pcbIUScale
);
85
BOOST_CHECK( !line.
HitTest
( broken, 0 ) );
86
}
87
88
89
BOOST_AUTO_TEST_CASE
( ToleranceScalesFromNanometersToSchIu )
90
{
91
SCH_LINE
line;
92
line.
SetStartPoint
(
VECTOR2I
( 0, 0 ) );
93
line.
SetEndPoint
(
VECTOR2I
( 100000, 0 ) );
94
95
// Sit outside the line by more than pen-width/2 but within 2 schematic IU. Proves the
96
// tolerance argument actually reaches HitTest at the intended magnitude.
97
VECTOR2I
offLine( 50000, line.
GetPenWidth
() / 2 + 2 );
98
99
const
int
toleranceNm = 10 * 100;
// 10 schematic IU worth, expressed in nm (100 nm/IU)
100
const
int
toleranceSchIu =
schIUScale
.NmToIU( toleranceNm );
101
102
BOOST_CHECK_EQUAL
( toleranceSchIu, 10 );
103
BOOST_CHECK( line.
HitTest
( offLine, toleranceSchIu ) );
104
BOOST_CHECK( !line.
HitTest
( offLine, 0 ) );
105
}
106
107
108
BOOST_AUTO_TEST_SUITE_END
()
api_utils.h
base_units.h
schIUScale
constexpr EDA_IU_SCALE schIUScale
Definition
base_units.h:127
pcbIUScale
constexpr EDA_IU_SCALE pcbIUScale
Definition
base_units.h:125
SCH_LINE
Segment description base class to describe items which have 2 end points (track, wire,...
Definition
sch_line.h:42
SCH_LINE::GetPenWidth
int GetPenWidth() const override
Definition
sch_line.cpp:394
SCH_LINE::SetStartPoint
void SetStartPoint(const VECTOR2I &aPosition)
Definition
sch_line.h:140
SCH_LINE::HitTest
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition
sch_line.cpp:857
SCH_LINE::SetEndPoint
void SetEndPoint(const VECTOR2I &aPosition)
Definition
sch_line.h:149
VECTOR2::x
T x
Definition
vector2d.h:79
VECTOR2::y
T y
Definition
vector2d.h:79
kiapi::common::UnpackVector2
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
Definition
api_utils.cpp:123
kiapi::common::PackVector2
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
Definition
api_utils.cpp:116
sch_line.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
Definition
test_api_enums.cpp:71
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(UnpackVector2RoundTripsThroughSchIuScale)
Regression tests for the IU-scale handling in API_HANDLER_EDITOR::handleHitTest.
Definition
test_sch_api_hittest.cpp:39
BOOST_CHECK_EQUAL
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2I
VECTOR2< int32_t > VECTOR2I
Definition
vector2d.h:687
src
qa
tests
eeschema
test_sch_api_hittest.cpp
Generated on Sun Apr 26 2026 00:06:58 for KiCad PCB EDA Suite by
1.13.2