You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.6 KiB

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