From 47ec2fd5ba9a6475f588e271ad56ae4122ac3695 Mon Sep 17 00:00:00 2001 From: John Beard Date: Mon, 28 Jan 2019 10:28:02 +0000 Subject: [PATCH] QA: Add some tests on SHAPE_POLY_SET::Distance These tests pick up the bug fixed in: 90178eb6812eead81a2523fbb7cce665413637eb (as it is already fixed there is no failure). --- qa/common/CMakeLists.txt | 1 + .../geometry/test_shape_poly_set_distance.cpp | 149 ++++++++++++++++++ qa/qa_utils/CMakeLists.txt | 5 + .../geometry/line_chain_construction.cpp | 49 ++++++ .../geometry/poly_set_construction.cpp | 54 +++++++ qa/qa_utils/geometry/seg_construction.cpp | 39 +++++ .../geometry/line_chain_construction.h | 56 +++++++ .../qa_utils/geometry/poly_set_construction.h | 59 +++++++ .../qa_utils/geometry/seg_construction.h | 56 +++++++ 9 files changed, 468 insertions(+) create mode 100644 qa/common/geometry/test_shape_poly_set_distance.cpp create mode 100644 qa/qa_utils/geometry/line_chain_construction.cpp create mode 100644 qa/qa_utils/geometry/poly_set_construction.cpp create mode 100644 qa/qa_utils/geometry/seg_construction.cpp create mode 100644 qa/qa_utils/include/qa_utils/geometry/line_chain_construction.h create mode 100644 qa/qa_utils/include/qa_utils/geometry/poly_set_construction.h create mode 100644 qa/qa_utils/include/qa_utils/geometry/seg_construction.h diff --git a/qa/common/CMakeLists.txt b/qa/common/CMakeLists.txt index 5b105f139c..24b0651411 100644 --- a/qa/common/CMakeLists.txt +++ b/qa/common/CMakeLists.txt @@ -50,6 +50,7 @@ set( common_srcs geometry/test_segment.cpp geometry/test_shape_arc.cpp geometry/test_shape_poly_set_collision.cpp + geometry/test_shape_poly_set_distance.cpp geometry/test_shape_poly_set_iterator.cpp view/test_zoom_controller.cpp diff --git a/qa/common/geometry/test_shape_poly_set_distance.cpp b/qa/common/geometry/test_shape_poly_set_distance.cpp new file mode 100644 index 0000000000..79de9620a1 --- /dev/null +++ b/qa/common/geometry/test_shape_poly_set_distance.cpp @@ -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 + * + * 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 + +#include + +#include + +#include +#include + +/** + * 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 GetSPSSegDistCases() +{ + namespace KT = KI_TEST; + std::vector 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() diff --git a/qa/qa_utils/CMakeLists.txt b/qa/qa_utils/CMakeLists.txt index bc04b72cad..1e8bcffe29 100644 --- a/qa/qa_utils/CMakeLists.txt +++ b/qa/qa_utils/CMakeLists.txt @@ -22,6 +22,10 @@ set( QA_UTIL_COMMON_SRC stdstream_line_reader.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 @@ -37,5 +41,6 @@ target_link_libraries( qa_utils ) target_include_directories( qa_utils PUBLIC + include ${CMAKE_CURRENT_SOURCE_DIR} ) \ No newline at end of file diff --git a/qa/qa_utils/geometry/line_chain_construction.cpp b/qa/qa_utils/geometry/line_chain_construction.cpp new file mode 100644 index 0000000000..56d1b906df --- /dev/null +++ b/qa/qa_utils/geometry/line_chain_construction.cpp @@ -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 + +namespace KI_TEST +{ + +SHAPE_LINE_CHAIN BuildRectChain( const VECTOR2I& aSize, const VECTOR2I& aCentre ) +{ + const std::vector 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 \ No newline at end of file diff --git a/qa/qa_utils/geometry/poly_set_construction.cpp b/qa/qa_utils/geometry/poly_set_construction.cpp new file mode 100644 index 0000000000..ab2326b7c8 --- /dev/null +++ b/qa/qa_utils/geometry/poly_set_construction.cpp @@ -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 + +#include + +namespace KI_TEST +{ + +SHAPE_POLY_SET BuildPolyset( const std::vector& 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 \ No newline at end of file diff --git a/qa/qa_utils/geometry/seg_construction.cpp b/qa/qa_utils/geometry/seg_construction.cpp new file mode 100644 index 0000000000..bf77eb9d66 --- /dev/null +++ b/qa/qa_utils/geometry/seg_construction.cpp @@ -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 + +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 \ No newline at end of file diff --git a/qa/qa_utils/include/qa_utils/geometry/line_chain_construction.h b/qa/qa_utils/include/qa_utils/geometry/line_chain_construction.h new file mode 100644 index 0000000000..ef35b05418 --- /dev/null +++ b/qa/qa_utils/include/qa_utils/geometry/line_chain_construction.h @@ -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 + +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 \ No newline at end of file diff --git a/qa/qa_utils/include/qa_utils/geometry/poly_set_construction.h b/qa/qa_utils/include/qa_utils/geometry/poly_set_construction.h new file mode 100644 index 0000000000..71a67a2e4e --- /dev/null +++ b/qa/qa_utils/include/qa_utils/geometry/poly_set_construction.h @@ -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 + +#include + +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& 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 \ No newline at end of file diff --git a/qa/qa_utils/include/qa_utils/geometry/seg_construction.h b/qa/qa_utils/include/qa_utils/geometry/seg_construction.h new file mode 100644 index 0000000000..f2c4b1b1c8 --- /dev/null +++ b/qa/qa_utils/include/qa_utils/geometry/seg_construction.h @@ -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 + +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 \ No newline at end of file