KiCad PCB EDA Suite
DIRECTION_45 Class Reference

DIRECTION_45. More...

#include <direction45.h>

Public Types

enum  Directions : int {
  N = 0, NE = 1, E = 2, SE = 3,
  S = 4, SW = 5, W = 6, NW = 7,
  LAST = 8, UNDEFINED = -1
}
 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. More...
 
enum  AngleType {
  ANG_OBTUSE = 0x01, ANG_RIGHT = 0x02, ANG_ACUTE = 0x04, ANG_STRAIGHT = 0x08,
  ANG_HALF_FULL = 0x10, ANG_UNDEFINED = 0x20
}
 Enum AngleType Represents kind of angle formed by vectors heading in two DIRECTION_45s. More...
 

Public Member Functions

 DIRECTION_45 (Directions aDir=UNDEFINED)
 
 DIRECTION_45 (const VECTOR2I &aVec, bool a90=false)
 Constructor. More...
 
 DIRECTION_45 (const SEG &aSeg, bool a90=false)
 Constructor. More...
 
const std::string Format () const
 Function Format() Formats the direction in a human readable word. More...
 
DIRECTION_45 Opposite () const
 Function Opposite() Returns a direction opposite (180 degree) to (this) More...
 
AngleType Angle (const DIRECTION_45 &aOther) const
 Function Angle() Returns the type of angle between directions (this) and aOther. More...
 
bool IsObtuse (const DIRECTION_45 &aOther) const
 Function IsObtuse() More...
 
bool IsDiagonal () const
 Function IsDiagonal() Returns true if the direction is diagonal (e.g. More...
 
bool IsDefined () const
 
const SHAPE_LINE_CHAIN BuildInitialTrace (const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, int aMaxRadius=0) const
 Function BuildInitialTrace() More...
 
bool operator== (const DIRECTION_45 &aOther) const
 
bool operator!= (const DIRECTION_45 &aOther) const
 
const DIRECTION_45 Right () const
 Function Right() More...
 
const DIRECTION_45 Left () const
 Function Left() More...
 
const VECTOR2I ToVector () const
 Function ToVector() More...
 
int Mask () const
 

Private Member Functions

void construct_ (const VECTOR2I &aVec)
 Function construct() Calculates the direction from a vector. More...
 

Private Attributes

Directions m_dir
 

our actual direction

More...
 
bool m_90deg
 

Are we routing on 45 or 90 degree increments

More...
 

Detailed Description

DIRECTION_45.

Represents route directions & corner angles in a 45-degree metric.

Definition at line 37 of file direction45.h.

Member Enumeration Documentation

◆ AngleType

Enum AngleType Represents kind of angle formed by vectors heading in two DIRECTION_45s.

Enumerator
ANG_OBTUSE 
ANG_RIGHT 
ANG_ACUTE 
ANG_STRAIGHT 
ANG_HALF_FULL 
ANG_UNDEFINED 

Definition at line 64 of file direction45.h.

◆ Directions

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.

Enumerator
NE 
SE 
SW 
NW 
LAST 
UNDEFINED 

Definition at line 46 of file direction45.h.

46  : int
47  {
48  N = 0,
49  NE = 1,
50  E = 2,
51  SE = 3,
52  S = 4,
53  SW = 5,
54  W = 6,
55  NW = 7,
56  LAST = 8,
57  UNDEFINED = -1
58  };

Constructor & Destructor Documentation

◆ DIRECTION_45() [1/3]

DIRECTION_45::DIRECTION_45 ( Directions  aDir = UNDEFINED)
inline

Definition at line 74 of file direction45.h.

74 : m_dir( aDir ), m_90deg( false ) {}
Directions m_dir
our actual direction
Definition: direction45.h:329
bool m_90deg
Are we routing on 45 or 90 degree increments
Definition: direction45.h:332

◆ DIRECTION_45() [2/3]

DIRECTION_45::DIRECTION_45 ( const VECTOR2I aVec,
bool  a90 = false 
)
inline

Constructor.

Parameters
aVecvector, whose direction will be translated into a DIRECTION_45.

Definition at line 80 of file direction45.h.

80  :
81  m_90deg( a90 )
82  {
83  construct_( aVec );
84  }
void construct_(const VECTOR2I &aVec)
Function construct() Calculates the direction from a vector.
Definition: direction45.h:300
bool m_90deg
Are we routing on 45 or 90 degree increments
Definition: direction45.h:332

References construct_().

◆ DIRECTION_45() [3/3]

DIRECTION_45::DIRECTION_45 ( const SEG aSeg,
bool  a90 = false 
)
inline

Constructor.

Parameters
aSegsegment, whose direction will be translated into a DIRECTION_45.

Definition at line 90 of file direction45.h.

90  :
91  m_90deg( a90 )
92  {
93  construct_( aSeg.B - aSeg.A );
94  }
VECTOR2I A
Definition: seg.h:47
void construct_(const VECTOR2I &aVec)
Function construct() Calculates the direction from a vector.
Definition: direction45.h:300
bool m_90deg
Are we routing on 45 or 90 degree increments
Definition: direction45.h:332
VECTOR2I B
Definition: seg.h:48

References SEG::A, SEG::B, and construct_().

Member Function Documentation

◆ Angle()

AngleType DIRECTION_45::Angle ( const DIRECTION_45 aOther) const
inline

Function Angle() Returns the type of angle between directions (this) and aOther.

Parameters
aOtherdirection to compare angle with

Definition at line 153 of file direction45.h.

154  {
155  if( m_dir == UNDEFINED || aOther.m_dir == UNDEFINED )
156  return ANG_UNDEFINED;
157 
158  int d = std::abs( m_dir - aOther.m_dir );
159 
160  if( d == 1 || d == 7 )
161  return ANG_OBTUSE;
162  else if( d == 2 || d == 6 )
163  return ANG_RIGHT;
164  else if( d == 3 || d == 5 )
165  return ANG_ACUTE;
166  else if( d == 4 )
167  return ANG_HALF_FULL;
168  else
169  return ANG_STRAIGHT;
170  }
Directions m_dir
our actual direction
Definition: direction45.h:329

References ANG_ACUTE, ANG_HALF_FULL, ANG_OBTUSE, ANG_RIGHT, ANG_STRAIGHT, ANG_UNDEFINED, m_dir, and UNDEFINED.

Referenced by PNS::angle(), PNS::LINE::CountCorners(), PNS::LINE::dragSegment45(), IsObtuse(), PNS::LINE_PLACER::mergeHead(), PNS::OPTIMIZER::smartPadsSingle(), and PNS::tightenSegment().

◆ BuildInitialTrace()

const SHAPE_LINE_CHAIN DIRECTION_45::BuildInitialTrace ( const VECTOR2I aP0,
const VECTOR2I aP1,
bool  aStartDiagonal = false,
int  aMaxRadius = 0 
) const

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.

Parameters
aP0starting point
aP1ending point
aStartDiagonalwhether the first segment has to be diagonal
aRadiusis the radius of curvature for rounding. If =0, do not insert arcs
Returns
the trace

Definition at line 22 of file direction_45.cpp.

24 {
25  bool start_diagonal;
26 
27  if( m_dir == UNDEFINED )
28  start_diagonal = aStartDiagonal;
29  else
30  start_diagonal = IsDiagonal();
31 
32  int w = abs( aP1.x - aP0.x );
33  int h = abs( aP1.y - aP0.y );
34  int sw = sign( aP1.x - aP0.x );
35  int sh = sign( aP1.y - aP0.y );
36 
37  int radius = std::min( aMaxRadius, std::min( w, h ) );
38  bool use_rounded = aMaxRadius > 0;
39  int dist90 = use_rounded ? KiROUND( ( M_SQRT2 - 1.0 ) * radius ) : 0;
40  int dist45 = use_rounded ? KiROUND( radius * ( 1.0 - M_SQRT1_2 ) ) : 0;
41 
42  VECTOR2I mp0, mp1, arc_offset_90, arc_offset_45;
43 
44  // we are more horizontal than vertical?
45 // if( m_90deg )
46 // {
47 // if( m_dir == N || m_dir == S )
48 //
49 // }
50 
51  if( w > h )
52  {
53  mp0 = VECTOR2I( ( w - h - dist90 ) * sw, 0 ); // direction: E
54  mp1 = VECTOR2I( ( h - dist45 ) * sw, ( h - dist45 ) * sh ); // direction: NE
55  arc_offset_90 = VECTOR2I( 0, radius * sh );
56  arc_offset_45 = VECTOR2I( sw * radius * M_SQRT1_2, -sh * radius * M_SQRT1_2 );
57  }
58  else
59  {
60  mp0 = VECTOR2I( 0, sh * ( h - w - dist90 ) ); // direction: N
61  mp1 = VECTOR2I( sw * ( w - dist45 ), sh * ( w - dist45 ) ); // direction: NE
62  arc_offset_90 = VECTOR2I( radius * sw, 0 );
63  arc_offset_45 = VECTOR2I( -sw * radius * M_SQRT1_2, sh * radius * M_SQRT1_2 );
64  }
65 
67  VECTOR2I arc_center;
68 
69  pl.Append( aP0 );
70  VECTOR2I next_point;
71 
72  if( start_diagonal )
73  {
74  next_point = aP0 + mp1;
75  arc_center = aP0 + mp1 + arc_offset_45;
76  }
77  else
78  {
79  next_point = aP0 + mp0;
80  arc_center = aP0 + mp0 + arc_offset_90;
81  }
82 
83  if( use_rounded )
84  {
85  int sa = start_diagonal ? -sw * sh : sw * sh;
86 
87  if( h > w )
88  sa = -sa;
89 
90  SHAPE_ARC new_arc( arc_center, next_point, sa * 45.0 );
91  pl.Append( new_arc );
92  }
93  else
94  {
95  pl.Append( next_point );
96  }
97 
98  pl.Append( aP1 );
99 
100  pl.Simplify();
101  return pl;
102 }
int sign(T val)
Definition: util.h:101
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
bool IsDiagonal() const
Function IsDiagonal() Returns true if the direction is diagonal (e.g.
Definition: direction45.h:186
SHAPE_LINE_CHAIN.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
Directions m_dir
our actual direction
Definition: direction45.h:329

References SHAPE_LINE_CHAIN::Append(), IsDiagonal(), KiROUND(), m_dir, sign(), SHAPE_LINE_CHAIN::Simplify(), UNDEFINED, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by PNS::DP_GATEWAYS::buildDpContinuation(), PNS::DP_GATEWAYS::buildEntries(), PNS::DIFF_PAIR::BuildInitial(), PNS::LINE_PLACER::buildInitialLine(), PNS::dragCornerInternal(), PNS::OPTIMIZER::fanoutCleanup(), PNS::OPTIMIZER::mergeDpStep(), PNS::OPTIMIZER::mergeStep(), PNS::LINE_PLACER::reduceTail(), and PNS::OPTIMIZER::smartPadsSingle().

◆ construct_()

void DIRECTION_45::construct_ ( const VECTOR2I aVec)
inlineprivate

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.

Parameters
aVecour vector

Definition at line 300 of file direction45.h.

301  {
302  m_dir = UNDEFINED;
303 
304  if( aVec.x == 0 && aVec.y == 0 )
305  return;
306 
307  double mag = 360.0 - ( 180.0 / M_PI * atan2( (double) aVec.y, (double) aVec.x ) ) + 90.0;
308 
309  if( mag >= 360.0 )
310  mag -= 360.0;
311 
312  if( mag < 0.0 )
313  mag += 360.0;
314 
315  int dir = ( mag + 22.5 ) / 45.0;
316 
317  if( dir >= LAST )
318  dir -= LAST;
319 
320  if( dir < 0 )
321  dir += LAST;
322 
323  m_dir = (Directions) dir;
324 
325  return;
326  }
Directions
Enum Directions Represents available directions - there are 8 of them, as on a rectilinear map (north...
Definition: direction45.h:46
Directions m_dir
our actual direction
Definition: direction45.h:329

References LAST, m_dir, UNDEFINED, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by DIRECTION_45().

◆ Format()

const std::string DIRECTION_45::Format ( ) const
inline

Function Format() Formats the direction in a human readable word.

Returns
name of the direction

Definition at line 101 of file direction45.h.

102  {
103  switch( m_dir )
104  {
105  case N:
106  return "north";
107 
108  case NE:
109  return "north-east";
110 
111  case E:
112  return "east";
113 
114  case SE:
115  return "south-east";
116 
117  case S:
118  return "south";
119 
120  case SW:
121  return "south-west";
122 
123  case W:
124  return "west";
125 
126  case NW:
127  return "north-west";
128 
129  case UNDEFINED:
130  return "undefined";
131 
132  default:
133  return "<Error>";
134  }
135  }
Directions m_dir
our actual direction
Definition: direction45.h:329

References E, m_dir, N, NE, NW, S, SE, SW, UNDEFINED, and W.

Referenced by PNS::LINE_PLACER::initPlacement(), PNS::LINE_PLACER::mergeHead(), and PNS::LINE_PLACER::routeStep().

◆ IsDefined()

bool DIRECTION_45::IsDefined ( ) const
inline

Definition at line 191 of file direction45.h.

192  {
193  return m_dir != UNDEFINED;
194  }
Directions m_dir
our actual direction
Definition: direction45.h:329

References m_dir, and UNDEFINED.

◆ IsDiagonal()

bool DIRECTION_45::IsDiagonal ( ) const
inline

Function IsDiagonal() Returns true if the direction is diagonal (e.g.

North-West, South-East, etc)

Returns
true, when diagonal.

Definition at line 186 of file direction45.h.

187  {
188  return ( m_dir % 2 ) == 1;
189  }
Directions m_dir
our actual direction
Definition: direction45.h:329

References m_dir.

Referenced by BuildInitialTrace(), and PNS::OPTIMIZER::mergeDpStep().

◆ IsObtuse()

bool DIRECTION_45::IsObtuse ( const DIRECTION_45 aOther) const
inline

Function IsObtuse()

Returns
true, when (this) forms an obtuse angle with aOther

Definition at line 176 of file direction45.h.

177  {
178  return Angle( aOther ) == ANG_OBTUSE;
179  }
AngleType Angle(const DIRECTION_45 &aOther) const
Function Angle() Returns the type of angle between directions (this) and aOther.
Definition: direction45.h:153

References ANG_OBTUSE, and Angle().

Referenced by PNS::LINE_PLACER::checkObtusity(), PNS::OPTIMIZER::mergeDpStep(), and PNS::tightenSegment().

◆ Left()

const DIRECTION_45 DIRECTION_45::Left ( ) const
inline

Function Left()

Returns the direction on the left side of this (i.e. turns left by 45 or 90 deg)

Definition at line 249 of file direction45.h.

250  {
251  DIRECTION_45 l;
252 
253  if ( m_dir != UNDEFINED )
254  {
255  if( m_90deg )
256  l.m_dir = static_cast<Directions>( ( m_dir + LAST - 2 ) % LAST );
257  else
258  l.m_dir = static_cast<Directions>( ( m_dir + LAST - 1 ) % LAST );
259  }
260 
261  return l;
262  }
DIRECTION_45.
Definition: direction45.h:37
Directions m_dir
our actual direction
Definition: direction45.h:329
bool m_90deg
Are we routing on 45 or 90 degree increments
Definition: direction45.h:332

References LAST, m_90deg, m_dir, and UNDEFINED.

Referenced by PNS::DP_GATEWAYS::buildDpContinuation(), and PNS::LINE::dragSegment45().

◆ Mask()

int DIRECTION_45::Mask ( ) const
inline

Definition at line 287 of file direction45.h.

288  {
289  return 1 << ( (int) m_dir );
290  }
Directions m_dir
our actual direction
Definition: direction45.h:329

References m_dir.

Referenced by PNS::ANGLE_CONSTRAINT_45::Check().

◆ operator!=()

bool DIRECTION_45::operator!= ( const DIRECTION_45 aOther) const
inline

Definition at line 217 of file direction45.h.

218  {
219  return aOther.m_dir != m_dir;
220  }
Directions m_dir
our actual direction
Definition: direction45.h:329

References m_dir.

◆ operator==()

bool DIRECTION_45::operator== ( const DIRECTION_45 aOther) const
inline

Definition at line 212 of file direction45.h.

213  {
214  return aOther.m_dir == m_dir;
215  }
Directions m_dir
our actual direction
Definition: direction45.h:329

References m_dir.

◆ Opposite()

DIRECTION_45 DIRECTION_45::Opposite ( ) const
inline

Function Opposite() Returns a direction opposite (180 degree) to (this)

Returns
opposite direction

Definition at line 142 of file direction45.h.

143  {
144  const Directions OppositeMap[] = { S, SW, W, NW, N, NE, E, SE, UNDEFINED };
145  return OppositeMap[m_dir];
146  }
Directions
Enum Directions Represents available directions - there are 8 of them, as on a rectilinear map (north...
Definition: direction45.h:46
Directions m_dir
our actual direction
Definition: direction45.h:329

References E, m_dir, N, NE, NW, S, SE, SW, UNDEFINED, and W.

◆ Right()

const DIRECTION_45 DIRECTION_45::Right ( ) const
inline

Function Right()

Returns the direction on the right side of this (i.e. turns right by 45 or 90 deg)

Definition at line 228 of file direction45.h.

229  {
230  DIRECTION_45 r;
231 
232  if ( m_dir != UNDEFINED )
233  {
234  if( m_90deg )
235  r.m_dir = static_cast<Directions>( ( m_dir + 2 ) % LAST );
236  else
237  r.m_dir = static_cast<Directions>( ( m_dir + 1 ) % LAST );
238  }
239 
240  return r;
241  }
DIRECTION_45.
Definition: direction45.h:37
Directions m_dir
our actual direction
Definition: direction45.h:329
bool m_90deg
Are we routing on 45 or 90 degree increments
Definition: direction45.h:332

References LAST, m_90deg, m_dir, and UNDEFINED.

Referenced by PNS::DP_GATEWAYS::buildDpContinuation(), PNS::LINE_PLACER::buildInitialLine(), PNS::LINE::dragSegment45(), PNS::LINE_PLACER::FlipPosture(), and PNS::LINE_PLACER::mergeHead().

◆ ToVector()

const VECTOR2I DIRECTION_45::ToVector ( ) const
inline

Function ToVector()

Returns a unit vector corresponding to our direction.

Definition at line 269 of file direction45.h.

270  {
271  switch( m_dir )
272  {
273  case N: return VECTOR2I( 0, 1 );
274  case S: return VECTOR2I( 0, -1 );
275  case E: return VECTOR2I( 1, 0 );
276  case W: return VECTOR2I( -1, 0 );
277  case NE: return VECTOR2I( 1, 1 );
278  case NW: return VECTOR2I( -1, 1 );
279  case SE: return VECTOR2I( 1, -1 );
280  case SW: return VECTOR2I( -1, -1 );
281 
282  default:
283  return VECTOR2I( 0, 0 );
284  }
285  }
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
Directions m_dir
our actual direction
Definition: direction45.h:329

References E, m_dir, N, NE, NW, S, SE, SW, and W.

Referenced by PNS_PCBNEW_DEBUG_DECORATOR::AddDirections(), PNS::DP_GATEWAYS::buildDpContinuation(), and PNS::LINE::dragSegment45().

Member Data Documentation

◆ m_90deg

bool DIRECTION_45::m_90deg
private

Are we routing on 45 or 90 degree increments

Definition at line 332 of file direction45.h.

Referenced by Left(), and Right().

◆ m_dir

Directions DIRECTION_45::m_dir
private

The documentation for this class was generated from the following files: