pns_item.h 7.66 KB
Newer Older
1 2 3
/*
 * KiRouter - a push-and-(sometimes-)shove PCB router
 *
4
 * Copyright (C) 2013-2014 CERN
5
 * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6
 *
7 8 9 10
 * 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.
11
 *
12 13 14 15
 * 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.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25 26 27 28
 */

#ifndef __PNS_ITEM_H
#define __PNS_ITEM_H

#include <math/vector2d.h>

#include <geometry/shape.h>
#include <geometry/shape_line_chain.h>

29 30
#include "trace.h"

31 32
#include "pns_layerset.h"

33
class BOARD_CONNECTED_ITEM;
34 35
class PNS_NODE;

36 37 38 39 40 41
enum LineMarker {
    MK_HEAD         = ( 1 << 0 ),
    MK_VIOLATION    = ( 1 << 3 ),
    MK_LOCKED       = ( 1 << 4 )
};

42 43 44 45 46 47
/**
 * Class PNS_ITEM
 *
 * Base class for PNS router board items. Implements the shared properties of all PCB items -
 * net, spanned layers, geometric shape & refererence to owning model.
 */
48
class PNS_ITEM
49 50
{
public:
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    static const int UnusedNet = INT_MAX;

    ///> Supported item types
    enum PnsKind
    {
        SOLID   = 1,
        LINE    = 2,
        JOINT   = 4,
        SEGMENT = 8,
        VIA     = 16,
        ANY     = 0xff
    };

    PNS_ITEM( PnsKind aKind )
    {
        m_net = UnusedNet;
        m_movable = true;
        m_kind = aKind;
        m_parent = NULL;
        m_owner = NULL;
71 72
        m_marker = 0;
        m_rank = -1;
73 74 75 76 77 78 79 80 81 82
    }

    PNS_ITEM( const PNS_ITEM& aOther )
    {
        m_layers = aOther.m_layers;
        m_net = aOther.m_net;
        m_movable = aOther.m_movable;
        m_kind = aOther.m_kind;
        m_parent = aOther.m_parent;
        m_owner = NULL;
83 84
        m_marker = aOther.m_marker;
        m_rank = aOther.m_rank;
85 86 87 88
    }

    virtual ~PNS_ITEM();

89 90 91 92 93
    /**
     * Function Clone()
     *
     * Returns a deep copy of the item 
     */
94
    virtual PNS_ITEM* Clone() const = 0;
95

96 97 98 99 100 101 102 103
    /*
     * Function Hull()
     *
     * Returns a convex polygon "hull" of a the item, that is used as the walk-around
     * path.
     * @param aClearance defines how far from the body of the item the hull should be,
     * @param aWalkaroundThickness is the width of the line that walks around this hull.
     */
104 105 106
    virtual const SHAPE_LINE_CHAIN Hull( int aClearance = 0, int aWalkaroundThickness = 0 ) const
    {
        return SHAPE_LINE_CHAIN();
107
    }
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    /**
     * Function Kind()
     *
     * Returns the type (kind) of the item
     */
    PnsKind Kind() const 
    { 
        return m_kind;
    }
    
    /**
     * Function OfKind()
     *
     * Returns true if the item's type matches the mask aKindMask.
     */
    bool OfKind( int aKindMask ) const 
    {
126
        return ( aKindMask & m_kind ) != 0;
127
    }
128

129 130 131 132 133 134
    /**
     * Function KindStr()
     *
     * Returns the kind of the item, as string
     */
    const std::string KindStr() const;
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    /**
     * Function SetParent()
     *
     * Sets the corresponding parent object in the host application's model.
     */
    void SetParent( BOARD_CONNECTED_ITEM* aParent ) 
    {
        m_parent = aParent;
    }
    
    /**
     * Function Parent()
     *
     * Returns the corresponding parent object in the host application's model.
     */
    BOARD_CONNECTED_ITEM* Parent() const 
    { 
        return m_parent; 
    }
155

156 157 158 159 160 161 162 163 164
    /**
     * Function SetNet()
     *
     * Sets the item's net to aNet
     */
    void SetNet( int aNet ) 
    { 
        m_net = aNet; 
    }
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    /**
     * Function Net()
     *
     * Returns the item's net.
     */
    int Net() const 
    { 
        return m_net; 
    }

    /**
     * Function SetLayers()
     *
     * Sets the layers spanned by the item to aLayers.
     */
    void SetLayers( const PNS_LAYERSET& aLayers ) 
    { 
        m_layers = aLayers; 
    }
    
    /**
     * Function SetLayer()
     *
     * Sets the layers spanned by the item to a single layer aLayer.
     */
191 192 193 194 195
    void SetLayer( int aLayer )
    {
        m_layers = PNS_LAYERSET( aLayer, aLayer );
    }

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    /** 
     * Function Layers()
     *
     * Returns the contiguous set of layers spanned by the item.
     */
    const PNS_LAYERSET& Layers() const 
    { 
        return m_layers; 
    }
    
    /**
     * Function Layer()
     *
     * Returns the item's layer, for single-layered items only.
     */
    virtual int Layer() const
    { 
        return Layers().Start();
    }

    /**
     * Function LayersOverlap()
     *
     * Returns true if the set of layers spanned by aOther overlaps our
     * layers.
     */
222
    bool LayersOverlap( const PNS_ITEM* aOther ) const
223 224 225
    {
        return Layers().Overlaps( aOther->Layers() );
    }
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    /**
     * Functon SetOwner()
     *
     * Sets the node that owns this item. An item can belong to a single 
     * PNS_NODE or stay unowned.
     */
    void SetOwner( PNS_NODE* aOwner ) 
    { 
        m_owner = aOwner; 
    }
    
    /**
     * Function BelongsTo()
     *
     * Returns true if the item is owned by the node aNode.
     */
    bool BelongsTo( PNS_NODE* aNode ) const 
    { 
        return m_owner == aNode; 
    }
    
    /**
     * Function Owner()
     * 
     * Returns the owner of this item, or NULL if there's none.
     */
    PNS_NODE* Owner() const { return m_owner; }
254

255 256 257 258 259 260 261 262 263 264 265 266 267 268
    /**
     * Function Collide()
     *
     * Checks for a collision (clearance violation) with between us and item aOther. 
     * Collision checking takes all PCB stuff into accound (layers, nets, DRC rules).
     * Optionally returns a minimum translation vector for force propagation
     * algorithm.
     *
     * @param aOther item to check collision against
     * @param aClearance desired clearance
     * @param aNeedMTV when true, the minimum translation vector is calculated
     * @param aMTV the minimum translation vector
     * @param true, if a collision was found.
     */
269 270 271
    virtual bool Collide( const PNS_ITEM* aOther, int aClearance, bool aNeedMTV,
            VECTOR2I& aMTV ) const;

272 273 274 275 276
    /**
     * Function Collide()
     *
     * A shortcut for PNS_ITEM::Colllide() without MTV stuff.
     */
277 278 279 280 281 282 283
    bool Collide( const PNS_ITEM* aOther, int aClearance ) const
    {
        VECTOR2I dummy;

        return Collide( aOther, aClearance, false, dummy );
    }

284 285 286 287 288 289 290
    /**
     * Function Shape()
     *
     * Returns the geometrical shape of the item. Used
     * for collision detection & spatial indexing.
     */
    virtual const SHAPE* Shape() const
291 292 293
    {
        return NULL;
    }
294

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    virtual void Mark(int aMarker) 
    {
        m_marker = aMarker;
    }

    virtual void Unmark () 
    {
        m_marker = 0;
    }

    virtual int Marker() const 
    {
        return m_marker;
    }

310
    virtual void SetRank( int aRank )
311 312 313 314 315 316 317 318 319
    {
        m_rank = aRank;
    }

    virtual int Rank() const 
    {
        return m_rank;
    }

320
    virtual VECTOR2I Anchor( int n ) const
321 322
    { 
        return VECTOR2I ();
323
    }
324 325 326 327 328 329
    
    virtual int AnchorCount() const 
    { 
        return 0; 
    }

330
private:
331 332
    bool collideSimple( const PNS_ITEM* aOther, int aClearance, bool aNeedMTV,
            VECTOR2I& aMTV ) const;
333 334

protected:
335
    PnsKind                 m_kind;
336

337 338
    BOARD_CONNECTED_ITEM*   m_parent;
    PNS_NODE*               m_owner;
339
    PNS_LAYERSET            m_layers;
340

341 342 343 344
    bool                    m_movable;
    int                     m_net;
    int                     m_marker;
    int                     m_rank;
345 346
};

347
#endif    // __PNS_ITEM_H