1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23  */
24 
25 /**
26  * @file round_segment_2d.h
27  */
28 
29 #ifndef _ROUND_SEGMENT_2D_H_
30 #define _ROUND_SEGMENT_2D_H_
31 
32 #include "object_2d.h"
33 #include "../ray.h"
34 #include <plugins/3dapi/xv3d_types.h>   // SFVEC2F
35 
36 class BOARD_ITEM;
37 
38 class ROUND_SEGMENT_2D : public OBJECT_2D
39 {
40 public:
41     ROUND_SEGMENT_2D( const SFVEC2F& aStart, const SFVEC2F& aEnd, float aWidth,
42                       const BOARD_ITEM& aBoardItem );
43 
GetRadius()44     float GetRadius() const { return m_radius; }
GetRadiusSquared()45     float GetRadiusSquared() const { return m_radius_squared; }
GetWidth()46     float GetWidth()  const { return m_width; }
GetLength()47     float GetLength() const { return m_segment.m_Length; }
48 
GetStart()49     const SFVEC2F& GetStart() const { return m_segment.m_Start; }
GetEnd()50     const SFVEC2F& GetEnd() const { return m_segment.m_End; }
GetEnd_minus_Start()51     const SFVEC2F& GetEnd_minus_Start() const { return m_segment.m_End_minus_start; }
52 
GetLeftStar()53     const SFVEC2F& GetLeftStar() const { return m_leftStart; }
GetLeftEnd()54     const SFVEC2F& GetLeftEnd()  const { return m_leftEnd; }
GetLeftEnd_minus_Start()55     const SFVEC2F& GetLeftEnd_minus_Start() const { return m_leftEndMinusStart; }
GetLeftDir()56     const SFVEC2F& GetLeftDir()  const { return m_leftDir; }
57 
GetRightStar()58     const SFVEC2F& GetRightStar() const { return m_rightStart; }
GetRightEnd()59     const SFVEC2F& GetRightEnd()  const { return m_rightEnd; }
GetRightEnd_minus_Start()60     const SFVEC2F& GetRightEnd_minus_Start() const { return m_rightEndMinusStart; }
GetRightDir()61     const SFVEC2F& GetRightDir()  const { return m_rightDir; }
62 
63     bool Overlaps( const BBOX_2D& aBBox ) const override;
64     bool Intersects( const BBOX_2D& aBBox ) const override;
65     bool Intersect( const RAYSEG2D& aSegRay, float* aOutT, SFVEC2F* aNormalOut ) const override;
66     INTERSECTION_RESULT IsBBoxInside( const BBOX_2D& aBBox ) const override;
67     bool IsPointInside( const SFVEC2F& aPoint ) const override;
68 
69 private:
70     friend class ROUND_SEGMENT;
71 
72     RAYSEG2D m_segment;
73 
74     SFVEC2F  m_leftStart;
75     SFVEC2F  m_leftEnd;
76     SFVEC2F  m_leftEndMinusStart;
77     SFVEC2F  m_leftDir;
78 
79     SFVEC2F  m_rightStart;
80     SFVEC2F  m_rightEnd;
81     SFVEC2F  m_rightEndMinusStart;
82     SFVEC2F  m_rightDir;
83 
84     float    m_radius;
85     float    m_radius_squared;
86     float    m_width;
87 };
88 
89 static const float s_min_dot = ( FLT_EPSILON * 4.0f * FLT_EPSILON * 4.0f );
90 
91 /**
92  * Check if segment start and end is very close to each other.
93  *
94  * This should used to check if the segment should be converted to a circle instead.
95  *
96  * @return true is it is better to convert the segment to circle
97  */
Is_segment_a_circle(const SFVEC2F & aStart,const SFVEC2F & aEnd)98 inline bool Is_segment_a_circle( const SFVEC2F& aStart, const SFVEC2F& aEnd )
99 {
100     const SFVEC2F vec = aEnd - aStart;
101 
102     // This is the same as calc the length squared (without the sqrt)
103     // and compare with a small value.
104     return (aStart == aEnd) || ( glm::dot( vec, vec ) <= s_min_dot );
105 }
106 
107 #endif // _ROUND_SEGMENT_2D_H_
108