2014-10-22 15:51:34 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-06-05 11:49:25 +00:00
|
|
|
* Copyright (C) 2012-2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2020-12-01 19:27:42 +00:00
|
|
|
* Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-10-22 15:51:34 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2020-12-01 19:27:42 +00:00
|
|
|
#pragma once
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2020-12-02 17:33:59 +00:00
|
|
|
|
|
|
|
/* Note about internal units and max size for boards and items
|
|
|
|
|
|
|
|
The largest distance that we (and Kicad) can support is INT_MAX, since it represents
|
|
|
|
distance often in a wxCoord or wxSize. As a scalar, a distance is always
|
|
|
|
positive. Because int is 32 bits and INT_MAX is
|
|
|
|
2147483647. The most difficult distance for a virtual (world) cartesian
|
|
|
|
space is the hypotenuse, or diagonal measurement at a 45 degree angle. This
|
|
|
|
puts the most stress on the distance magnitude within the bounded virtual
|
|
|
|
space. So if we allow this distance to be our constraint of <= INT_MAX, this
|
|
|
|
constraint then propagates to the maximum distance in X and in Y that can be
|
|
|
|
supported on each axis. Remember that the hypotenuse of a 1x1 square is
|
|
|
|
sqrt( 1x1 + 1x1 ) = sqrt(2) = 1.41421356.
|
|
|
|
|
|
|
|
hypotenuse of any square = sqrt(2) * deltaX;
|
|
|
|
|
|
|
|
Let maximum supported hypotenuse be INT_MAX, then:
|
|
|
|
|
|
|
|
MAX_AXIS = INT_MAX / sqrt(2) = 2147483647 / 1.41421356 = 1518500251
|
|
|
|
|
|
|
|
The next choice is what to use for internal units (IU), sometimes called
|
|
|
|
world units. If nanometers, then the virtual space must be limited to
|
|
|
|
about 1.5 x 1.5 meters square. This is 1518500251 divided by 1e9 nm/meter.
|
|
|
|
|
|
|
|
The maximum zoom factor then depends on the client window size. If we ask
|
|
|
|
wx to handle something outside INT_MIN to INT_MAX, there are unreported
|
|
|
|
problems in the non-Debug build because wxRound() goes silent.
|
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
Pcbnew uses nanometers because we need to convert coordinates and size between
|
|
|
|
millimeters and inches. using a iu = 1 nm avoid rounding issues
|
2020-12-02 17:33:59 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
Gerbview uses iu = 10 nm because we can have coordinates far from origin, and
|
2020-12-02 17:33:59 +00:00
|
|
|
1 nm is too small to avoid int overflow.
|
2021-06-09 19:32:58 +00:00
|
|
|
(Conversions between millimeters and inches are not critical)
|
2020-12-02 17:33:59 +00:00
|
|
|
*/
|
|
|
|
|
2012-05-03 18:37:56 +00:00
|
|
|
/**
|
|
|
|
* @brief some define and functions to convert a value in mils, decimils or mm
|
2012-05-06 20:10:43 +00:00
|
|
|
* to the internal unit used in pcbnew, cvpcb or gerbview (nanometer or deci-mil)
|
|
|
|
* depending on compile time option
|
2012-05-03 18:37:56 +00:00
|
|
|
*/
|
|
|
|
|
2020-07-08 18:29:16 +00:00
|
|
|
constexpr double GERB_IU_PER_MM = 1e5; // Gerbview IU is 10 nanometers.
|
|
|
|
constexpr double PCB_IU_PER_MM = 1e6; // Pcbnew IU is 1 nanometer.
|
|
|
|
constexpr double PL_IU_PER_MM = 1e3; // internal units in micron (should be enough)
|
|
|
|
constexpr double SCH_IU_PER_MM = 1e4; // Schematic internal units 1=100nm
|
|
|
|
|
2012-05-03 18:37:56 +00:00
|
|
|
/// Scaling factor to convert mils to internal units.
|
2020-07-08 18:29:16 +00:00
|
|
|
#if defined(PCBNEW) || defined(CVPCB)
|
|
|
|
constexpr double IU_PER_MM = PCB_IU_PER_MM;
|
|
|
|
#elif defined(GERBVIEW)
|
|
|
|
constexpr double IU_PER_MM = GERB_IU_PER_MM;
|
|
|
|
#elif defined(PL_EDITOR)
|
|
|
|
constexpr double IU_PER_MM = PL_IU_PER_MM;
|
|
|
|
#elif defined(EESCHEMA)
|
|
|
|
constexpr double IU_PER_MM = SCH_IU_PER_MM;
|
|
|
|
#else
|
|
|
|
#define UNKNOWN_IU
|
|
|
|
#endif
|
2016-12-09 15:43:41 +00:00
|
|
|
|
2020-07-08 18:29:16 +00:00
|
|
|
#ifndef UNKNOWN_IU
|
|
|
|
constexpr double IU_PER_MILS = (IU_PER_MM * 0.0254);
|
2012-05-03 18:37:56 +00:00
|
|
|
|
2020-07-08 18:29:16 +00:00
|
|
|
constexpr inline int Mils2iu( int mils )
|
2012-05-03 18:37:56 +00:00
|
|
|
{
|
|
|
|
double x = mils * IU_PER_MILS;
|
|
|
|
return int( x < 0 ? x - 0.5 : x + 0.5 );
|
|
|
|
}
|
|
|
|
|
2020-07-08 18:29:16 +00:00
|
|
|
#if defined(EESCHEMA)
|
|
|
|
constexpr inline int Iu2Mils( int iu )
|
2019-12-30 18:28:00 +00:00
|
|
|
{
|
|
|
|
double mils = iu / IU_PER_MILS;
|
|
|
|
|
|
|
|
return static_cast< int >( mils < 0 ? mils - 0.5 : mils + 0.5 );
|
|
|
|
}
|
2020-07-08 18:29:16 +00:00
|
|
|
#else
|
|
|
|
constexpr inline double Iu2Mils( int iu )
|
2019-12-30 18:28:00 +00:00
|
|
|
{
|
|
|
|
double mils = iu / IU_PER_MILS;
|
|
|
|
|
|
|
|
return static_cast< int >( mils < 0 ? mils - 0.5 : mils + 0.5 );
|
2012-05-03 18:37:56 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-05 11:49:25 +00:00
|
|
|
// Other definitions used in a few files
|
2016-12-09 15:43:41 +00:00
|
|
|
constexpr double MM_PER_IU = ( 1 / IU_PER_MM );
|
2016-06-05 11:49:25 +00:00
|
|
|
|
2012-08-11 12:52:13 +00:00
|
|
|
/// Convert mm to internal units (iu).
|
2016-12-09 15:43:41 +00:00
|
|
|
constexpr inline int Millimeter2iu( double mm )
|
2012-08-11 12:52:13 +00:00
|
|
|
{
|
2016-12-09 15:43:41 +00:00
|
|
|
return (int) ( mm < 0 ? mm * IU_PER_MM - 0.5 : mm * IU_PER_MM + 0.5 );
|
2012-08-11 12:52:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-05 11:49:25 +00:00
|
|
|
/// Convert mm to internal units (iu).
|
2016-12-09 15:43:41 +00:00
|
|
|
constexpr inline double Iu2Millimeter( int iu )
|
2016-06-05 11:49:25 +00:00
|
|
|
{
|
|
|
|
return iu / IU_PER_MM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert mm to internal units (iu).
|
2019-12-30 18:28:00 +00:00
|
|
|
// constexpr inline double Iu2Mils( int iu )
|
|
|
|
// {
|
|
|
|
// return iu / IU_PER_MILS;
|
|
|
|
// }
|
2018-03-22 18:02:45 +00:00
|
|
|
|
|
|
|
// The max error is the distance between the middle of a segment, and the circle
|
|
|
|
// for circle/arc to segment approximation.
|
|
|
|
// Warning: too small values can create very long calculation time in zone filling
|
|
|
|
// 0.05 to 0.005 mm are reasonable values
|
|
|
|
|
|
|
|
constexpr int ARC_LOW_DEF = Millimeter2iu( 0.02 );
|
|
|
|
constexpr int ARC_HIGH_DEF = Millimeter2iu( 0.005 );
|
|
|
|
|
2020-07-08 18:29:16 +00:00
|
|
|
#else
|
|
|
|
constexpr double PCB_IU_PER_MILS = (PCB_IU_PER_MM * 0.0254);
|
|
|
|
constexpr double SCH_IU_PER_MILS = (SCH_IU_PER_MM * 0.0254);
|
|
|
|
|
2021-07-25 17:08:01 +00:00
|
|
|
constexpr inline int SchMils2iu( double mils )
|
2020-07-08 18:29:16 +00:00
|
|
|
{
|
|
|
|
double x = mils * SCH_IU_PER_MILS;
|
|
|
|
return int( x < 0 ? x - 0.5 : x + 0.5 );
|
|
|
|
}
|
2020-07-27 21:14:44 +00:00
|
|
|
constexpr inline double SchIu2Mils( int iu )
|
|
|
|
{
|
|
|
|
return iu / SCH_IU_PER_MILS;
|
|
|
|
}
|
2020-07-08 18:29:16 +00:00
|
|
|
|
|
|
|
constexpr inline int PcbMillimeter2iu( double mm )
|
|
|
|
{
|
|
|
|
return (int) ( mm < 0 ? mm * PCB_IU_PER_MM - 0.5 : mm * PCB_IU_PER_MM + 0.5 );
|
|
|
|
}
|
|
|
|
constexpr inline double PcbIu2Millimeter( int iu )
|
|
|
|
{
|
|
|
|
return iu / PCB_IU_PER_MM;
|
|
|
|
}
|
2020-07-27 21:14:44 +00:00
|
|
|
|
2020-06-14 15:08:47 +00:00
|
|
|
#endif
|