Scaling.h
1 /* Copyright (C) 2015 Marc Boris Duerner
2 
3  This library is free software; you can redistribute it and/or
4  modify it under the terms of the GNU Lesser General Public
5  License as published by the Free Software Foundation; either
6  version 2.1 of the License, or (at your option) any later version.
7 
8  As a special exception, you may use this file as part of a free
9  software library without restriction. Specifically, if other files
10  instantiate templates or use macros or inline functions from this
11  file, or you compile this file and link it with other files to
12  produce an executable, this file does not by itself cause the
13  resulting executable to be covered by the GNU General Public
14  License. This exception does not however invalidate any other
15  reasons why the executable file might be covered by the GNU Library
16  General Public License.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with this library; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  MA 02110-1301 USA
27 */
28 
29 #ifndef Pt_Gfx_Scaling_h
30 #define Pt_Gfx_Scaling_h
31 
32 #include <Pt/Gfx/Api.h>
33 #include <Pt/Gfx/Point.h>
34 #include <Pt/Gfx/Size.h>
35 #include <Pt/Gfx/Rect.h>
36 
37 namespace Pt {
38 
39 namespace Gfx {
40 
43 class Scaling
44 {
45  friend bool operator ==(const Scaling&, const Scaling&);
46  friend bool operator <(const Scaling&, const Scaling&);
47 
48  public:
49  Scaling(double scaleFactor = 1.0)
50  : _scaleFactor(scaleFactor)
51  { }
52 
53  double scaleFactor() const
54  {
55  return _scaleFactor;
56  }
57 
58  void setScaleFactor(double scaleFactor)
59  {
60  _scaleFactor = scaleFactor;
61  }
62 
63  public:
64  double toPhysical(double n) const
65  {
66  return n * scaleFactor();
67  }
68 
69  Gfx::PointF toPhysical(const Gfx::PointF& p) const
70  {
71  return p * scaleFactor();
72  }
73 
74  Gfx::SizeF toPhysical(const Gfx::SizeF& s) const
75  {
76  return s * scaleFactor();
77  }
78 
79  Gfx::RectF toPhysical(const Gfx::RectF& r) const
80  {
81  Gfx::PointF p = toPhysical( r.topLeft() );
82  Gfx::SizeF s = toPhysical( r.size() );
83  return Gfx::RectF(p, s);
84  }
85 
86  double toLogical(double n) const
87  {
88  return n / scaleFactor();
89  }
90 
91  Gfx::PointF toLogical(const Gfx::PointF& p) const
92  {
93  return p / scaleFactor();
94  }
95 
96  Gfx::SizeF toLogical(const Gfx::SizeF& s) const
97  {
98  return s / scaleFactor();
99  }
100 
101  Gfx::RectF toLogical(const Gfx::RectF& r) const
102  {
103  Gfx::PointF p = toLogical( r.topLeft() );
104  Gfx::SizeF s = toLogical( r.size() );
105  return Gfx::RectF(p, s);
106  }
107 
108  public:
109  double align(double n) const
110  {
111  // better name: alignGrid()
112 
113  double p = toPhysical(n);
114  p = lround(p);
115  return toLogical(p);
116  }
117 
118  double alignPixel(double n) const
119  {
120  double p = toPhysical(n);
121  p = lround(p + 0.5) - 0.5;
122  return toLogical(p);
123  }
124 
125  double alignContour(size_t n) const
126  {
127  // keep contour size when downscaling
128  if (_scaleFactor < 1.0)
129  return toLogical( static_cast<double>(n) );
130 
131  double p = toPhysical( static_cast<double>(n) );
132  size_t s = static_cast<size_t>(p);
133  return toLogical( static_cast<double>(s) );
134  }
135 
136  Gfx::PointF align(const Gfx::PointF& p) const
137  {
138  Gfx::PointF pos = toPhysical(p);
139  pos.setX(lround(pos.x()));
140  pos.setY(lround(pos.y()));
141  return toLogical(pos);
142  }
143 
144  Gfx::SizeF align(const Gfx::SizeF& s) const
145  {
146  Gfx::SizeF size = toPhysical(s);
147  size.setWidth(lround(size.width()));
148  size.setHeight(lround(size.height()));
149  return toLogical(size);
150  }
151 
152  Gfx::RectF align(const Gfx::RectF& rect) const
153  {
154  Gfx::PointF pos = toPhysical(rect.topLeft());
155  pos.setX(lround(pos.x()));
156  pos.setY(lround(pos.y()));
157 
158  Gfx::SizeF size = toPhysical(rect.size());
159  size.setWidth(lround(size.width()));
160  size.setHeight(lround(size.height()));
161 
162  return toLogical(Gfx::RectF(pos, size));
163  }
164 
165  private:
166  double _scaleFactor;
167 };
168 
169 
170 inline bool operator == (const Scaling& a, const Scaling& b)
171 {
172  return a._scaleFactor == b._scaleFactor;
173 }
174 
175 
176 inline bool operator != (const Scaling& a, const Scaling& b)
177 {
178  return ! (a == b);
179 }
180 
181 
182 inline bool operator < (const Scaling& a, const Scaling& b)
183 {
184  return a._scaleFactor < b._scaleFactor;
185 }
186 
187 } // namespace
188 
189 } // namespace
190 
191 #endif
Rect with floating-point coordinates.
Definition: Rect.h:44
Pt::int32_t lround(float x)
Rounds to nearest integer value.
Definition: Math.h:289
Size with floating-point width and height.
Definition: Size.h:44
Surface scaling.
Definition: Scaling.h:43
Point with floating-point X and Y coordinates.
Definition: Point.h:44