KiCad PCB EDA Suite
Loading...
Searching...
No Matches
geometry_utils.h File Reference

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

#include <math.h>
#include <stdlib.h>
#include <math/box2.h>
#include <geometry/eda_angle.h>

Go to the source code of this file.

Classes

class  DISABLE_ARC_RADIUS_CORRECTION
 When creating polygons to create a clearance polygonal area, the polygon must be same or bigger than the original shape. More...
 

Enumerations

enum  ERROR_LOC { ERROR_OUTSIDE = 0 , ERROR_INSIDE }
 When approximating an arc or circle, should the error be placed on the outside or inside of the curve? (Generally speaking filled shape errors go on the inside and knockout errors go on the outside. More...
 

Functions

int GetArcToSegmentCount (int aRadius, int aErrorMax, const EDA_ANGLE &aArcAngle)
 
int CircleToEndSegmentDeltaRadius (int aInnerCircleRadius, int aSegCount)
 
int GetCircleToPolyCorrection (int aMaxError)
 
template<typename T >
VECTOR2< T > GetVectorSnapped45 (const VECTOR2< T > &aVec, bool only45=false)
 Snap a vector onto the nearest 0, 45 or 90 degree line. More...
 
template<typename in_type , typename ret_type = in_type, typename pad_type = unsigned int, typename = typename std::enable_if<std::is_unsigned<pad_type>::value>::type>
VECTOR2< ret_type > GetClampedCoords (const VECTOR2< in_type > &aCoords, pad_type aPadding=1u)
 Clamps a vector to values that can be negated, respecting numeric limits of coordinates data type with specified padding. More...
 
bool ClipLine (const BOX2I *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.

Enumeration Type Documentation

◆ ERROR_LOC

enum ERROR_LOC

When approximating an arc or circle, should the error be placed on the outside or inside of the curve? (Generally speaking filled shape errors go on the inside and knockout errors go on the outside.

This preserves minimum clearances.)

Enumerator
ERROR_OUTSIDE 
ERROR_INSIDE 

Definition at line 43 of file geometry_utils.h.

Function Documentation

◆ CircleToEndSegmentDeltaRadius()

int CircleToEndSegmentDeltaRadius ( int  aInnerCircleRadius,
int  aSegCount 
)
Returns
the radius diffence of the circle defined by segments inside the circle and the radius of the circle tangent to the middle of segments (defined by segments outside this circle)
Parameters
aInnerCircleRadiusis the radius of the circle tangent to the middle of segments
aSegCountis the seg count to approximate the circle

Definition at line 67 of file geometry_utils.cpp.

References std::abs(), delta, and KiROUND().

Referenced by ConvertArcToPolyline(), SHAPE_ARC::ConvertToPolyline(), CornerListToPolygon(), TransformCircleToPolygon(), and TransformOvalToPolygon().

◆ ClipLine()

bool ClipLine ( const BOX2I 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 137 of file geometry_utils.cpp.

References clipOutCode(), BOX2< Vec >::GetBottom(), BOX2< Vec >::GetRight(), BOX2< Vec >::GetX(), and BOX2< Vec >::GetY().

Referenced by ZONE_FILLER::buildThermalSpokes(), KIGFX::PCB_PAINTER::renderNetNameForSegment(), STROKE_PARAMS::Stroke(), KIGFX::ORIGIN_VIEWITEM::ViewDraw(), and PCB_TRACK::ViewGetLOD().

◆ GetArcToSegmentCount()

int GetArcToSegmentCount ( int  aRadius,
int  aErrorMax,
const EDA_ANGLE aArcAngle 
)

◆ GetCircleToPolyCorrection()

int GetCircleToPolyCorrection ( int  aMaxError)
Returns
the radius correction to approximate a circle.
Parameters
aMaxErroris the same error value used to calculate the number of segments.

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.

Definition at line 106 of file geometry_utils.cpp.

References s_disable_arc_correction.

Referenced by CornerListToPolygon(), TransformCircleToPolygon(), and TransformOvalToPolygon().

◆ GetClampedCoords()

template<typename in_type , typename ret_type = in_type, typename pad_type = unsigned int, typename = typename std::enable_if<std::is_unsigned<pad_type>::value>::type>
VECTOR2< ret_type > GetClampedCoords ( const VECTOR2< in_type > &  aCoords,
pad_type  aPadding = 1u 
)

◆ GetVectorSnapped45()

template<typename T >
VECTOR2< T > GetVectorSnapped45 ( const VECTOR2< T > &  aVec,
bool  only45 = false 
)

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

The magnitude of the vector is NOT kept, instead the coordinates 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 105 of file geometry_utils.h.

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

Referenced by EC_45DEGREE::Apply(), build45DegLeader(), DRAWING_TOOL::constrainDimension(), EDIT_TOOL::doMoveSelection(), DRAWING_TOOL::drawShape(), and KIGFX::PREVIEW::TWO_POINT_GEOMETRY_MANAGER::SetEnd().