shape.h 4.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 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 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
 */

#ifndef __SHAPE_H
#define __SHAPE_H

#include <math/vector2d.h>
#include <math/box2.h>

#include <geometry/seg.h>

/**
Maciej Suminski's avatar
Maciej Suminski committed
34
 * Enum SHAPE_TYPE
35 36 37
 * Lists all supported shapes
 */

Maciej Suminski's avatar
Maciej Suminski committed
38
enum SHAPE_TYPE
39 40 41 42
{
    SH_RECT = 0,        ///> axis-aligned rectangle
    SH_SEGMENT,         ///> line segment
    SH_LINE_CHAIN,      ///> line chain (polyline)
43
    SH_CIRCLE,          ///> circle
Maciej Suminski's avatar
Maciej Suminski committed
44
    SH_CONVEX,          ///> convex polygon
45 46
    SH_POLYGON,         ///> any polygon (with holes, etc.)
    SH_COMPOUND         ///> compound shape, consisting of multiple simple shapes
47 48 49 50
};

/**
 * Class SHAPE
51
 *
52
 * Represents an abstract shape on 2D plane.
53
 */
54 55
class SHAPE
{
Maciej Suminski's avatar
Maciej Suminski committed
56 57 58 59 60 61 62 63 64 65
protected:
    typedef VECTOR2I::extended_type ecoord;

public:
    /**
     * Constructor
     *
     * Creates an empty shape of type aType
     */

Maciej Suminski's avatar
Maciej Suminski committed
66
    SHAPE( SHAPE_TYPE aType ) : m_type( aType )
Maciej Suminski's avatar
Maciej Suminski committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    {}

    // Destructor
    virtual ~SHAPE()
    {}

    /**
     * Function Type()
     *
     * Returns the type of the shape.
     * @retval the type
     */
    SHAPE_TYPE Type() const
    {
        return m_type;
    }

    /**
     * Function Clone()
     *
     * Returns a dynamically allocated copy of the shape
     * @retval copy of the shape
     */
    virtual SHAPE* Clone() const
    {
        assert( false );
        return NULL;
    };

    /**
     * Function Collide()
     *
     * Checks if the boundary of shape (this) lies closer to the point aP than aClearance,
     * indicating a collision.
     * @return true, if there is a collision.
     */
    virtual bool Collide( const VECTOR2I& aP, int aClearance = 0 ) const
    {
        return Collide( SEG( aP, aP ), aClearance );
    }

    /**
     * Function Collide()
     *
     * Checks if the boundary of shape (this) lies closer to the shape aShape than aClearance,
     * indicating a collision.
     * @param aShape shape to check collision against
     * @param aClearance minimum clearance
     * @param aMTV minimum translation vector
     * @return true, if there is a collision.
     */
118 119
    virtual bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I& aMTV ) const;
    virtual bool Collide( const SHAPE* aShape, int aClearance = 0 ) const;
Maciej Suminski's avatar
Maciej Suminski committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    /**
     * Function Collide()
     *
     * Checks if the boundary of shape (this) lies closer to the segment aSeg than aClearance,
     * indicating a collision.
     * @return true, if there is a collision.
     */
    virtual bool Collide( const SEG& aSeg, int aClearance = 0 ) const = 0;

    /**
     * Function Collide()
     *
     * Computes a bounding box of the shape, with a margin of aClearance
     * a collision.
135
     * @param aClearance how much the bounding box is expanded wrs to the minimum enclosing rectangle
Maciej Suminski's avatar
Maciej Suminski committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
     * for the shape.
     * @return the bounding box.
     */
    virtual const BOX2I BBox( int aClearance = 0 ) const = 0;

    /**
     * Function Centre()
     *
     * Computes a center-of-mass of the shape
     * @return the center-of-mass point
     */
    virtual VECTOR2I Centre() const
    {
        return BBox( 0 ).Centre(); // if nothing better is available....
    }

152 153 154
    virtual void Move ( const VECTOR2I& aVector ) = 0;

    virtual bool IsSolid() const = 0;
Maciej Suminski's avatar
Maciej Suminski committed
155

156
protected:
Maciej Suminski's avatar
Maciej Suminski committed
157 158
    ///> type of our shape
    SHAPE_TYPE m_type;
159 160
};

Maciej Suminski's avatar
Maciej Suminski committed
161 162
bool CollideShapes( const SHAPE* aA, const SHAPE* aB, int aClearance,
                    bool aNeedMTV, VECTOR2I& aMTV );
163 164

#endif // __SHAPE_H