KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_roundrect.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
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
25
26#include <geometry/roundrect.h>
28
29BOOST_AUTO_TEST_SUITE( Roundrect )
30
31// The polygon must fit its rectangle. One maxError of slack covers the arc
32// chords. Catches the maxed-radius circle coming out too big (#24623).
33static void CheckPolygonFitsRect( int aWidth, int aHeight, int aRadius )
34{
35 const int maxError = 100;
36 const VECTOR2I pos( 1000, 2000 );
37
38 ROUNDRECT rr( SHAPE_RECT( pos, aWidth, aHeight ), aRadius );
39
40 SHAPE_POLY_SET poly;
41 rr.TransformToPolygon( poly, maxError );
42
43 BOOST_REQUIRE_EQUAL( poly.OutlineCount(), 1 );
44
45 const BOX2I bbox = poly.BBox();
46 const int tol = maxError;
47
48 BOOST_CHECK_LE( std::abs( bbox.GetLeft() - pos.x ), tol );
49 BOOST_CHECK_LE( std::abs( bbox.GetTop() - pos.y ), tol );
50 BOOST_CHECK_LE( std::abs( bbox.GetWidth() - aWidth ), tol );
51 BOOST_CHECK_LE( std::abs( bbox.GetHeight() - aHeight ), tol );
52}
53
54// Radius is half of both sides, so it becomes a circle (#24623).
55BOOST_AUTO_TEST_CASE( CircleFromSquare )
56{
57 CheckPolygonFitsRect( 10000000, 10000000, 5000000 );
58}
59
60// Radius maxed on the height only, giving a horizontal oval.
62{
63 CheckPolygonFitsRect( 20000000, 10000000, 5000000 );
64}
65
66// Radius maxed on the width only, giving a vertical oval.
68{
69 CheckPolygonFitsRect( 10000000, 20000000, 5000000 );
70}
71
72// Ordinary roundrect, corners below the max radius.
73BOOST_AUTO_TEST_CASE( NormalRoundrect )
74{
75 CheckPolygonFitsRect( 20000000, 10000000, 2000000 );
76}
77
78// Zero radius is a plain rectangle.
79BOOST_AUTO_TEST_CASE( PlainRectangle )
80{
81 CheckPolygonFitsRect( 20000000, 10000000, 0 );
82}
83
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr size_type GetWidth() const
Definition box2.h:210
constexpr size_type GetHeight() const
Definition box2.h:211
constexpr coord_type GetLeft() const
Definition box2.h:224
constexpr coord_type GetTop() const
Definition box2.h:225
A round rectangle shape, based on a rectangle and a radius.
Definition roundrect.h:32
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aMaxError) const
Get the polygonal representation of the roundrect.
Definition roundrect.cpp:79
Represent a set of closed polygons.
int OutlineCount() const
Return the number of outlines in the set.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(CircleFromSquare)
static void CheckPolygonFitsRect(int aWidth, int aHeight, int aRadius)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683