KiCad PCB EDA Suite
geometry_utils.h File Reference

a few functions useful in geometry calculations. More...

#include <math/vector2d.h>

Go to the source code of this file.

Macros

#define DOT_WIDTH_MILS   0.0254
 Dashed and dotted line patterns. More...
 
#define DOT_MARK_LEN(aLineWidth)   ( std::max( 1.0, IU_PER_MILS * DOT_WIDTH_MILS - aLineWidth ) )
 
#define DASH_GAP_LEN(aLineWidth)   ( 3.0 * DOT_MARK_LEN( aLineWidth ) + ( 2.0 * aLineWidth ) )
 
#define DASH_MARK_LEN(aLineWidth)   ( std::max( DASH_GAP_LEN( aLineWidth ), 5.0 * DOT_MARK_LEN( aLineWidth ) ) )
 

Functions

int GetArcToSegmentCount (int aRadius, int aErrorMax, double aArcAngleDegree)
 
double GetCircletoPolyCorrectionFactor (int aSegCountforCircle)
 
template<typename T >
VECTOR2< T > GetVectorSnapped45 (const VECTOR2< T > &aVec)
 Snap a vector onto the nearest 0, 45 or 90 degree line. More...
 
bool ClipLine (const EDA_RECT *aClipBox, int &x1, int &y1, int &x2, int &y2)
 Test if any part of a line falls within the bounds of a rectangle. More...
 

Detailed Description

a few functions useful in geometry calculations.

Definition in file geometry_utils.h.

Macro Definition Documentation

#define DASH_GAP_LEN (   aLineWidth)    ( 3.0 * DOT_MARK_LEN( aLineWidth ) + ( 2.0 * aLineWidth ) )

Definition at line 130 of file geometry_utils.h.

Referenced by KIGFX::SCH_PAINTER::draw(), and PLOTTER::GetDashGapLenIU().

#define DASH_MARK_LEN (   aLineWidth)    ( std::max( DASH_GAP_LEN( aLineWidth ), 5.0 * DOT_MARK_LEN( aLineWidth ) ) )

Definition at line 133 of file geometry_utils.h.

Referenced by KIGFX::SCH_PAINTER::draw(), and PLOTTER::GetDashMarkLenIU().

#define DOT_MARK_LEN (   aLineWidth)    ( std::max( 1.0, IU_PER_MILS * DOT_WIDTH_MILS - aLineWidth ) )

Definition at line 127 of file geometry_utils.h.

Referenced by KIGFX::SCH_PAINTER::draw(), and PLOTTER::GetDotMarkLenIU().

#define DOT_WIDTH_MILS   0.0254

Dashed and dotted line patterns.

Note: these are all macros because they're included from files with different IU definitions.

Definition at line 125 of file geometry_utils.h.

Function Documentation

bool ClipLine ( const EDA_RECT aClipBox,
int &  x1,
int &  y1,
int &  x2,
int &  y2 
)

Test if any part of a line falls within the bounds of a rectangle.

Please note that this is only accurate for lines that are one pixel wide.

Parameters
aClipBox- The rectangle to test.
x1- X coordinate of one end of a line.
y1- Y coordinate of one end of a line.
x2- X coordinate of the other end of a line.
y2- Y coordinate of the other end of a line.
Returns
- False if any part of the line lies within the rectangle.

Definition at line 87 of file geometry_utils.cpp.

References clipOutCode(), EDA_RECT::GetBottom(), EDA_RECT::GetRight(), EDA_RECT::GetX(), and EDA_RECT::GetY().

Referenced by KIGFX::SCH_PAINTER::draw(), GetVectorSnapped45(), GRCSegm(), GRLineArray(), and WinClipAndDrawLine().

88 {
89  // Stock Cohen-Sutherland algorithm; check *any* CG book for details
90  int outcode1 = clipOutCode( aClipBox, x1, y1 );
91  int outcode2 = clipOutCode( aClipBox, x2, y2 );
92 
93  while( outcode1 || outcode2 )
94  {
95  // Fast reject
96  if( outcode1 & outcode2 )
97  return true;
98 
99  // Choose a side to clip
100  int thisoutcode, x, y;
101  if( outcode1 )
102  thisoutcode = outcode1;
103  else
104  thisoutcode = outcode2;
105 
106  /* One clip round
107  * Since we use the full range of 32 bit ints, the proportion
108  * computation has to be done in 64 bits to avoid horrible
109  * results */
110  if( thisoutcode & 1 ) // Clip the bottom
111  {
112  y = aClipBox->GetBottom();
113  x = x1 + (x2 - x1) * int64_t(y - y1) / (y2 - y1);
114  }
115  else if( thisoutcode & 2 ) // Clip the top
116  {
117  y = aClipBox->GetY();
118  x = x1 + (x2 - x1) * int64_t(y - y1) / (y2 - y1);
119  }
120  else if( thisoutcode & 8 ) // Clip the right
121  {
122  x = aClipBox->GetRight();
123  y = y1 + (y2 - y1) * int64_t(x - x1) / (x2 - x1);
124  }
125  else // if( thisoutcode & 4), obviously, clip the left
126  {
127  x = aClipBox->GetX();
128  y = y1 + (y2 - y1) * int64_t(x - x1) / (x2 - x1);
129  }
130 
131  // Put the result back and update the boundary code
132  // No ambiguity, otherwise it would have been a fast reject
133  if( thisoutcode == outcode1 )
134  {
135  x1 = x;
136  y1 = y;
137  outcode1 = clipOutCode( aClipBox, x1, y1 );
138  }
139  else
140  {
141  x2 = x;
142  y2 = y;
143  outcode2 = clipOutCode( aClipBox, x2, y2 );
144  }
145  }
146  return false;
147 }
int clipOutCode(const EDA_RECT *aClipBox, int x, int y)
int GetBottom() const
Definition: eda_rect.h:122
int GetRight() const
Definition: eda_rect.h:119
int GetX() const
Definition: eda_rect.h:109
int GetY() const
Definition: eda_rect.h:110
int GetArcToSegmentCount ( int  aRadius,
int  aErrorMax,
double  aArcAngleDegree 
)
Returns
the number of segments to approximate a arc by segments with a given max error (this number is >= 1)
Parameters
aRadiusis the radius od the circle or arc
aErrorMaxis the max error This is the max distance between the middle of a segment and the circle.
aArcAngleDegreeis the arc angle in degrees

Definition at line 33 of file geometry_utils.cpp.

References max, and round_nearest().

Referenced by SHAPE_POLY_SET::chamferFilletPolygon(), ConvertOutlineToPolygon(), SHAPE_ARC::ConvertToPolyline(), CINFO3D_VISU::GetNrSegmentsCircle(), EAGLE_PLUGIN::loadPolygon(), EAGLE_PLUGIN::loadSignals(), DSN::SPECCTRA_DB::makeIMAGE(), EAGLE_PLUGIN::packagePolygon(), and DRAWSEGMENT::TransformShapeWithClearanceToPolygon().

34 {
35  // calculate the number of segments to approximate a circle by segments
36  // given the max distance between the middle of a segment and the circle
37 
38  // error relative to the radius value:
39  double rel_error = (double)aErrorMax / aRadius;
40  // minimal arc increment in degrees:
41  double step = 180 / M_PI * acos( 1.0 - rel_error ) * 2;
42  // the minimal seg count for a arc
43  int segCount = round_nearest( fabs( aArcAngleDegree ) / step );
44 
45  // Ensure at least one segment is used
46  return std::max( segCount, 1 );
47 }
static int round_nearest(double v)
Definition: math_util.h:56
#define max(a, b)
Definition: auxiliary.h:86
double GetCircletoPolyCorrectionFactor ( int  aSegCountforCircle)
Returns
the correction factor to approximate a circle by segments
Parameters
aSegCountforCircleis the number of segments to approximate the circle

When creating a polygon from a circle, the polygon is inside the circle. Only corners are on the circle. This is incorrect when building clearance areas of circles, that need to build the equivalent polygon outside the circle The correction factor is a scaling factor to apply to the radius to build a polygon outside the circle (only the middle of each segment is on the circle

Definition at line 50 of file geometry_utils.cpp.

Referenced by ZONE_FILLER::buildZoneFeatureHoleList(), ZONE_FILLER::computeRawFilledAreas(), BOARD::ConvertBrdLayerToPolygonalContours(), CINFO3D_VISU::GetCircleCorrectionFactor(), PlotSolderMaskLayer(), DRC::testCopperDrawItem(), and DRC::testCopperTextItem().

51 {
52  /* calculates the coeff to compensate radius reduction of circle
53  * due to the segment approx.
54  * For a circle the min radius is radius * cos( 2PI / aSegCountforCircle / 2)
55  * this is the distance between the center and the middle of the segment.
56  * therfore, to move the middle of the segment to the circle (distance = radius)
57  * the correctionFactor is 1 /cos( PI/aSegCountforCircle )
58  */
59  if( aSegCountforCircle < 6 )
60  aSegCountforCircle = 6;
61  if( 1 || aSegCountforCircle > 64 )
62  return 1.0 / cos( M_PI / aSegCountforCircle );
63 }
template<typename T >
VECTOR2<T> GetVectorSnapped45 ( const VECTOR2< T > &  aVec)

Snap a vector onto the nearest 0, 45 or 90 degree line.

The magnitude of the vector is NOT kept, instead the co-ordinates are set equal (and/or opposite) or to zero as needed. The effect of this is that if the starting vector is on a square grid, the resulting snapped vector will still be on the same grid.

Parameters
avector to be snapped
Returns
the snapped vector

Definition at line 73 of file geometry_utils.h.

References abs, ClipLine(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by DRAWING_TOOL::constrainDimension(), and KIGFX::PREVIEW::TWO_POINT_GEOMETRY_MANAGER::SetEnd().

74 {
75  auto newVec = aVec;
76  const VECTOR2<T> absVec { std::abs( aVec.x ), std::abs( aVec.y ) };
77 
78  if ( absVec.x > absVec.y * 2 )
79  {
80  // snap along x-axis
81  newVec.y = 0;
82  }
83  else if ( absVec.y > absVec.x * 2 )
84  {
85  // snap onto y-axis
86  newVec.x = 0;
87  }
88  else if ( absVec.x > absVec.y )
89  {
90  // snap away from x-axis towards 45
91  newVec.y = std::copysign( aVec.x, aVec.y );
92  } else
93  {
94  // snap away from y-axis towards 45
95  newVec.x = std::copysign( aVec.y, aVec.x );
96  }
97 
98  return newVec;
99 }
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
#define abs(a)
Definition: auxiliary.h:84