Horizon
Loading...
Searching...
No Matches
convert_to_biu.h
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-2020 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#pragma once
26
27
28/* Note about internal units and max size for boards and items
29
30 The largest distance that we (and Kicad) can support is INT_MAX, since it represents
31 distance often in a wxCoord or wxSize. As a scalar, a distance is always
32 positive. Because int is 32 bits and INT_MAX is
33 2147483647. The most difficult distance for a virtual (world) cartesian
34 space is the hypotenuse, or diagonal measurement at a 45 degree angle. This
35 puts the most stress on the distance magnitude within the bounded virtual
36 space. So if we allow this distance to be our constraint of <= INT_MAX, this
37 constraint then propagates to the maximum distance in X and in Y that can be
38 supported on each axis. Remember that the hypotenuse of a 1x1 square is
39 sqrt( 1x1 + 1x1 ) = sqrt(2) = 1.41421356.
40
41 hypotenuse of any square = sqrt(2) * deltaX;
42
43 Let maximum supported hypotenuse be INT_MAX, then:
44
45 MAX_AXIS = INT_MAX / sqrt(2) = 2147483647 / 1.41421356 = 1518500251
46
47 The next choice is what to use for internal units (IU), sometimes called
48 world units. If nanometers, then the virtual space must be limited to
49 about 1.5 x 1.5 meters square. This is 1518500251 divided by 1e9 nm/meter.
50
51 The maximum zoom factor then depends on the client window size. If we ask
52 wx to handle something outside INT_MIN to INT_MAX, there are unreported
53 problems in the non-Debug build because wxRound() goes silent.
54
55 Pcbnew uses nanometers because we need to convert coordinates and size between
56 millimeters and inches. using a iu = 1 nm avoid rounding issues
57
58 Gerbview uses iu = 10 nm because we can have coordinates far from origin, and
59 1 nm is too small to avoid int overflow.
60 (Conversions between millimeters and inches are not critical)
61*/
62
69constexpr double GERB_IU_PER_MM = 1e5; // Gerbview IU is 10 nanometers.
70constexpr double PCB_IU_PER_MM = 1e6; // Pcbnew IU is 1 nanometer.
71constexpr double PL_IU_PER_MM = 1e3; // internal units in micron (should be enough)
72constexpr double SCH_IU_PER_MM = 1e4; // Schematic internal units 1=100nm
73
75#if defined(PCBNEW) || defined(CVPCB)
76constexpr double IU_PER_MM = PCB_IU_PER_MM;
77#elif defined(GERBVIEW)
78constexpr double IU_PER_MM = GERB_IU_PER_MM;
79#elif defined(PL_EDITOR)
80constexpr double IU_PER_MM = PL_IU_PER_MM;
81#elif defined(EESCHEMA)
82constexpr double IU_PER_MM = SCH_IU_PER_MM;
83#else
84#define UNKNOWN_IU
85#endif
86
87#ifndef UNKNOWN_IU
88constexpr double IU_PER_MILS = (IU_PER_MM * 0.0254);
89
90constexpr inline int Mils2iu( int mils )
91{
92 double x = mils * IU_PER_MILS;
93 return int( x < 0 ? x - 0.5 : x + 0.5 );
94}
95
96#if defined(EESCHEMA)
97constexpr inline int Iu2Mils( int iu )
98{
99 double mils = iu / IU_PER_MILS;
100
101 return static_cast< int >( mils < 0 ? mils - 0.5 : mils + 0.5 );
102}
103#else
104constexpr inline double Iu2Mils( int iu )
105{
106 double mils = iu / IU_PER_MILS;
107
108 return static_cast< int >( mils < 0 ? mils - 0.5 : mils + 0.5 );
109}
110#endif
111
112// Other definitions used in a few files
113constexpr double MM_PER_IU = ( 1 / IU_PER_MM );
114
116constexpr inline int Millimeter2iu( double mm )
117{
118 return (int) ( mm < 0 ? mm * IU_PER_MM - 0.5 : mm * IU_PER_MM + 0.5 );
119}
120
122constexpr inline double Iu2Millimeter( int iu )
123{
124 return iu / IU_PER_MM;
125}
126
128// constexpr inline double Iu2Mils( int iu )
129// {
130// return iu / IU_PER_MILS;
131// }
132
133// The max error is the distance between the middle of a segment, and the circle
134// for circle/arc to segment approximation.
135// Warning: too small values can create very long calculation time in zone filling
136// 0.05 to 0.005 mm are reasonable values
137
138constexpr int ARC_LOW_DEF = Millimeter2iu( 0.02 );
139constexpr int ARC_HIGH_DEF = Millimeter2iu( 0.005 );
140
141#else
142constexpr double PCB_IU_PER_MILS = (PCB_IU_PER_MM * 0.0254);
143constexpr double SCH_IU_PER_MILS = (SCH_IU_PER_MM * 0.0254);
144
145constexpr inline int SchMils2iu( double mils )
146{
147 double x = mils * SCH_IU_PER_MILS;
148 return int( x < 0 ? x - 0.5 : x + 0.5 );
149}
150constexpr inline double SchIu2Mils( int iu )
151{
152 return iu / SCH_IU_PER_MILS;
153}
154
155constexpr inline int PcbMm2iu( double mm )
156{
157 return (int) ( mm < 0 ? mm * PCB_IU_PER_MM - 0.5 : mm * PCB_IU_PER_MM + 0.5 );
158}
159constexpr inline double PcbIu2mm( int iu )
160{
161 return iu / PCB_IU_PER_MM;
162}
163
164#endif