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@gmail.com>
5  * Copyright (C) 2015-2021 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 
42 wxPoint TRANSFORM::TransformCoordinate( const wxPoint& aPoint ) const
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 
58 {
59  int invx1;
60  int invx2;
61  int invy1;
62  int invy2;
63 
64  /* Calculates the inverse matrix coeffs:
65  * for a matrix m{x1, x2, y1, y2}
66  * the inverse matrix is 1/(x1*y2 -x2*y1) m{y2,-x2,-y1,x1)
67  */
68  int det = x1*y2 -x2*y1; // Is never null, because the inverse matrix exists
69  invx1 = y2/det;
70  invx2 = -x2/det;
71  invy1 = -y1/det;
72  invy2 = x1/det;
73 
74  TRANSFORM invtransform( invx1, invy1, invx2, invy2 );
75  return invtransform;
76 }
77 
78 
79 bool TRANSFORM::MapAngles( int* aAngle1, int* aAngle2 ) const
80 {
81  wxCHECK_MSG( aAngle1 != nullptr && aAngle2 != nullptr, false,
82  wxT( "Cannot map NULL point angles." ) );
83 
84  int Angle, Delta;
85  double x, y, t;
86  bool swap = false;
87 
88  Delta = *aAngle2 - *aAngle1;
89 
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 
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 
125  if( *aAngle2 < *aAngle1 )
126  *aAngle2 += 3600;
127 
128  swap = true;
129  }
130 
131  if( Delta >= 1800 )
132  {
133  *aAngle1 += 1;
134  *aAngle2 -= 1;
135  }
136 
137  return swap;
138 }
int y2
Definition: transform.h:51
TRANSFORM InverseTransform() const
Calculate the Inverse mirror/rotation transform.
Definition: transform.cpp:57
int x2
Definition: transform.h:50
double RAD2DECIDEG(double rad)
Definition: trigo.h:234
void SetOrigin(const wxPoint &pos)
Definition: eda_rect.h:121
int x1
Definition: transform.h:48
void NORMALIZE_ANGLE_POS(T &Angle)
Definition: trigo.h:290
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:103
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:45
bool operator==(const TRANSFORM &aTransform) const
Definition: transform.cpp:33
const wxPoint GetOrigin() const
Definition: eda_rect.h:101
void SetEnd(int x, int y)
Definition: eda_rect.h:182
Handle the component boundary box.
Definition: eda_rect.h:42
double DECIDEG2RAD(double deg)
Definition: trigo.h:233
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:73
The common library.
bool MapAngles(int *aAngle1, int *aAngle2) const
Calculate new angles according to the transform.
Definition: transform.cpp:79