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, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <board.h>
23#include <kiid.h>
24#include <pcb_shape.h>
27
28
29BOOST_AUTO_TEST_SUITE( PolySimplify )
30
31
32
40BOOST_AUTO_TEST_CASE( Issue22597SimplifyPolygon )
41{
42 const auto doBoardTest =
43 [&]( BOARD& aBoard )
44 {
45 PCB_SHAPE* xyPolygon = nullptr;
46 PCB_SHAPE* arcPolygon = nullptr;
47
48 for( BOARD_ITEM* item : aBoard.Drawings() )
49 {
50 if( item->Type() == PCB_SHAPE_T )
51 {
52 PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
53
54 if( shape->GetShape() == SHAPE_T::POLY )
55 {
56 SHAPE_POLY_SET& poly = shape->GetPolyShape();
57
58 if( poly.OutlineCount() > 0 )
59 {
60 SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
61
62 if( outline.ArcCount() == 0 )
63 xyPolygon = shape;
64 else if( outline.ArcCount() == 4 )
65 arcPolygon = shape;
66 }
67 }
68 }
69 }
70
71 BOOST_REQUIRE_MESSAGE( xyPolygon != nullptr,
72 "Could not find the xy-point polygon in the test file" );
73 BOOST_REQUIRE_MESSAGE( arcPolygon != nullptr,
74 "Could not find the 4-arc polygon in the test file" );
75
76 // Test the xy-point polygon - this is the original problematic case
77 {
78 SHAPE_POLY_SET& poly = xyPolygon->GetPolyShape();
79 SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
80
81 int originalPointCount = outline.PointCount();
82
83 BOOST_TEST_MESSAGE( "XY polygon original: " << originalPointCount
84 << " points, " << outline.ArcCount() << " arcs" );
85 BOOST_CHECK_EQUAL( originalPointCount, 164 );
86
87 poly.SimplifyOutlines( 2000000 );
88
89 int simplifiedCount = outline.PointCount();
90
91 BOOST_TEST_MESSAGE( "XY polygon simplified: " << simplifiedCount
92 << " points, " << outline.ArcCount() << " arcs" );
93
94 BOOST_CHECK_LT( simplifiedCount, originalPointCount );
95 BOOST_CHECK_LE( simplifiedCount, 20 );
96 }
97
98 // Test the arc-based polygon - arcs should be preserved
99 {
100 SHAPE_POLY_SET& poly = arcPolygon->GetPolyShape();
101 SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
102
103 int originalPointCount = outline.PointCount();
104 int originalArcCount = outline.ArcCount();
105
106 BOOST_TEST_MESSAGE( "Arc polygon original: " << originalPointCount
107 << " points, " << originalArcCount << " arcs" );
108 BOOST_CHECK_EQUAL( originalArcCount, 4 );
109
110 poly.SimplifyOutlines( 2000000 );
111
112 int simplifiedPointCount = outline.PointCount();
113 int simplifiedArcCount = outline.ArcCount();
114
115 BOOST_TEST_MESSAGE( "Arc polygon simplified: " << simplifiedPointCount
116 << " points, " << simplifiedArcCount << " arcs" );
117
118 // Arc polygon should remain valid and arcs should be preserved
119 BOOST_CHECK_EQUAL( simplifiedArcCount, originalArcCount );
120 BOOST_CHECK( poly.OutlineCount() > 0 );
121 }
122 };
123
124 KI_TEST::LoadAndTestBoardFile( "issue22597/issue22597", false, doBoardTest, std::nullopt );
125}
126
127
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:81
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
SHAPE_POLY_SET & GetPolyShape()
SHAPE_T GetShape() const
Definition eda_shape.h:185
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("\n=== Real-World Polygon PIP Benchmark ===\n"<< formatTable(table))
BOOST_CHECK_EQUAL(result, "25.4")
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:81