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.

136 lines
3.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010 Wayne Stambaugh <stambaughw@verizon.net>
  5. * Copyright (C) 2015-2017 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. #include <macros.h>
  25. #include <trigo.h>
  26. #include <transform.h>
  27. #include <common.h>
  28. #include <eda_rect.h>
  29. #include <math/util.h> // for KiROUND
  30. bool TRANSFORM::operator==( const TRANSFORM& aTransform ) const
  31. {
  32. return ( x1 == aTransform.x1 &&
  33. y1 == aTransform.y1 &&
  34. x2 == aTransform.x2 &&
  35. y2 == aTransform.y2 );
  36. }
  37. wxPoint TRANSFORM::TransformCoordinate( const wxPoint& aPoint ) const
  38. {
  39. return wxPoint( ( x1 * aPoint.x ) + ( y1 * aPoint.y ),
  40. ( x2 * aPoint.x ) + ( y2 * aPoint.y ) );
  41. }
  42. EDA_RECT TRANSFORM::TransformCoordinate( const EDA_RECT& aRect ) const
  43. {
  44. EDA_RECT rect;
  45. rect.SetOrigin( TransformCoordinate( aRect.GetOrigin() ) );
  46. rect.SetEnd( TransformCoordinate( aRect.GetEnd() ) );
  47. return rect;
  48. }
  49. /*
  50. * Calculate the Inverse mirror/rotation transform.
  51. */
  52. TRANSFORM TRANSFORM::InverseTransform( ) const
  53. {
  54. int invx1;
  55. int invx2;
  56. int invy1;
  57. int invy2;
  58. /* Calculates the inverse matrix coeffs:
  59. * for a matrix m{x1, x2, y1, y2}
  60. * the inverse matrix is 1/(x1*y2 -x2*y1) m{y2,-x2,-y1,x1)
  61. */
  62. int det = x1*y2 -x2*y1; // Is never null, because the inverse matrix exists
  63. invx1 = y2/det;
  64. invx2 = -x2/det;
  65. invy1 = -y1/det;
  66. invy2 = x1/det;
  67. TRANSFORM invtransform( invx1, invy1, invx2, invy2 );
  68. return invtransform;
  69. }
  70. bool TRANSFORM::MapAngles( int* aAngle1, int* aAngle2 ) const
  71. {
  72. wxCHECK_MSG( aAngle1 != NULL && aAngle2 != NULL, false,
  73. wxT( "Cannot map NULL point angles." ) );
  74. int Angle, Delta;
  75. double x, y, t;
  76. bool swap = false;
  77. Delta = *aAngle2 - *aAngle1;
  78. if( Delta >= 1800 )
  79. {
  80. *aAngle1 -= 1;
  81. *aAngle2 += 1;
  82. }
  83. x = cos( DECIDEG2RAD( *aAngle1 ) );
  84. y = sin( DECIDEG2RAD( *aAngle1 ) );
  85. t = x * x1 + y * y1;
  86. y = x * x2 + y * y2;
  87. x = t;
  88. *aAngle1 = KiROUND( RAD2DECIDEG( atan2( y, x ) ) );
  89. x = cos( DECIDEG2RAD( *aAngle2 ) );
  90. y = sin( DECIDEG2RAD( *aAngle2 ) );
  91. t = x * x1 + y * y1;
  92. y = x * x2 + y * y2;
  93. x = t;
  94. *aAngle2 = KiROUND( RAD2DECIDEG( atan2( y, x ) ) );
  95. NORMALIZE_ANGLE_POS( *aAngle1 );
  96. NORMALIZE_ANGLE_POS( *aAngle2 );
  97. if( *aAngle2 < *aAngle1 )
  98. *aAngle2 += 3600;
  99. if( *aAngle2 - *aAngle1 > 1800 ) // Need to swap the two angles
  100. {
  101. Angle = (*aAngle1);
  102. *aAngle1 = (*aAngle2);
  103. *aAngle2 = Angle;
  104. NORMALIZE_ANGLE_POS( *aAngle1 );
  105. NORMALIZE_ANGLE_POS( *aAngle2 );
  106. if( *aAngle2 < *aAngle1 )
  107. *aAngle2 += 3600;
  108. swap = true;
  109. }
  110. if( Delta >= 1800 )
  111. {
  112. *aAngle1 += 1;
  113. *aAngle2 -= 1;
  114. }
  115. return swap;
  116. }