KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbnew/test_shape_corner_radius.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) 2024 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/unit_test.hpp>
22
23#include <drc/drc_rtree.h>
24#include <pcb_shape.h>
27#include <properties/property.h>
28#include <i18n_utility.h>
29
30BOOST_AUTO_TEST_CASE( PCBShapeCornerRadius )
31{
32 PCB_SHAPE shape( nullptr, SHAPE_T::RECTANGLE );
33 shape.SetStart( VECTOR2I( 0, 0 ) );
34 shape.SetEnd( VECTOR2I( 1000, 600 ) );
35 shape.SetCornerRadius( 200 );
36 BOOST_CHECK_EQUAL( shape.GetCornerRadius(), 200 );
37
38 shape.SetCornerRadius( 400 );
39 BOOST_CHECK_EQUAL( shape.GetCornerRadius(), 300 );
40
41 shape.SetCornerRadius( -10 );
43}
44
45BOOST_AUTO_TEST_CASE( PCBShapeCornerRadiusValidation )
46{
47 PCB_SHAPE shape( nullptr, SHAPE_T::RECTANGLE );
48 shape.SetStart( VECTOR2I( 0, 0 ) );
49 shape.SetEnd( VECTOR2I( 1000, 500 ) );
50
52 propMgr.Rebuild();
53
54 PROPERTY_BASE* prop = propMgr.GetProperty( TYPE_HASH( EDA_SHAPE ), _HKI( "Corner Radius" ) );
55
56 auto resultTooLarge = prop->Validate( wxAny( 400 ), &shape );
57 BOOST_CHECK( resultTooLarge.has_value() );
58
59 auto resultOK = prop->Validate( wxAny( 200 ), &shape );
60 BOOST_CHECK( !resultOK.has_value() );
61}
62
63
64BOOST_AUTO_TEST_CASE( ShapeEffectiveGeometryHitsLineEnding )
65{
66 PCB_SHAPE shape( nullptr, SHAPE_T::SEGMENT );
67 shape.SetLayer( F_Cu );
68 shape.SetStart( VECTOR2I( 0, 0 ) );
69 shape.SetEnd( VECTOR2I( 1000, 0 ) );
70 shape.SetWidth( 100 );
72 shape.SetStartEndingLength( 400 );
73 shape.SetStartEndingWidth( 400 );
74
75 std::shared_ptr<SHAPE> effectiveShape = shape.GetEffectiveShape( F_Cu );
76 SHAPE_SEGMENT probe( VECTOR2I( 0, -150 ), VECTOR2I( 0, 150 ), 20 );
77
78 BOOST_REQUIRE( effectiveShape );
79 BOOST_CHECK( effectiveShape->Collide( probe.GetSeg(), 0 ) );
80}
81
82
83BOOST_AUTO_TEST_CASE( PCBShapeLineEndingRTreeQuery )
84{
85 PCB_SHAPE shape( nullptr, SHAPE_T::SEGMENT );
86 shape.SetLayer( F_Cu );
87 shape.SetStart( VECTOR2I( 0, 0 ) );
88 shape.SetEnd( VECTOR2I( 1000, 0 ) );
89 shape.SetWidth( 100 );
91 shape.SetStartEndingLength( 400 );
92 shape.SetStartEndingWidth( 400 );
93
94 PCB_SHAPE probe( nullptr, SHAPE_T::SEGMENT );
95 probe.SetLayer( F_Cu );
96 probe.SetStart( VECTOR2I( 0, -150 ) );
97 probe.SetEnd( VECTOR2I( 0, 150 ) );
98 probe.SetWidth( 20 );
99
100 DRC_RTREE rtree;
101 rtree.Insert( &shape, F_Cu, CLEARANCE_CONSTRAINT );
102 rtree.Build();
103
104 BOOST_CHECK_EQUAL( rtree.QueryColliding( &probe, F_Cu, F_Cu ), 1 );
105}
106
107
108BOOST_AUTO_TEST_CASE( PCBShapeHitTestHitsLineEnding )
109{
110 PCB_SHAPE shape( nullptr, SHAPE_T::SEGMENT );
111 shape.SetLayer( F_Cu );
112 shape.SetStart( VECTOR2I( 0, 0 ) );
113 shape.SetEnd( VECTOR2I( 1000, 0 ) );
114 shape.SetWidth( 100 );
116 shape.SetStartEndingLength( 400 );
117 shape.SetStartEndingWidth( 400 );
118
119 BOOST_CHECK( shape.HitTest( VECTOR2I( 0, 150 ), 0 ) );
120
121 BOX2I selection( VECTOR2I( -50, 100 ), VECTOR2I( 100, 80 ) );
122 BOOST_CHECK( shape.HitTest( selection, false, 0 ) );
123}
124
125
126BOOST_AUTO_TEST_CASE( PCBShapeShortLineHitTestHitsConsumedBodyEnding )
127{
128 PCB_SHAPE shape( nullptr, SHAPE_T::SEGMENT );
129 shape.SetLayer( F_Cu );
130 shape.SetStart( VECTOR2I( 0, 0 ) );
131 shape.SetEnd( VECTOR2I( 100, 0 ) );
132 shape.SetWidth( 20 );
134 shape.SetEndEndingLength( 400 );
135 shape.SetEndEndingWidth( 400 );
136
137 BOOST_CHECK( shape.HitTest( VECTOR2I( -200, 100 ), 0 ) );
138}
BOX2< VECTOR2I > BOX2I
Definition box2.h:927
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition drc_rtree.h:45
void Insert(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, DRC_CONSTRAINT_T aConstraintType, int aWorstClearance=0, bool aAtomicTables=false)
Insert an item into the tree on a particular layer with an optional worst clearance.
Definition drc_rtree.h:91
int QueryColliding(BOARD_ITEM *aRefItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer, std::function< bool(BOARD_ITEM *)> aFilter=nullptr, std::function< bool(BOARD_ITEM *)> aVisitor=nullptr, int aClearance=0) const
This is a fast test which essentially does bounding-box overlap given a worst-case clearance.
Definition drc_rtree.h:277
void Build()
Finalize all pending inserts by bulk-building packed R-trees from the staged items.
Definition drc_rtree.h:164
void SetEndEndingLength(int aLength)
Definition eda_shape.h:195
void SetStartEndingStyle(LINE_ENDING_STYLE aStyle)
Definition eda_shape.h:184
void SetEndEndingWidth(int aWidth)
Definition eda_shape.h:197
void SetCornerRadius(int aRadius)
void SetEndEndingStyle(LINE_ENDING_STYLE aStyle)
Definition eda_shape.h:187
void SetStartEndingLength(int aLength)
Definition eda_shape.h:190
void SetStartEndingWidth(int aWidth)
Definition eda_shape.h:192
int GetCornerRadius() const
void SetWidth(int aWidth) override
bool HitTest(const VECTOR2I &aPosition, int aAccuracy=0) const override
Test if aPosition is inside or on the boundary of this item.
Definition pcb_shape.h:160
void SetEnd(const VECTOR2I &aEnd) override
std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const override
Make a set of SHAPE objects representing the PCB_SHAPE.
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
void SetStart(const VECTOR2I &aStart) override
VALIDATOR_RESULT Validate(const wxAny &&aValue, EDA_ITEM *aItem)
Definition property.h:374
Provide class metadata.Helper macro to map type hashes to names.
static PROPERTY_MANAGER & Instance()
void Rebuild()
Rebuild the list of all registered properties.
PROPERTY_BASE * GetProperty(TYPE_ID aType, const wxString &aProperty) const
Return a property for a specific type.
const SEG & GetSeg() const
@ CLEARANCE_CONSTRAINT
Definition drc_rule.h:51
@ SEGMENT
Definition eda_shape.h:56
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:57
Some functions to handle hotkeys in KiCad.
@ F_Cu
Definition layer_ids.h:60
#define _HKI(x)
Definition page_info.cpp:40
BOOST_AUTO_TEST_CASE(PCBShapeCornerRadius)
#define TYPE_HASH(x)
Definition property.h:74
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683