kicad/pcbnew/ratsnest_data.h

311 lines
9.2 KiB
C++

/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2013-2015 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
*/
/**
* @file ratsnest_data.h
* @brief Class that computes missing connections on a PCB.
*/
#ifndef RATSNEST_DATA_H
#define RATSNEST_DATA_H
#include <core/typeinfo.h>
#include <math/box2.h>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <ttl/halfedge/hetriang.h>
#include <ttl/halfedge/hetraits.h>
#include <connectivity_algo.h>
class BOARD;
class BOARD_ITEM;
class BOARD_CONNECTED_ITEM;
class CN_CLUSTER;
class CN_CONNECTIVITY_ALGO;
struct RN_NODE_OR_FILTER;
struct RN_NODE_AND_FILTER;
#if 0
///> General interface for filtering out nodes in search functions.
struct RN_NODE_FILTER : public std::unary_function<const RN_NODE_PTR&, bool>
{
virtual ~RN_NODE_FILTER() {}
virtual bool operator()( const RN_NODE_PTR& aNode ) const
{
return true; // By default everything passes
}
friend RN_NODE_AND_FILTER operator&&( const RN_NODE_FILTER& aFilter1, const RN_NODE_FILTER& aFilter2 );
friend RN_NODE_OR_FILTER operator||( const RN_NODE_FILTER& aFilter1, const RN_NODE_FILTER& aFilter2 );
};
RN_NODE_AND_FILTER operator&&( const RN_NODE_FILTER& aFilter1, const RN_NODE_FILTER& aFilter2 );
RN_NODE_OR_FILTER operator||( const RN_NODE_FILTER& aFilter1, const RN_NODE_FILTER& aFilter2 );
///> Leaves nodes that can be a ratsnest line target
struct LINE_TARGET : public RN_NODE_FILTER
{
bool operator()( const RN_NODE_PTR& aNode ) const override
{
return true;
}
};
///> Leaves nodes that can be a ratsnest line target and have a specific tag
struct LINE_TARGET_SAME_TAG : public RN_NODE_FILTER
{
LINE_TARGET_SAME_TAG( int aTag ) :
m_tag( aTag )
{}
bool operator()( const RN_NODE_PTR& aNode ) const override
{
return aNode->GetTag() == m_tag;
}
private:
int m_tag;
};
struct LINE_TARGET_DIFF_TAG : public RN_NODE_FILTER
{
LINE_TARGET_DIFF_TAG( int aTag ) :
m_tag( aTag )
{}
bool operator()( const RN_NODE_PTR& aNode ) const override
{
return aNode->GetTag() != m_tag;
}
private:
int m_tag;
};
struct RN_NODE_AND_FILTER : public RN_NODE_FILTER
{
RN_NODE_AND_FILTER( const RN_NODE_FILTER& aFilter1, const RN_NODE_FILTER& aFilter2 ) :
m_filter1( aFilter1 ), m_filter2( aFilter2 )
{}
bool operator()( const RN_NODE_PTR& aNode ) const override
{
return m_filter1( aNode ) && m_filter2( aNode );
}
private:
const RN_NODE_FILTER& m_filter1;
const RN_NODE_FILTER& m_filter2;
};
struct RN_NODE_OR_FILTER : public RN_NODE_FILTER
{
RN_NODE_OR_FILTER( const RN_NODE_FILTER& aFilter1, const RN_NODE_FILTER& aFilter2 ) :
m_filter1( aFilter1 ), m_filter2( aFilter2 )
{}
bool operator()( const RN_NODE_PTR& aNode ) const override
{
return m_filter1( aNode ) || m_filter2( aNode );
}
private:
const RN_NODE_FILTER& m_filter1;
const RN_NODE_FILTER& m_filter2;
};
///> Functor comparing if two nodes are equal by their coordinates. It is required to make set of
///> shared pointers work properly.
struct RN_NODE_COMPARE : std::binary_function<CN_ANCHOR_PTR, CN_ANCHOR_PTR, bool>
{
bool operator()( const CN_ANCHOR_PTR& aNode1, const CN_ANCHOR_PTR& aNode2 ) const
{
if ( aNode1->GetY() < aNode2->GetY() )
return true;
else if ( aNode1->GetY() == aNode2->GetY() )
{
if ( aNode1->GetX() == aNode2->GetX() )
return aNode1->GetCluster() < aNode2->GetCluster();
else
return aNode1->GetX() < aNode2->GetX();
}
return false;
}
};
#endif
/**
* Class RN_NET
* Describes ratsnest for a single net.
*/
class RN_NET
{
public:
///> Default constructor.
RN_NET();
/**
* Function SetVisible()
* Sets state of the visibility flag.
* @param aEnabled is new state. True if ratsnest for a given net is meant to be displayed,
* false otherwise.
*/
void SetVisible( bool aEnabled );
/**
* Function MarkDirty()
* Marks ratsnest for given net as 'dirty', i.e. requiring recomputation.
*/
void MarkDirty()
{
m_dirty = true;
}
/**
* Function IsDirty()
* Returns state of the 'dirty' flag, indicating that ratsnest for a given net is invalid
* and requires an update.
* @return True if ratsnest requires recomputation, false otherwise.
*/
bool IsDirty() const
{
return m_dirty;
}
/**
* Function GetUnconnected()
* Returns pointer to a vector of edges that makes ratsnest for a given net.
* @return Pointer to a vector of edges that makes ratsnest for a given net.
*/
const std::vector<CN_EDGE> GetUnconnected() const
{
return m_rnEdges;
}
/**
* Function Update()
* Recomputes ratsnest for a net.
*/
void Update();
void Clear();
void AddCluster( std::shared_ptr<CN_CLUSTER> aCluster );
unsigned int GetNodeCount() const;
/**
* Function GetNodes()
* Returns list of nodes that are associated with a given item.
* @param aItem is an item for which the list is generated.
* @return List of associated nodes.
*/
std::list<CN_ANCHOR_PTR> GetNodes( const BOARD_CONNECTED_ITEM* aItem ) const;
const std::vector<CN_EDGE>& GetEdges() const
{
return m_rnEdges;
}
/**
* Function GetAllItems()
* Adds all stored items to a list.
* @param aOutput is the list that will have items added.
* @param aType determines the type of added items.
*/
void GetAllItems( std::list<BOARD_CONNECTED_ITEM*>& aOutput, const KICAD_T aTypes[] ) const;
/**
* Function GetClosestNode()
* Returns a single node that lies in the shortest distance from a specific node.
* @param aNode is the node for which the closest node is searched.
*/
const CN_ANCHOR_PTR GetClosestNode( const CN_ANCHOR_PTR& aNode ) const;
bool NearestBicoloredPair( const RN_NET& aOtherNet, CN_ANCHOR_PTR& aNode1, CN_ANCHOR_PTR& aNode2 ) const;
/**
* Function GetClosestNode()
* Returns a single node that lies in the shortest distance from a specific node and meets
* selected filter criterion.
* @param aNode is the node for which the closest node is searched.
* @param aFilter is a functor that filters nodes.
*/
/*const CN_ANCHOR_PTR GetClosestNode( const RN_NODE_PTR& aNode,
const RN_NODE_FILTER& aFilter ) const;*/
/**
* Function GetClosestNodes()
* Returns list of nodes sorted by the distance from a specific node.
* @param aNode is the node for which the closest nodes are searched.
* @param aNumber is asked number of returned nodes. If it is negative then all nodes that
* belong to the same net are returned. If asked number is greater than number of possible
* nodes then the size of list is limited to number of possible nodes.
*/
//std::list<RN_NODE_PTR> GetClosestNodes( const RN_NODE_PTR& aNode, int aNumber = -1 ) const;
/**
* Function GetClosestNodes()
* Returns filtered list of nodes sorted by the distance from a specific node.
* @param aNode is the node for which the closest nodes are searched.
* @param aFilter is a functor that filters nodes.
* @param aNumber is asked number of returned nodes. If it is negative then all nodes that
* belong to the same net are returned. If asked number is greater than number of possible
* nodes then the size of list is limited to number of possible nodes.
*/
//std::list<RN_NODE_PTR> GetClosestNodes( const RN_NODE_PTR& aNode,
// const RN_NODE_FILTER& aFilter, int aNumber = -1 ) const;
protected:
///> Recomputes ratsnset from scratch.
void compute();
///> Vector of nodes
std::vector<CN_ANCHOR_PTR> m_nodes;
///> Vector of edges that make pre-defined connections
std::vector<CN_EDGE> m_boardEdges;
///> Vector of edges that makes ratsnest for a given net.
std::vector<CN_EDGE> m_rnEdges;
///> Flag indicating necessity of recalculation of ratsnest for a net.
bool m_dirty;
class TRIANGULATOR_STATE;
std::shared_ptr<TRIANGULATOR_STATE> m_triangulator;
};
#endif /* RATSNEST_DATA_H */