KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_poly_simplify.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 <board.h>
27#include <kiid.h>
28#include <pcb_shape.h>
31
32
33BOOST_AUTO_TEST_SUITE( PolySimplify )
34
35
36
44BOOST_AUTO_TEST_CASE( Issue22597SimplifyPolygon )
45{
46 const auto doBoardTest =
47 [&]( BOARD& aBoard )
48 {
49 PCB_SHAPE* xyPolygon = nullptr;
50 PCB_SHAPE* arcPolygon = nullptr;
51
52 for( BOARD_ITEM* item : aBoard.Drawings() )
53 {
54 if( item->Type() == PCB_SHAPE_T )
55 {
56 PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
57
58 if( shape->GetShape() == SHAPE_T::POLY )
59 {
60 SHAPE_POLY_SET& poly = shape->GetPolyShape();
61
62 if( poly.OutlineCount() > 0 )
63 {
64 SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
65
66 if( outline.ArcCount() == 0 )
67 xyPolygon = shape;
68 else if( outline.ArcCount() == 4 )
69 arcPolygon = shape;
70 }
71 }
72 }
73 }
74
75 BOOST_REQUIRE_MESSAGE( xyPolygon != nullptr,
76 "Could not find the xy-point polygon in the test file" );
77 BOOST_REQUIRE_MESSAGE( arcPolygon != nullptr,
78 "Could not find the 4-arc polygon in the test file" );
79
80 // Test the xy-point polygon - this is the original problematic case
81 {
82 SHAPE_POLY_SET& poly = xyPolygon->GetPolyShape();
83 SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
84
85 int originalPointCount = outline.PointCount();
86
87 BOOST_TEST_MESSAGE( "XY polygon original: " << originalPointCount
88 << " points, " << outline.ArcCount() << " arcs" );
89 BOOST_CHECK_EQUAL( originalPointCount, 164 );
90
91 poly.SimplifyOutlines( 2000000 );
92
93 int simplifiedCount = outline.PointCount();
94
95 BOOST_TEST_MESSAGE( "XY polygon simplified: " << simplifiedCount
96 << " points, " << outline.ArcCount() << " arcs" );
97
98 BOOST_CHECK_LT( simplifiedCount, originalPointCount );
99 BOOST_CHECK_LE( simplifiedCount, 20 );
100 }
101
102 // Test the arc-based polygon - arcs should be preserved
103 {
104 SHAPE_POLY_SET& poly = arcPolygon->GetPolyShape();
105 SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
106
107 int originalPointCount = outline.PointCount();
108 int originalArcCount = outline.ArcCount();
109
110 BOOST_TEST_MESSAGE( "Arc polygon original: " << originalPointCount
111 << " points, " << originalArcCount << " arcs" );
112 BOOST_CHECK_EQUAL( originalArcCount, 4 );
113
114 poly.SimplifyOutlines( 2000000 );
115
116 int simplifiedPointCount = outline.PointCount();
117 int simplifiedArcCount = outline.ArcCount();
118
119 BOOST_TEST_MESSAGE( "Arc polygon simplified: " << simplifiedPointCount
120 << " points, " << simplifiedArcCount << " arcs" );
121
122 // Arc polygon should remain valid and arcs should be preserved
123 BOOST_CHECK_EQUAL( simplifiedArcCount, originalArcCount );
124 BOOST_CHECK( poly.OutlineCount() > 0 );
125 }
126 };
127
128 KI_TEST::LoadAndTestBoardFile( "issue22597/issue22597", false, doBoardTest, std::nullopt );
129}
130
131
General utilities for PCB file IO for QA programs.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
SHAPE_POLY_SET & GetPolyShape()
Definition eda_shape.h:337
SHAPE_T GetShape() const
Definition eda_shape.h:169
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
size_t ArcCount() const
Represent a set of closed polygons.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
void SimplifyOutlines(int aMaxError=0)
Simplifies the lines in the polyset.
int OutlineCount() const
Return the number of outlines in the set.
void LoadAndTestBoardFile(const wxString aRelativePath, bool aRoundtrip, std::function< void(BOARD &)> aBoardTestFunction, std::optional< int > aExpectedBoardVersion)
Perform "some test" on a board file loaded from the path, then optionally save and reload and run the...
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST_MESSAGE("Polyline has "<< chain.PointCount()<< " points")
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:88