KiCad PCB EDA Suite
Loading...
Searching...
No Matches
easypc_units.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
25
26#ifndef EASYPC_UNITS_H
27#define EASYPC_UNITS_H
28
29#include <cmath>
30#include <cstdint>
31#include <limits>
32
33
34namespace EASYPC
35{
36
37constexpr int64_t NM_PER_DSU = 100;
38
39constexpr int ANGLE_FULL_TURN = 360000;
40
42constexpr double COORD_LIMIT = std::numeric_limits<int>::max() / 2.0;
43
44
45constexpr int64_t DsuToNm( int64_t aDsu )
46{
47 return aDsu * NM_PER_DSU;
48}
49
50
51constexpr double AngleToDegrees( int32_t aAngle )
52{
53 return aAngle / 1000.0;
54}
55
56
58constexpr int32_t NormalizeAngle( int32_t aAngle )
59{
60 return ( aAngle % ANGLE_FULL_TURN + ANGLE_FULL_TURN ) % ANGLE_FULL_TURN;
61}
62
63
68inline void PointRotate( double aPx, double aPy, int32_t aAngle, double aCx, double aCy, int32_t& aOutX,
69 int32_t& aOutY )
70{
71 double x = aPx;
72 double y = aPy;
73
74 if( aAngle != 0 )
75 {
76 double c = std::cos( aAngle * M_PI / 180000.0 );
77 double s = std::sin( aAngle * M_PI / 180000.0 );
78
79 if( aAngle == 90000 || aAngle == 270000 )
80 {
81 c = 0.0;
82 s = aAngle == 90000 ? 1.0 : -1.0;
83 }
84 else if( aAngle == 180000 )
85 {
86 c = -1.0;
87 s = 0.0;
88 }
89
90 double dx = aPx - aCx;
91 double dy = aPy - aCy;
92 x = ( c * dx + aCx ) - s * dy;
93 y = s * dx + aCy + c * dy;
94 }
95
96 aOutX = static_cast<int32_t>( x );
97 aOutY = static_cast<int32_t>( y );
98}
99
100
101inline void PointRotate( int32_t& aX, int32_t& aY, int32_t aAngle, int32_t aCx, int32_t aCy )
102{
103 PointRotate( aX, aY, aAngle, aCx, aCy, aX, aY );
104}
105
106} // namespace EASYPC
107
108#endif // EASYPC_UNITS_H
constexpr double COORD_LIMIT
Half of KiCad's integer range, so shape code rotating or adding coordinates cannot overflow.
void PointRotate(double aPx, double aPy, int32_t aAngle, double aCx, double aCy, int32_t &aOutX, int32_t &aOutY)
Rotate aPx, aPy anticlockwise by aAngle about aCx, aCy as Easy-PC places points: in double with exact...
constexpr int32_t NormalizeAngle(int32_t aAngle)
Wrap an angle into [0, ANGLE_FULL_TURN)
constexpr double AngleToDegrees(int32_t aAngle)
constexpr int ANGLE_FULL_TURN
constexpr int64_t NM_PER_DSU
constexpr int64_t DsuToNm(int64_t aDsu)
#define M_PI