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, 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#pragma once
26
27class EDA_ANGLE;
28
41{
42public:
50 NOT_A_COORD, //< A non-coordinate value, never transformed
51 ABS_X_COORD, //< An absolute X coordinate
52 ABS_Y_COORD, //< An absolute Y coordinate
53 REL_X_COORD, //< A relative X coordinate
54 REL_Y_COORD, //< A relative Y coordinate
55 };
56
57 ~ORIGIN_TRANSFORMS() = default;
58
59 virtual int ToDisplay( int aValue, COORD_TYPES_T aCoordType ) const;
60 virtual long long int ToDisplay( long long int aValue, COORD_TYPES_T aCoordType ) const;
61 virtual double ToDisplay( double aValue, COORD_TYPES_T aCoordType ) const;
62 virtual double ToDisplay( const EDA_ANGLE& aValue, COORD_TYPES_T aCoordType ) const;
63
64 virtual int FromDisplay( int aValue, COORD_TYPES_T aCoordType ) const;
65 virtual long long int FromDisplay( long long int aValue, COORD_TYPES_T aCoordType ) const;
66 virtual double FromDisplay( double aValue, COORD_TYPES_T aCoordType ) const;
67 virtual EDA_ANGLE FromDisplay( const EDA_ANGLE& aValue, COORD_TYPES_T aCoordType ) const;
68
69 template<class T>
70 T ToDisplayAbs( const T& aValue ) const
71 {
72 T displayValue;
73
74 displayValue.x = ToDisplay( aValue.x, ABS_X_COORD );
75 displayValue.y = ToDisplay( aValue.y, ABS_Y_COORD );
76 return displayValue;
77 }
78
79 template<class T>
80 T ToDisplayRel( const T& aValue ) const
81 {
82 T displayValue;
83
84 displayValue.x = ToDisplay( aValue.x, REL_X_COORD );
85 displayValue.y = ToDisplay( aValue.y, REL_Y_COORD );
86 return displayValue;
87 }
88
89 template<class T>
90 T FromDisplayAbs( const T& aValue ) const
91 {
92 T displayValue;
93
94 displayValue.x = FromDisplay( aValue.x, ABS_X_COORD );
95 displayValue.y = FromDisplay( aValue.y, ABS_Y_COORD );
96 return displayValue;
97 }
98
99 template<class T>
100 T FromDisplayRel( const T& aValue ) const
101 {
102 T displayValue;
103
104 displayValue.x = FromDisplay( aValue.x, REL_X_COORD );
105 displayValue.y = FromDisplay( aValue.y, REL_Y_COORD );
106 return displayValue;
107 }
108
109protected:
110 template<class T>
111 inline static T ToDisplayRel( T aInternalValue, bool aInvertAxis )
112 {
113 T displayValue = aInternalValue;
114
115 // Invert the direction if needed
116 if( aInvertAxis && ( displayValue != static_cast<T>( 0 ) ) )
117 displayValue = -displayValue;
118
119 return displayValue;
120 }
121
122 template<class T>
123 inline static T FromDisplayRel( T aDisplayValue, bool aInvertAxis )
124 {
125 T internalValue = aDisplayValue;
126
127 // Invert the direction if needed
128 if( aInvertAxis && ( internalValue != static_cast<T>( 0 ) ) )
129 internalValue = -internalValue;
130
131 return internalValue;
132 }
133
134 template<class T>
135 inline static T ToDisplayAbs( T aInternalValue, int aUserOrigin, bool aInvertAxis )
136 {
137 T displayValue = aInternalValue;
138
139 // Make the value relative to the internal origin
140 displayValue -= aUserOrigin;
141
142 // Invert the direction if needed
143 if( aInvertAxis && ( displayValue != static_cast<T>( 0 ) ) )
144 displayValue = -displayValue;
145
146 return displayValue;
147 }
148
149 template<class T>
150 inline static T FromDisplayAbs( T aDisplayValue, int aUserOrigin, bool aInvertAxis )
151 {
152 T internalValue = aDisplayValue;
153
154 // Invert the direction if needed
155 if( aInvertAxis && ( internalValue != static_cast<T>( 0 ) ) )
156 internalValue = -internalValue;
157
158 // Make the value relative to the internal origin
159 internalValue += aUserOrigin;
160
161 return internalValue;
162 }
163};
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