2014-03-18 15:55:06 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 CERN
|
2022-02-22 21:00:48 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-03-18 15:55:06 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-05-20 10:23:32 +00:00
|
|
|
#include "tool/edit_constraints.h"
|
2019-05-08 18:56:03 +00:00
|
|
|
#include "tool/edit_points.h"
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
#include <geometry/seg.h>
|
2022-01-20 16:39:28 +00:00
|
|
|
#include <trigo.h>
|
2015-07-17 08:26:48 +00:00
|
|
|
|
2021-07-13 15:10:18 +00:00
|
|
|
#include <geometry/geometry_utils.h>
|
2017-09-23 09:20:10 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
void EC_VERTICAL::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
VECTOR2I point = aHandle.GetPosition();
|
2022-02-22 21:00:48 +00:00
|
|
|
|
|
|
|
if( aHandle.GetGridConstraint() == SNAP_TO_GRID )
|
|
|
|
point = aGrid.AlignGrid( point );
|
|
|
|
|
2014-03-18 15:55:06 +00:00
|
|
|
point.x = m_constrainer.GetPosition().x;
|
|
|
|
aHandle.SetPosition( point );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
void EC_HORIZONTAL::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
VECTOR2I point = aHandle.GetPosition();
|
2022-02-22 21:00:48 +00:00
|
|
|
|
|
|
|
if( aHandle.GetGridConstraint() == SNAP_TO_GRID )
|
|
|
|
point = aGrid.AlignGrid( point );
|
|
|
|
|
2014-03-18 15:55:06 +00:00
|
|
|
point.y = m_constrainer.GetPosition().y;
|
|
|
|
aHandle.SetPosition( point );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
void EC_45DEGREE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
|
2021-07-13 15:10:18 +00:00
|
|
|
VECTOR2I newLineVector = GetVectorSnapped45( lineVector );
|
2014-03-18 15:55:06 +00:00
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
if( aHandle.GetGridConstraint() == SNAP_TO_GRID
|
|
|
|
&& ( newLineVector.x == 0 || newLineVector.y == 0 ) )
|
|
|
|
{
|
|
|
|
VECTOR2I snap = aGrid.AlignGrid( m_constrainer.GetPosition() + newLineVector );
|
|
|
|
|
|
|
|
if( newLineVector.x == 0 )
|
|
|
|
aHandle.SetPosition( VECTOR2I( m_constrainer.GetPosition().x, snap.y ) );
|
|
|
|
else
|
|
|
|
aHandle.SetPosition( VECTOR2I( snap.x, m_constrainer.GetPosition().y ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
|
|
|
|
}
|
2014-03-18 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
|
2022-02-22 21:00:48 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ),
|
|
|
|
m_constrainer( aConstrainer )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
m_line = m_constrained.GetPosition() - m_constrainer.GetPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
void EC_LINE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
SEG main( m_constrainer.GetPosition(), m_constrainer.GetPosition() + m_line );
|
2022-02-22 21:00:48 +00:00
|
|
|
|
|
|
|
if( aHandle.GetGridConstraint() == SNAP_TO_GRID
|
|
|
|
&& ( m_line.x == 0 || m_line.y == 0 ) )
|
|
|
|
{
|
|
|
|
VECTOR2I snappedHandle = aGrid.AlignGrid( aHandle.GetPosition() );
|
|
|
|
|
|
|
|
if( m_line.x == 0 )
|
|
|
|
aHandle.SetPosition( VECTOR2I( aHandle.GetPosition().x, snappedHandle.y ) );
|
|
|
|
else
|
|
|
|
aHandle.SetPosition( VECTOR2I( snappedHandle.x, aHandle.GetPosition().y ) );
|
|
|
|
}
|
|
|
|
|
2014-03-18 15:55:06 +00:00
|
|
|
SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
|
|
|
|
|
|
|
|
if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
|
|
|
|
aHandle.SetPosition( *intersect );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
void EC_CIRCLE::Apply( EDIT_POINT& aHandle, const GRID_HELPER& aGrid )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
|
|
|
|
VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
|
|
|
|
|
2022-01-20 16:39:28 +00:00
|
|
|
int radius = centerToEnd.EuclideanNorm();
|
|
|
|
EDA_ANGLE angle( centerToPoint );
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
VECTOR2I newLine( radius, 0 );
|
2022-01-20 16:39:28 +00:00
|
|
|
RotatePoint( newLine, -angle );
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
aHandle.SetPosition( m_center.GetPosition() + newLine );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EC_CONVERGING::EC_CONVERGING( EDIT_LINE& aLine, EDIT_POINTS& aPoints ) :
|
2014-04-02 07:31:35 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_LINE>( aLine ),
|
2022-02-22 21:00:48 +00:00
|
|
|
m_colinearConstraint( nullptr ),
|
|
|
|
m_editPoints( aPoints )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
// Dragged segment endings
|
|
|
|
EDIT_POINT& origin = aLine.GetOrigin();
|
|
|
|
EDIT_POINT& end = aLine.GetEnd();
|
|
|
|
|
|
|
|
// Previous and next points, to make constraining lines (adjacent to the dragged line)
|
2015-01-16 15:44:42 +00:00
|
|
|
EDIT_POINT& prevOrigin = *aPoints.Previous( origin, false );
|
|
|
|
EDIT_POINT& nextEnd = *aPoints.Next( end, false );
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
// Constraints for segments adjacent to the dragged one
|
|
|
|
m_originSideConstraint = new EC_LINE( origin, prevOrigin );
|
|
|
|
m_endSideConstraint = new EC_LINE( end, nextEnd );
|
|
|
|
|
|
|
|
// Store the current vector of the line
|
|
|
|
m_draggedVector = end.GetPosition() - origin.GetPosition();
|
|
|
|
|
|
|
|
// Check for colinearity
|
|
|
|
SEG originSide( origin.GetPosition(), prevOrigin.GetPosition() );
|
|
|
|
SEG endSide( end.GetPosition(), nextEnd.GetPosition() );
|
|
|
|
SEG dragged( origin.GetPosition(), end.GetPosition() );
|
|
|
|
|
2023-03-16 02:41:58 +00:00
|
|
|
// Used to align lines that are almost collinear
|
|
|
|
const int alignAngle = 10;
|
|
|
|
|
|
|
|
m_originCollinear = dragged.Angle( originSide ).AsDegrees() < alignAngle;
|
|
|
|
m_endCollinear = dragged.Angle( endSide ).AsDegrees() < alignAngle;
|
|
|
|
|
|
|
|
if( m_originCollinear )
|
2014-03-18 15:55:06 +00:00
|
|
|
m_colinearConstraint = m_originSideConstraint;
|
2023-03-16 02:41:58 +00:00
|
|
|
else if( m_endCollinear )
|
2014-03-18 15:55:06 +00:00
|
|
|
m_colinearConstraint = m_endSideConstraint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EC_CONVERGING::~EC_CONVERGING()
|
|
|
|
{
|
|
|
|
delete m_originSideConstraint;
|
|
|
|
delete m_endSideConstraint;
|
|
|
|
// m_colinearConstraint should not be freed, it is a pointer to one of the above
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
void EC_CONVERGING::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
|
2014-03-18 15:55:06 +00:00
|
|
|
{
|
|
|
|
// The dragged segment endpoints
|
2014-04-02 13:57:21 +00:00
|
|
|
EDIT_POINT& origin = aHandle.GetOrigin();
|
|
|
|
EDIT_POINT& end = aHandle.GetEnd();
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
if( m_colinearConstraint )
|
|
|
|
{
|
2022-02-22 21:00:48 +00:00
|
|
|
m_colinearConstraint->Apply( origin, aGrid );
|
|
|
|
m_colinearConstraint->Apply( end, aGrid );
|
2014-03-18 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 02:41:58 +00:00
|
|
|
if( m_originCollinear && m_endCollinear )
|
|
|
|
return;
|
|
|
|
|
2014-03-18 15:55:06 +00:00
|
|
|
// The dragged segment
|
|
|
|
SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
|
|
|
|
|
|
|
|
// Do not allow points on the adjacent segments move freely
|
2022-02-22 21:00:48 +00:00
|
|
|
m_originSideConstraint->Apply( aGrid );
|
|
|
|
m_endSideConstraint->Apply( aGrid );
|
2014-03-18 15:55:06 +00:00
|
|
|
|
2015-01-16 15:44:42 +00:00
|
|
|
EDIT_POINT& prevOrigin = *m_editPoints.Previous( origin, false );
|
|
|
|
EDIT_POINT& nextEnd = *m_editPoints.Next( end, false );
|
2014-03-18 15:55:06 +00:00
|
|
|
|
|
|
|
// Two segments adjacent to the dragged segment
|
|
|
|
SEG originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
|
|
|
|
SEG endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
|
|
|
|
|
|
|
|
// First intersection point (dragged segment against origin side)
|
|
|
|
if( OPT_VECTOR2I originIntersect = dragged.IntersectLines( originSide ) )
|
|
|
|
origin.SetPosition( *originIntersect );
|
|
|
|
|
|
|
|
// Second intersection point (dragged segment against end side)
|
|
|
|
if( OPT_VECTOR2I endIntersect = dragged.IntersectLines( endSide ) )
|
|
|
|
end.SetPosition( *endIntersect );
|
|
|
|
|
|
|
|
// Check if adjacent segments intersect (did we dragged the line to the point that it may
|
|
|
|
// create a selfintersecting polygon?)
|
|
|
|
originSide = SEG( origin.GetPosition(), prevOrigin.GetPosition() );
|
|
|
|
endSide = SEG( end.GetPosition(), nextEnd.GetPosition() );
|
|
|
|
|
|
|
|
if( OPT_VECTOR2I originEndIntersect = endSide.Intersect( originSide ) )
|
|
|
|
{
|
2019-06-08 04:33:09 +00:00
|
|
|
// Triangle intersect by definition
|
|
|
|
if( m_editPoints.LinesSize() > 3 )
|
|
|
|
{
|
|
|
|
origin.SetPosition( *originEndIntersect );
|
|
|
|
end.SetPosition( *originEndIntersect );
|
|
|
|
}
|
2014-03-18 15:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-02 13:57:21 +00:00
|
|
|
|
|
|
|
|
2020-08-27 17:31:53 +00:00
|
|
|
EC_PERPLINE::EC_PERPLINE( EDIT_LINE& aLine ) :
|
|
|
|
EDIT_CONSTRAINT<EDIT_LINE>( aLine )
|
|
|
|
{
|
|
|
|
m_mid = aLine.GetPosition();
|
|
|
|
m_line = ( aLine.GetEnd().GetPosition() - aLine.GetOrigin().GetPosition() ).Perpendicular();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-22 21:00:48 +00:00
|
|
|
void EC_PERPLINE::Apply( EDIT_LINE& aHandle, const GRID_HELPER& aGrid )
|
2020-08-27 17:31:53 +00:00
|
|
|
{
|
|
|
|
SEG main( m_mid, m_mid + m_line );
|
|
|
|
SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
|
|
|
|
|
|
|
|
if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
|
|
|
|
aHandle.SetPosition( *intersect );
|
|
|
|
|
|
|
|
VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
|
|
|
|
|
|
|
|
aHandle.GetOrigin().SetPosition( aHandle.GetOrigin().GetPosition() );
|
|
|
|
aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
|
|
|
|
}
|