2013-09-18 17:55:16 +00:00
|
|
|
/*
|
2017-03-22 12:35:07 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2013-09-18 17:55:16 +00:00
|
|
|
*
|
2015-02-18 16:53:46 +00:00
|
|
|
* Copyright (C) 2013-2015 CERN
|
2021-01-25 12:42:36 +00:00
|
|
|
* Copyright (C) 2017-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* 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.
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* 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.
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.or/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-03-22 12:35:07 +00:00
|
|
|
#ifndef DIRECTION45_H
|
|
|
|
#define DIRECTION45_H
|
2013-09-18 17:55:16 +00:00
|
|
|
|
|
|
|
#include <geometry/seg.h>
|
|
|
|
#include <geometry/shape_line_chain.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/vector2d.h>
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2018-02-04 12:09:30 +00:00
|
|
|
// believe or not, X11 headers have a F****ING macro called Opposite...
|
|
|
|
#undef Opposite
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Represent route directions & corner angles in a 45-degree metric.
|
2013-09-18 17:55:16 +00:00
|
|
|
*/
|
|
|
|
class DIRECTION_45
|
|
|
|
{
|
|
|
|
public:
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @note North represents "up" to the user looking at the application, which is the negative-y
|
2021-01-07 02:47:06 +00:00
|
|
|
* direction in the world coordinate space!
|
2013-09-26 21:53:54 +00:00
|
|
|
*/
|
2019-05-17 00:13:21 +00:00
|
|
|
enum Directions : int
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
N = 0,
|
|
|
|
NE = 1,
|
|
|
|
E = 2,
|
|
|
|
SE = 3,
|
|
|
|
S = 4,
|
|
|
|
SW = 5,
|
|
|
|
W = 6,
|
|
|
|
NW = 7,
|
2019-05-17 00:13:21 +00:00
|
|
|
LAST = 8,
|
2013-09-26 21:53:54 +00:00
|
|
|
UNDEFINED = -1
|
|
|
|
};
|
|
|
|
|
2021-11-03 02:14:23 +00:00
|
|
|
/**
|
|
|
|
* Corner modes.
|
|
|
|
* A corner can either be 45° or 90° and can be fillet/rounded or mitered
|
|
|
|
*/
|
|
|
|
enum CORNER_MODE
|
|
|
|
{
|
|
|
|
MITERED_45 = 0, ///< H/V/45 with mitered corners (default)
|
|
|
|
ROUNDED_45 = 1, ///< H/V/45 with filleted corners
|
|
|
|
MITERED_90 = 2, ///< H/V only (90-degree corners)
|
|
|
|
ROUNDED_90 = 3, ///< H/V with filleted corners
|
|
|
|
};
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Represent kind of angle formed by vectors heading in two DIRECTION_45s.
|
2013-09-26 21:53:54 +00:00
|
|
|
*/
|
|
|
|
enum AngleType
|
|
|
|
{
|
|
|
|
ANG_OBTUSE = 0x01,
|
|
|
|
ANG_RIGHT = 0x02,
|
|
|
|
ANG_ACUTE = 0x04,
|
|
|
|
ANG_STRAIGHT = 0x08,
|
|
|
|
ANG_HALF_FULL = 0x10,
|
|
|
|
ANG_UNDEFINED = 0x20
|
|
|
|
};
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
DIRECTION_45( Directions aDir = UNDEFINED ) : m_dir( aDir ), m_90deg( false ) {}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-07 02:47:06 +00:00
|
|
|
* @param aVec vector in world space, whose direction will be translated into a DIRECTION_45.
|
2013-09-26 21:53:54 +00:00
|
|
|
*/
|
2019-05-17 00:13:21 +00:00
|
|
|
DIRECTION_45( const VECTOR2I &aVec, bool a90 = false ) :
|
|
|
|
m_90deg( a90 )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2021-01-07 02:47:06 +00:00
|
|
|
VECTOR2I vec( aVec );
|
|
|
|
vec.y = -vec.y;
|
2021-01-08 00:13:05 +00:00
|
|
|
construct_( vec );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param aSeg segment, whose direction will be translated into a DIRECTION_45.
|
|
|
|
*/
|
2019-05-17 00:13:21 +00:00
|
|
|
DIRECTION_45( const SEG& aSeg, bool a90 = false ) :
|
|
|
|
m_90deg( a90 )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2021-01-07 02:47:06 +00:00
|
|
|
VECTOR2I vec( aSeg.B - aSeg.A );
|
|
|
|
vec.y = -vec.y;
|
|
|
|
construct_( vec );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 02:37:24 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Create a DIRECTION_45 from the endpoints of a given arc.
|
|
|
|
*
|
2021-01-04 02:37:24 +00:00
|
|
|
* @param aArc will be translated into the closest DIRECTION_45
|
|
|
|
*/
|
|
|
|
DIRECTION_45( const SHAPE_ARC& aArc, bool a90 = false ) :
|
|
|
|
m_90deg( a90 )
|
|
|
|
{
|
2021-01-07 02:47:06 +00:00
|
|
|
VECTOR2I vec( aArc.GetP1() - aArc.GetP0() );
|
|
|
|
vec.y = -vec.y;
|
|
|
|
construct_( vec );
|
2021-01-04 02:37:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Format the direction in a human readable word.
|
|
|
|
*
|
2013-09-26 21:53:54 +00:00
|
|
|
* @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>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return a direction opposite (180 degree) to (this).
|
|
|
|
*
|
2013-09-26 21:53:54 +00:00
|
|
|
* @return opposite direction
|
|
|
|
*/
|
|
|
|
DIRECTION_45 Opposite() const
|
|
|
|
{
|
2014-02-17 10:33:03 +00:00
|
|
|
const Directions OppositeMap[] = { S, SW, W, NW, N, NE, E, SE, UNDEFINED };
|
2013-09-26 21:53:54 +00:00
|
|
|
return OppositeMap[m_dir];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return the type of angle between directions (this) and \a aOther.
|
|
|
|
*
|
2013-09-26 21:53:54 +00:00
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* @return true, when (this) forms an obtuse angle with \a aOther.
|
2013-09-26 21:53:54 +00:00
|
|
|
*/
|
|
|
|
bool IsObtuse( const DIRECTION_45& aOther ) const
|
|
|
|
{
|
|
|
|
return Angle( aOther ) == ANG_OBTUSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Returns true if the direction is diagonal (e.g. North-West, South-East, etc).
|
|
|
|
*
|
2013-09-26 21:53:54 +00:00
|
|
|
* @return true, when diagonal.
|
|
|
|
*/
|
|
|
|
bool IsDiagonal() const
|
|
|
|
{
|
|
|
|
return ( m_dir % 2 ) == 1;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
bool IsDefined() const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
return m_dir != UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Build a 2-segment line chain between points aP0 and aP1 and following 45-degree routing
|
2013-09-26 21:53:54 +00:00
|
|
|
* regime. If aStartDiagonal is true, the trace starts with a diagonal segment.
|
2021-01-25 12:42:36 +00:00
|
|
|
*
|
2013-09-26 21:53:54 +00:00
|
|
|
* @param aP0 starting point
|
|
|
|
* @param aP1 ending point
|
|
|
|
* @param aStartDiagonal whether the first segment has to be diagonal
|
2021-11-03 02:14:23 +00:00
|
|
|
* @param aMode How the corner is made. If it is a 90° corner, aStartDiagonal means
|
|
|
|
* start with the shorter direction first / use arc before the straight segment.
|
2013-09-26 21:53:54 +00:00
|
|
|
* @return the trace
|
|
|
|
*/
|
2020-12-30 05:06:49 +00:00
|
|
|
const SHAPE_LINE_CHAIN BuildInitialTrace( const VECTOR2I& aP0, const VECTOR2I& aP1,
|
2021-11-03 02:14:23 +00:00
|
|
|
bool aStartDiagonal = false,
|
|
|
|
CORNER_MODE aMode = CORNER_MODE::MITERED_45 ) const;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return the direction on the right side of this (i.e. turns right by 45 or 90 deg).
|
2014-05-14 13:53:54 +00:00
|
|
|
*/
|
2013-09-26 21:53:54 +00:00
|
|
|
const DIRECTION_45 Right() const
|
|
|
|
{
|
|
|
|
DIRECTION_45 r;
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
if ( m_dir != UNDEFINED )
|
2019-05-17 00:13:21 +00:00
|
|
|
{
|
|
|
|
if( m_90deg )
|
|
|
|
r.m_dir = static_cast<Directions>( ( m_dir + 2 ) % LAST );
|
|
|
|
else
|
|
|
|
r.m_dir = static_cast<Directions>( ( m_dir + 1 ) % LAST );
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return the direction on the left side of this (i.e. turns left by 45 or 90 deg).
|
2014-05-14 13:53:54 +00:00
|
|
|
*/
|
|
|
|
const DIRECTION_45 Left() const
|
|
|
|
{
|
|
|
|
DIRECTION_45 l;
|
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
if ( m_dir != UNDEFINED )
|
|
|
|
{
|
|
|
|
if( m_90deg )
|
|
|
|
l.m_dir = static_cast<Directions>( ( m_dir + LAST - 2 ) % LAST );
|
|
|
|
else
|
|
|
|
l.m_dir = static_cast<Directions>( ( m_dir + LAST - 1 ) % LAST );
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-08 18:52:23 +00:00
|
|
|
* @return a unit vector in world coordinate system corresponding to our direction.
|
2014-05-14 13:53:54 +00:00
|
|
|
*/
|
|
|
|
const VECTOR2I ToVector() const
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
switch( m_dir )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2021-01-08 18:52:23 +00:00
|
|
|
case N: return VECTOR2I( 0, -1 );
|
|
|
|
case S: return VECTOR2I( 0, 1 );
|
2014-05-16 11:37:31 +00:00
|
|
|
case E: return VECTOR2I( 1, 0 );
|
|
|
|
case W: return VECTOR2I( -1, 0 );
|
2021-01-08 18:52:23 +00:00
|
|
|
case NE: return VECTOR2I( 1, -1 );
|
|
|
|
case NW: return VECTOR2I( -1, -1 );
|
|
|
|
case SE: return VECTOR2I( 1, 1 );
|
|
|
|
case SW: return VECTOR2I( -1, 1 );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
default:
|
2014-05-16 11:37:31 +00:00
|
|
|
return VECTOR2I( 0, 0 );
|
2015-02-18 16:53:46 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
int Mask() const
|
|
|
|
{
|
|
|
|
return 1 << ( (int) m_dir );
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
private:
|
2013-09-26 21:53:54 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Calculate the direction from a vector. If the vector's angle is not a multiple of 45
|
2013-09-26 21:53:54 +00:00
|
|
|
* degrees, the direction is rounded to the nearest octant.
|
2021-01-25 12:42:36 +00:00
|
|
|
*
|
2015-02-18 16:53:46 +00:00
|
|
|
* @param aVec our vector
|
|
|
|
*/
|
2015-02-18 00:29:54 +00:00
|
|
|
void construct_( const VECTOR2I& aVec )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2014-06-05 10:40:26 +00:00
|
|
|
int dir = ( mag + 22.5 ) / 45.0;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-05-17 00:13:21 +00:00
|
|
|
if( dir >= LAST )
|
|
|
|
dir -= LAST;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-06-05 10:40:26 +00:00
|
|
|
if( dir < 0 )
|
2019-05-17 00:13:21 +00:00
|
|
|
dir += LAST;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2014-06-05 10:40:26 +00:00
|
|
|
m_dir = (Directions) dir;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< our actual direction
|
2015-02-18 16:53:46 +00:00
|
|
|
Directions m_dir;
|
2019-05-17 00:13:21 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Are we routing on 45 or 90 degree increments
|
2019-05-17 00:13:21 +00:00
|
|
|
bool m_90deg;
|
2013-09-26 21:53:54 +00:00
|
|
|
};
|
|
|
|
|
2017-03-22 12:35:07 +00:00
|
|
|
#endif // DIRECTION45_H
|