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