Equalizer  2.1.0
Parallel Rendering Framework
pixelViewport.h
1 
2 /* Copyright (c) 2006-2017, Stefan Eilemann <eile@equalizergraphics.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef EQFABRIC_PIXELVIEWPORT_H
19 #define EQFABRIC_PIXELVIEWPORT_H
20 
21 #include <eq/fabric/pixel.h> // used in inline method
22 #include <eq/fabric/viewport.h> // used in inline method
23 #include <eq/fabric/zoom.h> // used in inline method
24 
25 #include <lunchbox/debug.h>
26 
27 #include <limits>
28 
29 namespace eq
30 {
31 namespace fabric
32 {
35 {
36 public:
41  : x(0)
42  , y(0)
43  , w(-1)
44  , h(-1)
45  {
46  }
47 
49  PixelViewport(const int32_t x_, const int32_t y_, const int32_t w_,
50  const int32_t h_)
51  : x(x_)
52  , y(y_)
53  , w(w_)
54  , h(h_)
55  {
56  }
57 
59  explicit PixelViewport(const int32_t pvp[4])
60  : x(pvp[0])
61  , y(pvp[1])
62  , w(pvp[2])
63  , h(pvp[3])
64  {
65  }
67 
71  void invalidate()
72  {
73  x = 0;
74  y = 0;
75  w = -1;
76  h = -1;
77  }
78 
84  bool isValid() const { return (w >= 0 && h >= 0); }
90  bool hasArea() const { return (w > 0 && h > 0); }
92  uint32_t getArea() const { return w * h; }
97  bool isInside(const int32_t pX, const int32_t pY) const
98  {
99  if (pX < x || pY < y || pX > (x + w) || pY > (y + h))
100  return false;
101  return true;
102  }
104 
109  {
110  // honor position over size to avoid rounding artifacts
111  const int32_t xEnd = x + int32_t((vp.x + vp.w) * w);
112  const int32_t yEnd = y + int32_t((vp.y + vp.h) * h);
113 
114  x += int32_t(w * vp.x);
115  y += int32_t(h * vp.y);
116  w = xEnd - x;
117  h = yEnd - y;
118  return *this;
119  }
120 
122  void apply(const Pixel& pixel)
123  {
124  if (pixel.w > 1)
125  {
126  int32_t newWidth = w / pixel.w;
127  // This would be the correct thing to do, but it would
128  // require frustum adaptations in CUV::_computeFrustum:
129  // if( w - ( newWidth * pixel.w ) > pixel.x )
130  if (w - (newWidth * pixel.w) != 0)
131  ++newWidth;
132 
133  w = newWidth;
134  }
135  if (pixel.h > 1)
136  {
137  int32_t newHeight = h / pixel.h;
138  // This would be the correct thing to do, but it would
139  // require frustum adaptations in CUV::_computeFrustum:
140  // if( w - ( newWidth * pixel.h ) > pixel.y )
141  if (h - (newHeight * pixel.h) != 0)
142  ++newHeight;
143 
144  h = newHeight;
145  }
146  }
147 
149  void apply(const Zoom& zoom)
150  {
151  if (zoom == Zoom::NONE)
152  return;
153 
154  x = static_cast<int32_t>(x * zoom.x() + .5f);
155  y = static_cast<int32_t>(y * zoom.y() + .5f);
156  w = static_cast<int32_t>(w * zoom.x() + .5f);
157  h = static_cast<int32_t>(h * zoom.y() + .5f);
158  }
159 
165  const Zoom getZoom(const PixelViewport& rhs) const
166  {
167  if (*this == rhs)
168  return Zoom::NONE;
169 
170  if (!rhs.hasArea())
171  return Zoom(std::numeric_limits<float>::max(),
172  std::numeric_limits<float>::max());
173 
174  return Zoom(w / static_cast<float>(rhs.w),
175  h / static_cast<float>(rhs.h));
176  }
177 
179  int32_t getXEnd() const { return x + w; }
181  int32_t getYEnd() const { return y + h; }
183  void convertToPlugin(uint64_t dims[4]) const
184  {
185  dims[0] = x;
186  dims[1] = w;
187  dims[2] = y;
188  dims[3] = h;
189  }
190 
192  void convertFromPlugin(const uint64_t dims[4])
193  {
194  x = int32_t(dims[0]);
195  w = int32_t(dims[1]);
196  y = int32_t(dims[2]);
197  h = int32_t(dims[3]);
198  }
199 
201  const PixelViewport operator+(const Vector2i& offset) const
202  {
203  return PixelViewport(x + offset.x(), y + offset.y(), w, h);
204  }
205 
210  Viewport operator/(const PixelViewport& rhs) const
211  {
212  if (*this == rhs)
213  return Viewport::FULL;
214 
215  if (!rhs.hasArea())
216  return Viewport(static_cast<float>(x), static_cast<float>(y), 0.f,
217  0.f);
218 
219  return Viewport((x - rhs.x) / static_cast<float>(rhs.w),
220  (y - rhs.y) / static_cast<float>(rhs.h),
221  (w) / static_cast<float>(rhs.w),
222  (h) / static_cast<float>(rhs.h));
223  }
224 
226  const PixelViewport& operator-=(const Vector2i& offset)
227  {
228  x -= offset.x();
229  y -= offset.y();
230  return *this;
231  }
232 
238  void unapply(const Pixel& pixel)
239  {
240  w *= pixel.w;
241  h *= pixel.h;
242  x += pixel.x;
243  y += pixel.y;
244  }
245 
250  bool operator==(const PixelViewport& rhs) const
251  {
252  return (x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h);
253  }
254 
259  bool operator!=(const PixelViewport& rhs) const
260  {
261  return (x != rhs.x || y != rhs.y || w != rhs.w || h != rhs.h);
262  }
263 
269  {
270  if (*this == rhs || !rhs.isValid())
271  return *this;
272 
273  if (!hasArea())
274  {
275  *this = rhs;
276  return *this;
277  }
278  if (!rhs.hasArea())
279  return *this;
280 
281  const int32_t sEx = x + w;
282  const int32_t sEy = y + h;
283  const int32_t dEx = rhs.x + rhs.w;
284  const int32_t dEy = rhs.y + rhs.h;
285 
286  x = LB_MIN(x, rhs.x);
287  y = LB_MIN(y, rhs.y);
288  w = LB_MAX(sEx, dEx) - x;
289  h = LB_MAX(sEy, dEy) - y;
290  return *this;
291  }
292 
294  void intersect(const PixelViewport& rhs)
295  {
296  if (*this == rhs)
297  return;
298 
299  if (!rhs.isValid() || !isValid())
300  {
301  invalidate();
302  return;
303  }
304 
305  if (!rhs.hasArea() || !hasArea())
306  {
307  x = 0;
308  y = 0;
309  w = 0;
310  h = 0;
311  return;
312  }
313 
314  const int32_t sEx = x + w;
315  const int32_t sEy = y + h;
316  const int32_t dEx = rhs.x + rhs.w;
317  const int32_t dEy = rhs.y + rhs.h;
318 
319  x = LB_MAX(x, rhs.x);
320  y = LB_MAX(y, rhs.y);
321  w = LB_MIN(sEx, dEx) - x;
322  h = LB_MIN(sEy, dEy) - y;
323  }
324 
326 
327  int32_t x;
328  int32_t y;
329  int32_t w;
330  int32_t h;
331 };
332 
333 inline std::ostream& operator<<(std::ostream& os, const PixelViewport& pvp)
334 {
335  os << "[ " << pvp.x << " " << pvp.y << " " << pvp.w << " " << pvp.h << " ]";
336  return os;
337 }
338 }
339 }
340 
341 #endif // EQFABRIC_PIXELVIEWPORT_H
A zoom specification with methods for manipulation.
Definition: zoom.h:35
bool operator!=(const PixelViewport &rhs) const
void invalidate()
Invalidate the pixel viewport.
Definition: pixelViewport.h:71
const Zoom getZoom(const PixelViewport &rhs) const
PixelViewport(const int32_t pvp[4])
Construct a new pixel viewport with default values.
Definition: pixelViewport.h:59
A fractional viewport with methods for manipulation.
Definition: viewport.h:34
const PixelViewport & operator-=(const Vector2i &offset)
void convertToPlugin(uint64_t dims[4]) const
Convert into a lunchbox::Plugin usable format.
PixelViewport(const int32_t x_, const int32_t y_, const int32_t w_, const int32_t h_)
Construct a new pixel viewport with default values.
Definition: pixelViewport.h:49
float y
The Y coordinate.
Definition: viewport.h:199
void convertFromPlugin(const uint64_t dims[4])
Convert from a lunchbox::Plugin format.
void apply(const Zoom &zoom)
Apply a zoom to this pixel viewport.
float x
The X coordinate.
Definition: viewport.h:198
const PixelViewport operator+(const Vector2i &offset) const
PixelViewport & apply(const Viewport &vp)
Apply a fractional viewport to this pixel viewport.
void intersect(const PixelViewport &rhs)
Create the intersection of the two pixel viewports.
int32_t getYEnd() const
PixelViewport()
Construct a new, invalid pixel viewport.
Definition: pixelViewport.h:40
Holds a pixel decomposition specification with methods for manipulation.
Definition: pixel.h:40
The Equalizer client library.
Definition: eq/agl/types.h:23
uint32_t getArea() const
Definition: pixelViewport.h:92
static EQFABRIC_API const Viewport FULL
A full viewport.
Definition: viewport.h:203
std::ostream & operator<<(std::ostream &os, const AxisEvent &event)
Print the axis event to the given output stream.
Definition: axisEvent.h:49
bool operator==(const PixelViewport &rhs) const
void unapply(const Pixel &pixel)
Perform the inverse operation of applying a pixel decomposition to this pixel viewport.
void apply(const Pixel &pixel)
Apply a pixel decomposition to this pixel viewport.
float h
The height.
Definition: viewport.h:201
bool isInside(const int32_t pX, const int32_t pY) const
Definition: pixelViewport.h:97
int32_t getXEnd() const
Holds a 2D pixel viewport with methods for manipulation.
Definition: pixelViewport.h:34
PixelViewport & merge(const PixelViewport &rhs)
Create a pixel viewport that includes both viewports (union).
static EQFABRIC_API const Zoom NONE
The zoom NONE (1,1) value.
Definition: zoom.h:72
float w
The width.
Definition: viewport.h:200