KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_footprint_realignment.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#include <boost/test/data/test_case.hpp>
22
23#include <optional>
24#include <string>
25#include <utility>
26#include <vector>
27
28#include <footprint_utils.h>
29
30#include <footprint.h>
31#include <pad.h>
32
33
35{
36 std::string m_TestName;
37 const std::vector<std::pair<std::string, VECTOR2I>>& m_OrigPadPositions;
38 const std::vector<std::pair<std::string, VECTOR2I>>& m_NewPadPositions;
39 std::optional<VECTOR2I> m_ExpectedPosShift;
40 std::optional<EDA_ANGLE> m_ExpectedAngleShift;
41
42 // Alls a data test case to print the name
43 friend std::ostream& operator<<( std::ostream& os, const FP_ALIGNMENT_TEST_CASE& testCase )
44 {
45 return os << testCase.m_TestName;
46 }
47};
48
49
51{
54
55 std::unique_ptr<FOOTPRINT> CreateFootprint( const std::vector<std::pair<std::string, VECTOR2I>>& padPositions )
56 {
57 std::unique_ptr<FOOTPRINT> fp = std::make_unique<FOOTPRINT>( nullptr );
58
59 for( const auto& [number, pos] : padPositions )
60 {
61 PAD* pad = new PAD( fp.get() );
62 pad->SetNumber( number );
63 pad->SetFPRelativePosition( pos );
64 fp->Add( pad );
65 }
66
67 return fp;
68 }
69
70
72 {
73 const std::unique_ptr<FOOTPRINT> fpOrig = CreateFootprint( testCase.m_OrigPadPositions );
74 const std::unique_ptr<FOOTPRINT> fpNew = CreateFootprint( testCase.m_NewPadPositions );
75
76 bool ok = ComputeFootprintShift( *fpOrig, *fpNew, m_PosShift, m_AngleShift );
77
78 if( testCase.m_ExpectedPosShift.has_value() && testCase.m_ExpectedAngleShift.has_value() )
79 {
80 BOOST_REQUIRE( ok );
81 BOOST_TEST( m_PosShift == testCase.m_ExpectedPosShift.value() );
82 BOOST_TEST( m_AngleShift == testCase.m_ExpectedAngleShift.value() );
83 }
84 else
85 {
86 BOOST_REQUIRE( !ok );
87 }
88
89 // Check that when the original footprint is rotated, we compute the same
90 // transform, but:
91 // - the move vector is rotated by the same angle
92 // - the angle shift is the same
93
94 fpOrig->SetOrientation( ANGLE_45 );
95
96 bool ok2 = ComputeFootprintShift( *fpOrig, *fpNew, m_PosShift, m_AngleShift );
97 if( testCase.m_ExpectedPosShift.has_value() && testCase.m_ExpectedAngleShift.has_value() )
98 {
99 VECTOR2I expectedPosShift = testCase.m_ExpectedPosShift.value();
100 RotatePoint( expectedPosShift, ANGLE_45 );
101
102 BOOST_REQUIRE( ok2 );
103 BOOST_TEST( m_PosShift == expectedPosShift );
104 BOOST_TEST( m_AngleShift == testCase.m_ExpectedAngleShift.value() );
105 }
106 else
107 {
108 BOOST_REQUIRE( !ok2 );
109 }
110 }
111};
112
113
114static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_NoPins{
115};
116
117
118static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_DIP4_OrigPin1{
119 { "1", { 0, 0 } },
120 { "2", { 0, 1000 } },
121 { "3", { 2000, 0 } },
122 { "4", { 2000, 1000 } },
123};
124
125
126static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_DIP4_OrigBodyCenter{
127 { "1", { -1000, -500 } },
128 { "2", { -1000, 500 } },
129 { "3", { 1000, -500 } },
130 { "4", { 1000, 500 } },
131};
132
133
134// Pin 1 bottom left = Level B style
135// Rotated 90 degrees CCW around origin
136static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_DIP4_Rotated90DegCCW{
137 { "1", { 0, 0 } },
138 { "2", { 1000, 0 } },
139 { "3", { 1000, -2000 } },
140 { "4", { 0, -2000 } },
141};
142
143
144static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_DIP4_OrigBodyCenter_Rotated90DegCCW{
145 { "1", { -500, 1000 } },
146 { "2", { 500, 1000 } },
147 { "3", { 500, -1000 } },
148 { "4", { -500, -1000 } },
149};
150
151
152static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_DIP4_Pin4Deleted{
153 { "1", { 0, 0 } },
154 { "2", { 0, 1000 } },
155 { "3", { 2000, 0 } },
156};
157
158
159static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_DIP4_Pin4Moved{
160 { "1", { 0, 0 } },
161 { "2", { 0, 1000 } },
162 { "3", { 2000, 0 } },
163 { "4", { 2100, 1000 } },
164};
165
166
167static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_DIP4_Widened{
168 { "1", { 0, 0 } },
169 { "2", { 0, 1000 } },
170 { "3", { 2400, 0 } },
171 { "4", { 2400, 1000 } },
172};
173
174
175static const std::vector<std::pair<std::string, VECTOR2I>> FpDef_DIP4_WithMPs{
176 { "1", { 0, 0 } },
177 { "2", { 0, 1000 } },
178 { "3", { 2000, 0 } },
179 { "4", { 2000, 1000 } },
180 { "MP", { -1000, 500 } },
181 { "MP", { 3000, 500 } },
182};
183
184
185static const std::vector<FP_ALIGNMENT_TEST_CASE> FpAlignmentTestCases = {
186 {
187 "No pins",
190 std::nullopt,
191 std::nullopt,
192 },
193 {
194 "Origin point change",
197 VECTOR2I( 1000, 500 ),
198 ANGLE_0,
199 },
200 {
201 "Rotation",
204 VECTOR2I( 0, 0 ),
205 ANGLE_270,
206 },
207 {
208 "Rotation and origin point change",
211 VECTOR2I( 1000, 500 ),
212 ANGLE_270,
213 },
214 {
215 "Pin deleted",
218 VECTOR2I( 0, 0 ),
219 ANGLE_0,
220 },
221 {
222 "Pin moved",
225 VECTOR2I( 0, 0 ),
226 ANGLE_0,
227 },
228 {
229 "Footprint widened (all pins moved)",
232 VECTOR2I( -200, 0 ),
233 ANGLE_0,
234 },
235 {
236 "Ignore same named pads",
239 VECTOR2I( 0, 0 ),
240 ANGLE_0,
241 }
242};
243
244
245BOOST_FIXTURE_TEST_SUITE( FootprintRealignment, FP_REALIGNMENT_TEST_FIXTURE )
246
247
249 boost::unit_test::data::make( FpAlignmentTestCases ),
250 testCase )
251{
252 ExecuteTestCase( testCase );
253}
254
Definition pad.h:61
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
static constexpr EDA_ANGLE ANGLE_45
Definition eda_angle.h:412
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:416
bool ComputeFootprintShift(const FOOTPRINT &aExisting, const FOOTPRINT &aNew, VECTOR2I &aShift, EDA_ANGLE &aAngleShift)
Compute position and angle shift between two footprints.
Collection of reusable/testable functions for footprint manipulation.
std::optional< EDA_ANGLE > m_ExpectedAngleShift
std::optional< VECTOR2I > m_ExpectedPosShift
friend std::ostream & operator<<(std::ostream &os, const FP_ALIGNMENT_TEST_CASE &testCase)
const std::vector< std::pair< std::string, VECTOR2I > > & m_OrigPadPositions
const std::vector< std::pair< std::string, VECTOR2I > > & m_NewPadPositions
void ExecuteTestCase(const FP_ALIGNMENT_TEST_CASE &testCase)
std::unique_ptr< FOOTPRINT > CreateFootprint(const std::vector< std::pair< std::string, VECTOR2I > > &padPositions)
BOOST_DATA_TEST_CASE(ConvertToKicadUnit, boost::unit_test::data::make(altium_to_kicad_unit), input_value, expected_result)
Test conversation from Altium internal units into KiCad internal units.
static const std::vector< FP_ALIGNMENT_TEST_CASE > FpAlignmentTestCases
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_DIP4_Widened
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_DIP4_OrigBodyCenter_Rotated90DegCCW
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_DIP4_Rotated90DegCCW
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_DIP4_Pin4Deleted
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_DIP4_OrigBodyCenter
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_DIP4_Pin4Moved
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_NoPins
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_DIP4_OrigPin1
static const std::vector< std::pair< std::string, VECTOR2I > > FpDef_DIP4_WithMPs
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_TEST(netlist.find("R_G1 ARM_OUT1 DIE_B R='0.001 / ((SW_STATE)") !=std::string::npos)
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:225
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683