From 1a2e02040aa8aaf9673f38d65510567a2e2b4ec6 Mon Sep 17 00:00:00 2001 From: Tomasz Wlostowski Date: Wed, 22 Jul 2020 22:50:31 +0200 Subject: [PATCH] qa/libs/kimath: simple test suite for collisions of SHAPE_COMPOUNDs --- qa/libs/kimath/CMakeLists.txt | 1 + .../test_shape_compound_collision.cpp | 124 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 qa/libs/kimath/geometry/test_shape_compound_collision.cpp diff --git a/qa/libs/kimath/CMakeLists.txt b/qa/libs/kimath/CMakeLists.txt index d549dafe3f..afeb28a9c4 100644 --- a/qa/libs/kimath/CMakeLists.txt +++ b/qa/libs/kimath/CMakeLists.txt @@ -29,6 +29,7 @@ set( KIMATH_SRCS geometry/test_fillet.cpp geometry/test_segment.cpp + geometry/test_shape_compound_collision.cpp geometry/test_shape_arc.cpp geometry/test_shape_poly_set_collision.cpp geometry/test_shape_poly_set_distance.cpp diff --git a/qa/libs/kimath/geometry/test_shape_compound_collision.cpp b/qa/libs/kimath/geometry/test_shape_compound_collision.cpp new file mode 100644 index 0000000000..13e44abc5c --- /dev/null +++ b/qa/libs/kimath/geometry/test_shape_compound_collision.cpp @@ -0,0 +1,124 @@ +/* + * 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 "fixtures_geometry.h" + +/** + * Fixture for the Collision test suite. It contains an instance of the common data and two + * vectors containing colliding and non-colliding points. + */ +struct CollisionFixture +{ + // Structure to store the common data. + struct KI_TEST::CommonTestData common; + + // Vectors containing colliding and non-colliding points + std::vector shapesA, shapesB, shapesC; + + SHAPE_COMPOUND* compoundA; + SHAPE_COMPOUND* compoundB; + SHAPE_COMPOUND* compoundC; + + /** + * Constructor + */ + CollisionFixture() + { + shapesA.push_back( new SHAPE_CIRCLE( VECTOR2I( 0, 0 ), 100 ) ); + shapesA.push_back( new SHAPE_CIRCLE( VECTOR2I( 80, 0 ), 100 ) ); + + shapesB.push_back( new SHAPE_CIRCLE( VECTOR2I( 0, 80 ), 100 ) ); + shapesB.push_back( new SHAPE_CIRCLE( VECTOR2I( 80, 80 ), 100 ) ); + + shapesC.push_back( new SHAPE_CIRCLE( VECTOR2I( 0, 280 ), 100 ) ); + shapesC.push_back( new SHAPE_CIRCLE( VECTOR2I( 80, 280 ), 100 ) ); + + compoundA = new SHAPE_COMPOUND( shapesA ); + compoundB = new SHAPE_COMPOUND( shapesB ); + compoundC = new SHAPE_COMPOUND( shapesC ); + } + + ~CollisionFixture() + { + } +}; + +/** + * Declares the CollisionFixture as the boost test suite fixture. + */ +BOOST_FIXTURE_TEST_SUITE( SCompoundCollision, CollisionFixture ) + +/** + * This test checks basic behaviour of PointOnEdge, testing if points on corners, outline edges + * and hole edges are detected as colliding. + */ +BOOST_AUTO_TEST_CASE( ShapeCompoundCollide ) +{ + int actual; + // Check points on corners + BOOST_CHECK( compoundA->Collide( compoundB, 0, &actual ) ); + BOOST_CHECK( actual == 0 ); + + BOOST_CHECK( !compoundA->Collide( compoundC, 0, &actual ) ); + BOOST_CHECK( actual == 0 ); + + BOOST_CHECK( compoundA->Collide( compoundC, 100, &actual ) ); + BOOST_CHECK( actual == 80 ); + + BOOST_CHECK( shapesA[0]->Collide( compoundB, 0 ) ); + BOOST_CHECK( shapesA[1]->Collide( compoundB, 0 ) ); + BOOST_CHECK( compoundB->Collide( shapesA[0], 0 ) ); + BOOST_CHECK( compoundB->Collide( shapesA[1], 0 ) ); + + BOOST_CHECK( shapesB[0]->Collide( compoundA, 0 ) ); + BOOST_CHECK( shapesB[1]->Collide( compoundA, 0 ) ); + BOOST_CHECK( compoundA->Collide( shapesB[0], 0 ) ); + BOOST_CHECK( compoundA->Collide( shapesB[1], 0 ) ); + + BOOST_CHECK( ! shapesC[0]->Collide( compoundA, 0 ) ); + BOOST_CHECK( ! shapesC[1]->Collide( compoundA, 0 ) ); + BOOST_CHECK( ! compoundA->Collide( shapesC[0], 0 ) ); + BOOST_CHECK( ! compoundA->Collide( shapesC[1], 0 ) ); + + BOOST_CHECK( ! shapesA[0]->Collide( compoundC, 0 ) ); + BOOST_CHECK( ! shapesA[1]->Collide( compoundC, 0 ) ); + BOOST_CHECK( ! compoundC->Collide( shapesA[0], 0 ) ); + BOOST_CHECK( ! compoundC->Collide( shapesA[1], 0 ) ); + + BOOST_CHECK( shapesC[0]->Collide( compoundA, 100, &actual ) ); + BOOST_CHECK( actual == 80 ); + BOOST_CHECK( shapesC[1]->Collide( compoundA, 100, &actual ) ); + BOOST_CHECK( actual == 80 ); + BOOST_CHECK( compoundA->Collide( shapesC[0], 100, &actual ) ); + BOOST_CHECK( actual == 80 ); + BOOST_CHECK( compoundA->Collide( shapesC[1], 100, &actual ) ); + BOOST_CHECK( actual == 80 ); +} + +BOOST_AUTO_TEST_SUITE_END()