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
21#include <trigo.h>
22
23
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
33{
34 VECTOR2D r = Apply( VECTOR2D( aPoint ) );
35 return VECTOR2I( KiROUND( r.x ), KiROUND( r.y ) );
36}
37
38
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
48{
49 VECTOR2D r = InverseApply( VECTOR2D( aPoint ) );
50 return VECTOR2I( KiROUND( r.x ), KiROUND( r.y ) );
51}
52
53
55 double aSx, double aSy ) const
56{
57 TRANSFORM_TRS result = *this;
58
59 // aSx/aSy are scale multipliers in the footprint's own frame, so the local
60 // scale factors scale directly.
61 result.m_scaleX = m_scaleX * aSx;
62 result.m_scaleY = m_scaleY * aSy;
63
64 // The offset from the fixed point is in board axes. Rotate it into the
65 // footprint frame, scale it there, then rotate back (R D R^-1) so the position
66 // move matches the local scaling when the footprint is rotated.
67 VECTOR2D offset( m_translate.x - aFixedPoint.x, m_translate.y - aFixedPoint.y );
68 RotatePoint( offset, -m_rotate );
69 offset.x *= aSx;
70 offset.y *= aSy;
71 RotatePoint( offset, m_rotate );
72
73 result.m_translate = VECTOR2I( KiROUND( aFixedPoint.x + offset.x ), KiROUND( aFixedPoint.y + offset.y ) );
74 return result;
75}
76
77
79{
80 return m_translate == VECTOR2I( 0, 0 )
81 && m_rotate.IsZero()
82 && m_scaleX == 1.0
83 && m_scaleY == 1.0;
84}
85
86
88{
89 return m_scaleX == m_scaleY;
90}
91
92
93double TRANSFORM_TRS::ApplyLinearScale( double aLength ) const
94{
95 double scale = IsUniformScale() ? m_scaleX : ( m_scaleX + m_scaleY ) / 2;
96
97 return aLength * scale;
98}
99
100
101double TRANSFORM_TRS::InverseApplyLinearScale( double aLength ) const
102{
103 double scale = IsUniformScale() ? m_scaleX : ( m_scaleX + m_scaleY ) / 2;
104
105 return aLength / scale;
106}
107
108
109bool TRANSFORM_TRS::operator==( const TRANSFORM_TRS& aOther ) const
110{
111 return m_translate == aOther.m_translate
112 && m_rotate == aOther.m_rotate
113 && m_scaleX == aOther.m_scaleX
114 && m_scaleY == aOther.m_scaleY;
115}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
VECTOR2I InverseApply(const VECTOR2I &aPoint) const
bool IsUniformScale() const
VECTOR2I Apply(const VECTOR2I &aPoint) const
EDA_ANGLE m_rotate
double ApplyLinearScale(double aLength) const
TRANSFORM_TRS RescaleAround(const VECTOR2I &aFixedPoint, double aSx, double aSy) const
bool operator==(const TRANSFORM_TRS &aOther) const
double InverseApplyLinearScale(double aLength) const
bool IsIdentity() const
VECTOR2I m_translate
const int scale
wxString result
Test unit parsing edge cases and error handling.
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:227
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707