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-2016 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 
38 #if defined(PCBNEW) || defined(CVPCB) || defined(GERBVIEW)
40  #if defined(GERBVIEW)
41  constexpr double IU_PER_MM = 1e5; // Gerbview IU is 10 nanometers.
42  #else
43  constexpr double IU_PER_MM = 1e6; // Pcbnew IU is 1 nanometer.
44  #endif
45 
46 constexpr double IU_PER_MILS = IU_PER_MM * 0.0254;
47 
49 inline int Mils2iu( int mils )
50 {
51  double x = mils * IU_PER_MILS;
52  return int( x < 0 ? x - 0.5 : x + 0.5 );
53 }
54 
55 #elif defined (PL_EDITOR)
56 constexpr double IU_PER_MM = 1e3; // internal units in micron (should be enough)
57 constexpr double IU_PER_MILS = (IU_PER_MM * 0.0254);
58 
60 inline int Mils2iu( int mils )
61 {
62  double x = mils * IU_PER_MILS;
63  return int( x < 0 ? x - 0.5 : x + 0.5 );
64 }
65 
66 #elif defined (EESCHEMA) // Eeschema
67 constexpr double IU_PER_MILS = 1.0;
68 constexpr double IU_PER_MM = ( IU_PER_MILS / 0.0254 );
69 
70 constexpr inline int Mils2iu( int mils )
71 {
72  return mils;
73 }
74 #else
75 // Here, we do not know the value of internal units: do not define
76 // conversion functions (They do not have meaning)
77 #define UNKNOWN_IU
78 #endif
79 
80 #ifndef UNKNOWN_IU
81 // Other definitions used in a few files
82 constexpr double MM_PER_IU = ( 1 / IU_PER_MM );
83 
85 constexpr inline int Millimeter2iu( double mm )
86 {
87  return (int) ( mm < 0 ? mm * IU_PER_MM - 0.5 : mm * IU_PER_MM + 0.5 );
88 }
89 
91 constexpr inline double Iu2Millimeter( int iu )
92 {
93  return iu / IU_PER_MM;
94 }
95 
97 constexpr inline double Iu2Mils( int iu )
98 {
99  return iu / IU_PER_MILS;
100 }
101 #endif
102 
103 #endif // CONVERT_TO_BIU_H_