kicad/qa/pcbnew/drc/test_drc_courtyard_overlap.cpp

491 lines
14 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 KiCad Developers, see AUTHORS.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
2021-02-06 22:20:31 +00:00
#include <qa_utils/wx_utils/unit_test_utils.h>
#include <pcbnew_utils/board_construction_utils.h>
#include <pcbnew_utils/board_file_utils.h>
#include <board.h>
2021-06-06 19:33:45 +00:00
#include <board_design_settings.h>
#include <footprint.h>
2020-11-14 18:11:28 +00:00
#include <pcb_marker.h>
#include <drc/drc_item.h>
2020-09-14 17:54:14 +00:00
#include <drc/drc_engine.h>
#include <widgets/ui_common.h>
#include "../board_test_utils.h"
#include "drc_test_utils.h"
/**
* Simple definition of a rectangle, can be rounded
*/
struct RECT_DEFINITION
{
VECTOR2I m_centre;
VECTOR2I m_size;
int m_corner_rad;
// On front or back layer (the exact layer is context-dependent)
bool m_front;
};
/*
2020-11-13 15:15:52 +00:00
* A simple mock footprint with a set of courtyard rectangles and some other information
*/
2020-11-13 15:15:52 +00:00
struct COURTYARD_TEST_FP
{
std::string m_refdes;
std::vector<RECT_DEFINITION> m_rects;
VECTOR2I m_pos;
};
/*
* Struct holding information about a courtyard collision
*/
struct COURTYARD_COLLISION
{
// The two colliding parts
std::string m_refdes_a;
std::string m_refdes_b;
};
std::ostream& operator<<( std::ostream& os, const COURTYARD_COLLISION& aColl )
{
os << "COURTYARD_COLLISION[ " << aColl.m_refdes_a << " -> " << aColl.m_refdes_b << "]";
return os;
}
/**
* A complete courtyard overlap test case: a name, the board footprint list
* and the expected collisions.
*/
struct COURTYARD_OVERLAP_TEST_CASE
{
2020-11-13 15:15:52 +00:00
std::string m_case_name;
std::vector<COURTYARD_TEST_FP> m_fpDefs; // The footprint in the test case
std::vector<COURTYARD_COLLISION> m_collisions; // The expected number of collisions
};
/**
2020-11-13 15:15:52 +00:00
* Add a rectangular courtyard outline to a footprint.
*/
2020-11-13 15:15:52 +00:00
void AddRectCourtyard( FOOTPRINT& aFootprint, const RECT_DEFINITION& aRect )
{
const PCB_LAYER_ID layer = aRect.m_front ? F_CrtYd : B_CrtYd;
const int width = Millimeter2iu( 0.1 );
2020-11-13 15:15:52 +00:00
KI_TEST::DrawRect( aFootprint, aRect.m_centre, aRect.m_size, aRect.m_corner_rad, width, layer );
}
/**
2020-11-13 15:15:52 +00:00
* Construct a #FOOTPRINT to use in a courtyard test from a #COURTYARD_TEST_FP definition.
*/
2020-11-13 15:15:52 +00:00
std::unique_ptr<FOOTPRINT> MakeCourtyardTestFP( BOARD& aBoard, const COURTYARD_TEST_FP& aFPDef )
{
2020-11-13 15:15:52 +00:00
std::unique_ptr<FOOTPRINT> footprint = std::make_unique<FOOTPRINT>( &aBoard );
2020-11-13 15:15:52 +00:00
for( const RECT_DEFINITION& rect : aFPDef.m_rects )
AddRectCourtyard( *footprint, rect );
2020-11-13 15:15:52 +00:00
footprint->SetReference( aFPDef.m_refdes );
2020-11-13 15:15:52 +00:00
// This has to go after adding the courtyards, or all the poly sets are empty when DRC'd
footprint->SetPosition( (wxPoint) aFPDef.m_pos );
2020-11-13 15:15:52 +00:00
return footprint;
}
/**
* Make a board for courtyard testing.
*
2020-11-13 15:15:52 +00:00
* @param aFPDefs the list of footprint definitions to add to the board
*/
2020-11-13 15:15:52 +00:00
std::unique_ptr<BOARD> MakeBoard( const std::vector<COURTYARD_TEST_FP>& aFPDefs )
{
2020-11-13 15:15:52 +00:00
std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
2020-11-13 15:15:52 +00:00
for( const COURTYARD_TEST_FP& fpDef : aFPDefs )
{
2020-11-13 15:15:52 +00:00
std::unique_ptr<FOOTPRINT> footprint = MakeCourtyardTestFP( *board, fpDef );
2020-11-13 15:15:52 +00:00
board->Add( footprint.release() );
}
return board;
}
struct COURTYARD_TEST_FIXTURE
{
const KI_TEST::BOARD_DUMPER m_dumper;
};
BOOST_FIXTURE_TEST_SUITE( DrcCourtyardOverlap, COURTYARD_TEST_FIXTURE )
// clang-format off
static std::vector<COURTYARD_OVERLAP_TEST_CASE> courtyard_cases = {
{
"empty board",
{}, // no footprint
{}, // no collisions
},
{
"single empty footprint",
{
{
"U1",
{}, // no courtyard
{ 0, 0 }, // at origin
},
},
{}, // no collisions
},
{
// A single footprint can't overlap itself
"single footprint, single courtyard",
{
{
"U1",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ 0, 0 },
},
},
{}, // no collisions
},
{
"two footprint, no overlap",
{
{
"U1",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ 0, 0 },
},
{
"U2",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
2020-11-13 15:15:52 +00:00
{ Millimeter2iu( 3 ), Millimeter2iu( 1 ) }, // One footprint is far from the other
},
},
{}, // no collisions
},
{
"two footprints, touching, no overlap",
{
{
"U1",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ 0, 0 },
},
{
"U2",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ Millimeter2iu( 1 ), Millimeter2iu( 0 ) }, // Just touching
},
},
{}, // Touching means not colliding
},
{
"two footprints, overlap",
{
{
"U1",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ 0, 0 },
},
{
"U2",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ Millimeter2iu( 0.5 ), Millimeter2iu( 0 ) }, // Partial overlap
},
},
{
{ "U1", "U2" }, // These two collide
},
},
{
"two footprints, overlap, different sides",
{
{
"U1",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ 0, 0 },
},
{
"U2",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
false,
},
},
{ 0, 0 }, // complete overlap
},
},
{}, // but on different sides
},
{
"two footprints, multiple courtyards, overlap",
{
{
"U1",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
{
{ Millimeter2iu( 2 ), Millimeter2iu( 0 ) },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ 0, 0 },
},
{
"U2",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
0,
true,
},
},
{ 0, 0 }, // complete overlap with one of the others
},
},
{
{ "U1", "U2" },
},
},
{
// The courtyards do not overlap, but their bounding boxes do
"two footprints, no overlap, bbox overlap",
{
{
"U1",
{
{
{ 0, 0 },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
Millimeter2iu( 0.5 ),
true,
},
},
{ 0, 0 },
},
{
"U2",
{
{
{ Millimeter2iu( 0.9 ), Millimeter2iu( 0.9 ) },
{ Millimeter2iu( 1 ), Millimeter2iu( 1 ) },
Millimeter2iu( 0.5 ),
true,
},
},
{ 0, 0 },
},
},
{},
},
};
// clang-format on
/**
2020-11-14 18:11:28 +00:00
* Check if a #PCB_MARKER is described by a particular #COURTYARD_COLLISION object.
*/
2020-11-14 18:11:28 +00:00
static bool CollisionMatchesExpected( BOARD& aBoard, const PCB_MARKER& aMarker,
const COURTYARD_COLLISION& aCollision )
{
2020-08-11 13:33:16 +00:00
auto reporter = std::static_pointer_cast<DRC_ITEM>( aMarker.GetRCItem() );
2020-11-13 15:15:52 +00:00
const FOOTPRINT* item_a = dynamic_cast<FOOTPRINT*>( aBoard.GetItem( reporter->GetMainItemID() ) );
const FOOTPRINT* item_b = dynamic_cast<FOOTPRINT*>( aBoard.GetItem( reporter->GetAuxItemID() ) );
// can't find the items!
if( !item_a || !item_b )
return false;
const bool ref_match_aa_bb = ( item_a->GetReference() == aCollision.m_refdes_a )
&& ( item_b->GetReference() == aCollision.m_refdes_b );
const bool ref_match_ab_ba = ( item_a->GetReference() == aCollision.m_refdes_b )
&& ( item_b->GetReference() == aCollision.m_refdes_a );
// Doesn't matter which way around it is, but both have to match somehow
return ref_match_aa_bb || ref_match_ab_ba;
}
/**
* Check that the produced markers match the expected. This does NOT check ordering,
* as that is not part of the contract of the DRC function.
*
* @param aMarkers list of markers produced by the DRC
* @param aCollisions list of expected collisions
*/
static void CheckCollisionsMatchExpected( BOARD& aBoard,
2020-11-14 18:11:28 +00:00
const std::vector<std::unique_ptr<PCB_MARKER>>& aMarkers,
const std::vector<COURTYARD_COLLISION>& aExpCollisions )
{
for( const auto& marker : aMarkers )
{
BOOST_CHECK_PREDICATE(
KI_TEST::IsDrcMarkerOfType, ( *marker )( DRCE_OVERLAPPING_FOOTPRINTS ) );
}
KI_TEST::CheckUnorderedMatches( aExpCollisions, aMarkers,
2020-11-14 18:11:28 +00:00
[&]( const COURTYARD_COLLISION& aColl, const std::unique_ptr<PCB_MARKER>& aMarker )
{
return CollisionMatchesExpected( aBoard, *aMarker, aColl );
} );
}
/**
* Run a single courtyard overlap testcase
* @param aCase The testcase to run.
*/
static void DoCourtyardOverlapTest( const COURTYARD_OVERLAP_TEST_CASE& aCase,
const KI_TEST::BOARD_DUMPER& aDumper )
{
2020-11-13 15:15:52 +00:00
auto board = MakeBoard( aCase.m_fpDefs );
// Dump if env var set
aDumper.DumpBoardToFile( *board, aCase.m_case_name );
BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
bds.m_DRCSeverities[ DRCE_OVERLAPPING_FOOTPRINTS ] = RPT_SEVERITY_ERROR;
// we might not always have courtyards - that's a separate test
bds.m_DRCSeverities[ DRCE_MISSING_COURTYARD ] = RPT_SEVERITY_IGNORE;
// list of markers to collect
2020-11-14 18:11:28 +00:00
std::vector<std::unique_ptr<PCB_MARKER>> markers;
2020-09-14 17:54:14 +00:00
DRC_ENGINE drcEngine( board.get(), &board->GetDesignSettings() );
drcEngine.InitEngine( wxFileName() );
drcEngine.SetViolationHandler(
[&]( const std::shared_ptr<DRC_ITEM>& aItem, wxPoint aPos )
{
2020-09-14 17:54:14 +00:00
if( aItem->GetErrorCode() == DRCE_OVERLAPPING_FOOTPRINTS
|| aItem->GetErrorCode() == DRCE_MALFORMED_COURTYARD
2020-09-14 17:54:14 +00:00
|| aItem->GetErrorCode() == DRCE_MISSING_COURTYARD )
{
2020-11-14 18:11:28 +00:00
markers.push_back( std::make_unique<PCB_MARKER>( aItem, aPos ) );
2020-09-14 17:54:14 +00:00
}
} );
drcEngine.RunTests( EDA_UNITS::MILLIMETRES, true, false );
CheckCollisionsMatchExpected( *board, markers, aCase.m_collisions );
}
BOOST_AUTO_TEST_CASE( OverlapCases )
{
for( const auto& c : courtyard_cases )
{
BOOST_TEST_CONTEXT( c.m_case_name )
{
DoCourtyardOverlapTest( c, m_dumper );
}
}
}
BOOST_AUTO_TEST_SUITE_END()