KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_eda_shape.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) 2023 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 3
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 */
17
18
20
21#include <eda_shape.h>
22#include <qa_utils/geometry/geometry.h> // For KI_TEST::IsVecWithinTol
23#include <geometry/shape_arc.h> // For SHAPE_ARC::DefaultAccuracyForPCB()
24
25
26BOOST_AUTO_TEST_SUITE( EdaShape )
27
29{
30public:
31 EDA_SHAPE_MOCK( SHAPE_T aShapeType ) : EDA_SHAPE( aShapeType, 0, FILL_T::NO_FILL ){};
32};
33
34
36{
37 std::string m_CaseName;
40 double m_Angle;
43};
44
45
46static const std::vector<SET_ANGLE_END_CASE> set_angle_end_cases =
47{
48 {
49 "Issue 13626: clockwise semicircle",
50 {-428880000, 117229160 },
51 {-430060565, 113472820 },
52 180.0,
53 {-431241130, 109716480 },
54 false
55 },
56 {
57 "Issue 13626: anticlockwise arc",
58 { -431241130, 109716480 },
59 { -434923630, 112954230 },
60 -138.46654568595355,
61 { -439827050, 112936200 },
62 true
63 }
64};
65
66
67BOOST_AUTO_TEST_CASE( SetAngleAndEnd )
68{
69 for( const auto& c : set_angle_end_cases )
70 {
71 BOOST_TEST_INFO_SCOPE( c.m_CaseName );
72
74 shape.SetStart( c.m_Start );
75 shape.SetCenter( c.m_Center );
76
77 shape.SetArcAngleAndEnd( EDA_ANGLE( c.m_Angle, DEGREES_T ), true );
78
79 BOOST_CHECK_EQUAL( shape.EndsSwapped(), c.m_ExpectedStartEndSwapped );
80
81 const VECTOR2I newEnd = shape.EndsSwapped() ? shape.GetStart() : shape.GetEnd();
82
83 BOOST_CHECK_PREDICATE(
84 KI_TEST::IsVecWithinTol<VECTOR2I>,
85 (newEnd) ( c.m_ExpectedEndBeforeSwap ) ( SHAPE_ARC::DefaultAccuracyForPCB() ) );
86 }
87}
88
89
91{
92 std::string m_CaseName;
101};
102
103static const std::vector<SET_ARC_GEOMETRY_CASE> set_arc_geometry_cases = {
104 {
105 // Test that when setting an arc by start/mid/end, the winding
106 // direction is correctly determined (in 15694, this was in FP_SHAPE,
107 // but the logic has since been merged with EDA_SHAPE).
108 "Issue 15694: clockwise arc",
109 { 10000000, 0 },
110 { 0, 10000000 },
111 { -10000000, 0 },
112 { 0, 0 },
113 10000000,
114 false,
115 { -10000000, 0 }, // unchanged
116 180.0,
117 },
118 {
119 "Issue 15694: anticlockwise arc",
120 { -10000000, 0 },
121 { 0, 10000000 },
122 { 10000000, 0 },
123 { 0, 0 },
124 10000000,
125 true,
126 { 10000000, 0 }, // the start is the end after swapping
127 180.0, // angle is positive after swapping
128 },
129};
130
131BOOST_AUTO_TEST_CASE( SetArcGeometry )
132{
133 const double angle_tol = 0.1;
134
135 for( const auto& c : set_arc_geometry_cases )
136 {
137 BOOST_TEST_INFO_SCOPE( c.m_CaseName );
138
140
141 shape.SetArcGeometry( c.m_Start, c.m_Mid, c.m_End );
142
143 const VECTOR2I center = shape.getCenter();
144
145 BOOST_CHECK_PREDICATE(
146 KI_TEST::IsVecWithinTol<VECTOR2I>,
147 (center) ( c.m_ExpectedCenter ) ( SHAPE_ARC::DefaultAccuracyForPCB() ) );
148
149 const int radius = shape.GetRadius();
150
151 BOOST_CHECK_PREDICATE(
152 KI_TEST::IsWithin<int>,
153 (radius) ( c.m_ExpectedRadius ) ( SHAPE_ARC::DefaultAccuracyForPCB() ) );
154
155 BOOST_CHECK_EQUAL( shape.EndsSwapped(), c.m_ExpectedStartEndSwapped );
156
157 const VECTOR2I newEnd = shape.EndsSwapped() ? shape.GetStart() : shape.GetEnd();
158
159 BOOST_CHECK_PREDICATE(
160 KI_TEST::IsVecWithinTol<VECTOR2I>,
161 (newEnd) ( c.m_ExpectedEndAfterSwap ) ( SHAPE_ARC::DefaultAccuracyForPCB() ) );
162
163 const EDA_ANGLE angle = shape.GetArcAngle();
164
165 BOOST_CHECK_PREDICATE(
166 KI_TEST::IsWithinWrapped<double>,
167 ( angle.AsDegrees() )( c.m_ExpectedAngleAfterSwapDeg )( 360.0 )( angle_tol ) );
168
169 // Check that the centre is still correct
170 }
171}
172
double AsDegrees() const
Definition: eda_angle.h:113
EDA_SHAPE_MOCK(SHAPE_T aShapeType)
EDA_ANGLE GetArcAngle() const
Definition: eda_shape.cpp:896
void SetCenter(const VECTOR2I &aCenter)
Definition: eda_shape.cpp:778
VECTOR2I getCenter() const
Definition: eda_shape.cpp:752
int GetRadius() const
Definition: eda_shape.cpp:826
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:174
void SetStart(const VECTOR2I &aStart)
Definition: eda_shape.h:141
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:137
bool EndsSwapped() const
Have the start and end points been swapped since they were set?
Definition: eda_shape.h:234
void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Set the three controlling points for an arc.
Definition: eda_shape.cpp:858
void SetArcAngleAndEnd(const EDA_ANGLE &aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
Definition: eda_shape.cpp:924
static double DefaultAccuracyForPCB()
Definition: shape_arc.h:232
@ DEGREES_T
Definition: eda_angle.h:31
SHAPE_T
Definition: eda_shape.h:43
@ ARC
use RECTANGLE instead of RECT to avoid collision in a Windows header
FILL_T
Definition: eda_shape.h:56
std::string m_CaseName
VECTOR2I m_ExpectedEndBeforeSwap
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
static const std::vector< SET_ARC_GEOMETRY_CASE > set_arc_geometry_cases
BOOST_AUTO_TEST_CASE(SetAngleAndEnd)
static const std::vector< SET_ANGLE_END_CASE > set_angle_end_cases
BOOST_AUTO_TEST_SUITE_END()