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.

195 lines
5.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2020 Reece R. Pollack <reece@his.com>
  5. * Copyright (C) 1992-2019 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 ORIGIN_TRANSFORMS_H_
  25. #define ORIGIN_TRANSFORMS_H_ 1
  26. /**
  27. * A class to perform either relative or absolute display origin
  28. * transforms for a single axis of a point.
  29. *
  30. * The coordinate argument is transformed between an offset from
  31. * the internal origin and an offset from the user-specified origin
  32. * and coordinate direction.
  33. *
  34. * The functions are templated to allow use with any size scalar
  35. * parameter: an int, a long long int, or a double.
  36. */
  37. class ORIGIN_TRANSFORMS {
  38. public:
  39. /**
  40. * The supported Display Origin Transform types
  41. *
  42. * Absolute coordinates require both translation and direction
  43. * inversion. Relative coordinates require only direction inversion.
  44. */
  45. enum COORD_TYPES_T {
  46. NOT_A_COORD, //< A non-coordinate value, never transformed
  47. ABS_X_COORD, //< An absolute X coordinate
  48. ABS_Y_COORD, //< An absolute Y coordinate
  49. REL_X_COORD, //< A relative X coordinate
  50. REL_Y_COORD, //< A relative Y coordinate
  51. };
  52. public:
  53. ORIGIN_TRANSFORMS();
  54. virtual ~ORIGIN_TRANSFORMS();
  55. // =============== Single-axis Transforms ===============
  56. virtual int ToDisplay( int aValue,
  57. COORD_TYPES_T aCoordType );
  58. virtual long long int ToDisplay( long long int aValue,
  59. COORD_TYPES_T aCoordType );
  60. virtual double ToDisplay( double aValue,
  61. COORD_TYPES_T aCoordType );
  62. virtual int FromDisplay( int aValue,
  63. COORD_TYPES_T aCoordType );
  64. virtual long long int FromDisplay( long long int aValue,
  65. COORD_TYPES_T aCoordType );
  66. virtual double FromDisplay( double aValue,
  67. COORD_TYPES_T aCoordType );
  68. // =============== Two-axis Transforms ===============
  69. template<class T>
  70. T ToDisplayAbs( const T& aValue )
  71. {
  72. T displayValue;
  73. displayValue.x = ToDisplay( aValue.x, ABS_X_COORD );
  74. displayValue.x = ToDisplay( aValue.y, ABS_Y_COORD );
  75. return displayValue;
  76. }
  77. template<class T>
  78. T ToDisplayRel( const T& aValue )
  79. {
  80. T displayValue;
  81. displayValue.x = ToDisplay( aValue.x, REL_X_COORD );
  82. displayValue.x = ToDisplay( aValue.y, REL_Y_COORD );
  83. return displayValue;
  84. }
  85. template<class T>
  86. T FromDisplayAbs( const T& aValue )
  87. {
  88. T displayValue;
  89. displayValue.x = FromDisplay( aValue.x, ABS_X_COORD );
  90. displayValue.x = FromDisplay( aValue.y, ABS_Y_COORD );
  91. return displayValue;
  92. }
  93. template<class T>
  94. T FromDisplayRel( const T& aValue )
  95. {
  96. T displayValue;
  97. displayValue.x = FromDisplay( aValue.x, REL_X_COORD );
  98. displayValue.x = FromDisplay( aValue.y, REL_Y_COORD );
  99. return displayValue;
  100. }
  101. protected:
  102. // =============== Generic Relative Transforms ===============
  103. template<class T> inline static
  104. T ToDisplayRel( T aInternalValue,
  105. bool aInvertAxis )
  106. {
  107. T displayValue = aInternalValue;
  108. // Invert the direction if needed
  109. if( aInvertAxis && (displayValue != static_cast<T>(0)) )
  110. displayValue = -displayValue;
  111. return displayValue;
  112. }
  113. template<class T> inline static
  114. T FromDisplayRel( T aDisplayValue,
  115. bool aInvertAxis )
  116. {
  117. T internalValue = aDisplayValue;
  118. // Invert the direction if needed
  119. if( aInvertAxis && (internalValue != static_cast<T>(0)) )
  120. internalValue = -internalValue;
  121. return internalValue;
  122. }
  123. // =============== Generic Absolute Transforms ===============
  124. template<class T> inline static
  125. T ToDisplayAbs( T aInternalValue,
  126. int aUserOrigin,
  127. bool aInvertAxis )
  128. {
  129. T displayValue = aInternalValue;
  130. // Make the value relative to the internal origin
  131. displayValue -= aUserOrigin;
  132. // Invert the direction if needed
  133. if( aInvertAxis && (displayValue != static_cast<T>(0)) )
  134. displayValue = -displayValue;
  135. return displayValue;
  136. }
  137. template<class T> inline static
  138. T FromDisplayAbs( T aDisplayValue,
  139. int aUserOrigin,
  140. bool aInvertAxis )
  141. {
  142. T internalValue = aDisplayValue;
  143. // Invert the direction if needed
  144. if( aInvertAxis && (internalValue != static_cast<T>(0)) )
  145. internalValue = -internalValue;
  146. // Make the value relative to the internal origin
  147. internalValue += aUserOrigin;
  148. return internalValue;
  149. }
  150. };
  151. #endif // ORIGIN_TRANSFORMS_H