KiCad PCB EDA Suite
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 <reece@his.com>
5  * Copyright (C) 1992-2019 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 #ifndef ORIGIN_TRANSFORMS_H_
26 #define ORIGIN_TRANSFORMS_H_
27 
40 public:
48  NOT_A_COORD, //< A non-coordinate value, never transformed
49  ABS_X_COORD, //< An absolute X coordinate
50  ABS_Y_COORD, //< An absolute Y coordinate
51  REL_X_COORD, //< A relative X coordinate
52  REL_Y_COORD, //< A relative Y coordinate
53  };
54 
55 public:
57 
58  virtual ~ORIGIN_TRANSFORMS();
59 
60 
61  // =============== Single-axis Transforms ===============
62 
63  virtual int ToDisplay( int aValue,
64  COORD_TYPES_T aCoordType ) const;
65 
66  virtual long long int ToDisplay( long long int aValue,
67  COORD_TYPES_T aCoordType ) const;
68 
69  virtual double ToDisplay( double aValue,
70  COORD_TYPES_T aCoordType ) const;
71 
72  virtual int FromDisplay( int aValue,
73  COORD_TYPES_T aCoordType ) const;
74 
75  virtual long long int FromDisplay( long long int aValue,
76  COORD_TYPES_T aCoordType ) const;
77 
78  virtual double FromDisplay( double aValue,
79  COORD_TYPES_T aCoordType ) const;
80 
81 
82  // =============== Two-axis Transforms ===============
83 
84  template<class T>
85  T ToDisplayAbs( const T& aValue ) const
86  {
87  T displayValue;
88 
89  displayValue.x = ToDisplay( aValue.x, ABS_X_COORD );
90  displayValue.y = ToDisplay( aValue.y, ABS_Y_COORD );
91  return displayValue;
92  }
93 
94  template<class T>
95  T ToDisplayRel( const T& aValue ) const
96  {
97  T displayValue;
98 
99  displayValue.x = ToDisplay( aValue.x, REL_X_COORD );
100  displayValue.y = ToDisplay( aValue.y, REL_Y_COORD );
101  return displayValue;
102  }
103 
104 
105  template<class T>
106  T FromDisplayAbs( const T& aValue ) const
107  {
108  T displayValue;
109 
110  displayValue.x = FromDisplay( aValue.x, ABS_X_COORD );
111  displayValue.y = FromDisplay( aValue.y, ABS_Y_COORD );
112  return displayValue;
113  }
114 
115  template<class T>
116  T FromDisplayRel( const T& aValue ) const
117  {
118  T displayValue;
119 
120  displayValue.x = FromDisplay( aValue.x, REL_X_COORD );
121  displayValue.y = FromDisplay( aValue.y, REL_Y_COORD );
122  return displayValue;
123  }
124 
125 
126 protected:
127 
128  // =============== Generic Relative Transforms ===============
129 
130  template<class T> inline static
131  T ToDisplayRel( T aInternalValue,
132  bool aInvertAxis )
133  {
134  T displayValue = aInternalValue;
135 
136  // Invert the direction if needed
137  if( aInvertAxis && (displayValue != static_cast<T>(0)) )
138  displayValue = -displayValue;
139 
140  return displayValue;
141  }
142 
143 
144  template<class T> inline static
145  T FromDisplayRel( T aDisplayValue,
146  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  return internalValue;
155  }
156 
157 
158  // =============== Generic Absolute Transforms ===============
159 
160  template<class T> inline static
161  T ToDisplayAbs( T aInternalValue,
162  int aUserOrigin,
163  bool aInvertAxis )
164  {
165  T displayValue = aInternalValue;
166 
167  // Make the value relative to the internal origin
168  displayValue -= aUserOrigin;
169 
170  // Invert the direction if needed
171  if( aInvertAxis && (displayValue != static_cast<T>(0)) )
172  displayValue = -displayValue;
173 
174  return displayValue;
175  }
176 
177  template<class T> inline static
178  T FromDisplayAbs( T aDisplayValue,
179  int aUserOrigin,
180  bool aInvertAxis )
181  {
182  T internalValue = aDisplayValue;
183 
184  // Invert the direction if needed
185  if( aInvertAxis && (internalValue != static_cast<T>(0)) )
186  internalValue = -internalValue;
187 
188  // Make the value relative to the internal origin
189  internalValue += aUserOrigin;
190 
191  return internalValue;
192  }
193 };
194 
195 #endif // ORIGIN_TRANSFORMS_H
T FromDisplayRel(const T &aValue) const
static T FromDisplayAbs(T aDisplayValue, int aUserOrigin, bool aInvertAxis)
static T ToDisplayRel(T aInternalValue, bool aInvertAxis)
static T ToDisplayAbs(T aInternalValue, int aUserOrigin, bool aInvertAxis)
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.
virtual int FromDisplay(int aValue, COORD_TYPES_T aCoordType) const
A class to perform either relative or absolute display origin transforms for a single axis of a point...
T ToDisplayRel(const T &aValue) const
T FromDisplayAbs(const T &aValue) const
static T FromDisplayRel(T aDisplayValue, bool aInvertAxis)