KiCad PCB EDA Suite
Loading...
Searching...
No Matches
base_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 (C) 2012-2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The 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#ifndef _BASE_UNITS_H_
27#define _BASE_UNITS_H_
28
29/* Note about internal units and max size for boards and items
30
31 The largest distance that we (and Kicad) can support is INT_MAX, since it represents
32 distance often in a wxCoord or wxSize. As a scalar, a distance is always
33 positive. Because int is 32 bits and INT_MAX is
34 2147483647. The most difficult distance for a virtual (world) cartesian
35 space is the hypotenuse, or diagonal measurement at a 45 degree angle. This
36 puts the most stress on the distance magnitude within the bounded virtual
37 space. So if we allow this distance to be our constraint of <= INT_MAX, this
38 constraint then propagates to the maximum distance in X and in Y that can be
39 supported on each axis. Remember that the hypotenuse of a 1x1 square is
40 sqrt( 1x1 + 1x1 ) = sqrt(2) = 1.41421356.
41
42 hypotenuse of any square = sqrt(2) * deltaX;
43
44 Let maximum supported hypotenuse be INT_MAX, then:
45
46 MAX_AXIS = INT_MAX / sqrt(2) = 2147483647 / 1.41421356 = 1518500251
47
48 The next choice is what to use for internal units (IU), sometimes called
49 world units. If nanometers, then the virtual space must be limited to
50 about 1.5 x 1.5 meters square. This is 1518500251 divided by 1e9 nm/meter.
51
52 The maximum zoom factor then depends on the client window size. If we ask
53 wx to handle something outside INT_MIN to INT_MAX, there are unreported
54 problems in the non-Debug build because wxRound() goes silent.
55
56 Pcbnew uses nanometers because we need to convert coordinates and size between
57 millimeters and inches. using a iu = 1 nm avoid rounding issues
58
59 Gerbview uses iu = 10 nm because we can have coordinates far from origin, and
60 1 nm is too small to avoid int overflow.
61 (Conversions between millimeters and inches are not critical)
62*/
63
69constexpr double GERB_IU_PER_MM = 1e5;
70constexpr double PCB_IU_PER_MM = 1e6;
71constexpr double PL_IU_PER_MM = 1e3;
72constexpr double SCH_IU_PER_MM = 1e4;
73
75{
76 const double IU_PER_MM;
77 const double IU_PER_MILS;
78 const double IU_PER_PS{ 1e6 };
79 const double IU_PER_PS_PER_MM{ 1e6 };
80 const double MM_PER_IU;
81
82
83 constexpr EDA_IU_SCALE( double aIUPerMM ) :
84 IU_PER_MM( aIUPerMM ), IU_PER_MILS( aIUPerMM * 0.0254 ), MM_PER_IU( 1 / IU_PER_MM )
85 {
86 }
87
88 constexpr inline double IUTomm( int iu ) const { return iu / IU_PER_MM; }
89
90 constexpr inline int mmToIU( double mm ) const
91 {
92 return (int) ( mm < 0 ? ( mm * IU_PER_MM - 0.5 ) : ( mm * IU_PER_MM + 0.5 ) );
93 }
94
95 constexpr inline int MilsToIU( int mils ) const
96 {
97 double x = mils * IU_PER_MILS;
98 return int( x < 0 ? x - 0.5 : x + 0.5 );
99 }
100
101 constexpr inline int IUToMils( int iu ) const
102 {
103 double mils = iu / IU_PER_MILS;
104
105 return static_cast<int>( mils < 0 ? mils - 0.5 : mils + 0.5 );
106 }
107};
108
114
115// Allowed error to approximate an arg by segments, in millimeters
116constexpr double ARC_LOW_DEF_MM = 0.02;
117constexpr double ARC_HIGH_DEF_MM = 0.005;
118
119#ifndef SWIG
120// The max error is the distance between the middle of a segment, and the circle
121// for circle/arc to segment approximation.
122// Warning: too small values can create very long calculation time in zone filling
123// 0.05 to 0.005 mm are reasonable values
124
125// Allowed error to approximate an arg by segments, in Pcbnew IU
128#endif
129
130#endif // _BASE_UNITS_H_
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:112
constexpr double SCH_IU_PER_MM
Schematic internal units 1=100nm.
Definition: base_units.h:72
constexpr int ARC_HIGH_DEF
Definition: base_units.h:127
constexpr double PCB_IU_PER_MM
Pcbnew IU is 1 nanometer.
Definition: base_units.h:70
constexpr double GERB_IU_PER_MM
some macros and functions to convert a value in mils, decimils or mm to the internal unit used in pcb...
Definition: base_units.h:69
constexpr EDA_IU_SCALE drawSheetIUScale
Definition: base_units.h:111
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:110
constexpr int ARC_LOW_DEF
Definition: base_units.h:126
constexpr double ARC_HIGH_DEF_MM
Definition: base_units.h:117
constexpr EDA_IU_SCALE unityScale
Definition: base_units.h:113
constexpr EDA_IU_SCALE gerbIUScale
Definition: base_units.h:109
constexpr double ARC_LOW_DEF_MM
Definition: base_units.h:116
constexpr double PL_IU_PER_MM
Internal units in micron (should be enough).
Definition: base_units.h:71
constexpr double IUTomm(int iu) const
Definition: base_units.h:88
constexpr int IUToMils(int iu) const
Definition: base_units.h:101
const double IU_PER_PS
Internal time units are attoseconds.
Definition: base_units.h:78
const double IU_PER_MM
Definition: base_units.h:76
constexpr EDA_IU_SCALE(double aIUPerMM)
Definition: base_units.h:83
const double IU_PER_MILS
Definition: base_units.h:77
const double MM_PER_IU
Definition: base_units.h:80
constexpr int MilsToIU(int mils) const
Definition: base_units.h:95
const double IU_PER_PS_PER_MM
Internal velocity units are attoseconds/mm.
Definition: base_units.h:79
constexpr int mmToIU(double mm) const
Definition: base_units.h:90