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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <hash.h>
22#include <trigo.h>
23#include <transform.h>
24#include <math/util.h> // for KiROUND
25#include <math/box2.h>
26
27// a transform matrix, to display symbols in lib editor
29
30
31bool TRANSFORM::operator==( const TRANSFORM& aTransform ) const
32{
33 return ( x1 == aTransform.x1 &&
34 y1 == aTransform.y1 &&
35 x2 == aTransform.x2 &&
36 y2 == aTransform.y2 );
37}
38
39
41{
42 return VECTOR2I( ( x1 * aPoint.x ) + ( y1 * aPoint.y ), ( x2 * aPoint.x ) + ( y2 * aPoint.y ) );
43}
44
45
47{
48 BOX2I rect;
49 rect.SetOrigin( TransformCoordinate( aRect.GetOrigin() ) );
50 rect.SetEnd( TransformCoordinate( aRect.GetEnd() ) );
51 return rect;
52}
53
54
56{
57 int invx1;
58 int invx2;
59 int invy1;
60 int invy2;
61
62 /* Calculates the inverse matrix coeffs:
63 * for a matrix m{x1, x2, y1, y2}
64 * the inverse matrix is 1/(x1*y2 -x2*y1) m{y2,-x2,-y1,x1)
65 */
66 int det = x1*y2 -x2*y1; // Is never null, because the inverse matrix exists
67 invx1 = y2/det;
68 invx2 = -x2/det;
69 invy1 = -y1/det;
70 invy2 = x1/det;
71
72 TRANSFORM invtransform( invx1, invy1, invx2, invy2 );
73 return invtransform;
74}
75
76
77bool TRANSFORM::MapAngles( EDA_ANGLE* aAngle1, EDA_ANGLE* aAngle2 ) const
78{
79 static const EDA_ANGLE epsilon( 0.1, DEGREES_T );
80
81 wxCHECK_MSG( aAngle1 != nullptr && aAngle2 != nullptr, false,
82 wxT( "Cannot map NULL point angles." ) );
83
84 double x, y;
85 VECTOR2D v;
86 bool swap = false;
87
88 EDA_ANGLE delta = *aAngle2 - *aAngle1;
89
90 x = aAngle1->Cos();
91 y = aAngle1->Sin();
92 v = VECTOR2D( x * x1 + y * y1, x * x2 + y * y2 );
93 *aAngle1 = EDA_ANGLE( v );
94
95 x = aAngle2->Cos();
96 y = aAngle2->Sin();
97 v = VECTOR2D( x * x1 + y * y1, x * x2 + y * y2 );
98 *aAngle2 = EDA_ANGLE( v );
99
100 EDA_ANGLE deltaTransformed = *aAngle2 - *aAngle1;
101 EDA_ANGLE residualError( deltaTransformed - delta );
102 residualError.Normalize();
103
104 if( residualError > epsilon || residualError < epsilon.Invert().Normalize() )
105 {
106 std::swap( *aAngle1, *aAngle2 );
107 swap = true;
108 }
109
110 if( *aAngle2 < *aAngle1 )
111 {
112 if( *aAngle2 < ANGLE_0 )
113 aAngle2->Normalize();
114 else
115 *aAngle1 = aAngle1->Normalize() - ANGLE_360;
116 }
117
118 return swap;
119}
120
121
123{
124 size_t seed = std::hash<int>{}( s.x1 );
125 hash_combine( seed, s.y1, s.x2, s.y2 );
126 return seed;
127}
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr const Vec GetEnd() const
Definition box2.h:208
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:233
constexpr const Vec & GetOrigin() const
Definition box2.h:206
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:293
EDA_ANGLE Normalize()
Definition eda_angle.h:229
double Sin() const
Definition eda_angle.h:178
double Cos() const
Definition eda_angle.h:197
for transforming drawing coordinates for a wxDC device context.
Definition transform.h:42
bool MapAngles(EDA_ANGLE *aAngle1, EDA_ANGLE *aAngle2) const
Calculate new angles according to the transform.
Definition transform.cpp:77
TRANSFORM InverseTransform() const
Calculate the Inverse mirror/rotation transform.
Definition transform.cpp:55
bool operator==(const TRANSFORM &aTransform) const
Definition transform.cpp:31
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition transform.cpp:40
TRANSFORM()
The default construct creates a transform that draws object is the normal orientation.
Definition transform.h:52
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
@ DEGREES_T
Definition eda_angle.h:31
static constexpr EDA_ANGLE ANGLE_360
Definition eda_angle.h:417
TRANSFORM DefaultTransform
Definition transform.cpp:28
static constexpr void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition hash.h:28
const double epsilon
size_t operator()(const TRANSFORM &k) const
const uint32_t seed
int delta
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682