KiCad PCB EDA Suite
convert_to_biu.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-2019 KiCad Developers, see CHANGELOG.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 #ifndef CONVERT_TO_BIU_H_
26 #define CONVERT_TO_BIU_H_
27 
34 constexpr double GERB_IU_PER_MM = 1e5; // Gerbview IU is 10 nanometers.
35 constexpr double PCB_IU_PER_MM = 1e6; // Pcbnew IU is 1 nanometer.
36 constexpr double PL_IU_PER_MM = 1e3; // internal units in micron (should be enough)
37 constexpr double SCH_IU_PER_MM = 1e4; // Schematic internal units 1=100nm
38 
40 #if defined(PCBNEW) || defined(CVPCB)
41 constexpr double IU_PER_MM = PCB_IU_PER_MM;
42 #elif defined(GERBVIEW)
43 constexpr double IU_PER_MM = GERB_IU_PER_MM;
44 #elif defined(PL_EDITOR)
45 constexpr double IU_PER_MM = PL_IU_PER_MM;
46 #elif defined(EESCHEMA)
47 constexpr double IU_PER_MM = SCH_IU_PER_MM;
48 #else
49 #define UNKNOWN_IU
50 #endif
51 
52 #ifndef UNKNOWN_IU
53 constexpr double IU_PER_MILS = (IU_PER_MM * 0.0254);
54 
55 constexpr inline int Mils2iu( int mils )
56 {
57  double x = mils * IU_PER_MILS;
58  return int( x < 0 ? x - 0.5 : x + 0.5 );
59 }
60 
61 #if defined(EESCHEMA)
62 constexpr inline int Iu2Mils( int iu )
63 {
64  double mils = iu / IU_PER_MILS;
65 
66  return static_cast< int >( mils < 0 ? mils - 0.5 : mils + 0.5 );
67 }
68 #else
69 constexpr inline double Iu2Mils( int iu )
70 {
71  double mils = iu / IU_PER_MILS;
72 
73  return static_cast< int >( mils < 0 ? mils - 0.5 : mils + 0.5 );
74 }
75 #endif
76 
77 // Other definitions used in a few files
78 constexpr double MM_PER_IU = ( 1 / IU_PER_MM );
79 
81 constexpr inline int Millimeter2iu( double mm )
82 {
83  return (int) ( mm < 0 ? mm * IU_PER_MM - 0.5 : mm * IU_PER_MM + 0.5 );
84 }
85 
87 constexpr inline double Iu2Millimeter( int iu )
88 {
89  return iu / IU_PER_MM;
90 }
91 
93 // constexpr inline double Iu2Mils( int iu )
94 // {
95 // return iu / IU_PER_MILS;
96 // }
97 
98 // The max error is the distance between the middle of a segment, and the circle
99 // for circle/arc to segment approximation.
100 // Warning: too small values can create very long calculation time in zone filling
101 // 0.05 to 0.005 mm are reasonable values
102 
103 constexpr int ARC_LOW_DEF = Millimeter2iu( 0.02 );
104 constexpr int ARC_HIGH_DEF = Millimeter2iu( 0.005 );
105 
106 #else
107 constexpr double PCB_IU_PER_MILS = (PCB_IU_PER_MM * 0.0254);
108 constexpr double SCH_IU_PER_MILS = (SCH_IU_PER_MM * 0.0254);
109 
110 constexpr inline int SchMils2iu( int mils )
111 {
112  double x = mils * SCH_IU_PER_MILS;
113  return int( x < 0 ? x - 0.5 : x + 0.5 );
114 }
115 constexpr inline double SchIu2Mils( int iu )
116 {
117  return iu / SCH_IU_PER_MILS;
118 }
119 
120 constexpr inline int PcbMillimeter2iu( double mm )
121 {
122  return (int) ( mm < 0 ? mm * PCB_IU_PER_MM - 0.5 : mm * PCB_IU_PER_MM + 0.5 );
123 }
124 constexpr inline double PcbIu2Millimeter( int iu )
125 {
126  return iu / PCB_IU_PER_MM;
127 }
128 
129 #endif
130 
131 /* ZOOM LIMITS
132 
133  The largest distance that wx can support is INT_MAX, since it represents
134  distance often in a wxCoord or wxSize. As a scalar, a distance is always
135  positive. On most machines which run KiCad, int is 32 bits and INT_MAX is
136  2147483647. The most difficult distance for a virtual (world) cartesian
137  space is the hypotenuse, or diagonal measurement at a 45 degree angle. This
138  puts the most stress on the distance magnitude within the bounded virtual
139  space. So if we allow this distance to be our constraint of <= INT_MAX, this
140  constraint then propagates to the maximum distance in X and in Y that can be
141  supported on each axis. Remember that the hypotenuse of a 1x1 square is
142  sqrt( 1x1 + 1x1 ) = sqrt(2) = 1.41421356.
143 
144  hypotenuse of any square = sqrt(2) * deltaX;
145 
146  Let maximum supported hypotenuse be INT_MAX, then:
147 
148  MAX_AXIS = INT_MAX / sqrt(2) = 2147483647 / 1.41421356 = 1518500251
149 
150  This maximum distance is imposed by wxWidgets, not by KiCad. The imposition
151  comes in the form of the data structures used in the graphics API at the
152  wxDC level. Obviously when we are not interacting with wx we can use double
153  to compute distances larger than this. For example the computation of the
154  total length of a net, can and should be done in double, since it might
155  actually be longer than a single diagonal line.
156 
157  The next choice is what to use for internal units (IU), sometimes called
158  world units. If nanometers, then the virtual space must be limited to
159  about 1.5 x 1.5 meters square. This is 1518500251 divided by 1e9 nm/meter.
160 
161  The maximum zoom factor then depends on the client window size. If we ask
162  wx to handle something outside INT_MIN to INT_MAX, there are unreported
163  problems in the non-Debug build because wxRound() goes silent.
164 
165  Let:
166  const double MAX_AXIS = 1518500251;
167 
168  Then a maximum zoom factor for a screen of 1920 pixels wide is
169  1518500251 / 1920 = 790885.
170 
171  The largest zoom factor allowed is therefore ~ 300 (which computes to 762000).
172 */
173 
174 #define MAX_ZOOM_FACTOR 300.0
175 
176 // Adjusted to display zoom level ~ 1 when the screen shows a 1:1 image.
177 // Obviously depends on the monitor, but this is an acceptable value.
178 #define ZOOM_COEFF 1.1
179 
180 
181 
182 #endif // CONVERT_TO_BIU_H_
constexpr double PCB_IU_PER_MILS
static constexpr double IU_PER_MM
Mock up a conversion function.
constexpr double PcbIu2Millimeter(int iu)
constexpr int PcbMillimeter2iu(double mm)
constexpr double PCB_IU_PER_MM
constexpr double SchIu2Mils(int iu)
constexpr double SCH_IU_PER_MM
constexpr double SCH_IU_PER_MILS
constexpr double PL_IU_PER_MM
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...
#define IU_PER_MILS
Definition: plotter.cpp:137
constexpr int SchMils2iu(int mils)
static constexpr int Millimeter2iu(double mm)