KiCad PCB EDA Suite
test_eda_rect.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) 2021 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 #include <math/util.h>
26 
27 // Code under test
28 #include <eda_rect.h>
29 
30 
34 BOOST_AUTO_TEST_SUITE( EdaRect )
35 
36 
40 {
41  int width = 20;
42  int height = 30;
43  wxSize sizes[] = { wxSize( width, height ), wxSize( width, -height ), wxSize( -width, height ),
44  wxSize( -width, -height ) };
45 
46  // We need also to check what happens when deflation exceeds the size.
47  int deltas[] = { width, width / 2, width / 4 };
48 
49  for( int delta : deltas )
50  {
51  for( wxSize size : sizes )
52  {
53  wxPoint origin = wxPoint( 100, 100 );
54  EDA_RECT rect( origin, size );
55 
56  // The four corners of the rectangle.
57  wxPoint corners[] = { origin + wxSize( 0, 0 ), origin + wxSize( 0, size.y ),
58  origin + wxSize( size.x, 0 ), origin + wxSize( size.x, size.y ) };
59 
60  // Inflation
61 
62  wxPoint inflation_corners[] = {
63  corners[0] + wxSize( -delta * sign( size.x ), -delta * sign( size.y ) ),
64  corners[1] + wxSize( -delta * sign( size.x ), delta * sign( size.y ) ),
65  corners[2] + wxSize( delta * sign( size.x ), -delta * sign( size.y ) ),
66  corners[3] + wxSize( delta * sign( size.x ), delta * sign( size.y ) )
67  };
68 
69  for( wxPoint corner : inflation_corners )
70  {
71  EDA_RECT inflated_rect = rect;
72  inflated_rect.Inflate( delta );
73 
74  BOOST_CHECK( !rect.Contains( corner ) );
75  BOOST_CHECK( inflated_rect.Contains( corner ) );
76  }
77 
78  // Deflation
79 
80  wxPoint deflation_corners[] = {
81  corners[0]
82  + wxSize( ( delta - 1 ) * sign( size.x ), ( delta - 1 ) * sign( size.y ) ),
83  corners[1]
84  + wxSize( ( delta - 1 ) * sign( size.x ), -( delta - 1 ) * sign( size.y ) ),
85  corners[2]
86  + wxSize( -( delta - 1 ) * sign( size.x ), ( delta - 1 ) * sign( size.y ) ),
87  corners[3]
88  + wxSize( -( delta - 1 ) * sign( size.x ), -( delta - 1 ) * sign( size.y ) )
89  };
90 
91  for( wxPoint corner : deflation_corners )
92  {
93  EDA_RECT deflated_rect = rect;
94  deflated_rect.Inflate( -delta );
95 
96  // If true, deflation exceeds the size.
97  bool zeroed = false;
98 
99  if( abs( rect.GetSize().x ) < 2 * delta )
100  {
101  BOOST_CHECK_EQUAL( deflated_rect.GetSize().x, 0 );
102  zeroed = true;
103  }
104 
105  if( abs( rect.GetSize().y ) < 2 * delta )
106  {
107  BOOST_CHECK_EQUAL( deflated_rect.GetSize().y, 0 );
108  zeroed = true;
109  }
110 
111  if( !zeroed )
112  {
113  BOOST_CHECK( rect.Contains( corner ) );
114  BOOST_CHECK( !deflated_rect.Contains( corner ) );
115  }
116  }
117  }
118  }
119 }
120 
121 BOOST_AUTO_TEST_SUITE_END()
int sign(T val)
Definition: util.h:104
bool Contains(const wxPoint &aPoint) const
Definition: eda_rect.cpp:57
BOOST_CHECK(v2.Cross(v1)==1)
BOOST_AUTO_TEST_CASE(Inflate)
Declare the test suite.
Handle the component boundary box.
Definition: eda_rect.h:42
constexpr int delta
EDA_RECT & Inflate(wxCoord dx, wxCoord dy)
Inflate the rectangle horizontally by dx and vertically by dy.
Definition: eda_rect.cpp:364
const wxSize GetSize() const
Definition: eda_rect.h:100