2014-05-14 13:53:54 +00:00
|
|
|
/*
|
|
|
|
* KiRouter - a push-and-(sometimes-)shove PCB router
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 CERN
|
|
|
|
* Author: Tomasz Wlostowski <tomasz.wlostowski@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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
#include "pns_dragger.h"
|
|
|
|
#include "pns_shove.h"
|
|
|
|
#include "pns_router.h"
|
|
|
|
|
|
|
|
PNS_DRAGGER::PNS_DRAGGER( PNS_ROUTER* aRouter ) :
|
2014-11-14 19:19:00 +00:00
|
|
|
PNS_ALGO_BASE( aRouter )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
m_world = NULL;
|
|
|
|
m_shove = NULL;
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
PNS_DRAGGER::~PNS_DRAGGER()
|
|
|
|
{
|
|
|
|
if( m_shove )
|
|
|
|
delete m_shove;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
void PNS_DRAGGER::SetWorld( PNS_NODE* aWorld )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
m_world = aWorld;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
bool PNS_DRAGGER::startDragSegment( const VECTOR2D& aP, PNS_SEGMENT* aSeg )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
int w2 = aSeg->Width() / 2;
|
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
m_draggedLine = m_world->AssembleLine( aSeg, &m_draggedSegmentIndex );
|
2014-05-16 11:37:31 +00:00
|
|
|
m_shove->SetInitialLine( m_draggedLine );
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lastValidDraggedLine = *m_draggedLine;
|
|
|
|
m_lastValidDraggedLine.ClearSegmentLinks();
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( ( aP - aSeg->Seg().A ).EuclideanNorm() <= w2 )
|
2014-05-14 13:53:54 +00:00
|
|
|
m_mode = CORNER;
|
2014-05-16 11:37:31 +00:00
|
|
|
else if( ( aP - aSeg->Seg().B ).EuclideanNorm() <= w2 )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
m_draggedSegmentIndex++;
|
2014-05-14 13:53:54 +00:00
|
|
|
m_mode = CORNER;
|
2014-11-14 19:19:00 +00:00
|
|
|
} else
|
2014-05-14 13:53:54 +00:00
|
|
|
m_mode = SEGMENT;
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
bool PNS_DRAGGER::startDragVia( const VECTOR2D& aP, PNS_VIA* aVia )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
m_draggedVia = aVia;
|
|
|
|
m_initialVia = aVia;
|
|
|
|
m_mode = VIA;
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
VECTOR2I p0( aVia->Pos() );
|
2014-11-14 19:19:00 +00:00
|
|
|
PNS_JOINT* jt = m_world->FindJoint( p0, aVia->Layers().Start(), aVia->Net() );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
BOOST_FOREACH( PNS_ITEM* item, jt->LinkList() )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
if( item->OfKind( PNS_ITEM::SEGMENT ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
int segIndex;
|
2014-11-14 19:19:00 +00:00
|
|
|
PNS_SEGMENT* seg = ( PNS_SEGMENT*) item;
|
2014-05-16 11:37:31 +00:00
|
|
|
std::auto_ptr<PNS_LINE> l( m_world->AssembleLine( seg, &segIndex ) );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( segIndex != 0 )
|
|
|
|
l->Reverse();
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
m_origViaConnections.push_back( *l );
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
bool PNS_DRAGGER::Start( const VECTOR2I& aP, PNS_ITEM* aStartItem )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
m_shove = new PNS_SHOVE( m_world, Router() );
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lastNode = NULL;
|
|
|
|
m_draggedItems.Clear();
|
|
|
|
m_currentMode = Settings().Mode();
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
TRACE( 2, "StartDragging: item %p [kind %d]", aStartItem % aStartItem->Kind() );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
switch( aStartItem->Kind() )
|
|
|
|
{
|
|
|
|
case PNS_ITEM::SEGMENT:
|
|
|
|
return startDragSegment( aP, static_cast<PNS_SEGMENT*>( aStartItem ) );
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
case PNS_ITEM::VIA:
|
|
|
|
return startDragVia( aP, static_cast<PNS_VIA*>( aStartItem ) );
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
bool PNS_DRAGGER::dragMarkObstacles( const VECTOR2I& aP )
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
if( m_lastNode )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
delete m_lastNode;
|
|
|
|
m_lastNode = NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
switch( m_mode )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
case SEGMENT:
|
|
|
|
case CORNER:
|
|
|
|
{
|
|
|
|
int thresh = Settings().SmoothDraggedSegments() ? m_draggedLine->Width() / 4 : 0;
|
2014-05-16 11:37:31 +00:00
|
|
|
PNS_LINE tmp( *m_draggedLine );
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( m_mode == SEGMENT )
|
2014-11-14 19:19:00 +00:00
|
|
|
tmp.DragSegment( aP, m_draggedSegmentIndex, thresh );
|
2014-05-14 13:53:54 +00:00
|
|
|
else
|
2014-11-14 19:19:00 +00:00
|
|
|
tmp.DragCorner( aP, m_draggedSegmentIndex, thresh );
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lastNode = m_shove->CurrentNode()->Branch();
|
|
|
|
|
|
|
|
m_lastValidDraggedLine = tmp;
|
|
|
|
m_lastValidDraggedLine.ClearSegmentLinks();
|
|
|
|
m_lastValidDraggedLine.Unmark();
|
2014-11-14 19:19:00 +00:00
|
|
|
|
|
|
|
m_lastNode->Add( &m_lastValidDraggedLine );
|
|
|
|
m_draggedItems = PNS_ITEMSET( &m_lastValidDraggedLine );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
case VIA: // fixme...
|
|
|
|
{
|
|
|
|
m_lastNode = m_shove->CurrentNode()->Branch();
|
2014-11-14 19:19:00 +00:00
|
|
|
dumbDragVia( m_initialVia, m_lastNode, aP );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( Settings().CanViolateDRC() )
|
2014-05-14 13:53:54 +00:00
|
|
|
m_dragStatus = true;
|
|
|
|
else
|
|
|
|
m_dragStatus = !m_world->CheckColliding( m_draggedItems );
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
void PNS_DRAGGER::dumbDragVia( PNS_VIA* aVia, PNS_NODE* aNode, const VECTOR2I& aP )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
// fixme: this is awful.
|
|
|
|
m_draggedVia = aVia->Clone();
|
|
|
|
m_draggedVia->SetPos( aP );
|
|
|
|
m_draggedItems.Clear();
|
2014-05-16 11:37:31 +00:00
|
|
|
m_draggedItems.Add( m_draggedVia );
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
m_lastNode->Remove( aVia );
|
|
|
|
m_lastNode->Add( m_draggedVia );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
BOOST_FOREACH( PNS_LINE &l, m_origViaConnections )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
PNS_LINE origLine( l );
|
2014-05-16 11:37:31 +00:00
|
|
|
PNS_LINE* draggedLine = l.Clone();
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
draggedLine->DragCorner( aP, 0 );
|
|
|
|
draggedLine->ClearSegmentLinks();
|
|
|
|
|
2014-11-14 18:15:58 +00:00
|
|
|
m_draggedItems.Add( draggedLine ); // FIXME: mem leak
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
m_lastNode->Remove( &origLine );
|
|
|
|
m_lastNode->Add( draggedLine );
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
bool PNS_DRAGGER::dragShove( const VECTOR2I& aP )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( m_lastNode )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
delete m_lastNode;
|
|
|
|
m_lastNode = NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
switch( m_mode )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
case SEGMENT:
|
|
|
|
case CORNER:
|
|
|
|
{
|
|
|
|
int thresh = Settings().SmoothDraggedSegments() ? m_draggedLine->Width() / 4 : 0;
|
|
|
|
PNS_LINE tmp( *m_draggedLine );
|
|
|
|
|
|
|
|
if( m_mode == SEGMENT )
|
|
|
|
tmp.DragSegment( aP, m_draggedSegmentIndex, thresh );
|
|
|
|
else
|
|
|
|
tmp.DragCorner( aP, m_draggedSegmentIndex, thresh );
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
PNS_SHOVE::SHOVE_STATUS st = m_shove->ShoveLines( tmp );
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( st == PNS_SHOVE::SH_OK )
|
2014-11-14 19:19:00 +00:00
|
|
|
ok = true;
|
2014-05-16 11:37:31 +00:00
|
|
|
else if( st == PNS_SHOVE::SH_HEAD_MODIFIED )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
tmp = m_shove->NewHead();
|
|
|
|
ok = true;
|
2014-11-14 19:19:00 +00:00
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
m_lastNode = m_shove->CurrentNode()->Branch();
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( ok )
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lastValidDraggedLine = tmp;
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lastValidDraggedLine.ClearSegmentLinks();
|
|
|
|
m_lastValidDraggedLine.Unmark();
|
2014-05-16 11:37:31 +00:00
|
|
|
m_lastNode->Add( &m_lastValidDraggedLine );
|
|
|
|
m_draggedItems = PNS_ITEMSET( &m_lastValidDraggedLine );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
break;
|
2014-11-14 19:19:00 +00:00
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
case VIA:
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
|
|
|
PNS_VIA* newVia;
|
2014-05-16 11:37:31 +00:00
|
|
|
PNS_SHOVE::SHOVE_STATUS st = m_shove->ShoveDraggingVia( m_draggedVia, aP, &newVia );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( st == PNS_SHOVE::SH_OK || st == PNS_SHOVE::SH_HEAD_MODIFIED )
|
2014-05-14 13:53:54 +00:00
|
|
|
ok = true;
|
|
|
|
|
|
|
|
m_lastNode = m_shove->CurrentNode()->Branch();
|
|
|
|
|
|
|
|
if( ok )
|
|
|
|
{
|
|
|
|
m_draggedVia = newVia;
|
|
|
|
m_draggedItems.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
m_dragStatus = ok;
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
bool PNS_DRAGGER::FixRoute()
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
if( m_dragStatus )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
Router()->CommitRouting( CurrentNode() );
|
|
|
|
return true;
|
2014-11-14 19:19:00 +00:00
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool PNS_DRAGGER::Drag( const VECTOR2I& aP )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
switch( m_currentMode )
|
|
|
|
{
|
|
|
|
case RM_MarkObstacles:
|
|
|
|
return dragMarkObstacles( aP );
|
|
|
|
|
|
|
|
case RM_Shove:
|
|
|
|
case RM_Walkaround:
|
|
|
|
case RM_Smart:
|
|
|
|
return dragShove( aP );
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-11-14 19:19:00 +00:00
|
|
|
PNS_NODE* PNS_DRAGGER::CurrentNode() const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
return m_lastNode;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
const PNS_ITEMSET PNS_DRAGGER::Traces()
|
|
|
|
{
|
|
|
|
return m_draggedItems;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
|
|
|
|
PNS_LOGGER* PNS_DRAGGER::Logger()
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
if( m_shove )
|
2014-05-14 13:53:54 +00:00
|
|
|
return m_shove->Logger();
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|