2017-02-26 20:08:45 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-01-25 12:42:36 +00:00
|
|
|
* Copyright (C) 2017-2021 Kicad Developers, see AUTHORS.txt for contributors.
|
2017-02-26 20:08:45 +00:00
|
|
|
*
|
|
|
|
* 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 <preview_items/arc_geom_manager.h>
|
|
|
|
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
2022-01-20 16:39:28 +00:00
|
|
|
#include <geometry/eda_angle.h>
|
|
|
|
#include <trigo.h>
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
using namespace KIGFX::PREVIEW;
|
|
|
|
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Snap an angle to the nearest 45 degrees
|
2022-01-20 16:39:28 +00:00
|
|
|
static EDA_ANGLE snapAngle( const EDA_ANGLE& aAngle )
|
2017-02-26 20:08:45 +00:00
|
|
|
{
|
2022-01-20 16:39:28 +00:00
|
|
|
return ANGLE_45 * KiROUND( aAngle / ANGLE_45 );
|
2017-02-26 20:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ARC_GEOM_MANAGER::acceptPoint( const VECTOR2I& aPt )
|
|
|
|
{
|
|
|
|
switch( getStep() )
|
|
|
|
{
|
2020-11-07 21:21:56 +00:00
|
|
|
case SET_ORIGIN: return setOrigin( aPt );
|
|
|
|
case SET_START: return setStart( aPt );
|
|
|
|
case SET_ANGLE: return setEnd( aPt );
|
|
|
|
case COMPLETE: return false;
|
2017-02-26 20:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ARC_GEOM_MANAGER::SetClockwise( bool aCw )
|
|
|
|
{
|
|
|
|
m_clockwise = aCw;
|
2020-07-03 15:05:17 +00:00
|
|
|
m_directionLocked = true;
|
2017-02-26 20:08:45 +00:00
|
|
|
setGeometryChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ARC_GEOM_MANAGER::ToggleClockwise()
|
|
|
|
{
|
|
|
|
m_clockwise = !m_clockwise;
|
2020-07-03 15:05:17 +00:00
|
|
|
m_directionLocked = true;
|
2017-02-26 20:08:45 +00:00
|
|
|
setGeometryChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VECTOR2I ARC_GEOM_MANAGER::GetOrigin() const
|
|
|
|
{
|
|
|
|
return m_origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VECTOR2I ARC_GEOM_MANAGER::GetStartRadiusEnd() const
|
|
|
|
{
|
2022-01-20 16:39:28 +00:00
|
|
|
VECTOR2I vec( m_radius, 0 );
|
|
|
|
RotatePoint( vec, -m_startAngle );
|
|
|
|
return m_origin +vec;
|
2017-02-26 20:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VECTOR2I ARC_GEOM_MANAGER::GetEndRadiusEnd() const
|
|
|
|
{
|
2022-01-20 16:39:28 +00:00
|
|
|
VECTOR2I vec( m_radius, 0 );
|
|
|
|
RotatePoint( vec, -m_endAngle );
|
|
|
|
return m_origin + vec;
|
2017-02-26 20:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double ARC_GEOM_MANAGER::GetRadius() const
|
|
|
|
{
|
|
|
|
return m_radius;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-20 16:39:28 +00:00
|
|
|
EDA_ANGLE ARC_GEOM_MANAGER::GetStartAngle() const
|
2017-02-26 20:08:45 +00:00
|
|
|
{
|
2022-01-20 16:39:28 +00:00
|
|
|
EDA_ANGLE angle = m_startAngle;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
if( m_clockwise )
|
2022-01-20 16:39:28 +00:00
|
|
|
angle -= ANGLE_360;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
return -angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-20 16:39:28 +00:00
|
|
|
EDA_ANGLE ARC_GEOM_MANAGER::GetSubtended() const
|
2017-02-26 20:08:45 +00:00
|
|
|
{
|
2022-01-20 16:39:28 +00:00
|
|
|
EDA_ANGLE angle = m_endAngle - m_startAngle;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
if( m_endAngle <= m_startAngle )
|
2022-01-20 16:39:28 +00:00
|
|
|
angle += ANGLE_360;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
if( m_clockwise )
|
2022-01-20 16:39:28 +00:00
|
|
|
angle -= ANGLE_360;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
return -angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ARC_GEOM_MANAGER::setOrigin( const VECTOR2I& aOrigin )
|
|
|
|
{
|
2022-01-20 16:39:28 +00:00
|
|
|
m_origin = aOrigin;
|
|
|
|
m_startAngle = ANGLE_0;
|
|
|
|
m_endAngle = ANGLE_0;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ARC_GEOM_MANAGER::setStart( const VECTOR2I& aEnd )
|
|
|
|
{
|
2020-11-07 21:21:56 +00:00
|
|
|
const VECTOR2I radVec = aEnd - m_origin;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
m_radius = radVec.EuclideanNorm();
|
2022-01-20 16:39:28 +00:00
|
|
|
m_startAngle = EDA_ANGLE( radVec );
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
if( m_angleSnap )
|
|
|
|
m_startAngle = snapAngle( m_startAngle );
|
|
|
|
|
2022-01-20 16:39:28 +00:00
|
|
|
// normalise to 0..360
|
|
|
|
while( m_startAngle < ANGLE_0 )
|
|
|
|
m_startAngle += ANGLE_360;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
m_endAngle = m_startAngle;
|
|
|
|
|
|
|
|
return m_radius != 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ARC_GEOM_MANAGER::setEnd( const VECTOR2I& aCursor )
|
|
|
|
{
|
2020-11-07 21:21:56 +00:00
|
|
|
const VECTOR2I radVec = aCursor - m_origin;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
2022-01-20 16:39:28 +00:00
|
|
|
m_endAngle = EDA_ANGLE( radVec );
|
2017-02-26 20:08:45 +00:00
|
|
|
|
|
|
|
if( m_angleSnap )
|
|
|
|
m_endAngle = snapAngle( m_endAngle );
|
|
|
|
|
2022-01-20 16:39:28 +00:00
|
|
|
// normalise to 0..360
|
|
|
|
while( m_endAngle < ANGLE_0 )
|
|
|
|
m_endAngle += ANGLE_360;
|
2017-02-26 20:08:45 +00:00
|
|
|
|
2020-07-03 15:05:17 +00:00
|
|
|
if( !m_directionLocked )
|
|
|
|
{
|
2022-01-20 16:39:28 +00:00
|
|
|
EDA_ANGLE ccwAngle = m_endAngle - m_startAngle;
|
2020-07-03 15:05:17 +00:00
|
|
|
|
|
|
|
if( m_endAngle <= m_startAngle )
|
2022-01-20 16:39:28 +00:00
|
|
|
ccwAngle += ANGLE_360;
|
2020-07-03 15:05:17 +00:00
|
|
|
|
2022-01-20 16:39:28 +00:00
|
|
|
EDA_ANGLE cwAngle = std::abs( ccwAngle - ANGLE_360 );
|
2020-07-03 15:05:17 +00:00
|
|
|
|
2022-01-20 16:39:28 +00:00
|
|
|
if( std::min( ccwAngle, cwAngle ) >= ANGLE_90 )
|
2020-07-03 15:05:17 +00:00
|
|
|
m_directionLocked = true;
|
|
|
|
else
|
|
|
|
m_clockwise = cwAngle < ccwAngle;
|
|
|
|
}
|
2022-01-20 16:39:28 +00:00
|
|
|
else if( std::abs( GetSubtended() ) < ANGLE_90 )
|
2020-07-03 15:05:17 +00:00
|
|
|
{
|
|
|
|
m_directionLocked = false;
|
|
|
|
}
|
|
|
|
|
2017-02-26 20:08:45 +00:00
|
|
|
// if the end is the same as the start, this is a bad point
|
|
|
|
return m_endAngle != m_startAngle;
|
|
|
|
}
|