2013-11-25 15:50:03 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
2017-03-22 13:43:10 +00:00
|
|
|
* Copyright (C) 2013-2017 CERN
|
2022-09-29 16:07:42 +00:00
|
|
|
* Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-06-18 02:25:46 +00:00
|
|
|
*
|
2013-11-25 15:50:03 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
2017-03-22 13:43:10 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2013-11-25 15:50:03 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file ratsnest_data.cpp
|
|
|
|
* @brief Class that computes missing connections on a PCB.
|
|
|
|
*/
|
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
#ifdef PROFILE
|
|
|
|
#include <profile.h>
|
|
|
|
#endif
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2020-06-16 18:15:14 +00:00
|
|
|
#include <ratsnest/ratsnest_data.h>
|
2016-06-29 10:23:11 +00:00
|
|
|
#include <functional>
|
|
|
|
using namespace std::placeholders;
|
2013-11-25 15:50:03 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2020-06-18 02:25:46 +00:00
|
|
|
#include <cassert>
|
2013-11-25 15:50:03 +00:00
|
|
|
#include <limits>
|
2020-06-21 15:56:24 +00:00
|
|
|
|
|
|
|
#include <delaunator.hpp>
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
class disjoint_set
|
2014-01-31 12:19:59 +00:00
|
|
|
{
|
2017-06-23 11:56:28 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
public:
|
|
|
|
disjoint_set( size_t size )
|
|
|
|
{
|
|
|
|
m_data.resize( size );
|
|
|
|
m_depth.resize( size, 0 );
|
2015-06-05 15:49:00 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
for( size_t i = 0; i < size; i++ )
|
|
|
|
m_data[i] = i;
|
|
|
|
}
|
2015-06-05 15:49:00 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
int find( int aVal )
|
|
|
|
{
|
|
|
|
int root = aVal;
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
while( m_data[root] != root )
|
|
|
|
root = m_data[root];
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
// Compress the path
|
|
|
|
while( m_data[aVal] != aVal )
|
|
|
|
{
|
|
|
|
auto& tmp = m_data[aVal];
|
|
|
|
aVal = tmp;
|
|
|
|
tmp = root;
|
|
|
|
}
|
2016-09-13 15:13:16 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
return root;
|
2015-03-09 12:41:34 +00:00
|
|
|
}
|
2013-11-25 15:50:03 +00:00
|
|
|
|
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
bool unite( int aVal1, int aVal2 )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2020-06-18 02:25:46 +00:00
|
|
|
aVal1 = find( aVal1 );
|
|
|
|
aVal2 = find( aVal2 );
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
if( aVal1 != aVal2 )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2020-06-18 02:25:46 +00:00
|
|
|
if( m_depth[aVal1] < m_depth[aVal2] )
|
2014-03-05 13:57:14 +00:00
|
|
|
{
|
2020-06-18 02:25:46 +00:00
|
|
|
m_data[aVal1] = aVal2;
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
2015-03-09 12:41:34 +00:00
|
|
|
else
|
|
|
|
{
|
2020-06-18 02:25:46 +00:00
|
|
|
m_data[aVal2] = aVal1;
|
2016-09-13 15:13:16 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
if( m_depth[aVal1] == m_depth[aVal2] )
|
|
|
|
m_depth[aVal1]++;
|
2015-03-09 12:41:34 +00:00
|
|
|
}
|
2016-09-13 15:13:16 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
return true;
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
return false;
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
private:
|
|
|
|
std::vector<int> m_data;
|
|
|
|
std::vector<int> m_depth;
|
|
|
|
};
|
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
|
2022-09-29 16:07:42 +00:00
|
|
|
void RN_NET::kruskalMST( const std::vector<CN_EDGE> &aEdges )
|
2020-06-18 02:25:46 +00:00
|
|
|
{
|
|
|
|
disjoint_set dset( m_nodes.size() );
|
|
|
|
|
|
|
|
m_rnEdges.clear();
|
|
|
|
|
2020-06-23 02:18:46 +00:00
|
|
|
int i = 0;
|
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
for( const std::shared_ptr<CN_ANCHOR>& node : m_nodes )
|
2020-06-23 02:18:46 +00:00
|
|
|
node->SetTag( i++ );
|
2020-06-18 02:25:46 +00:00
|
|
|
|
2022-09-29 16:07:42 +00:00
|
|
|
for( const CN_EDGE& tmp : aEdges )
|
2020-06-18 02:25:46 +00:00
|
|
|
{
|
2022-09-29 16:07:42 +00:00
|
|
|
const std::shared_ptr<CN_ANCHOR>& source = tmp.GetSourceNode();
|
|
|
|
const std::shared_ptr<CN_ANCHOR>& target = tmp.GetTargetNode();
|
2020-06-18 02:25:46 +00:00
|
|
|
|
2021-12-01 14:42:44 +00:00
|
|
|
if( dset.unite( source->GetTag(), target->GetTag() ) )
|
2020-06-18 02:25:46 +00:00
|
|
|
{
|
|
|
|
if( tmp.GetWeight() > 0 )
|
|
|
|
m_rnEdges.push_back( tmp );
|
|
|
|
}
|
|
|
|
}
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-06-23 11:56:28 +00:00
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
class RN_NET::TRIANGULATOR_STATE
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
private:
|
2022-02-13 11:54:41 +00:00
|
|
|
std::multiset<std::shared_ptr<CN_ANCHOR>, CN_PTR_CMP> m_allNodes;
|
2017-08-04 12:43:02 +00:00
|
|
|
|
|
|
|
|
2018-01-23 10:55:03 +00:00
|
|
|
// Checks if all nodes in aNodes lie on a single line. Requires the nodes to
|
|
|
|
// have unique coordinates!
|
2022-02-13 11:54:41 +00:00
|
|
|
bool areNodesColinear( const std::vector<std::shared_ptr<CN_ANCHOR>>& aNodes ) const
|
2017-08-04 12:43:02 +00:00
|
|
|
{
|
2018-01-23 10:55:03 +00:00
|
|
|
if ( aNodes.size() <= 2 )
|
|
|
|
return true;
|
2017-08-04 12:43:02 +00:00
|
|
|
|
2020-06-21 15:56:24 +00:00
|
|
|
const VECTOR2I p0( aNodes[0]->Pos() );
|
|
|
|
const VECTOR2I v0( aNodes[1]->Pos() - p0 );
|
2017-08-04 12:43:02 +00:00
|
|
|
|
2018-01-28 21:13:40 +00:00
|
|
|
for( unsigned i = 2; i < aNodes.size(); i++ )
|
2017-08-04 12:43:02 +00:00
|
|
|
{
|
2020-06-21 15:56:24 +00:00
|
|
|
const VECTOR2I v1 = aNodes[i]->Pos() - p0;
|
2017-08-04 12:43:02 +00:00
|
|
|
|
2018-01-23 10:55:03 +00:00
|
|
|
if( v0.Cross( v1 ) != 0 )
|
|
|
|
return false;
|
2017-08-04 12:43:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 10:55:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-04 12:43:02 +00:00
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
void Clear()
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
m_allNodes.clear();
|
|
|
|
}
|
2016-09-13 15:13:16 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
void AddNode( std::shared_ptr<CN_ANCHOR> aNode )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
2020-06-21 15:56:24 +00:00
|
|
|
m_allNodes.insert( aNode );
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
2016-09-13 15:13:16 +00:00
|
|
|
|
2021-12-01 14:42:44 +00:00
|
|
|
void Triangulate( std::vector<CN_EDGE>& mstEdges )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
std::vector<double> node_pts;
|
|
|
|
std::vector<std::shared_ptr<CN_ANCHOR>> anchors;
|
|
|
|
std::vector< std::vector<std::shared_ptr<CN_ANCHOR>> > anchorChains( m_allNodes.size() );
|
2016-09-13 15:13:16 +00:00
|
|
|
|
2020-06-21 15:56:24 +00:00
|
|
|
node_pts.reserve( 2 * m_allNodes.size() );
|
|
|
|
anchors.reserve( m_allNodes.size() );
|
2016-09-13 15:13:16 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
auto addEdge =
|
|
|
|
[&]( const std::shared_ptr<CN_ANCHOR>& src, const std::shared_ptr<CN_ANCHOR>& dst )
|
|
|
|
{
|
|
|
|
mstEdges.emplace_back( src, dst, src->Dist( *dst ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<CN_ANCHOR> prev = nullptr;
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
for( const std::shared_ptr<CN_ANCHOR>& n : m_allNodes )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
|
|
|
if( !prev || prev->Pos() != n->Pos() )
|
|
|
|
{
|
2020-06-21 15:56:24 +00:00
|
|
|
node_pts.push_back( n->Pos().x );
|
|
|
|
node_pts.push_back( n->Pos().y );
|
|
|
|
anchors.push_back( n );
|
|
|
|
prev = n;
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
2015-09-01 11:44:07 +00:00
|
|
|
|
2020-06-21 15:56:24 +00:00
|
|
|
anchorChains[anchors.size() - 1].push_back( n );
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
2015-09-01 11:44:07 +00:00
|
|
|
|
2020-06-21 15:56:24 +00:00
|
|
|
if( anchors.size() < 2 )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
2020-06-21 15:56:24 +00:00
|
|
|
return;
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
2020-06-21 15:56:24 +00:00
|
|
|
else if( areNodesColinear( anchors ) )
|
2017-07-01 21:54:17 +00:00
|
|
|
{
|
2018-01-23 10:55:03 +00:00
|
|
|
// special case: all nodes are on the same line - there's no
|
|
|
|
// triangulation for such set. In this case, we sort along any coordinate
|
|
|
|
// and chain the nodes together.
|
2020-06-21 15:56:24 +00:00
|
|
|
for( size_t i = 0; i < anchors.size() - 1; i++ )
|
2022-02-13 11:54:41 +00:00
|
|
|
addEdge( anchors[i], anchors[i + 1] );
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
2017-07-01 21:54:17 +00:00
|
|
|
else
|
|
|
|
{
|
2020-06-21 15:56:24 +00:00
|
|
|
delaunator::Delaunator delaunator( node_pts );
|
|
|
|
auto& triangles = delaunator.triangles;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < triangles.size(); i += 3 )
|
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
addEdge( anchors[triangles[i]], anchors[triangles[i + 1]] );
|
|
|
|
addEdge( anchors[triangles[i + 1]], anchors[triangles[i + 2]] );
|
|
|
|
addEdge( anchors[triangles[i + 2]], anchors[triangles[i]] );
|
2020-06-21 15:56:24 +00:00
|
|
|
}
|
2017-07-01 21:54:17 +00:00
|
|
|
|
2020-06-21 15:56:24 +00:00
|
|
|
for( size_t i = 0; i < delaunator.halfedges.size(); i++ )
|
2017-07-01 21:54:17 +00:00
|
|
|
{
|
2020-06-21 15:56:24 +00:00
|
|
|
if( delaunator.halfedges[i] == delaunator::INVALID_INDEX )
|
|
|
|
continue;
|
2017-07-01 21:54:17 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
addEdge( anchors[triangles[i]], anchors[triangles[delaunator.halfedges[i]]] );
|
2017-07-01 21:54:17 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2020-06-21 15:56:24 +00:00
|
|
|
for( size_t i = 0; i < anchorChains.size(); i++ )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
std::vector<std::shared_ptr<CN_ANCHOR>>& chain = anchorChains[i];
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
if( chain.size() < 2 )
|
|
|
|
continue;
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
std::sort( chain.begin(), chain.end(),
|
2022-02-13 11:54:41 +00:00
|
|
|
[] ( const std::shared_ptr<CN_ANCHOR>& a, const std::shared_ptr<CN_ANCHOR>& b )
|
2021-11-30 14:19:39 +00:00
|
|
|
{
|
|
|
|
return a->GetCluster().get() < b->GetCluster().get();
|
|
|
|
} );
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-06-23 17:22:44 +00:00
|
|
|
for( unsigned int j = 1; j < chain.size(); j++ )
|
2017-03-22 13:43:10 +00:00
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
const std::shared_ptr<CN_ANCHOR>& prevNode = chain[j - 1];
|
|
|
|
const std::shared_ptr<CN_ANCHOR>& curNode = chain[j];
|
2017-03-22 13:43:10 +00:00
|
|
|
int weight = prevNode->GetCluster() != curNode->GetCluster() ? 1 : 0;
|
2020-06-21 15:56:24 +00:00
|
|
|
mstEdges.emplace_back( prevNode, curNode, weight );
|
2017-03-22 13:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-07 13:15:40 +00:00
|
|
|
}
|
2017-03-22 13:43:10 +00:00
|
|
|
};
|
2014-01-07 13:15:40 +00:00
|
|
|
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-03-22 13:51:07 +00:00
|
|
|
RN_NET::RN_NET() : m_dirty( true )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
m_triangulator.reset( new TRIANGULATOR_STATE );
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-06-23 11:56:28 +00:00
|
|
|
|
2022-09-29 16:07:42 +00:00
|
|
|
void RN_NET::compute()
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2016-09-13 15:13:16 +00:00
|
|
|
// Special cases do not need complicated algorithms (actually, it does not work well with
|
|
|
|
// the Delaunay triangulator)
|
2017-03-22 13:43:10 +00:00
|
|
|
if( m_nodes.size() <= 2 )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
m_rnEdges.clear();
|
2013-11-25 15:50:03 +00:00
|
|
|
|
|
|
|
// Check if the only possible connection exists
|
2017-03-22 13:43:10 +00:00
|
|
|
if( m_boardEdges.size() == 0 && m_nodes.size() == 2 )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
auto last = ++m_nodes.begin();
|
2013-11-25 15:50:03 +00:00
|
|
|
|
|
|
|
// There can be only one possible connection, but it is missing
|
2022-09-29 16:07:42 +00:00
|
|
|
CN_EDGE edge ( *m_nodes.begin(), *last );
|
|
|
|
edge.GetSourceNode()->SetTag( 0 );
|
|
|
|
edge.GetTargetNode()->SetTag( 1 );
|
2017-03-22 13:43:10 +00:00
|
|
|
|
|
|
|
m_rnEdges.push_back( edge );
|
2016-09-13 15:13:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
// Set tags to m_nodes as connected
|
2022-02-13 11:54:41 +00:00
|
|
|
for( const std::shared_ptr<CN_ANCHOR>& node : m_nodes )
|
2016-09-13 15:13:16 +00:00
|
|
|
node->SetTag( 0 );
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
m_triangulator->Clear();
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
for( const std::shared_ptr<CN_ANCHOR>& n : m_nodes )
|
2017-03-22 13:43:10 +00:00
|
|
|
m_triangulator->AddNode( n );
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2020-06-21 15:56:24 +00:00
|
|
|
std::vector<CN_EDGE> triangEdges;
|
|
|
|
triangEdges.reserve( m_nodes.size() + m_boardEdges.size() );
|
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
#ifdef PROFILE
|
2022-09-17 18:43:46 +00:00
|
|
|
PROF_TIMER cnt( "triangulate" );
|
2021-11-30 14:19:39 +00:00
|
|
|
#endif
|
2020-06-21 15:56:24 +00:00
|
|
|
m_triangulator->Triangulate( triangEdges );
|
2021-11-30 14:19:39 +00:00
|
|
|
#ifdef PROFILE
|
2017-03-22 13:43:10 +00:00
|
|
|
cnt.Show();
|
2021-11-30 14:19:39 +00:00
|
|
|
#endif
|
2015-07-27 19:45:57 +00:00
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
for( const CN_EDGE& e : m_boardEdges )
|
2020-06-21 15:56:24 +00:00
|
|
|
triangEdges.emplace_back( e );
|
|
|
|
|
|
|
|
std::sort( triangEdges.begin(), triangEdges.end() );
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
// Get the minimal spanning tree
|
|
|
|
#ifdef PROFILE
|
2022-09-17 18:43:46 +00:00
|
|
|
PROF_TIMER cnt2( "mst" );
|
2017-03-22 13:43:10 +00:00
|
|
|
#endif
|
2022-09-29 16:07:42 +00:00
|
|
|
kruskalMST( triangEdges );
|
2017-03-22 13:43:10 +00:00
|
|
|
#ifdef PROFILE
|
|
|
|
cnt2.Show();
|
|
|
|
#endif
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-30 19:04:54 +00:00
|
|
|
void RN_NET::optimizeRNEdges()
|
|
|
|
{
|
|
|
|
auto findZoneAnchor =
|
|
|
|
[&]( const VECTOR2I& aPos, const LSET& aLayerSet,
|
|
|
|
const std::shared_ptr<CN_ANCHOR> aAnchor )
|
|
|
|
{
|
|
|
|
SEG::ecoord closest_dist_sq = ( aAnchor->Pos() - aPos ).SquaredEuclideanNorm();
|
|
|
|
VECTOR2I closest_pt;
|
|
|
|
CN_ITEM* closest_item = nullptr;
|
|
|
|
|
|
|
|
for( CN_ITEM* item : aAnchor->Item()->ConnectedItems() )
|
|
|
|
{
|
|
|
|
CN_ZONE_LAYER* zoneLayer = dynamic_cast<CN_ZONE_LAYER*>( item );
|
|
|
|
|
|
|
|
if( zoneLayer && aLayerSet.test( zoneLayer->Layer() ) )
|
|
|
|
{
|
|
|
|
const std::vector<VECTOR2I>& pts = zoneLayer->GetOutline().CPoints();
|
|
|
|
|
|
|
|
for( VECTOR2I pt : pts )
|
|
|
|
{
|
|
|
|
SEG::ecoord dist_sq = ( pt - aPos ).SquaredEuclideanNorm();
|
|
|
|
|
|
|
|
if( dist_sq < closest_dist_sq )
|
|
|
|
{
|
|
|
|
closest_pt = pt;
|
|
|
|
closest_item = zoneLayer;
|
|
|
|
closest_dist_sq = dist_sq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( closest_item )
|
|
|
|
{
|
|
|
|
closest_item->AddAnchor( closest_pt );
|
|
|
|
return closest_item->GetAnchorItem( closest_item->GetAnchorItemCount() - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return aAnchor;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto findZoneToZoneAnchors =
|
|
|
|
[&]( std::shared_ptr<CN_ANCHOR>& a, std::shared_ptr<CN_ANCHOR>& b )
|
|
|
|
{
|
|
|
|
for( CN_ITEM* itemA : a->Item()->ConnectedItems() )
|
|
|
|
{
|
|
|
|
CN_ZONE_LAYER* zoneLayerA = dynamic_cast<CN_ZONE_LAYER*>( itemA );
|
|
|
|
|
|
|
|
if( !zoneLayerA )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for( CN_ITEM* itemB : b->Item()->ConnectedItems() )
|
|
|
|
{
|
|
|
|
CN_ZONE_LAYER* zoneLayerB = dynamic_cast<CN_ZONE_LAYER*>( itemB );
|
|
|
|
|
|
|
|
if( zoneLayerB && zoneLayerB->Layer() == zoneLayerA->Layer() )
|
|
|
|
{
|
|
|
|
// Process the first matching layer. We don't really care if it's
|
|
|
|
// the "best" layer or not, as anything will be better than the
|
|
|
|
// original anchors (which are connected to the zone and so certainly
|
|
|
|
// don't look like they should have ratsnest lines coming off them).
|
|
|
|
|
|
|
|
VECTOR2I startA = zoneLayerA->GetOutline().GetPoint( 0 );
|
|
|
|
VECTOR2I startB = zoneLayerB->GetOutline().GetPoint( 0 );
|
|
|
|
const SHAPE* shapeA = &zoneLayerA->GetOutline();
|
|
|
|
const SHAPE* shapeB = &zoneLayerB->GetOutline();
|
|
|
|
int startDist = ( startA - startB ).EuclideanNorm();
|
|
|
|
|
|
|
|
VECTOR2I ptA;
|
|
|
|
shapeA->Collide( shapeB, startDist + 10, nullptr, &ptA );
|
|
|
|
zoneLayerA->AddAnchor( ptA );
|
|
|
|
a = zoneLayerA->GetAnchorItem( zoneLayerA->GetAnchorItemCount() - 1 );
|
|
|
|
|
|
|
|
VECTOR2I ptB;
|
|
|
|
shapeB->Collide( shapeA, startDist + 10, nullptr, &ptB );
|
|
|
|
zoneLayerB->AddAnchor( ptB );
|
|
|
|
b = zoneLayerB->GetAnchorItem( zoneLayerB->GetAnchorItemCount() - 1 );
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for( CN_EDGE& edge : m_rnEdges )
|
|
|
|
{
|
|
|
|
std::shared_ptr<CN_ANCHOR> source = edge.GetSourceNode();
|
|
|
|
std::shared_ptr<CN_ANCHOR> target = edge.GetTargetNode();
|
|
|
|
|
|
|
|
if( source->ConnectedItemsCount() == 0 )
|
|
|
|
{
|
|
|
|
edge.SetTargetNode( findZoneAnchor( source->Pos(), source->Parent()->GetLayerSet(),
|
|
|
|
target ) );
|
|
|
|
}
|
|
|
|
else if( target->ConnectedItemsCount() == 0 )
|
|
|
|
{
|
|
|
|
edge.SetSourceNode( findZoneAnchor( target->Pos(), target->Parent()->GetLayerSet(),
|
|
|
|
source ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
findZoneToZoneAnchors( source, target );
|
|
|
|
edge.SetSourceNode( source );
|
|
|
|
edge.SetTargetNode( target );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2022-09-29 16:07:42 +00:00
|
|
|
void RN_NET::Update()
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2022-09-29 16:07:42 +00:00
|
|
|
compute();
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2022-09-30 19:04:54 +00:00
|
|
|
#ifdef PROFILE
|
|
|
|
PROF_TIMER cnt( "optimize" );
|
|
|
|
#endif
|
|
|
|
optimizeRNEdges();
|
|
|
|
#ifdef PROFILE
|
|
|
|
cnt.Show();
|
|
|
|
#endif
|
|
|
|
|
2013-11-25 15:50:03 +00:00
|
|
|
m_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
void RN_NET::Clear()
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
m_rnEdges.clear();
|
|
|
|
m_boardEdges.clear();
|
|
|
|
m_nodes.clear();
|
2013-11-25 15:50:03 +00:00
|
|
|
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2015-03-09 12:41:34 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
void RN_NET::AddCluster( std::shared_ptr<CN_CLUSTER> aCluster )
|
2015-07-03 18:58:12 +00:00
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
std::shared_ptr<CN_ANCHOR> firstAnchor;
|
2015-07-03 18:58:12 +00:00
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
for( CN_ITEM* item : *aCluster )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
std::vector<std::shared_ptr<CN_ANCHOR>>& anchors = item->Anchors();
|
|
|
|
unsigned int nAnchors = dynamic_cast<CN_ZONE_LAYER*>( item ) ? 1 : anchors.size();
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-06-23 11:56:28 +00:00
|
|
|
if( nAnchors > anchors.size() )
|
2017-03-22 13:43:10 +00:00
|
|
|
nAnchors = anchors.size();
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-06-23 17:22:44 +00:00
|
|
|
for( unsigned int i = 0; i < nAnchors; i++ )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
anchors[i]->SetCluster( aCluster );
|
2020-06-23 02:18:46 +00:00
|
|
|
m_nodes.insert( anchors[i] );
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
if( firstAnchor )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
if( firstAnchor != anchors[i] )
|
|
|
|
m_boardEdges.emplace_back( firstAnchor, anchors[i], 0 );
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
2017-03-22 13:43:10 +00:00
|
|
|
else
|
2015-09-01 11:44:07 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
firstAnchor = anchors[i];
|
2015-09-01 11:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-23 11:56:28 +00:00
|
|
|
|
2022-10-05 00:00:08 +00:00
|
|
|
bool RN_NET::NearestBicoloredPair( RN_NET* aOtherNet, VECTOR2I& aPos1, VECTOR2I& aPos2 ) const
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2017-03-22 13:43:10 +00:00
|
|
|
bool rv = false;
|
2013-11-25 15:50:03 +00:00
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
SEG::ecoord distMax_sq = VECTOR2I::ECOORD_MAX;
|
2020-06-23 23:48:24 +00:00
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
auto verify =
|
|
|
|
[&]( const std::shared_ptr<CN_ANCHOR>& aTestNode1,
|
|
|
|
const std::shared_ptr<CN_ANCHOR>& aTestNode2 )
|
2020-06-23 23:48:24 +00:00
|
|
|
{
|
2021-11-30 14:19:39 +00:00
|
|
|
VECTOR2I diff = aTestNode1->Pos() - aTestNode2->Pos();
|
|
|
|
SEG::ecoord dist_sq = diff.SquaredEuclideanNorm();
|
|
|
|
|
|
|
|
if( dist_sq < distMax_sq )
|
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
rv = true;
|
2021-11-30 14:19:39 +00:00
|
|
|
distMax_sq = dist_sq;
|
2022-10-05 00:00:08 +00:00
|
|
|
aPos1 = aTestNode1->Pos();
|
|
|
|
aPos2 = aTestNode2->Pos();
|
2021-11-30 14:19:39 +00:00
|
|
|
}
|
|
|
|
};
|
2020-06-23 23:48:24 +00:00
|
|
|
|
2022-10-05 00:00:08 +00:00
|
|
|
std::multiset<std::shared_ptr<CN_ANCHOR>, CN_PTR_CMP> nodes_b;
|
|
|
|
|
|
|
|
std::copy_if( m_nodes.begin(), m_nodes.end(), std::inserter( nodes_b, nodes_b.end() ),
|
|
|
|
[]( const std::shared_ptr<CN_ANCHOR> &aVal )
|
|
|
|
{ return !aVal->GetNoLine(); } );
|
|
|
|
|
2020-06-24 22:17:01 +00:00
|
|
|
/// Sweep-line algorithm to cut the number of comparisons to find the closest point
|
|
|
|
///
|
|
|
|
/// Step 1: The outer loop needs to be the subset (selected nodes) as it is a linear search
|
2022-10-05 00:00:08 +00:00
|
|
|
for( const std::shared_ptr<CN_ANCHOR>& nodeA : aOtherNet->m_nodes )
|
2014-01-28 15:30:58 +00:00
|
|
|
{
|
2022-10-05 00:00:08 +00:00
|
|
|
|
2020-06-18 02:25:46 +00:00
|
|
|
if( nodeA->GetNoLine() )
|
|
|
|
continue;
|
|
|
|
|
2020-06-24 22:17:01 +00:00
|
|
|
/// Step 2: O( log n ) search to identify a close element ordered by x
|
|
|
|
/// The fwd_it iterator will move forward through the elements while
|
|
|
|
/// the rev_it iterator will move backward through the same set
|
2022-10-05 00:00:08 +00:00
|
|
|
auto fwd_it = nodes_b.lower_bound( nodeA );
|
2020-06-23 17:32:58 +00:00
|
|
|
auto rev_it = std::make_reverse_iterator( fwd_it );
|
|
|
|
|
2022-10-05 00:00:08 +00:00
|
|
|
for( ; fwd_it != nodes_b.end(); ++fwd_it )
|
2020-06-23 17:32:58 +00:00
|
|
|
{
|
2021-01-08 00:26:32 +00:00
|
|
|
const std::shared_ptr<CN_ANCHOR>& nodeB = *fwd_it;
|
2020-06-23 17:32:58 +00:00
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
SEG::ecoord distX_sq = SEG::Square( nodeA->Pos().x - nodeB->Pos().x );
|
2020-06-23 17:32:58 +00:00
|
|
|
|
2020-06-24 22:17:01 +00:00
|
|
|
/// As soon as the x distance (primary sort) is larger than the smallest distance,
|
|
|
|
/// stop checking further elements
|
2021-11-30 14:19:39 +00:00
|
|
|
if( distX_sq > distMax_sq )
|
2020-06-23 17:32:58 +00:00
|
|
|
break;
|
|
|
|
|
2020-06-23 23:48:24 +00:00
|
|
|
verify( nodeA, nodeB );
|
2020-06-23 17:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 22:17:01 +00:00
|
|
|
/// Step 3: using the same starting point, check points backwards for closer points
|
2022-10-05 00:00:08 +00:00
|
|
|
for( ; rev_it != nodes_b.rend(); ++rev_it )
|
2013-11-25 15:50:03 +00:00
|
|
|
{
|
2021-01-08 00:26:32 +00:00
|
|
|
const std::shared_ptr<CN_ANCHOR>& nodeB = *rev_it;
|
2020-06-23 17:32:58 +00:00
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
SEG::ecoord distX_sq = SEG::Square( nodeA->Pos().x - nodeB->Pos().x );
|
2020-06-23 17:32:58 +00:00
|
|
|
|
2021-11-30 14:19:39 +00:00
|
|
|
if( distX_sq > distMax_sq )
|
2020-06-23 17:32:58 +00:00
|
|
|
break;
|
|
|
|
|
2020-06-23 23:48:24 +00:00
|
|
|
verify( nodeA, nodeB );
|
2014-02-28 10:12:55 +00:00
|
|
|
}
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:43:10 +00:00
|
|
|
return rv;
|
2013-11-25 15:50:03 +00:00
|
|
|
}
|
2014-01-31 17:05:11 +00:00
|
|
|
|