direction.h 7.92 KB
Newer Older
1 2 3 4 5
/*
 * KiRouter - a push-and-(sometimes-)shove PCB router
 *
 * Copyright (C) 2013  CERN
 * 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.or/licenses/>.
 */

#ifndef __DIRECTION_H
#define __DIRECTION_H

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

/**
 * Class DIRECTION_45.
 * Represents route directions & corner angles in a 45-degree metric.
 */

class DIRECTION_45
{
public:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

    /**
     * Enum Directions
     * Represents available directions - there are 8 of them, as on a rectilinear map (north = up) +
     * an extra undefined direction, reserved for traces that don't respect 45-degree routing regime.
     */
    enum Directions
    {
        N           = 0,
        NE          = 1,
        E           = 2,
        SE          = 3,
        S           = 4,
        SW          = 5,
        W           = 6,
        NW          = 7,
        UNDEFINED   = -1
    };

    /**
     * Enum AngleType
     * Represents kind of angle formed by vectors heading in two DIRECTION_45s.
     */
    enum AngleType
    {
        ANG_OBTUSE      = 0x01,
        ANG_RIGHT       = 0x02,
        ANG_ACUTE       = 0x04,
        ANG_STRAIGHT    = 0x08,
        ANG_HALF_FULL   = 0x10,
        ANG_UNDEFINED   = 0x20
    };

    DIRECTION_45( Directions aDir = UNDEFINED ) : m_dir( aDir ) {};

    /**
     * Constructor
     * @param aVec vector, whose direction will be translated into a DIRECTION_45.
     */
    DIRECTION_45( const VECTOR2I& aVec )
    {
        construct( aVec );
    }

    /**
     * Constructor
     * @param aSeg segment, whose direction will be translated into a DIRECTION_45.
     */
    DIRECTION_45( const SEG& aSeg )
    {
Maciej Suminski's avatar
Maciej Suminski committed
85
        construct( aSeg.B - aSeg.A );
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 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 191 192 193 194 195 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 222 223 224 225 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 254 255 256 257 258 259 260
    }

    /**
     * Function Format()
     * Formats the direction in a human readable word.
     * @return name of the direction
     */
    const std::string Format() const
    {
        switch( m_dir )
        {
        case N:
            return "north";

        case NE:
            return "north-east";

        case E:
            return "east";

        case SE:
            return "south-east";

        case S:
            return "south";

        case SW:
            return "south-west";

        case W:
            return "west";

        case NW:
            return "north-west";

        case UNDEFINED:
            return "undefined";

        default:
            return "<Error>";
        }
    }

    /**
     * Function Opposite()
     * Returns a direction opposite (180 degree) to (this)
     * @return opposite direction
     */
    DIRECTION_45 Opposite() const
    {
        if( m_dir == UNDEFINED )
            return UNDEFINED;

        const Directions OppositeMap[] = { S, SW, W, NW, N, NE, E, SE };
        return OppositeMap[m_dir];
    }

    /**
     * Function Angle()
     * Returns the type of angle between directions (this) and aOther.
     * @param aOther direction to compare angle with
     */
    AngleType Angle( const DIRECTION_45& aOther ) const
    {
        if( m_dir == UNDEFINED || aOther.m_dir == UNDEFINED )
            return ANG_UNDEFINED;

        int d = std::abs( m_dir - aOther.m_dir );

        if( d == 1 || d == 7 )
            return ANG_OBTUSE;
        else if( d == 2 || d == 6 )
            return ANG_RIGHT;
        else if( d == 3 || d == 5 )
            return ANG_ACUTE;
        else if( d == 4 )
            return ANG_HALF_FULL;
        else
            return ANG_STRAIGHT;
    }

    /**
     * Function IsObtuse()
     * @return true, when (this) forms an obtuse angle with aOther
     */
    bool IsObtuse( const DIRECTION_45& aOther ) const
    {
        return Angle( aOther ) == ANG_OBTUSE;
    }

    /**
     * Function IsDiagonal()
     * Returns true if the direction is diagonal (e.g. North-West, South-East, etc)
     * @return true, when diagonal.
     */
    bool IsDiagonal() const
    {
        return ( m_dir % 2 ) == 1;
    }

    /**
     * Function BuildInitialTrace()
     *
     * Builds a 2-segment line chain between points aP0 and aP1 and following 45-degree routing
     * regime. If aStartDiagonal is true, the trace starts with a diagonal segment.
     * @param aP0 starting point
     * @param aP1 ending point
     * @param aStartDiagonal whether the first segment has to be diagonal
     * @return the trace
     */
    const SHAPE_LINE_CHAIN BuildInitialTrace( const VECTOR2I& aP0,
            const VECTOR2I& aP1,
            bool aStartDiagonal = false ) const
    {
        int w = abs( aP1.x - aP0.x );
        int h = abs( aP1.y - aP0.y );
        int sw  = sign( aP1.x - aP0.x );
        int sh  = sign( aP1.y - aP0.y );

        VECTOR2I mp0, mp1;

        // we are more horizontal than vertical?
        if( w > h )
        {
            mp0 = VECTOR2I( (w - h) * sw, 0 );      // direction: E
            mp1 = VECTOR2I( h * sw, h * sh );       // direction: NE
        }
        else
        {
            mp0 = VECTOR2I( 0, sh * (h - w) );      // direction: N
            mp1 = VECTOR2I( sw * w, sh * w );       // direction: NE
        }

        bool start_diagonal;

        if( m_dir == UNDEFINED )
            start_diagonal = aStartDiagonal;
        else
            start_diagonal = IsDiagonal();

        SHAPE_LINE_CHAIN pl;

        pl.Append( aP0 );

        if( start_diagonal )
            pl.Append( aP0 + mp1 );
        else
            pl.Append( aP0 + mp0 );

        pl.Append( aP1 );
        pl.Simplify();
        return pl;
    };

    bool operator==( const DIRECTION_45& aOther ) const
    {
        return aOther.m_dir == m_dir;
    }

    bool operator!=( const DIRECTION_45& aOther ) const
    {
        return aOther.m_dir != m_dir;
    }

    const DIRECTION_45 Right() const
    {
        DIRECTION_45 r;

        r.m_dir = (Directions) (m_dir + 1);

        if( r.m_dir == NW )
            r.m_dir = N;

        return r;
    }
261 262 263

private:

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    template <typename T>
    int sign( T val ) const
    {
        return (T( 0 ) < val) - ( val < T( 0 ) );
    }

    /**
     * Function construct()
     * Calculates the direction from a vector. If the vector's angle is not a multiple of 45
     * degrees, the direction is rounded to the nearest octant.
     * @param aVec our vector
     */
    void construct( const VECTOR2I& aVec )
    {
        m_dir = UNDEFINED;

        if( aVec.x == 0 && aVec.y == 0 )
            return;

        double mag = 360.0 - ( 180.0 / M_PI * atan2( (double) aVec.y, (double) aVec.x ) ) + 90.0;

        if( mag >= 360.0 )
            mag -= 360.0;

        if( mag < 0.0 )
            mag += 360.0;

        m_dir = (Directions)( ( mag + 22.5 ) / 45.0 );

        if( m_dir >= 8 )
            m_dir = (Directions)( m_dir - 8 );

        if( m_dir < 0 )
            m_dir = (Directions)( m_dir + 8 );

        return;

        if( aVec.y < 0 )
        {
            if( aVec.x > 0 )
                m_dir = NE;
            else if( aVec.x < 0 )
                m_dir = NW;
            else
                m_dir = N;
        }
        else if( aVec.y == 0 )
        {
            if( aVec.x > 0 )
                m_dir = E;
            else
                m_dir = W;
        }
        else    // aVec.y>0
        {
            if( aVec.x > 0 )
                m_dir = SE;
            else if( aVec.x < 0 )
                m_dir = SW;
            else
                m_dir = S;
        }
    }

    Directions m_dir;    ///> our actual direction
};

#endif    // __DIRECTION_H