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