KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_box2.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) 2022 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 2
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
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, you may find one here:
18
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19
* or you may search the http://www.gnu.org website for the version 2 license,
20
* or you may write to the Free Software Foundation, Inc.,
21
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22
*/
23
28
#include <
qa_utils/wx_utils/unit_test_utils.h
>
29
30
// Code under test
31
#include <
math/box2.h
>
32
36
BOOST_AUTO_TEST_SUITE
( BOX2TESTS )
37
38
BOOST_AUTO_TEST_CASE
( test_closest_point_to, *boost::unit_test::tolerance( 0.000001 ) )
39
{
40
BOX2D
box(
VECTOR2D
( 1, 2 ),
VECTOR2D
( 3, 4 ) );
41
42
// check all quadrants
43
44
// top left
45
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 0, 0 ) ) ==
VECTOR2D
( 1, 2 ) );
46
47
// top
48
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 2, 0 ) ) ==
VECTOR2D
( 2, 2 ) );
49
50
// top right
51
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 6, 0 ) ) ==
VECTOR2D
( 4, 2 ) );
52
53
// right
54
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 6, 5 ) ) ==
VECTOR2D
( 4, 5 ) );
55
56
// bottom right
57
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 6, 7 ) ) ==
VECTOR2D
( 4, 6 ) );
58
59
// bottom
60
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 3, 7 ) ) ==
VECTOR2D
( 3, 6 ) );
61
62
// bottom left
63
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 0, 7 ) ) ==
VECTOR2D
( 1, 6 ) );
64
65
// left
66
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 0, 3 ) ) ==
VECTOR2D
( 1, 3 ) );
67
68
// inside
69
BOOST_CHECK
( box.ClosestPointTo(
VECTOR2D
( 2, 4 ) ) ==
VECTOR2D
( 2, 4 ) );
70
}
71
72
BOOST_AUTO_TEST_CASE
( test_farthest_point_to, *boost::unit_test::tolerance( 0.000001 ) )
73
{
74
BOX2D
box(
VECTOR2D
( 1, 2 ),
VECTOR2D
( 3, 4 ) );
75
76
// note: the farthest point always is on a corner of the box
77
78
// outside:
79
80
// top left
81
BOOST_CHECK
( box.FarthestPointTo(
VECTOR2D
( 0, 0 ) ) ==
VECTOR2D
( 4, 6 ) );
82
83
// top right
84
BOOST_CHECK
( box.FarthestPointTo(
VECTOR2D
( 6, 0 ) ) ==
VECTOR2D
( 1, 6 ) );
85
86
// bottom right
87
BOOST_CHECK
( box.FarthestPointTo(
VECTOR2D
( 6, 7 ) ) ==
VECTOR2D
( 1, 2 ) );
88
89
// bottom left
90
BOOST_CHECK
( box.FarthestPointTo(
VECTOR2D
( 0, 7 ) ) ==
VECTOR2D
( 4, 2 ) );
91
92
// inside:
93
94
// top left
95
BOOST_CHECK
( box.FarthestPointTo(
VECTOR2D
( 2, 3 ) ) ==
VECTOR2D
( 4, 6 ) );
96
97
// top right
98
BOOST_CHECK
( box.FarthestPointTo(
VECTOR2D
( 3, 3 ) ) ==
VECTOR2D
( 1, 6 ) );
99
100
// bottom right
101
BOOST_CHECK
( box.FarthestPointTo(
VECTOR2D
( 3, 5 ) ) ==
VECTOR2D
( 1, 2 ) );
102
103
// bottom left
104
BOOST_CHECK
( box.FarthestPointTo(
VECTOR2D
( 2, 5 ) ) ==
VECTOR2D
( 4, 2 ) );
105
}
106
107
BOOST_AUTO_TEST_CASE
( test_intersects_circle, *boost::unit_test::tolerance( 0.000001 ) )
108
{
109
BOX2D
box(
VECTOR2D
( 1, 2 ),
VECTOR2D
( 6, 8 ) );
110
111
// box inside circle (touching corners)
112
BOOST_CHECK
( box.IntersectsCircle(
VECTOR2D
( 4, 6 ), 5 ) ==
true
);
113
114
// box completely inside circle
115
BOOST_CHECK
( box.IntersectsCircle(
VECTOR2D
( 4, 6 ), 6 ) ==
true
);
116
117
// circle completely inside box
118
BOOST_CHECK
( box.IntersectsCircle(
VECTOR2D
( 4, 6 ), 2 ) ==
true
);
119
120
// circle outside box
121
BOOST_CHECK
( box.IntersectsCircle(
VECTOR2D
( 14, 6 ), 5 ) ==
false
);
122
}
123
124
BOOST_AUTO_TEST_CASE
( test_intersects_circle_edge, *boost::unit_test::tolerance( 0.000001 ) )
125
{
126
BOX2D
box(
VECTOR2D
( 1, 2 ),
VECTOR2D
( 6, 8 ) );
127
128
// box touching edge
129
BOOST_CHECK
( box.IntersectsCircleEdge(
VECTOR2D
( 4, 6 ), 5, 1 ) ==
true
);
130
131
// box completely inside circle
132
BOOST_CHECK
( box.IntersectsCircleEdge(
VECTOR2D
( 4, 6 ), 6, 1 ) ==
false
);
133
134
// circle completely inside box
135
BOOST_CHECK
( box.IntersectsCircleEdge(
VECTOR2D
( 4, 6 ), 2, 1 ) ==
true
);
136
137
// circle outside box
138
BOOST_CHECK
( box.IntersectsCircleEdge(
VECTOR2D
( 14, 6 ), 5, 1 ) ==
false
);
139
}
140
141
BOOST_AUTO_TEST_SUITE_END()
box2.h
BOX2< VECTOR2D >
VECTOR2< double >
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
Definition:
test_api_enums.cpp:128
BOOST_CHECK
BOOST_CHECK(box.ClosestPointTo(VECTOR2D(0, 0))==VECTOR2D(1, 2))
Test suite for KiCad math code.
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
unit_test_utils.h
VECTOR2D
VECTOR2< double > VECTOR2D
Definition:
vector2d.h:587
src
qa
tests
libs
kimath
math
test_box2.cpp
Generated on Fri Apr 19 2024 00:04:56 for KiCad PCB EDA Suite by
1.9.6