KiCad PCB EDA Suite
SHAPE_LINE_CHAIN_BASE Class Referenceabstract

#include <shape.h>

Inheritance diagram for SHAPE_LINE_CHAIN_BASE:
SHAPE SHAPE_BASE SHAPE_LINE_CHAIN SHAPE_POLY_SET::TRIANGULATED_POLYGON::TRI SHAPE_SIMPLE

Public Member Functions

 SHAPE_LINE_CHAIN_BASE (SHAPE_TYPE aType)
 
virtual ~SHAPE_LINE_CHAIN_BASE ()
 
virtual bool Collide (const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
 Function Collide() More...
 
virtual bool Collide (const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
 Function Collide() More...
 
SEG::ecoord SquaredDistance (const VECTOR2I &aP, bool aOutlineOnly=false) const
 
bool PointInside (const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
 Function PointInside() More...
 
bool PointOnEdge (const VECTOR2I &aP, int aAccuracy=0) const
 Function PointOnEdge() More...
 
int EdgeContainingPoint (const VECTOR2I &aP, int aAccuracy=0) const
 Function EdgeContainingPoint() More...
 
virtual const VECTOR2I GetPoint (int aIndex) const =0
 
virtual const SEG GetSegment (int aIndex) const =0
 
virtual size_t GetPointCount () const =0
 
virtual size_t GetSegmentCount () const =0
 
virtual bool IsClosed () const =0
 
virtual SHAPEClone () const
 Function Clone() More...
 
bool IsNull () const
 Function IsNull() More...
 
virtual bool Collide (const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const
 Function Collide() More...
 
virtual bool Collide (const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
 
virtual const BOX2I BBox (int aClearance=0) const =0
 Function BBox() More...
 
virtual VECTOR2I Centre () const
 Function Centre() More...
 
virtual void Rotate (double aAngle, const VECTOR2I &aCenter={ 0, 0 })=0
 Function Rotate. More...
 
virtual void Move (const VECTOR2I &aVector)=0
 
virtual bool IsSolid () const =0
 
virtual bool Parse (std::stringstream &aStream)
 
virtual const std::string Format () const
 
FACETNewFacet ()
 
SGNODECalcShape (SGNODE *aParent, SGNODE *aColor, WRL1_ORDER aVertexOrder, float aCreaseLimit=0.74317, bool isVRML2=false)
 
SHAPE_TYPE Type () const
 Function Type() More...
 
virtual bool HasIndexableSubshapes () const
 
virtual size_t GetIndexableSubshapeCount () const
 
virtual void GetIndexableSubshapes (std::vector< SHAPE * > &aSubshapes)
 

Protected Types

typedef VECTOR2I::extended_type ecoord
 

Protected Attributes

SHAPE_TYPE m_type
 

type of our shape

More...
 

Detailed Description

Definition at line 254 of file shape.h.

Member Typedef Documentation

◆ ecoord

typedef VECTOR2I::extended_type SHAPE::ecoord
protectedinherited

Definition at line 125 of file shape.h.

Constructor & Destructor Documentation

◆ SHAPE_LINE_CHAIN_BASE()

SHAPE_LINE_CHAIN_BASE::SHAPE_LINE_CHAIN_BASE ( SHAPE_TYPE  aType)
inline

Definition at line 257 of file shape.h.

257  : SHAPE( aType )
258  {
259  }
SHAPE(SHAPE_TYPE aType)
Constructor.
Definition: shape.h:134

◆ ~SHAPE_LINE_CHAIN_BASE()

virtual SHAPE_LINE_CHAIN_BASE::~SHAPE_LINE_CHAIN_BASE ( )
inlinevirtual

Definition at line 262 of file shape.h.

263  {
264  }

Member Function Documentation

◆ BBox()

virtual const BOX2I SHAPE::BBox ( int  aClearance = 0) const
pure virtualinherited

◆ CalcShape()

SGNODE * SHAPE::CalcShape ( SGNODE aParent,
SGNODE aColor,
WRL1_ORDER  aVertexOrder,
float  aCreaseLimit = 0.74317,
bool  isVRML2 = false 
)
inherited

Definition at line 713 of file wrlfacet.cpp.

715 {
716  if( facets.empty() || !facets.front()->HasMinPoints() )
717  return NULL;
718 
719  std::vector< std::list< FACET* > > flist;
720 
721  // determine the max. index and size flist as appropriate
722  std::list< FACET* >::iterator sF = facets.begin();
723  std::list< FACET* >::iterator eF = facets.end();
724 
725  int maxIdx = 0;
726  int tmi;
727  float maxV = 0.0;
728  float tV = 0.0;
729 
730  while( sF != eF )
731  {
732  tV = ( *sF )->CalcFaceNormal();
733  tmi = ( *sF )->GetMaxIndex();
734 
735  if( tmi > maxIdx )
736  maxIdx = tmi;
737 
738  if( tV > maxV )
739  maxV = tV;
740 
741  ++sF;
742  }
743 
744  ++maxIdx;
745 
746  if( maxIdx < 3 )
747  return NULL;
748 
749  flist.resize( maxIdx );
750 
751  // create the lists of facets common to indices
752  sF = facets.begin();
753 
754  while( sF != eF )
755  {
756  ( *sF )->Renormalize( tV );
757  ( *sF )->CollectVertices( flist );
758  ++sF;
759  }
760 
761  // calculate the normals
762  size_t vs = flist.size();
763 
764  for( size_t i = 0; i < vs; ++i )
765  {
766  sF = flist[i].begin();
767  eF = flist[i].end();
768 
769  while( sF != eF )
770  {
771  ( *sF )->CalcVertexNormal( static_cast<int>( i ), flist[i], aCreaseLimit );
772  ++sF;
773  }
774  }
775 
776  std::vector< WRLVEC3F > vertices;
777  std::vector< WRLVEC3F > normals;
778  std::vector< SGCOLOR > colors;
779 
780  // push the facet data to the final output list
781  sF = facets.begin();
782  eF = facets.end();
783 
784  while( sF != eF )
785  {
786  ( *sF )->GetData( vertices, normals, colors, aVertexOrder );
787  ++sF;
788  }
789 
790  flist.clear();
791 
792  if( vertices.size() < 3 )
793  return NULL;
794 
795  IFSG_SHAPE shapeNode( false );
796 
797  if( !isVRML2 )
798  {
799  shapeNode.NewNode( aParent );
800 
801  if( aColor )
802  {
803  if( NULL == S3D::GetSGNodeParent( aColor ) )
804  shapeNode.AddChildNode( aColor );
805  else
806  shapeNode.AddRefNode( aColor );
807  }
808  }
809 
810  std::vector< SGPOINT > lCPts; // vertex points in SGPOINT (double) format
811  std::vector< SGVECTOR > lCNorm; // per-vertex normals
812  vs = vertices.size();
813 
814  for( size_t i = 0; i < vs; ++i )
815  {
816  SGPOINT pt;
817  pt.x = vertices[i].x;
818  pt.y = vertices[i].y;
819  pt.z = vertices[i].z;
820  lCPts.push_back( pt );
821  lCNorm.emplace_back( normals[i].x, normals[i].y, normals[i].z );
822  }
823 
824  vertices.clear();
825  normals.clear();
826 
827  IFSG_FACESET fsNode( false );
828 
829  if( !isVRML2 )
830  fsNode.NewNode( shapeNode );
831  else
832  fsNode.NewNode( aParent );
833 
834  IFSG_COORDS cpNode( fsNode );
835  cpNode.SetCoordsList( lCPts.size(), &lCPts[0] );
836  IFSG_COORDINDEX ciNode( fsNode );
837 
838  for( int i = 0; i < (int)lCPts.size(); ++i )
839  ciNode.AddIndex( i );
840 
841  IFSG_NORMALS nmNode( fsNode );
842  nmNode.SetNormalList( lCNorm.size(), &lCNorm[0] );
843 
844  if( !colors.empty() )
845  {
846  IFSG_COLORS nmColor( fsNode );
847  nmColor.SetColorList( colors.size(), &colors[0] );
848  colors.clear();
849  }
850 
851  if( !isVRML2 )
852  return shapeNode.GetRawPtr();
853 
854  return fsNode.GetRawPtr();
855 }
double x
Definition: sg_base.h:70
IFSG_COORDS is the wrapper for SGCOORDS.
Definition: ifsg_coords.h:40
IFSG_COORDINDEX is the wrapper for SGCOORDINDEX.
IFSG_COLORS is the wrapper for SGCOLORS.
Definition: ifsg_colors.h:41
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
Definition: ifsg_api.cpp:636
double y
Definition: sg_base.h:71
IFSG_NORMALS is the wrapper for the SGNORMALS class.
Definition: ifsg_normals.h:40
#define NULL
std::list< FACET * > facets
Definition: wrlfacet.h:143
IFSG_FACESET is the wrapper for the SGFACESET class.
Definition: ifsg_faceset.h:40
double z
Definition: sg_base.h:72
static VRML_COLOR colors[VRML_COLOR_LAST]
IFSG_SHAPE is the wrapper for the SGSHAPE class.
Definition: ifsg_shape.h:40

References IFSG_NODE::AddChildNode(), IFSG_INDEX::AddIndex(), IFSG_NODE::AddRefNode(), colors, SHAPE::facets, IFSG_NODE::GetRawPtr(), S3D::GetSGNodeParent(), IFSG_FACESET::NewNode(), IFSG_SHAPE::NewNode(), NULL, IFSG_COLORS::SetColorList(), IFSG_COORDS::SetCoordsList(), IFSG_NORMALS::SetNormalList(), SGPOINT::x, SGPOINT::y, and SGPOINT::z.

Referenced by WRL1FACESET::TranslateToSG(), X3DIFACESET::TranslateToSG(), and WRL2FACESET::TranslateToSG().

◆ Centre()

virtual VECTOR2I SHAPE::Centre ( ) const
inlinevirtualinherited

Function Centre()

Computes a center-of-mass of the shape

Returns
the center-of-mass point

Definition at line 232 of file shape.h.

233  {
234  return BBox( 0 ).Centre(); // if nothing better is available....
235  }
virtual const BOX2I BBox(int aClearance=0) const =0
Function BBox()
Vec Centre() const
Definition: box2.h:79

References SHAPE::BBox(), and BOX2< Vec >::Centre().

Referenced by Collide(), and PNS_PCBNEW_RULE_RESOLVER::CollideHoles().

◆ Clone()

virtual SHAPE* SHAPE::Clone ( ) const
inlinevirtualinherited

Function Clone()

Returns a dynamically allocated copy of the shape

Return values
copyof the shape

Reimplemented in SHAPE_POLY_SET, SHAPE_LINE_CHAIN, SHAPE_ARC, SHAPE_RECT, SHAPE_SIMPLE, SHAPE_CIRCLE, SHAPE_COMPOUND, SHAPE_SEGMENT, and SHAPE_NULL.

Definition at line 148 of file shape.h.

149  {
150  assert( false );
151  return NULL;
152  };
#define NULL

References NULL.

Referenced by PNS::SOLID::SOLID(), and ROUTER_PREVIEW_ITEM::Update().

◆ Collide() [1/4]

bool SHAPE::Collide ( const SHAPE aShape,
int  aClearance,
VECTOR2I aMTV 
) const
virtualinherited

Function Collide()

Checks if the boundary of shape (this) lies closer to the shape aShape than aClearance, indicating a collision.

Parameters
aShapeshape to check collision against
aClearanceminimum clearance
aMTVminimum translation vector
aActual[out] an optional pointer to an int to store the actual distance in the event of a collision.
aLocation[out] an option pointer to a point to store a nearby location in the event of a collision.
Returns
true, if there is a collision.

Reimplemented in SHAPE_RECT, SHAPE_SEGMENT, and SHAPE_COMPOUND.

Definition at line 824 of file shape_collisions.cpp.

825 {
826  return collideShapes( this, aShape, aClearance, nullptr, nullptr, aMTV );
827 }
static bool collideShapes(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)

References collideShapes().

◆ Collide() [2/4]

bool SHAPE::Collide ( const SHAPE aShape,
int  aClearance = 0,
int *  aActual = nullptr,
VECTOR2I aLocation = nullptr 
) const
virtualinherited

Reimplemented in SHAPE_POLY_SET, SHAPE_RECT, SHAPE_SEGMENT, and SHAPE_COMPOUND.

Definition at line 830 of file shape_collisions.cpp.

831 {
832  return collideShapes( this, aShape, aClearance, aActual, aLocation, nullptr );
833 }
static bool collideShapes(const SHAPE *aA, const SHAPE *aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)

References collideShapes().

◆ Collide() [3/4]

bool SHAPE_LINE_CHAIN_BASE::Collide ( const VECTOR2I aP,
int  aClearance = 0,
int *  aActual = nullptr,
VECTOR2I aLocation = nullptr 
) const
overridevirtual

Function Collide()

Checks if point aP lies closer to us than aClearance.

Parameters
aPthe point to check for collisions with
aClearanceminimum distance that does not qualify as a collision.
aActualan optional pointer to an int to store the actual distance in the event of a collision.
Returns
true, when a collision has been found

Reimplemented from SHAPE.

Definition at line 88 of file shape_line_chain.cpp.

90 {
91  if( IsClosed() && PointInside( aP, aClearance ) )
92  {
93  if( aLocation )
94  *aLocation = aP;
95 
96  if( aActual )
97  *aActual = 0;
98 
99  return true;
100  }
101 
102  SEG::ecoord closest_dist_sq = VECTOR2I::ECOORD_MAX;
103  SEG::ecoord clearance_sq = SEG::Square( aClearance );
104  VECTOR2I nearest;
105 
106  for( int i = 0; i < GetSegmentCount(); i++ )
107  {
108  const SEG& s = GetSegment( i );
109  VECTOR2I pn = s.NearestPoint( aP );
110  SEG::ecoord dist_sq = ( pn - aP ).SquaredEuclideanNorm();
111 
112  if( dist_sq < closest_dist_sq )
113  {
114  nearest = pn;
115  closest_dist_sq = dist_sq;
116 
117  if( closest_dist_sq == 0 )
118  break;
119 
120  // If we're not looking for aActual then any collision will do
121  if( closest_dist_sq < clearance_sq && !aActual )
122  break;
123  }
124  }
125 
126  if( closest_dist_sq == 0 || closest_dist_sq < clearance_sq )
127  {
128  if( aLocation )
129  *aLocation = nearest;
130 
131  if( aActual )
132  *aActual = sqrt( closest_dist_sq );
133 
134  return true;
135  }
136 
137  return false;
138 }
virtual bool IsClosed() const =0
VECTOR2I::extended_type ecoord
Definition: seg.h:42
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
virtual size_t GetSegmentCount() const =0
static SEG::ecoord Square(int a)
Definition: seg.h:116
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:80
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:395
Definition: seg.h:39
virtual const SEG GetSegment(int aIndex) const =0

References VECTOR2< T >::ECOORD_MAX, GetSegment(), GetSegmentCount(), IsClosed(), SEG::NearestPoint(), PointInside(), and SEG::Square().

Referenced by PNS::MEANDERED_LINE::CheckSelfIntersections(), SHAPE_SIMPLE::Collide(), Collide(), and SELECTION_TOOL::GuessSelectionCandidates().

◆ Collide() [4/4]

bool SHAPE_LINE_CHAIN_BASE::Collide ( const SEG aSeg,
int  aClearance = 0,
int *  aActual = nullptr,
VECTOR2I aLocation = nullptr 
) const
overridevirtual

Function Collide()

Checks if segment aSeg lies closer to us than aClearance.

Parameters
aSegthe segment to check for collisions with
aClearanceminimum distance that does not qualify as a collision.
aActualan optional pointer to an int to store the actual distance in the event of a collision.
Returns
true, when a collision has been found

Implements SHAPE.

Reimplemented in SHAPE_SIMPLE.

Definition at line 155 of file shape_line_chain.cpp.

157 {
158  if( IsClosed() && PointInside( aSeg.A ) )
159  {
160  if( aLocation )
161  *aLocation = aSeg.A;
162 
163  if( aActual )
164  *aActual = 0;
165 
166  return true;
167  }
168 
169  SEG::ecoord closest_dist_sq = VECTOR2I::ECOORD_MAX;
170  SEG::ecoord clearance_sq = SEG::Square( aClearance );
171  VECTOR2I nearest;
172 
173  for( int i = 0; i < GetSegmentCount(); i++ )
174  {
175  const SEG& s = GetSegment( i );
176  SEG::ecoord dist_sq =s.SquaredDistance( aSeg );
177 
178  if( dist_sq < closest_dist_sq )
179  {
180  if( aLocation )
181  nearest = s.NearestPoint( aSeg );
182 
183  closest_dist_sq = dist_sq;
184 
185  if( closest_dist_sq == 0)
186  break;
187 
188  // If we're not looking for aActual then any collision will do
189  if( closest_dist_sq < clearance_sq && !aActual )
190  break;
191  }
192  }
193 
194  if( closest_dist_sq == 0 || closest_dist_sq < clearance_sq )
195  {
196  if( aLocation )
197  *aLocation = nearest;
198 
199  if( aActual )
200  *aActual = sqrt( closest_dist_sq );
201 
202  return true;
203  }
204 
205  return false;
206 }
virtual bool IsClosed() const =0
VECTOR2I::extended_type ecoord
Definition: seg.h:42
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:37
virtual size_t GetSegmentCount() const =0
static SEG::ecoord Square(int a)
Definition: seg.h:116
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:80
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:395
Definition: seg.h:39
virtual const SEG GetSegment(int aIndex) const =0
VECTOR2I A
Definition: seg.h:47

References SEG::A, VECTOR2< T >::ECOORD_MAX, GetSegment(), GetSegmentCount(), IsClosed(), SEG::NearestPoint(), PointInside(), SEG::Square(), and SEG::SquaredDistance().

◆ EdgeContainingPoint()

int SHAPE_LINE_CHAIN_BASE::EdgeContainingPoint ( const VECTOR2I aP,
int  aAccuracy = 0 
) const

Function EdgeContainingPoint()

Checks if point aP lies on an edge or vertex of the line chain.

Parameters
aPpoint to check
Returns
index of the first edge containing the point, otherwise negative

Definition at line 718 of file shape_line_chain.cpp.

719 {
720  if( !GetPointCount() )
721  return -1;
722 
723  else if( GetPointCount() == 1 )
724  {
725  VECTOR2I dist = GetPoint(0) - aPt;
726  return ( hypot( dist.x, dist.y ) <= aAccuracy + 1 ) ? 0 : -1;
727  }
728 
729  for( int i = 0; i < GetSegmentCount(); i++ )
730  {
731  const SEG s = GetSegment( i );
732 
733  if( s.A == aPt || s.B == aPt )
734  return i;
735 
736  if( s.Distance( aPt ) <= aAccuracy + 1 )
737  return i;
738  }
739 
740  return -1;
741 }
int Distance(const SEG &aSeg) const
Function Distance()
Definition: seg.h:207
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
virtual size_t GetPointCount() const =0
virtual size_t GetSegmentCount() const =0
Definition: seg.h:39
virtual const SEG GetSegment(int aIndex) const =0
VECTOR2I A
Definition: seg.h:47
virtual const VECTOR2I GetPoint(int aIndex) const =0
VECTOR2I B
Definition: seg.h:48

References SEG::A, SEG::B, SEG::Distance(), GetPoint(), GetPointCount(), GetSegment(), GetSegmentCount(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by PointOnEdge(), and PNS::LINE::Walkaround().

◆ Format()

const std::string SHAPE::Format ( ) const
virtualinherited

Reimplemented in SHAPE_POLY_SET, SHAPE_LINE_CHAIN, SHAPE_RECT, SHAPE_SEGMENT, and SHAPE_COMPOUND.

Definition at line 35 of file shape.cpp.

36 {
37  assert( false );
38  return std::string( "" );
39 }

Referenced by SHAPE_FILE_IO::Write().

◆ GetIndexableSubshapeCount()

virtual size_t SHAPE_BASE::GetIndexableSubshapeCount ( ) const
inlinevirtualinherited

Reimplemented in SHAPE_POLY_SET, and SHAPE_COMPOUND.

Definition at line 108 of file shape.h.

108 { return 0; }

◆ GetIndexableSubshapes()

virtual void SHAPE_BASE::GetIndexableSubshapes ( std::vector< SHAPE * > &  aSubshapes)
inlinevirtualinherited

Reimplemented in SHAPE_POLY_SET, and SHAPE_COMPOUND.

Definition at line 110 of file shape.h.

110 { }

Referenced by SHAPE_COMPOUND::AddShape(), and ROUTER_PREVIEW_ITEM::ViewDraw().

◆ GetPoint()

virtual const VECTOR2I SHAPE_LINE_CHAIN_BASE::GetPoint ( int  aIndex) const
pure virtual

◆ GetPointCount()

virtual size_t SHAPE_LINE_CHAIN_BASE::GetPointCount ( ) const
pure virtual

◆ GetSegment()

virtual const SEG SHAPE_LINE_CHAIN_BASE::GetSegment ( int  aIndex) const
pure virtual

◆ GetSegmentCount()

virtual size_t SHAPE_LINE_CHAIN_BASE::GetSegmentCount ( ) const
pure virtual

◆ HasIndexableSubshapes()

virtual bool SHAPE_BASE::HasIndexableSubshapes ( ) const
inlinevirtualinherited

Reimplemented in SHAPE_POLY_SET, and SHAPE_COMPOUND.

Definition at line 103 of file shape.h.

104  {
105  return false;
106  }

Referenced by SHAPE_COMPOUND::AddShape(), and ROUTER_PREVIEW_ITEM::ViewDraw().

◆ IsClosed()

virtual bool SHAPE_LINE_CHAIN_BASE::IsClosed ( ) const
pure virtual

◆ IsNull()

bool SHAPE::IsNull ( ) const
inlineinherited

Function IsNull()

Returns true if the shape is a null shape.

Return values
trueif null :-)

Definition at line 160 of file shape.h.

161  {
162  return m_type == SH_NULL;
163  }
SHAPE_TYPE m_type
type of our shape
Definition: shape.h:114
circular arc
Definition: shape.h:51

References SHAPE_BASE::m_type, and SH_NULL.

◆ IsSolid()

◆ Move()

virtual void SHAPE::Move ( const VECTOR2I aVector)
pure virtualinherited

◆ NewFacet()

FACET * SHAPE::NewFacet ( )
inherited

Definition at line 705 of file wrlfacet.cpp.

706 {
707  FACET* fp = new FACET;
708  facets.push_back( fp );
709  return fp;
710 }
Definition: wrlfacet.h:41
std::list< FACET * > facets
Definition: wrlfacet.h:143

References SHAPE::facets.

Referenced by WRL1FACESET::TranslateToSG(), X3DIFACESET::TranslateToSG(), and WRL2FACESET::TranslateToSG().

◆ Parse()

bool SHAPE::Parse ( std::stringstream &  aStream)
virtualinherited

Reimplemented in SHAPE_POLY_SET, and SHAPE_LINE_CHAIN.

Definition at line 28 of file shape.cpp.

29 {
30  assert( false );
31  return false;
32 }

◆ PointInside()

bool SHAPE_LINE_CHAIN_BASE::PointInside ( const VECTOR2I aPt,
int  aAccuracy = 0,
bool  aUseBBoxCache = false 
) const

Function PointInside()

Checks if point aP lies inside a polygon (any type) defined by the line chain. For closed shapes only.

Parameters
aPtpoint to check
aUseBBoxCachegives better peformance if the bounding box caches have been generated.
Returns
true if the point is inside the shape (edge is not treated as being inside).

To check for interior points, we draw a line in the positive x direction from the point. If it intersects an even number of segments, the point is outside the line chain (it had to first enter and then exit). Otherwise, it is inside the chain.

Note: slope might be denormal here in the case of a horizontal line but we require our y to move from above to below the point (or vice versa)

Note: we open-code CPoint() here so that we don't end up calculating the size of the vector number-of-points times. This has a non-trivial impact on zone fill times.

Definition at line 659 of file shape_line_chain.cpp.

661 {
662  /*
663  * Don't check the bounding box unless it's cached. Building it is about the same speed as
664  * the rigorous test below and so just slows things down by doing potentially two tests.
665  */
666  //if( aUseBBoxCache && !m_bbox.Contains( aPt ) )
667  //return false;
668 
669  // fixme: bbox cache...
670 
671  if( !IsClosed() || GetPointCount() < 3 )
672  return false;
673 
674  bool inside = false;
675 
687  int pointCount = GetPointCount();
688 
689  for( int i = 0; i < pointCount; )
690  {
691  const auto p1 = GetPoint( i++ );
692  const auto p2 = GetPoint( i == pointCount ? 0 : i );
693  const auto diff = p2 - p1;
694 
695  if( diff.y != 0 )
696  {
697  const int d = rescale( diff.x, ( aPt.y - p1.y ), diff.y );
698 
699  if( ( ( p1.y > aPt.y ) != ( p2.y > aPt.y ) ) && ( aPt.x - p1.x < d ) )
700  inside = !inside;
701  }
702  }
703 
704  // If accuracy is <= 1 (nm) then we skip the accuracy test for performance. Otherwise
705  // we use "OnEdge(accuracy)" as a proxy for "Inside(accuracy)".
706  if( aAccuracy <= 1 )
707  return inside;
708  else
709  return inside || PointOnEdge( aPt, aAccuracy );
710 }
virtual bool IsClosed() const =0
virtual size_t GetPointCount() const =0
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Function PointOnEdge()
T rescale(T aNumerator, T aValue, T aDenominator)
Function rescale()
Definition: util.h:95
virtual const VECTOR2I GetPoint(int aIndex) const =0

References GetPoint(), GetPointCount(), IsClosed(), PointOnEdge(), rescale(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by Collide(), Collide(), POLY_GRID_PARTITION::containsPoint(), SHAPE_POLY_SET::containsSingle(), LIB_POLYLINE::HitTest(), ZONE::HitTestCutout(), MARKER_BASE::HitTestMarker(), SquaredDistance(), and PNS::LINE::Walkaround().

◆ PointOnEdge()

bool SHAPE_LINE_CHAIN_BASE::PointOnEdge ( const VECTOR2I aP,
int  aAccuracy = 0 
) const

Function PointOnEdge()

Checks if point aP lies on an edge or vertex of the line chain.

Parameters
aPpoint to check
Returns
true if the point lies on the edge.

Definition at line 713 of file shape_line_chain.cpp.

714 {
715  return EdgeContainingPoint( aPt, aAccuracy ) >= 0;
716 }
int EdgeContainingPoint(const VECTOR2I &aP, int aAccuracy=0) const
Function EdgeContainingPoint()

References EdgeContainingPoint().

Referenced by LIB_POLYLINE::HitTest(), and PointInside().

◆ Rotate()

virtual void SHAPE::Rotate ( double  aAngle,
const VECTOR2I aCenter = { 0, 0 } 
)
pure virtualinherited

Function Rotate.

Parameters
aCenteris the rotation center
aAnglerotation angle in radians

Implemented in SHAPE_POLY_SET, SHAPE_LINE_CHAIN, SHAPE_SIMPLE, SHAPE_RECT, SHAPE_SEGMENT, SHAPE_ARC, SHAPE_CIRCLE, SHAPE_COMPOUND, SHAPE_POLY_SET::TRIANGULATED_POLYGON::TRI, and SHAPE_NULL.

◆ SquaredDistance()

SEG::ecoord SHAPE_LINE_CHAIN_BASE::SquaredDistance ( const VECTOR2I aP,
bool  aOutlineOnly = false 
) const

Definition at line 350 of file shape_line_chain.cpp.

351 {
353 
354  if( IsClosed() && PointInside( aP ) && !aOutlineOnly )
355  return 0;
356 
357  for( int s = 0; s < GetSegmentCount(); s++ )
358  d = std::min( d, GetSegment( s ).SquaredDistance( aP ) );
359 
360  return d;
361 }
virtual bool IsClosed() const =0
ecoord SquaredDistance(const SEG &aSeg) const
Definition: seg.cpp:37
virtual size_t GetSegmentCount() const =0
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:80
VECTOR2I::extended_type ecoord
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Function PointInside()
virtual const SEG GetSegment(int aIndex) const =0

References VECTOR2< T >::ECOORD_MAX, GetSegment(), GetSegmentCount(), IsClosed(), PointInside(), and SEG::SquaredDistance().

Referenced by SHAPE_LINE_CHAIN::Distance().

◆ Type()

SHAPE_TYPE SHAPE_BASE::Type ( ) const
inlineinherited

Function Type()

Returns the type of the shape.

Return values
thetype

Definition at line 98 of file shape.h.

99  {
100  return m_type;
101  }
SHAPE_TYPE m_type
type of our shape
Definition: shape.h:114

References SHAPE_BASE::m_type.

Referenced by PNS::DP_GATEWAYS::BuildFromPrimitivePair(), PNS::buildHullForPrimitiveShape(), SHAPE_POLY_SET::Collide(), collideShapes(), collideSingleShapes(), PNS::OPTIMIZER::computeBreakouts(), ROUTER_PREVIEW_ITEM::drawShape(), PNS::SOLID::Hull(), and SHAPE_FILE_IO::Write().

Member Data Documentation

◆ m_type

SHAPE_TYPE SHAPE_BASE::m_type
protectedinherited

type of our shape

Definition at line 114 of file shape.h.

Referenced by SHAPE::IsNull(), and SHAPE_BASE::Type().


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