KiCad PCB EDA Suite
transform.cpp
Go to the documentation of this file.
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 
25 #include <macros.h>
26 #include <trigo.h>
27 #include <transform.h>
28 #include <common.h>
29 #include <class_eda_rect.h>
30 
31 
32 bool TRANSFORM::operator==( const TRANSFORM& aTransform ) const
33 {
34  return ( x1 == aTransform.x1 &&
35  y1 == aTransform.y1 &&
36  x2 == aTransform.x2 &&
37  y2 == aTransform.y2 );
38 }
39 
40 
42 {
43  return wxPoint( ( x1 * aPoint.x ) + ( y1 * aPoint.y ),
44  ( x2 * aPoint.x ) + ( y2 * aPoint.y ) );
45 }
46 
48 {
49  EDA_RECT rect;
50  rect.SetOrigin( TransformCoordinate( aRect.GetOrigin() ) );
51  rect.SetEnd( TransformCoordinate( aRect.GetEnd() ) );
52  return rect;
53 }
54 
55 /*
56 * Calculate the Inverse mirror/rotation transform.
57 */
59 {
60  int invx1;
61  int invx2;
62  int invy1;
63  int invy2;
64 
65  /* Calculates the inverse matrix coeffs:
66  * for a matrix m{x1, x2, y1, y2}
67  * the inverse matrix is 1/(x1*y2 -x2*y1) m{y2,-x2,-y1,x1)
68  */
69  int det = x1*y2 -x2*y1; // Is never null, because the inverse matrix exists
70  invx1 = y2/det;
71  invx2 = -x2/det;
72  invy1 = -y1/det;
73  invy2 = x1/det;
74 
75  TRANSFORM invtransform( invx1, invy1, invx2, invy2 );
76  return invtransform;
77 }
78 
79 
80 bool TRANSFORM::MapAngles( int* aAngle1, int* aAngle2 ) const
81 {
82  wxCHECK_MSG( aAngle1 != NULL && aAngle2 != NULL, false,
83  wxT( "Cannot map NULL point angles." ) );
84 
85  int Angle, Delta;
86  double x, y, t;
87  bool swap = false;
88 
89  Delta = *aAngle2 - *aAngle1;
90  if( Delta >= 1800 )
91  {
92  *aAngle1 -= 1;
93  *aAngle2 += 1;
94  }
95 
96  x = cos( DECIDEG2RAD( *aAngle1 ) );
97  y = sin( DECIDEG2RAD( *aAngle1 ) );
98  t = x * x1 + y * y1;
99  y = x * x2 + y * y2;
100  x = t;
101  *aAngle1 = KiROUND( RAD2DECIDEG( atan2( y, x ) ) );
102 
103  x = cos( DECIDEG2RAD( *aAngle2 ) );
104  y = sin( DECIDEG2RAD( *aAngle2 ) );
105  t = x * x1 + y * y1;
106  y = x * x2 + y * y2;
107  x = t;
108  *aAngle2 = KiROUND( RAD2DECIDEG( atan2( y, x ) ) );
109 
110  NORMALIZE_ANGLE_POS( *aAngle1 );
111  NORMALIZE_ANGLE_POS( *aAngle2 );
112  if( *aAngle2 < *aAngle1 )
113  *aAngle2 += 3600;
114 
115  if( *aAngle2 - *aAngle1 > 1800 ) // Need to swap the two angles
116  {
117  Angle = (*aAngle1);
118  *aAngle1 = (*aAngle2);
119  *aAngle2 = Angle;
120 
121  NORMALIZE_ANGLE_POS( *aAngle1 );
122  NORMALIZE_ANGLE_POS( *aAngle2 );
123  if( *aAngle2 < *aAngle1 )
124  *aAngle2 += 3600;
125  swap = true;
126  }
127 
128  if( Delta >= 1800 )
129  {
130  *aAngle1 += 1;
131  *aAngle2 -= 1;
132  }
133 
134  return swap;
135 }
const wxPoint GetOrigin() const
int y2
Definition: transform.h:51
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
int x2
Definition: transform.h:50
double RAD2DECIDEG(double rad)
Definition: trigo.h:204
void SetOrigin(const wxPoint &pos)
int x1
Definition: transform.h:48
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:241
This file contains miscellaneous commonly used macros and functions.
wxPoint TransformCoordinate(const wxPoint &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:41
int y1
Definition: transform.h:49
Class for tranforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
bool MapAngles(int *aAngle1, int *aAngle2) const
Calculate new angles according to the transform.
Definition: transform.cpp:80
void SetEnd(int x, int y)
const wxPoint GetEnd() const
Class EDA_RECT handles the component boundary box.
double DECIDEG2RAD(double deg)
Definition: trigo.h:203
The common library.
bool operator==(const TRANSFORM &aTransform) const
Definition: transform.cpp:32
double t
Definition: trace.cpp:1114
TRANSFORM InverseTransform() const
Calculate the Inverse mirror/rotation transform.
Definition: transform.cpp:58