KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <hash.h>
26#include <trigo.h>
27#include <transform.h>
28#include <math/util.h> // for KiROUND
29#include <math/box2.h>
30
31// a transform matrix, to display symbols in lib editor
33
34
35bool TRANSFORM::operator==( const TRANSFORM& aTransform ) const
36{
37 return ( x1 == aTransform.x1 &&
38 y1 == aTransform.y1 &&
39 x2 == aTransform.x2 &&
40 y2 == aTransform.y2 );
41}
42
43
45{
46 return VECTOR2I( ( x1 * aPoint.x ) + ( y1 * aPoint.y ), ( x2 * aPoint.x ) + ( y2 * aPoint.y ) );
47}
48
49
51{
52 BOX2I rect;
53 rect.SetOrigin( TransformCoordinate( aRect.GetOrigin() ) );
54 rect.SetEnd( TransformCoordinate( aRect.GetEnd() ) );
55 return rect;
56}
57
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
81bool TRANSFORM::MapAngles( EDA_ANGLE* aAngle1, EDA_ANGLE* aAngle2 ) const
82{
83 static const EDA_ANGLE epsilon( 0.1, DEGREES_T );
84
85 wxCHECK_MSG( aAngle1 != nullptr && aAngle2 != nullptr, false,
86 wxT( "Cannot map NULL point angles." ) );
87
88 double x, y;
89 VECTOR2D v;
90 bool swap = false;
91
92 EDA_ANGLE delta = *aAngle2 - *aAngle1;
93
94 x = aAngle1->Cos();
95 y = aAngle1->Sin();
96 v = VECTOR2D( x * x1 + y * y1, x * x2 + y * y2 );
97 *aAngle1 = EDA_ANGLE( v );
98
99 x = aAngle2->Cos();
100 y = aAngle2->Sin();
101 v = VECTOR2D( x * x1 + y * y1, x * x2 + y * y2 );
102 *aAngle2 = EDA_ANGLE( v );
103
104 EDA_ANGLE deltaTransformed = *aAngle2 - *aAngle1;
105 EDA_ANGLE residualError( deltaTransformed - delta );
106 residualError.Normalize();
107
108 if( residualError > epsilon || residualError < epsilon.Invert().Normalize() )
109 {
110 std::swap( *aAngle1, *aAngle2 );
111 swap = true;
112 }
113
114 if( *aAngle2 < *aAngle1 )
115 {
116 if( *aAngle2 < ANGLE_0 )
117 aAngle2->Normalize();
118 else
119 *aAngle1 = aAngle1->Normalize() - ANGLE_360;
120 }
121
122 return swap;
123}
124
125
126size_t std::hash<TRANSFORM>::operator()( const TRANSFORM& s ) const
127{
128 size_t seed = std::hash<int>{}( s.x1 );
129 hash_combine( seed, s.y1, s.x2, s.y2 );
130 return seed;
131}
void SetOrigin(const Vec &pos)
Definition: box2.h:203
const Vec & GetOrigin() const
Definition: box2.h:184
const Vec GetEnd() const
Definition: box2.h:186
void SetEnd(coord_type x, coord_type y)
Definition: box2.h:256
EDA_ANGLE Normalize()
Definition: eda_angle.h:255
double Sin() const
Definition: eda_angle.h:212
double Cos() const
Definition: eda_angle.h:227
for transforming drawing coordinates for a wxDC device context.
Definition: transform.h:46
int x2
Definition: transform.h:50
int y1
Definition: transform.h:49
bool MapAngles(EDA_ANGLE *aAngle1, EDA_ANGLE *aAngle2) const
Calculate new angles according to the transform.
Definition: transform.cpp:81
TRANSFORM InverseTransform() const
Calculate the Inverse mirror/rotation transform.
Definition: transform.cpp:59
bool operator==(const TRANSFORM &aTransform) const
Definition: transform.cpp:35
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition: transform.cpp:44
int y2
Definition: transform.h:51
int x1
Definition: transform.h:48
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:435
@ DEGREES_T
Definition: eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_360
Definition: eda_angle.h:441
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash.h:34
const double epsilon
constexpr int delta
TRANSFORM DefaultTransform
Definition: transform.cpp:32
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588