QA: Add some tests on SHAPE_POLY_SET::Distance

These tests pick up the bug fixed in: 90178eb681
(as it is already fixed there is no failure).
This commit is contained in:
John Beard 2019-01-28 10:28:02 +00:00 committed by Wayne Stambaugh
parent 016a544606
commit 47ec2fd5ba
9 changed files with 468 additions and 0 deletions

View File

@ -50,6 +50,7 @@ set( common_srcs
geometry/test_segment.cpp geometry/test_segment.cpp
geometry/test_shape_arc.cpp geometry/test_shape_arc.cpp
geometry/test_shape_poly_set_collision.cpp geometry/test_shape_poly_set_collision.cpp
geometry/test_shape_poly_set_distance.cpp
geometry/test_shape_poly_set_iterator.cpp geometry/test_shape_poly_set_iterator.cpp
view/test_zoom_controller.cpp view/test_zoom_controller.cpp

View File

@ -0,0 +1,149 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 CERN
* @author Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
*
* 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
*/
#include <unit_test_utils/unit_test_utils.h>
#include <convert_to_biu.h>
#include <geometry/shape_poly_set.h>
#include <qa_utils/geometry/poly_set_construction.h>
#include <qa_utils/geometry/seg_construction.h>
/**
* Declares the Boost test suite fixture.
*/
BOOST_AUTO_TEST_SUITE( SPSDistance )
struct SPS_DISTANCE_TO_SEG_CASE
{
std::string m_case_name;
/// list of lists of polygon points
SHAPE_POLY_SET m_polyset;
/// the segment to check distance to
SEG m_seg;
int m_seg_width;
/// The expected answer
int m_exp_dist;
};
static std::vector<SPS_DISTANCE_TO_SEG_CASE> GetSPSSegDistCases()
{
namespace KT = KI_TEST;
std::vector<SPS_DISTANCE_TO_SEG_CASE> cases;
// Single 10mm square at origin
const SHAPE_POLY_SET square_10mm_0_0 = KT::BuildPolyset( {
KT::BuildSquareChain( Millimeter2iu( 10 ) ),
} );
// Double square: 10mm each, one at (0, 0), one at (10, 0)
const SHAPE_POLY_SET squares_10mm_0_0_and_20_0 = KT::BuildPolyset( {
KT::BuildSquareChain( Millimeter2iu( 10 ) ),
KT::BuildSquareChain( Millimeter2iu( 10 ), //
{ Millimeter2iu( 20 ), Millimeter2iu( 0 ) } ),
} );
// Hollow square: 10mm hole in 20mm square, at origin
const SHAPE_POLY_SET hollow_square_20_10_at_0_0 =
KT::BuildHollowSquare( Millimeter2iu( 20 ), Millimeter2iu( 10 ) );
cases.push_back( {
"Square poly -> 1D segment",
square_10mm_0_0,
KT::BuildHSeg( { Millimeter2iu( 0 ), Millimeter2iu( 15 ) }, Millimeter2iu( 10 ) ),
Millimeter2iu( 0 ), // 1-d segment
Millimeter2iu( 10 ),
} );
cases.push_back( {
"Square poly -> 2D (thick) segment",
square_10mm_0_0,
KT::BuildHSeg( { Millimeter2iu( 0 ), Millimeter2iu( 15 ) }, Millimeter2iu( 10 ) ),
Millimeter2iu( 2 ), // thick segment
Millimeter2iu( 9 ),
} );
cases.push_back( {
"Two Squares poly -> 2D segment (nearest second square)", squares_10mm_0_0_and_20_0,
KT::BuildHSeg( { Millimeter2iu( 15 ), Millimeter2iu( 15 ) }, Millimeter2iu( 10 ) ),
Millimeter2iu( 2 ), // thick segment
Millimeter2iu( 9 ), // from line to second square
} );
cases.push_back( {
"Square poly -> one intersect", square_10mm_0_0,
KT::BuildHSeg( { Millimeter2iu( -5 ), Millimeter2iu( 0 ) }, Millimeter2iu( 10 ) ),
Millimeter2iu( 0 ), // 1-d segment
Millimeter2iu( 0 ), // intersect
} );
cases.push_back( {
"Square poly -> multiple intersection", square_10mm_0_0,
KT::BuildHSeg( { Millimeter2iu( -5 ), Millimeter2iu( 0 ) }, Millimeter2iu( 20 ) ),
Millimeter2iu( 0 ), // 1-d segment
Millimeter2iu( 0 ), // intersect
} );
cases.push_back( {
"Square poly -> 1D seg touching", square_10mm_0_0,
// touch left side at (-5, 0)
KT::BuildHSeg( { Millimeter2iu( -10 ), Millimeter2iu( 0 ) }, Millimeter2iu( 5 ) ),
Millimeter2iu( 0 ), // 2D segment
Millimeter2iu( 0 ), // intersect
} );
cases.push_back( {
"Square poly -> 2D seg (end cap is nearest)", square_10mm_0_0,
KT::BuildHSeg( { Millimeter2iu( -20 ), Millimeter2iu( 0 ) }, Millimeter2iu( 10 ) ),
Millimeter2iu( 2 ), // 2D segment, 1mm cap radius
Millimeter2iu( 4 ), // 4mm short, 5mm to wire end, -1mm radius
} );
return cases;
};
/**
* Check segment distances
*/
BOOST_AUTO_TEST_CASE( SegDistance )
{
for( const auto& c : GetSPSSegDistCases() )
{
BOOST_TEST_CONTEXT( c.m_case_name )
{
SHAPE_POLY_SET polyset = c.m_polyset;
int dist = polyset.Distance( c.m_seg, c.m_seg_width );
// right answer?
BOOST_CHECK_EQUAL( dist, c.m_exp_dist );
}
}
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -22,6 +22,10 @@
set( QA_UTIL_COMMON_SRC set( QA_UTIL_COMMON_SRC
stdstream_line_reader.cpp stdstream_line_reader.cpp
utility_program.cpp utility_program.cpp
geometry/line_chain_construction.cpp
geometry/poly_set_construction.cpp
geometry/seg_construction.cpp
) )
# A generic library of useful functions for various testing purposes # A generic library of useful functions for various testing purposes
@ -37,5 +41,6 @@ target_link_libraries( qa_utils
) )
target_include_directories( qa_utils PUBLIC target_include_directories( qa_utils PUBLIC
include
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
) )

View File

@ -0,0 +1,49 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019 KiCad Developers, see CHANGELOG.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
*/
#include <qa_utils/geometry/line_chain_construction.h>
namespace KI_TEST
{
SHAPE_LINE_CHAIN BuildRectChain( const VECTOR2I& aSize, const VECTOR2I& aCentre )
{
const std::vector<VECTOR2I> pts = {
{ aCentre.x - aSize.x / 2, aCentre.y - aSize.y / 2 },
{ aCentre.x - aSize.x / 2, aCentre.y + aSize.y / 2 },
{ aCentre.x + aSize.x / 2, aCentre.y + aSize.y / 2 },
{ aCentre.x + aSize.x / 2, aCentre.y - aSize.y / 2 },
};
SHAPE_LINE_CHAIN chain( pts.data(), pts.size() );
chain.SetClosed( true );
return chain;
}
SHAPE_LINE_CHAIN BuildSquareChain( int aSize, const VECTOR2I& aCentre )
{
return BuildRectChain( { aSize, aSize }, aCentre );
}
} // namespace KI_TEST

View File

@ -0,0 +1,54 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019 KiCad Developers, see CHANGELOG.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
*/
#include <qa_utils/geometry/poly_set_construction.h>
#include <qa_utils/geometry/line_chain_construction.h>
namespace KI_TEST
{
SHAPE_POLY_SET BuildPolyset( const std::vector<SHAPE_LINE_CHAIN>& aOutlines )
{
SHAPE_POLY_SET polyset;
for( const auto& outline : aOutlines )
{
polyset.AddOutline( outline );
}
return polyset;
}
SHAPE_POLY_SET BuildHollowSquare( int aOuterSize, int aInnerSize, const VECTOR2I& aCentre )
{
SHAPE_POLY_SET polyset;
polyset.AddOutline( BuildRectChain( { aOuterSize, aOuterSize }, aCentre ) );
polyset.AddHole( BuildRectChain( { aInnerSize, aInnerSize }, aCentre ) );
return polyset;
}
} // namespace KI_TEST

View File

@ -0,0 +1,39 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019 KiCad Developers, see CHANGELOG.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
*/
#include <qa_utils/geometry/seg_construction.h>
namespace KI_TEST
{
SEG BuildHSeg( const VECTOR2I& aStart, int aLength )
{
return SEG{ { aStart.x, aStart.y }, { aStart.x + aLength, aStart.y } };
}
SEG BuildVSeg( const VECTOR2I& aStart, int aLength )
{
return SEG{ { aStart.x, aStart.y }, { aStart.x, aStart.y + aLength } };
}
} // namespace KI_TEST

View File

@ -0,0 +1,56 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019 KiCad Developers, see CHANGELOG.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
*/
/**
* @file
* Utilities for creating useful line chain idioms commonly founds in
* QA utilities and tests
*/
#ifndef QA_UTILS_GEOMETRY_LINE_CHAIN_CONSTRUCTION__H
#define QA_UTILS_GEOMETRY_LINE_CHAIN_CONSTRUCTION__H
#include <geometry/shape_line_chain.h>
namespace KI_TEST
{
/**
* Builds a rectangular #SHAPE_LINE_CHAIN of a certain size at a certain centre
* @param aSize the rectangle size
* @param aCentre centre of the rectangle
* @return a closed line chain of the rectangle
*/
SHAPE_LINE_CHAIN BuildRectChain( const VECTOR2I& aSize, const VECTOR2I& aCentre = { 0, 0 } );
/**
* Builds a square #SHAPE_LINE_CHAIN of a certain size at a certain centre
* @param aSize the square size (x == y)
* @param aCentre centre of the square
* @return a closed line chain of the square
*/
SHAPE_LINE_CHAIN BuildSquareChain( int aSize, const VECTOR2I& aCentre = { 0, 0 } );
} // namespace KI_TEST
#endif // QA_UTILS_GEOMETRY_LINE_CHAIN_CONSTRUCTION__H

View File

@ -0,0 +1,59 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019 KiCad Developers, see CHANGELOG.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
*/
/**
* @file
* Utilities for creating useful polygon shapes that are commonly
* found in QA utilities and tests.
*/
#ifndef QA_UTILS_GEOMETRY_POLY_SET_CONSTRUCTION__H
#define QA_UTILS_GEOMETRY_POLY_SET_CONSTRUCTION__H
#include <geometry/shape_poly_set.h>
#include <qa_utils/geometry/line_chain_construction.h>
namespace KI_TEST
{
/**
* Construct a #SHAPE_POLY_SET from a list of points for each of outlines
* (no holes)
*/
SHAPE_POLY_SET BuildPolyset( const std::vector<SHAPE_LINE_CHAIN>& aOutlines );
/**
* Build a #SHAPE_POLY_SET in the shape of a square outline, with a square
* hole, both centred at the given point.
* @param aOuterSize the size (x and y) of the outer square
* @param aInnerSize the size (x and y) of the hole
* @param aCentre the centre point of both squares (default: origin)
* @return a SHAPE_POLY_SET of the desired shape
*/
SHAPE_POLY_SET BuildHollowSquare(
int aOuterSize, int aInnerSize, const VECTOR2I& aCentre = { 0, 0 } );
} // namespace KI_TEST
#endif // QA_UTILS_GEOMETRY_POLY_SET_CONSTRUCTION__H

View File

@ -0,0 +1,56 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2019 KiCad Developers, see CHANGELOG.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
*/
/**
* @file
* Utilities for creating useful line chain idioms commonly founds in
* QA utilities and tests
*/
#ifndef QA_UTILS_GEOMETRY_SEG_CONSTRUCTION__H
#define QA_UTILS_GEOMETRY_SEG_CONSTRUCTION__H
#include <geometry/seg.h>
namespace KI_TEST
{
/**
* Build a horizontal segment from a point with a length
* @param aStart the starting point
* @param aLength the segment length
* @return the resulting segment
*/
SEG BuildHSeg( const VECTOR2I& aStart, int aLength );
/**
* Build a vertical segment from a point with a length
* @param aStart the starting point
* @param aLength the segment length
* @return the resulting segment
*/
SEG BuildVSeg( const VECTOR2I& aStart, int aLength );
} // namespace KI_TEST
#endif // QA_UTILS_GEOMETRY_SEG_CONSTRUCTION__H