1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2013-2015 CERN
5  * Copyright (C) 2019-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
25  */
26 
27 /**
28  * @file ratsnest_data.h
29  * @brief Class that computes missing connections on a PCB.
30  */
31 
32 #ifndef RATSNEST_DATA_H
33 #define RATSNEST_DATA_H
34 
35 #include <core/typeinfo.h>
36 #include <math/box2.h>
37 
38 #include <set>
39 #include <vector>
40 
41 #include <connectivity/connectivity_algo.h>
42 
43 class BOARD_ITEM;
44 class BOARD_CONNECTED_ITEM;
45 class CN_CLUSTER;
46 
47 struct CN_PTR_CMP
48 {
operatorCN_PTR_CMP49     bool operator()( const CN_ANCHOR_PTR& aItem, const CN_ANCHOR_PTR& bItem ) const
50     {
51         if( aItem->Pos().x == bItem->Pos().x )
52             return aItem->Pos().y < bItem->Pos().y;
53         else
54             return aItem->Pos().x < bItem->Pos().x;
55     }
56 };
57 
58 /**
59  * Describe ratsnest for a single net.
60  */
61 class RN_NET
62 {
63 public:
64     RN_NET();
65 
66     /**
67      * Set state of the visibility flag.
68      *
69      * @param aEnabled is new state. True if ratsnest for a given net is meant to be displayed,
70      *                 false otherwise.
71      */
72     void SetVisible( bool aEnabled );
73 
74     /**
75      * Mark ratsnest for given net as 'dirty', i.e. requiring recomputation.
76      */
MarkDirty()77     void MarkDirty() { m_dirty = true; }
78 
79     /**
80      * Return state of the 'dirty' flag, indicating that ratsnest for a given net is invalid
81      * and requires an update.
82      *
83      * @return True if ratsnest requires recomputation, false otherwise.
84      */
IsDirty()85     bool IsDirty() const { return m_dirty; }
86 
87     /**
88      * Return pointer to a vector of edges that makes ratsnest for a given net.
89      */
GetUnconnected()90     const std::vector<CN_EDGE> GetUnconnected() const { return m_rnEdges; }
91 
92     /**
93      * Recompute ratsnest for a net.
94      */
95     void Update();
96     void Clear();
97 
98     void AddCluster( std::shared_ptr<CN_CLUSTER> aCluster );
99 
GetNodeCount()100     unsigned int GetNodeCount() const { return m_nodes.size(); }
101 
GetEdges()102     const std::vector<CN_EDGE>& GetEdges() const { return m_rnEdges; }
103 
104     bool NearestBicoloredPair( const RN_NET& aOtherNet, CN_ANCHOR_PTR& aNode1,
105                                CN_ANCHOR_PTR& aNode2 ) const;
106 
107 protected:
108     ///< Recompute ratsnest from scratch.
109     void compute();
110 
111     ///< Compute the minimum spanning tree using Kruskal's algorithm
112     void kruskalMST( const std::vector<CN_EDGE> &aEdges );
113 
114     ///< Vector of nodes
115     std::multiset<CN_ANCHOR_PTR, CN_PTR_CMP> m_nodes;
116 
117     ///< Vector of edges that make pre-defined connections
118     std::vector<CN_EDGE> m_boardEdges;
119 
120     ///< Vector of edges that makes ratsnest for a given net.
121     std::vector<CN_EDGE> m_rnEdges;
122 
123     ///< Flag indicating necessity of recalculation of ratsnest for a net.
124     bool m_dirty;
125 
126     class TRIANGULATOR_STATE;
127 
128     std::shared_ptr<TRIANGULATOR_STATE> m_triangulator;
129 };
130 
131 #endif /* RATSNEST_DATA_H */
132