KiCad PCB EDA Suite
Loading...
Searching...
No Matches
transform_trs.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 The KiCad Developers, see AUTHORS.txt for contributors.
5
*
6
* This program is free software: you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License as published by the
8
* Free Software Foundation, either version 3 of the License, or (at your
9
* option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License along
17
* with this program. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
#include <
geometry/transform_trs.h
>
21
#include <
trigo.h
>
22
23
24
VECTOR2D
TRANSFORM_TRS::Apply
(
const
VECTOR2D
& aPoint )
const
25
{
26
VECTOR2D
scaled( aPoint.
x
*
m_scaleX
, aPoint.
y
*
m_scaleY
);
27
RotatePoint
( scaled,
m_rotate
);
28
return
scaled +
VECTOR2D
(
m_translate
);
29
}
30
31
32
VECTOR2I
TRANSFORM_TRS::Apply
(
const
VECTOR2I
& aPoint )
const
33
{
34
VECTOR2D
r =
Apply
(
VECTOR2D
( aPoint ) );
35
return
VECTOR2I
(
KiROUND
( r.
x
),
KiROUND
( r.
y
) );
36
}
37
38
39
VECTOR2D
TRANSFORM_TRS::InverseApply
(
const
VECTOR2D
& aPoint )
const
40
{
41
VECTOR2D
shifted = aPoint -
VECTOR2D
(
m_translate
);
42
RotatePoint
( shifted, -
m_rotate
);
43
return
VECTOR2D
( shifted.
x
/
m_scaleX
, shifted.
y
/
m_scaleY
);
44
}
45
46
47
VECTOR2I
TRANSFORM_TRS::InverseApply
(
const
VECTOR2I
& aPoint )
const
48
{
49
VECTOR2D
r =
InverseApply
(
VECTOR2D
( aPoint ) );
50
return
VECTOR2I
(
KiROUND
( r.
x
),
KiROUND
( r.
y
) );
51
}
52
53
54
TRANSFORM_TRS
TRANSFORM_TRS::Invert
()
const
55
{
56
TRANSFORM_TRS
inv;
57
inv.
m_scaleX
= 1.0 /
m_scaleX
;
58
inv.
m_scaleY
= 1.0 /
m_scaleY
;
59
inv.
m_rotate
= -
m_rotate
;
60
61
VECTOR2D
t(
m_translate
);
62
RotatePoint
( t, -
m_rotate
);
63
inv.
m_translate
=
VECTOR2I
(
KiROUND
( -t.
x
/
m_scaleX
),
KiROUND
( -t.
y
/
m_scaleY
) );
64
65
return
inv;
66
}
67
68
69
TRANSFORM_TRS
TRANSFORM_TRS::Compose
(
const
TRANSFORM_TRS
& aOuter )
const
70
{
71
TRANSFORM_TRS
result
;
72
result
.m_scaleX =
m_scaleX
* aOuter.
m_scaleX
;
73
result
.m_scaleY =
m_scaleY
* aOuter.
m_scaleY
;
74
result
.m_rotate =
m_rotate
+ aOuter.
m_rotate
;
75
result
.m_translate = aOuter.
Apply
(
m_translate
);
76
return
result
;
77
}
78
79
80
TRANSFORM_TRS
TRANSFORM_TRS::RescaleAround
(
const
VECTOR2I
& aFixedPoint,
81
double
aSx,
double
aSy )
const
82
{
83
TRANSFORM_TRS
result
= *
this
;
84
85
// aSx/aSy are scale multipliers in the footprint's own frame, so the local
86
// scale factors scale directly.
87
result
.m_scaleX =
m_scaleX
* aSx;
88
result
.m_scaleY =
m_scaleY
* aSy;
89
90
// The offset from the fixed point is in board axes. Rotate it into the
91
// footprint frame, scale it there, then rotate back (R D R^-1) so the position
92
// move matches the local scaling when the footprint is rotated.
93
VECTOR2D
offset(
m_translate
.x - aFixedPoint.
x
,
m_translate
.y - aFixedPoint.
y
);
94
RotatePoint
( offset, -
m_rotate
);
95
offset.
x
*= aSx;
96
offset.
y
*= aSy;
97
RotatePoint
( offset,
m_rotate
);
98
99
result
.m_translate =
VECTOR2I
(
KiROUND
( aFixedPoint.
x
+ offset.
x
),
KiROUND
( aFixedPoint.
y
+ offset.
y
) );
100
return
result
;
101
}
102
103
104
bool
TRANSFORM_TRS::IsIdentity
()
const
105
{
106
return
m_translate
==
VECTOR2I
( 0, 0 )
107
&&
m_rotate
.IsZero()
108
&&
m_scaleX
== 1.0
109
&&
m_scaleY
== 1.0;
110
}
111
112
113
bool
TRANSFORM_TRS::IsUniformScale
()
const
114
{
115
return
m_scaleX
==
m_scaleY
;
116
}
117
118
119
double
TRANSFORM_TRS::ApplyLinearScale
(
double
aLength )
const
120
{
121
return
aLength * 0.5 * (
m_scaleX
+
m_scaleY
);
122
}
123
124
125
bool
TRANSFORM_TRS::operator==
(
const
TRANSFORM_TRS
& aOther )
const
126
{
127
return
m_translate
== aOther.
m_translate
128
&&
m_rotate
== aOther.
m_rotate
129
&&
m_scaleX
== aOther.
m_scaleX
130
&&
m_scaleY
== aOther.
m_scaleY
;
131
}
KiROUND
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition
box2.h:986
TRANSFORM_TRS::InverseApply
VECTOR2I InverseApply(const VECTOR2I &aPoint) const
Definition
transform_trs.cpp:47
TRANSFORM_TRS::Compose
TRANSFORM_TRS Compose(const TRANSFORM_TRS &aOuter) const
Definition
transform_trs.cpp:69
TRANSFORM_TRS::m_scaleX
double m_scaleX
Definition
transform_trs.h:80
TRANSFORM_TRS::IsUniformScale
bool IsUniformScale() const
Definition
transform_trs.cpp:113
TRANSFORM_TRS::Invert
TRANSFORM_TRS Invert() const
Definition
transform_trs.cpp:54
TRANSFORM_TRS::Apply
VECTOR2I Apply(const VECTOR2I &aPoint) const
Definition
transform_trs.cpp:32
TRANSFORM_TRS::m_rotate
EDA_ANGLE m_rotate
Definition
transform_trs.h:79
TRANSFORM_TRS::ApplyLinearScale
double ApplyLinearScale(double aLength) const
Definition
transform_trs.cpp:119
TRANSFORM_TRS::m_scaleY
double m_scaleY
Definition
transform_trs.h:81
TRANSFORM_TRS::RescaleAround
TRANSFORM_TRS RescaleAround(const VECTOR2I &aFixedPoint, double aSx, double aSy) const
Definition
transform_trs.cpp:80
TRANSFORM_TRS::operator==
bool operator==(const TRANSFORM_TRS &aOther) const
Definition
transform_trs.cpp:125
TRANSFORM_TRS::TRANSFORM_TRS
TRANSFORM_TRS()
Definition
transform_trs.h:33
TRANSFORM_TRS::IsIdentity
bool IsIdentity() const
Definition
transform_trs.cpp:104
TRANSFORM_TRS::m_translate
VECTOR2I m_translate
Definition
transform_trs.h:78
VECTOR2::x
T x
Definition
vector2d.h:75
VECTOR2::y
T y
Definition
vector2d.h:75
result
wxString result
Test unit parsing edge cases and error handling.
Definition
test_text_eval_numeric_compat.cpp:598
transform_trs.h
trigo.h
RotatePoint
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition
trigo.cpp:225
VECTOR2I
VECTOR2< int32_t > VECTOR2I
Definition
vector2d.h:683
VECTOR2D
VECTOR2< double > VECTOR2D
Definition
vector2d.h:682
src
libs
kimath
src
geometry
transform_trs.cpp
Generated on Fri Jun 26 2026 00:05:38 for KiCad PCB EDA Suite by
1.13.2