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.

123 lines
4.9 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 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, 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. #ifndef _BASE_UNITS_H_
  25. #define _BASE_UNITS_H_
  26. /* Note about internal units and max size for boards and items
  27. The largest distance that we (and Kicad) can support is INT_MAX, since it represents
  28. distance often in a wxCoord or wxSize. As a scalar, a distance is always
  29. positive. Because int is 32 bits and INT_MAX is
  30. 2147483647. The most difficult distance for a virtual (world) cartesian
  31. space is the hypotenuse, or diagonal measurement at a 45 degree angle. This
  32. puts the most stress on the distance magnitude within the bounded virtual
  33. space. So if we allow this distance to be our constraint of <= INT_MAX, this
  34. constraint then propagates to the maximum distance in X and in Y that can be
  35. supported on each axis. Remember that the hypotenuse of a 1x1 square is
  36. sqrt( 1x1 + 1x1 ) = sqrt(2) = 1.41421356.
  37. hypotenuse of any square = sqrt(2) * deltaX;
  38. Let maximum supported hypotenuse be INT_MAX, then:
  39. MAX_AXIS = INT_MAX / sqrt(2) = 2147483647 / 1.41421356 = 1518500251
  40. The next choice is what to use for internal units (IU), sometimes called
  41. world units. If nanometers, then the virtual space must be limited to
  42. about 1.5 x 1.5 meters square. This is 1518500251 divided by 1e9 nm/meter.
  43. The maximum zoom factor then depends on the client window size. If we ask
  44. wx to handle something outside INT_MIN to INT_MAX, there are unreported
  45. problems in the non-Debug build because wxRound() goes silent.
  46. Pcbnew uses nanometers because we need to convert coordinates and size between
  47. millimeters and inches. using a iu = 1 nm avoid rounding issues
  48. Gerbview uses iu = 10 nm because we can have coordinates far from origin, and
  49. 1 nm is too small to avoid int overflow.
  50. (Conversions between millimeters and inches are not critical)
  51. */
  52. /**
  53. * @brief some macros and functions to convert a value in mils, decimils or mm to the internal
  54. * unit used in pcbnew, cvpcb or gerbview (nanometer or deci-mil) 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. struct EDA_IU_SCALE
  61. {
  62. const double IU_PER_MM;
  63. const double IU_PER_MILS;
  64. const double MM_PER_IU;
  65. constexpr EDA_IU_SCALE( double aIUPerMM ) :
  66. IU_PER_MM( aIUPerMM ), IU_PER_MILS( aIUPerMM * 0.0254 ), MM_PER_IU( 1 / IU_PER_MM )
  67. {
  68. }
  69. constexpr inline double IUTomm( int iu ) const { return iu / IU_PER_MM; }
  70. constexpr inline int mmToIU( double mm ) const
  71. {
  72. return (int) ( mm < 0 ? ( mm * IU_PER_MM - 0.5 ) : ( mm * IU_PER_MM + 0.5 ) );
  73. }
  74. constexpr inline int MilsToIU( int mils ) const
  75. {
  76. double x = mils * IU_PER_MILS;
  77. return int( x < 0 ? x - 0.5 : x + 0.5 );
  78. }
  79. constexpr inline int IUToMils( int iu ) const
  80. {
  81. double mils = iu / IU_PER_MILS;
  82. return static_cast<int>( mils < 0 ? mils - 0.5 : mils + 0.5 );
  83. }
  84. };
  85. constexpr EDA_IU_SCALE gerbIUScale = EDA_IU_SCALE( GERB_IU_PER_MM );
  86. constexpr EDA_IU_SCALE pcbIUScale = EDA_IU_SCALE( PCB_IU_PER_MM );
  87. constexpr EDA_IU_SCALE drawSheetIUScale = EDA_IU_SCALE( PL_IU_PER_MM );
  88. constexpr EDA_IU_SCALE schIUScale = EDA_IU_SCALE( SCH_IU_PER_MM );
  89. constexpr EDA_IU_SCALE unityScale = EDA_IU_SCALE( 1 );
  90. #ifndef SWIG
  91. // The max error is the distance between the middle of a segment, and the circle
  92. // for circle/arc to segment approximation.
  93. // Warning: too small values can create very long calculation time in zone filling
  94. // 0.05 to 0.005 mm are reasonable values
  95. constexpr int ARC_LOW_DEF = pcbIUScale.mmToIU( 0.02 );
  96. constexpr int ARC_HIGH_DEF = pcbIUScale.mmToIU( 0.005 );
  97. #endif
  98. #endif // _BASE_UNITS_H_