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.

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