diff --git a/libs/kimath/include/geometry/eda_angle.h b/libs/kimath/include/geometry/eda_angle.h index b2bedbe887..ba71e1754f 100644 --- a/libs/kimath/include/geometry/eda_angle.h +++ b/libs/kimath/include/geometry/eda_angle.h @@ -257,6 +257,17 @@ public: return *this; } + inline EDA_ANGLE NormalizeNegative() + { + while( m_value <= -360.0 ) + m_value += 360.0; + + while( m_value > 0.0 ) + m_value -= 360.0; + + return *this; + } + inline EDA_ANGLE Normalize90() { while( m_value < -90.0 ) diff --git a/qa/unittests/libs/kimath/CMakeLists.txt b/qa/unittests/libs/kimath/CMakeLists.txt index e4fd1a0e95..6f78930f55 100644 --- a/qa/unittests/libs/kimath/CMakeLists.txt +++ b/qa/unittests/libs/kimath/CMakeLists.txt @@ -27,6 +27,7 @@ set( QA_KIMATH_SRCS test_kimath.cpp + geometry/test_eda_angle.cpp geometry/test_ellipse_to_bezier.cpp geometry/test_fillet.cpp geometry/test_circle.cpp diff --git a/qa/unittests/libs/kimath/geometry/test_eda_angle.cpp b/qa/unittests/libs/kimath/geometry/test_eda_angle.cpp new file mode 100644 index 0000000000..62e3337929 --- /dev/null +++ b/qa/unittests/libs/kimath/geometry/test_eda_angle.cpp @@ -0,0 +1,97 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2023 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 + */ + +#include + +#include + + +BOOST_AUTO_TEST_SUITE( EdaAngle ) + + +struct EDA_ANGLE_NORMALISE_CASE +{ + double m_Angle; + + // Expected: + double m_ExpNormalized; + double m_ExpNormalizedNegative; + double m_ExpNormalized90; + double m_ExpNormalized180; + double m_ExpNormalized720; +}; + + +static const std::vector normalize_cases = +{ + //@todo: should we unify the ranges of Normalize180, Normalize720 to be the same + // as Normalize90 (i.e. inclusive of both sides of the range)? + + // [0,360) (-360,0] [-90,90] (-180,180] [-360,360) + // Original Normalized NormNeg Norm90 Norm180 Norm720 + { 90.0, 90.0, -270.0, 90.0, 90.0, 90.0 }, + { -90.0, 270.0, -90.0, -90.0, -90.0, -90.0 }, + { 135.0, 135.0, -225.0, -45.0, 135.0, 135.0 }, + { -135.0, 225.0, -135.0, 45.0, -135.0, -135.0 }, + { 180.0, 180.0, -180.0, 0.0, 180.0, 180.0 }, + { -180.0, 180.0, -180.0, 0.0, 180.0, -180.0 }, + { 360.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, + { -360.0, 0.0, 0.0, 0.0, 0.0, -360.0 }, + { 390.0, 30.0, -330.0, 30.0, 30.0, 30.0 }, + { -390.0, 330.0, -30.0, -30.0, -30.0, -30.0 }, + { 720.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, + { -720.0, 0.0, 0.0, 0.0, 0.0, -360.0 }, +}; + + +BOOST_AUTO_TEST_CASE( Normalize ) +{ + for( const auto& c : normalize_cases ) + { + BOOST_TEST_INFO_SCOPE( "Original angle: " << c.m_Angle << " degrees" ); + + EDA_ANGLE normalized( c.m_Angle, DEGREES_T ); + normalized.Normalize(); + + EDA_ANGLE normalizedNegative( c.m_Angle, DEGREES_T ); + normalizedNegative.NormalizeNegative(); + + EDA_ANGLE normalized90( c.m_Angle, DEGREES_T ); + normalized90.Normalize90(); + + EDA_ANGLE normalized180( c.m_Angle, DEGREES_T ); + normalized180.Normalize180(); + + EDA_ANGLE normalized720( c.m_Angle, DEGREES_T ); + normalized720.Normalize720(); + + BOOST_CHECK_EQUAL( normalized.AsDegrees(), c.m_ExpNormalized ); + BOOST_CHECK_EQUAL( normalizedNegative.AsDegrees(), c.m_ExpNormalizedNegative ); + BOOST_CHECK_EQUAL( normalized90.AsDegrees(), c.m_ExpNormalized90 ); + BOOST_CHECK_EQUAL( normalized180.AsDegrees(), c.m_ExpNormalized180 ); + BOOST_CHECK_EQUAL( normalized720.AsDegrees(), c.m_ExpNormalized720 ); + } +} + + +BOOST_AUTO_TEST_SUITE_END()