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 <[email protected]>
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 <math/util.h> // for KiROUND
30#include <math/box2.h>
31
32// a transform matrix, to display symbols in lib editor
34
35
36bool TRANSFORM::operator==( const TRANSFORM& aTransform ) const
37{
38 return ( x1 == aTransform.x1 &&
39 y1 == aTransform.y1 &&
40 x2 == aTransform.x2 &&
41 y2 == aTransform.y2 );
42}
43
44
46{
47 return VECTOR2I( ( x1 * aPoint.x ) + ( y1 * aPoint.y ), ( x2 * aPoint.x ) + ( y2 * aPoint.y ) );
48}
49
50
52{
53 BOX2I rect;
54 rect.SetOrigin( TransformCoordinate( aRect.GetOrigin() ) );
55 rect.SetEnd( TransformCoordinate( aRect.GetEnd() ) );
56 return rect;
57}
58
59
61{
62 int invx1;
63 int invx2;
64 int invy1;
65 int invy2;
66
67 /* Calculates the inverse matrix coeffs:
68 * for a matrix m{x1, x2, y1, y2}
69 * the inverse matrix is 1/(x1*y2 -x2*y1) m{y2,-x2,-y1,x1)
70 */
71 int det = x1*y2 -x2*y1; // Is never null, because the inverse matrix exists
72 invx1 = y2/det;
73 invx2 = -x2/det;
74 invy1 = -y1/det;
75 invy2 = x1/det;
76
77 TRANSFORM invtransform( invx1, invy1, invx2, invy2 );
78 return invtransform;
79}
80
81
82bool TRANSFORM::MapAngles( EDA_ANGLE* aAngle1, EDA_ANGLE* aAngle2 ) const
83{
84 static const EDA_ANGLE epsilon( 0.1, DEGREES_T );
85
86 wxCHECK_MSG( aAngle1 != nullptr && aAngle2 != nullptr, false,
87 wxT( "Cannot map NULL point angles." ) );
88
89 double x, y;
90 VECTOR2D v;
91 bool swap = false;
92
93 EDA_ANGLE delta = *aAngle2 - *aAngle1;
94
95 x = aAngle1->Cos();
96 y = aAngle1->Sin();
97 v = VECTOR2D( x * x1 + y * y1, x * x2 + y * y2 );
98 *aAngle1 = EDA_ANGLE( v );
99
100 x = aAngle2->Cos();
101 y = aAngle2->Sin();
102 v = VECTOR2D( x * x1 + y * y1, x * x2 + y * y2 );
103 *aAngle2 = EDA_ANGLE( v );
104
105 EDA_ANGLE deltaTransformed = *aAngle2 - *aAngle1;
106 EDA_ANGLE residualError( deltaTransformed - delta );
107 residualError.Normalize();
108
109 if( residualError > epsilon || residualError < epsilon.Invert().Normalize() )
110 {
111 std::swap( *aAngle1, *aAngle2 );
112 swap = true;
113 }
114
115 if( *aAngle2 < *aAngle1 )
116 {
117 if( *aAngle2 < ANGLE_0 )
118 aAngle2->Normalize();
119 else
120 *aAngle1 = aAngle1->Normalize() - ANGLE_360;
121 }
122
123 return swap;
124}
125
126
void SetOrigin(const Vec &pos)
Definition: box2.h:202
const Vec & GetOrigin() const
Definition: box2.h:183
const Vec GetEnd() const
Definition: box2.h:185
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:255
EDA_ANGLE Normalize()
Definition: eda_angle.h:249
double Sin() const
Definition: eda_angle.h:206
EDA_ANGLE Invert() const
Definition: eda_angle.h:201
double Cos() const
Definition: eda_angle.h:221
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:47
int x2
Definition: transform.h:51
int y1
Definition: transform.h:50
bool MapAngles(EDA_ANGLE *aAngle1, EDA_ANGLE *aAngle2) const
Calculate new angles according to the transform.
Definition: transform.cpp:82
TRANSFORM InverseTransform() const
Calculate the Inverse mirror/rotation transform.
Definition: transform.cpp:60
bool operator==(const TRANSFORM &aTransform) const
Definition: transform.cpp:36
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:45
int y2
Definition: transform.h:52
int x1
Definition: transform.h:49
The common library.
@ DEGREES_T
Definition: eda_angle.h:31
static constexpr EDA_ANGLE & ANGLE_360
Definition: eda_angle.h:418
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:412
This file contains miscellaneous commonly used macros and functions.
constexpr int delta
TRANSFORM DefaultTransform
Definition: transform.cpp:33
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618