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
|
|
|
|
* @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>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
2014-03-18 15:55:06 +00:00
|
|
|
|
2015-07-17 08:26:48 +00:00
|
|
|
#include <common.h>
|
|
|
|
|
2017-09-23 09:20:10 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2014-03-18 15:55:06 +00:00
|
|
|
void EC_VERTICAL::Apply( EDIT_POINT& aHandle )
|
|
|
|
{
|
|
|
|
VECTOR2I point = aHandle.GetPosition();
|
|
|
|
point.x = m_constrainer.GetPosition().x;
|
|
|
|
aHandle.SetPosition( point );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EC_HORIZONTAL::Apply( EDIT_POINT& aHandle )
|
|
|
|
{
|
|
|
|
VECTOR2I point = aHandle.GetPosition();
|
|
|
|
point.y = m_constrainer.GetPosition().y;
|
|
|
|
aHandle.SetPosition( point );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EC_45DEGREE::Apply( EDIT_POINT& aHandle )
|
|
|
|
{
|
|
|
|
// Current line vector
|
|
|
|
VECTOR2I lineVector( aHandle.GetPosition() - m_constrainer.GetPosition() );
|
|
|
|
double angle = lineVector.Angle();
|
|
|
|
|
|
|
|
// Find the closest angle, which is a multiple of 45 degrees
|
2015-07-17 08:26:48 +00:00
|
|
|
double newAngle = KiROUND( angle / ( M_PI / 4.0 ) ) * M_PI / 4.0;
|
2014-03-18 15:55:06 +00:00
|
|
|
VECTOR2I newLineVector = lineVector.Rotate( newAngle - angle );
|
|
|
|
|
|
|
|
aHandle.SetPosition( m_constrainer.GetPosition() + newLineVector );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
|
|
|
|
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
|
|
|
|
{
|
|
|
|
m_line = m_constrained.GetPosition() - m_constrainer.GetPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EC_LINE::Apply( EDIT_POINT& aHandle )
|
|
|
|
{
|
|
|
|
SEG main( m_constrainer.GetPosition(), m_constrainer.GetPosition() + m_line );
|
|
|
|
SEG projection( aHandle.GetPosition(), aHandle.GetPosition() + m_line.Perpendicular() );
|
|
|
|
|
|
|
|
if( OPT_VECTOR2I intersect = projection.IntersectLines( main ) )
|
|
|
|
aHandle.SetPosition( *intersect );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EC_CIRCLE::Apply( EDIT_POINT& aHandle )
|
|
|
|
{
|
|
|
|
VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
|
|
|
|
VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
|
|
|
|
|
|
|
|
int radius = centerToEnd.EuclideanNorm();
|
|
|
|
double angle = centerToPoint.Angle();
|
|
|
|
|
|
|
|
VECTOR2I newLine( radius, 0 );
|
|
|
|
newLine = newLine.Rotate( angle );
|
|
|
|
|
|
|
|
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 ),
|
|
|
|
m_colinearConstraint( NULL ), 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() );
|
|
|
|
|
|
|
|
if( dragged.Collinear( originSide ) )
|
|
|
|
m_colinearConstraint = m_originSideConstraint;
|
|
|
|
else if( dragged.Collinear( endSide ) )
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-02 07:31:35 +00:00
|
|
|
void EC_CONVERGING::Apply( EDIT_LINE& aHandle )
|
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 )
|
|
|
|
{
|
|
|
|
m_colinearConstraint->Apply( origin );
|
|
|
|
m_colinearConstraint->Apply( end );
|
|
|
|
}
|
|
|
|
|
|
|
|
// The dragged segment
|
|
|
|
SEG dragged( origin.GetPosition(), origin.GetPosition() + m_draggedVector );
|
|
|
|
|
|
|
|
// Do not allow points on the adjacent segments move freely
|
|
|
|
m_originSideConstraint->Apply();
|
|
|
|
m_endSideConstraint->Apply();
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
EC_SNAPLINE::EC_SNAPLINE( EDIT_LINE& aLine, V2D_TRANSFORM_FUN aSnapFun ) :
|
2017-09-23 09:20:10 +00:00
|
|
|
EDIT_CONSTRAINT<EDIT_LINE>( aLine ), m_snapFun( std::move(aSnapFun) )
|
2014-04-02 13:57:21 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void EC_SNAPLINE::Apply( EDIT_LINE& aHandle )
|
|
|
|
{
|
|
|
|
VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition();
|
|
|
|
|
|
|
|
aHandle.GetOrigin().SetPosition( m_snapFun( aHandle.GetOrigin().GetPosition() ) );
|
|
|
|
aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta );
|
|
|
|
}
|