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