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
36BOOST_AUTO_TEST_SUITE( ApiSchHitTest )
37
38
39BOOST_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
50 BOOST_CHECK_EQUAL( roundTrip.x, nativeSchIu.x );
51 BOOST_CHECK_EQUAL( roundTrip.y, nativeSchIu.y );
52}
53
54
55BOOST_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
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
70BOOST_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;
80
82 BOOST_CHECK( line.HitTest( correct, 0 ) );
83
85 BOOST_CHECK( !line.HitTest( broken, 0 ) );
86}
87
88
89BOOST_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
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:127
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
int GetPenWidth() const override
Definition sch_line.cpp:394
void SetStartPoint(const VECTOR2I &aPosition)
Definition sch_line.h:140
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
void SetEndPoint(const VECTOR2I &aPosition)
Definition sch_line.h:149
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(UnpackVector2RoundTripsThroughSchIuScale)
Regression tests for the IU-scale handling in API_HANDLER_EDITOR::handleHitTest.
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687