KiCad PCB EDA Suite
Loading...
Searching...
No Matches
origin_transforms.h
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) 2019-2020 Reece R. Pollack <[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#pragma once
22
23class EDA_ANGLE;
24
37{
38public:
46 NOT_A_COORD, //< A non-coordinate value, never transformed
47 ABS_X_COORD, //< An absolute X coordinate
48 ABS_Y_COORD, //< An absolute Y coordinate
49 REL_X_COORD, //< A relative X coordinate
50 REL_Y_COORD, //< A relative Y coordinate
51 };
52
53 ~ORIGIN_TRANSFORMS() = default;
54
55 virtual int ToDisplay( int aValue, COORD_TYPES_T aCoordType ) const;
56 virtual long long int ToDisplay( long long int aValue, COORD_TYPES_T aCoordType ) const;
57 virtual double ToDisplay( double aValue, COORD_TYPES_T aCoordType ) const;
58 virtual double ToDisplay( const EDA_ANGLE& aValue, COORD_TYPES_T aCoordType ) const;
59
60 virtual int FromDisplay( int aValue, COORD_TYPES_T aCoordType ) const;
61 virtual long long int FromDisplay( long long int aValue, COORD_TYPES_T aCoordType ) const;
62 virtual double FromDisplay( double aValue, COORD_TYPES_T aCoordType ) const;
63 virtual EDA_ANGLE FromDisplay( const EDA_ANGLE& aValue, COORD_TYPES_T aCoordType ) const;
64
65 template<class T>
66 T ToDisplayAbs( const T& aValue ) const
67 {
68 T displayValue;
69
70 displayValue.x = ToDisplay( aValue.x, ABS_X_COORD );
71 displayValue.y = ToDisplay( aValue.y, ABS_Y_COORD );
72 return displayValue;
73 }
74
75 template<class T>
76 T ToDisplayRel( const T& aValue ) const
77 {
78 T displayValue;
79
80 displayValue.x = ToDisplay( aValue.x, REL_X_COORD );
81 displayValue.y = ToDisplay( aValue.y, REL_Y_COORD );
82 return displayValue;
83 }
84
85 template<class T>
86 T FromDisplayAbs( const T& aValue ) const
87 {
88 T displayValue;
89
90 displayValue.x = FromDisplay( aValue.x, ABS_X_COORD );
91 displayValue.y = FromDisplay( aValue.y, ABS_Y_COORD );
92 return displayValue;
93 }
94
95 template<class T>
96 T FromDisplayRel( const T& aValue ) const
97 {
98 T displayValue;
99
100 displayValue.x = FromDisplay( aValue.x, REL_X_COORD );
101 displayValue.y = FromDisplay( aValue.y, REL_Y_COORD );
102 return displayValue;
103 }
104
105protected:
106 template<class T>
107 inline static T ToDisplayRel( T aInternalValue, bool aInvertAxis )
108 {
109 T displayValue = aInternalValue;
110
111 // Invert the direction if needed
112 if( aInvertAxis && ( displayValue != static_cast<T>( 0 ) ) )
113 displayValue = -displayValue;
114
115 return displayValue;
116 }
117
118 template<class T>
119 inline static T FromDisplayRel( T aDisplayValue, bool aInvertAxis )
120 {
121 T internalValue = aDisplayValue;
122
123 // Invert the direction if needed
124 if( aInvertAxis && ( internalValue != static_cast<T>( 0 ) ) )
125 internalValue = -internalValue;
126
127 return internalValue;
128 }
129
130 template<class T>
131 inline static T ToDisplayAbs( T aInternalValue, int aUserOrigin, bool aInvertAxis )
132 {
133 T displayValue = aInternalValue;
134
135 // Make the value relative to the internal origin
136 displayValue -= aUserOrigin;
137
138 // Invert the direction if needed
139 if( aInvertAxis && ( displayValue != static_cast<T>( 0 ) ) )
140 displayValue = -displayValue;
141
142 return displayValue;
143 }
144
145 template<class T>
146 inline static T FromDisplayAbs( T aDisplayValue, int aUserOrigin, bool aInvertAxis )
147 {
148 T internalValue = aDisplayValue;
149
150 // Invert the direction if needed
151 if( aInvertAxis && ( internalValue != static_cast<T>( 0 ) ) )
152 internalValue = -internalValue;
153
154 // Make the value relative to the internal origin
155 internalValue += aUserOrigin;
156
157 return internalValue;
158 }
159};
A class to perform either relative or absolute display origin transforms for a single axis of a point...
virtual int FromDisplay(int aValue, COORD_TYPES_T aCoordType) const
static T ToDisplayAbs(T aInternalValue, int aUserOrigin, bool aInvertAxis)
static T FromDisplayRel(T aDisplayValue, bool aInvertAxis)
static T FromDisplayAbs(T aDisplayValue, int aUserOrigin, bool aInvertAxis)
T FromDisplayRel(const T &aValue) const
~ORIGIN_TRANSFORMS()=default
static T ToDisplayRel(T aInternalValue, bool aInvertAxis)
T ToDisplayRel(const T &aValue) const
T ToDisplayAbs(const T &aValue) const
virtual int ToDisplay(int aValue, COORD_TYPES_T aCoordType) const
COORD_TYPES_T
The supported Display Origin Transform types.
T FromDisplayAbs(const T &aValue) const