File: arc_geom_manager.cpp

package info (click to toggle)
kicad 9.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 770,320 kB
  • sloc: cpp: 961,692; ansic: 121,001; xml: 66,428; python: 18,387; sh: 1,010; awk: 301; asm: 292; makefile: 227; javascript: 167; perl: 10
file content (187 lines) | stat: -rw-r--r-- 4,333 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <preview_items/arc_geom_manager.h>

#include <math/util.h>      // for KiROUND
#include <geometry/eda_angle.h>
#include <trigo.h>

using namespace KIGFX::PREVIEW;


///< Snap an angle to the nearest 45 degrees
static EDA_ANGLE snapAngle( const EDA_ANGLE& aAngle )
{
    return ANGLE_45 * KiROUND( aAngle / ANGLE_45 );
}


bool ARC_GEOM_MANAGER::acceptPoint( const VECTOR2I& aPt )
{
    switch( getStep() )
    {
    case SET_ORIGIN: return setOrigin( aPt );
    case SET_START:  return setStart( aPt );
    case SET_ANGLE:  return setEnd( aPt );
    case COMPLETE:   return false;
    }

    return false;
}


void ARC_GEOM_MANAGER::SetClockwise( bool aCw )
{
    m_clockwise = aCw;
    m_directionLocked = true;
    setGeometryChanged();
}


void ARC_GEOM_MANAGER::ToggleClockwise()
{
    m_clockwise = !m_clockwise;
    m_directionLocked = true;
    setGeometryChanged();
}


VECTOR2I ARC_GEOM_MANAGER::GetOrigin() const
{
    return m_origin;
}


VECTOR2I ARC_GEOM_MANAGER::GetStartRadiusEnd() const
{
    VECTOR2I vec( static_cast<int>( m_radius ), 0 );
    RotatePoint( vec, -m_startAngle );
    return m_origin +vec;
}


VECTOR2I ARC_GEOM_MANAGER::GetEndRadiusEnd() const
{
    VECTOR2I vec( static_cast<int>( m_radius ), 0 );
    RotatePoint( vec, -m_endAngle );
    return m_origin + vec;
}


double ARC_GEOM_MANAGER::GetRadius() const
{
    return m_radius;
}


EDA_ANGLE ARC_GEOM_MANAGER::GetStartAngle() const
{
    EDA_ANGLE angle = m_startAngle;

    if( m_clockwise )
        angle -= ANGLE_360;

    return -angle;
}


EDA_ANGLE ARC_GEOM_MANAGER::GetSubtended() const
{
    EDA_ANGLE angle = m_endAngle - m_startAngle;

    if( m_endAngle <= m_startAngle )
        angle += ANGLE_360;

    if( m_clockwise )
        angle -= ANGLE_360;

    return -angle;
}


bool ARC_GEOM_MANAGER::setOrigin( const VECTOR2I& aOrigin )
{
    m_origin     = aOrigin;
    m_startAngle = ANGLE_0;
    m_endAngle   = ANGLE_0;

    return true;
}


bool ARC_GEOM_MANAGER::setStart( const VECTOR2I& aEnd )
{
    const VECTOR2I radVec = aEnd - m_origin;

    m_radius = radVec.EuclideanNorm();
    m_startAngle = EDA_ANGLE( radVec );

    if( m_angleSnap )
        m_startAngle = snapAngle( m_startAngle );

    // normalise to 0..360
    while( m_startAngle < ANGLE_0 )
        m_startAngle += ANGLE_360;

    m_endAngle = m_startAngle;

    return m_radius != 0.0;
}


bool ARC_GEOM_MANAGER::setEnd( const VECTOR2I& aCursor )
{
    const VECTOR2I radVec = aCursor - m_origin;

    m_endAngle = EDA_ANGLE( radVec );

    if( m_angleSnap )
        m_endAngle = snapAngle( m_endAngle );

    // normalise to 0..360
    while( m_endAngle < ANGLE_0 )
        m_endAngle += ANGLE_360;

    if( !m_directionLocked )
    {
        EDA_ANGLE ccwAngle = m_endAngle - m_startAngle;

        if( m_endAngle <= m_startAngle )
            ccwAngle += ANGLE_360;

        EDA_ANGLE cwAngle = std::abs( ccwAngle - ANGLE_360 );

        if( std::min( ccwAngle, cwAngle ) >= ANGLE_90 )
            m_directionLocked = true;
        else
            m_clockwise = cwAngle < ccwAngle;
    }
    else if( std::abs( GetSubtended() ) < ANGLE_90 )
    {
        m_directionLocked = false;
    }

    // if the end is the same as the start, this is a bad point
    return m_endAngle != m_startAngle;
}