KiCad PCB EDA Suite
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 (C) 1992-2022 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
70constexpr double GERB_IU_PER_MM = 1e5; // Gerbview IU is 10 nanometers.
71constexpr double PCB_IU_PER_MM = 1e6; // Pcbnew IU is 1 nanometer.
72constexpr double PL_IU_PER_MM = 1e3; // internal units in micron (should be enough)
73constexpr double SCH_IU_PER_MM = 1e4; // Schematic internal units 1=100nm
74
76{
77 const double IU_PER_MM;
78 const double IU_PER_MILS;
79 const double MM_PER_IU;
80
81
82 constexpr EDA_IU_SCALE( double aIUPerMM ) :
83 IU_PER_MM( aIUPerMM ), IU_PER_MILS( aIUPerMM * 0.0254 ), MM_PER_IU( 1 / IU_PER_MM )
84 {
85 }
86
87 constexpr inline double IUTomm( int iu ) const { return iu / IU_PER_MM; }
88
89 constexpr inline int mmToIU( double mm ) const
90 {
91 return (int) ( mm < 0 ? ( mm * IU_PER_MM - 0.5 ) : ( mm * IU_PER_MM + 0.5 ) );
92 }
93
94 constexpr inline int MilsToIU( int mils ) const
95 {
96 double x = mils * IU_PER_MILS;
97 return int( x < 0 ? x - 0.5 : x + 0.5 );
98 }
99
100 constexpr inline int IUToMils( int iu ) const
101 {
102 double mils = iu / IU_PER_MILS;
103
104 return static_cast<int>( mils < 0 ? mils - 0.5 : mils + 0.5 );
105 }
106};
107
113
114#ifndef SWIG
115// The max error is the distance between the middle of a segment, and the circle
116// for circle/arc to segment approximation.
117// Warning: too small values can create very long calculation time in zone filling
118// 0.05 to 0.005 mm are reasonable values
119
120constexpr int ARC_LOW_DEF = pcbIUScale.mmToIU( 0.02 );
121constexpr int ARC_HIGH_DEF = pcbIUScale.mmToIU( 0.005 );
122#endif
123
124#endif // _BASE_UNITS_H_
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
constexpr double SCH_IU_PER_MM
Definition: base_units.h:73
constexpr int ARC_HIGH_DEF
Definition: base_units.h:121
constexpr double PCB_IU_PER_MM
Definition: base_units.h:71
constexpr double GERB_IU_PER_MM
some define and functions to convert a value in mils, decimils or mm to the internal unit used in pcb...
Definition: base_units.h:70
constexpr EDA_IU_SCALE drawSheetIUScale
Definition: base_units.h:110
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
constexpr int ARC_LOW_DEF
Definition: base_units.h:120
constexpr EDA_IU_SCALE unityScale
Definition: base_units.h:112
constexpr EDA_IU_SCALE gerbIUScale
Definition: base_units.h:108
constexpr double PL_IU_PER_MM
Definition: base_units.h:72
constexpr double IUTomm(int iu) const
Definition: base_units.h:87
constexpr int IUToMils(int iu) const
Definition: base_units.h:100
const double IU_PER_MM
Definition: base_units.h:77
constexpr EDA_IU_SCALE(double aIUPerMM)
Definition: base_units.h:82
const double IU_PER_MILS
Definition: base_units.h:78
const double MM_PER_IU
Definition: base_units.h:79
constexpr int MilsToIU(int mils) const
Definition: base_units.h:94
constexpr int mmToIU(double mm) const
Definition: base_units.h:89