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