KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shape_segment.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2013 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef __SHAPE_SEGMENT_H
23#define __SHAPE_SEGMENT_H
24
25#include <geometry/seg.h>
26#include <geometry/shape.h>
27#include <math/box2.h>
28#include <math/vector2d.h>
29#include <trigo.h>
30
31#include <algorithm>
32
33class SHAPE_SEGMENT : public SHAPE
34{
35public:
38 m_width( 0 )
39 {};
40
41 SHAPE_SEGMENT( const VECTOR2I& aA, const VECTOR2I& aB, int aWidth = 0 ) :
43 m_seg( aA, aB ),
44 m_width( aWidth )
45 {};
46
47 SHAPE_SEGMENT( const SEG& aSeg, int aWidth = 0 ) :
49 m_seg( aSeg ),
50 m_width( aWidth )
51 {};
52
53 static SHAPE_SEGMENT BySizeAndCenter( const VECTOR2I& aSize, const VECTOR2I& aCenter, const EDA_ANGLE& aRotation );
54
56
57 SHAPE* Clone() const override
58 {
59 return new SHAPE_SEGMENT( m_seg, m_width );
60 }
61
62 const BOX2I BBox( int aClearance = 0 ) const override
63 {
64 return BOX2I( m_seg.A, m_seg.B - m_seg.A ).Inflate( aClearance + ( m_width + 1 ) / 2 );
65 }
66
67 bool Collide( const SHAPE* aShape, int aClearance, VECTOR2I* aMTV ) const override
68 {
69 return SHAPE::Collide( aShape, aClearance, aMTV );
70 }
71
72 bool Collide( const SHAPE* aShape, int aClearance = 0, int* aActual = nullptr,
73 VECTOR2I* aLocation = nullptr ) const override
74 {
75 return SHAPE::Collide( aShape, aClearance, aActual, aLocation );
76 }
77
78 bool Collide( const SEG& aSeg, int aClearance = 0, int* aActual = nullptr,
79 VECTOR2I* aLocation = nullptr ) const override
80 {
81 if( aSeg.A == aSeg.B )
82 return Collide( aSeg.A, aClearance, aActual, aLocation );
83
84 int min_dist = ( m_width + 1 ) / 2 + aClearance;
85 ecoord dist_sq = m_seg.SquaredDistance( aSeg );
86
87 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
88 {
89 if( aLocation )
90 *aLocation = m_seg.NearestPoint( aSeg );
91
92 if( aActual )
93 *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
94
95 return true;
96 }
97
98 return false;
99 }
100
101 bool Collide( const VECTOR2I& aP, int aClearance = 0, int* aActual = nullptr,
102 VECTOR2I* aLocation = nullptr ) const override
103 {
104 int min_dist = ( m_width + 1 ) / 2 + aClearance;
105 ecoord dist_sq = m_seg.SquaredDistance( aP );
106
107 if( dist_sq == 0 || dist_sq < SEG::Square( min_dist ) )
108 {
109 if( aLocation )
110 *aLocation = m_seg.NearestPoint( aP );
111
112 if( aActual )
113 *aActual = std::max( 0, (int) sqrt( dist_sq ) - ( m_width + 1 ) / 2 );
114
115 return true;
116 }
117
118 return false;
119 }
120
121 void SetSeg( const SEG& aSeg )
122 {
123 m_seg = aSeg;
124 }
125
126 const SEG& GetSeg() const
127 {
128 return m_seg;
129 }
130
131 VECTOR2I GetStart() const override { return m_seg.A; }
132 VECTOR2I GetEnd() const override { return m_seg.B; }
133
134 void SetWidth( int aWidth ) override
135 {
136 m_width = aWidth;
137 }
138
139 int GetWidth() const override
140 {
141 return m_width;
142 }
143
147 int GetTotalLength() const
148 {
149 return m_seg.Length() + m_width;
150 }
151
153 {
154 return m_seg.Center();
155 }
156
158 {
159 return EDA_ANGLE( m_seg.B - m_seg.A );
160 }
161
162 bool IsSolid() const override
163 {
164 return true;
165 }
166
167 void Rotate( const EDA_ANGLE& aAngle, const VECTOR2I& aCenter = { 0, 0 } ) override
168 {
169 RotatePoint( m_seg.A, aCenter, aAngle );
170 RotatePoint( m_seg.B, aCenter, aAngle );
171 }
172
173 void Move( const VECTOR2I& aVector ) override
174 {
175 m_seg.A += aVector;
176 m_seg.B += aVector;
177 }
178
179 bool Is45Degree( EDA_ANGLE aTollerance = EDA_ANGLE( 1.0, DEGREES_T ) ) const;
180
181 virtual const std::string Format( bool aCplusPlus = true ) const override;
182
183 void TransformToPolygon( SHAPE_POLY_SET& aBuffer, int aError,
184 ERROR_LOC aErrorLoc ) const override;
185
186private:
189};
190
191#endif
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:554
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
static SEG::ecoord Square(int a)
Definition seg.h:119
Represent a set of closed polygons.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
const SEG & GetSeg() const
bool IsSolid() const override
VECTOR2I GetEnd() const override
EDA_ANGLE GetAngle() const
bool Is45Degree(EDA_ANGLE aTollerance=EDA_ANGLE(1.0, DEGREES_T)) const
SHAPE_SEGMENT(const VECTOR2I &aA, const VECTOR2I &aB, int aWidth=0)
VECTOR2I GetStart() const override
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
SHAPE * Clone() const override
Return a dynamically allocated copy of the shape.
bool Collide(const SEG &aSeg, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the segment aSeg than aClearance,...
SHAPE_SEGMENT(const SEG &aSeg, int aWidth=0)
void Move(const VECTOR2I &aVector) override
virtual const std::string Format(bool aCplusPlus=true) const override
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
int GetWidth() const override
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
int GetTotalLength() const
Get the total length of the segment, from tip to tip.
void SetWidth(int aWidth) override
void SetSeg(const SEG &aSeg)
bool Collide(const SHAPE *aShape, int aClearance, VECTOR2I *aMTV) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
static SHAPE_SEGMENT BySizeAndCenter(const VECTOR2I &aSize, const VECTOR2I &aCenter, const EDA_ANGLE &aRotation)
bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
VECTOR2I GetCenter() const
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition shape.h:179
VECTOR2I::extended_type ecoord
Definition shape.h:299
SHAPE(SHAPE_TYPE aType)
Create an empty shape of type aType.
Definition shape.h:134
@ DEGREES_T
Definition eda_angle.h:31
@ SH_SEGMENT
line segment
Definition shape.h:44
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:225
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683