Equalizer  2.1.0
Parallel Rendering Framework
viewport.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_VIEWPORT_H
19 #define EQFABRIC_VIEWPORT_H
20 
21 #include <eq/fabric/api.h>
22 #include <eq/fabric/types.h>
23 #include <eq/fabric/vmmlib.h>
24 #include <iostream>
25 #include <lunchbox/debug.h>
26 
27 namespace eq
28 {
29 namespace fabric
30 {
31 std::ostream& operator<<(std::ostream& os, const Viewport& vp);
32 
34 class Viewport
35 {
36 public:
41  : x(0.0f)
42  , y(0.0f)
43  , w(1.0f)
44  , h(1.0f)
45  {
46  }
47 
49  Viewport(const float x_, const float y_, const float w_, const float h_)
50  : x(x_)
51  , y(y_)
52  , w(w_)
53  , h(h_)
54  {
55  }
56 
58  explicit Viewport(const Vector4f& from)
59  : x(from[0])
60  , y(from[1])
61  , w(from[2])
62  , h(from[3])
63  {
64  }
66 
68  void invalidate()
69  {
70  x = 0.0f;
71  y = 0.0f;
72  w = -1.0f;
73  h = -1.0f;
74  }
75 
77  void apply(const Viewport& rhs)
78  {
79  LBASSERTINFO(isValid(), *this);
80  LBASSERTINFO(rhs.isValid(), rhs);
81  x += rhs.x * w;
82  y += rhs.y * h;
83  w *= rhs.w;
84  h *= rhs.h;
85  }
86 
88  void transform(const Viewport& rhs)
89  {
90  w = w / rhs.w;
91  h = h / rhs.h;
92  x = (x - rhs.x) / rhs.w;
93  y = (y - rhs.y) / rhs.h;
94  }
95 
100  bool operator==(const Viewport& rhs) const
101  {
102  return (x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h);
103  }
104 
109  bool operator!=(const Viewport& rhs) const
110  {
111  return (x != rhs.x || y != rhs.y || w != rhs.w || h != rhs.h);
112  }
113 
119  bool isValid() const
120  {
121  return (x >= 0.0f && y >= 0.0f && w >= 0.0f && h >= 0.0f);
122  }
123 
129  bool hasArea() const { return (w > 0.0f && h > 0.0f); }
131  float getArea() const { return w * h; }
133  float getXEnd() const { return x + w; }
135  float getYEnd() const { return y + h; }
137  void intersect(const Viewport& rhs)
138  {
139  if (*this == rhs)
140  return;
141 
142  if (!rhs.isValid() || !isValid())
143  {
144  invalidate();
145  return;
146  }
147 
148  if (!rhs.hasArea() || !hasArea())
149  {
150  x = 0;
151  y = 0;
152  w = 0;
153  h = 0;
154  return;
155  }
156 
157  const float sEx = static_cast<float>(x + w);
158  const float sEy = static_cast<float>(y + h);
159  const float dEx = static_cast<float>(rhs.x + rhs.w);
160  const float dEy = static_cast<float>(rhs.y + rhs.h);
161 
162  x = LB_MAX(x, rhs.x);
163  y = LB_MAX(y, rhs.y);
164  w = LB_MIN(sEx, dEx) - x;
165  h = LB_MIN(sEy, dEy) - y;
166  }
167 
169  void unite(const Viewport& rhs)
170  {
171  const float xEnd = LB_MAX(getXEnd(), rhs.getXEnd());
172  const float yEnd = LB_MAX(getYEnd(), rhs.getYEnd());
173  x = LB_MIN(x, rhs.x);
174  y = LB_MIN(y, rhs.y);
175  w = xEnd - x;
176  h = yEnd - y;
177  }
178 
183  Viewport getCoverage(const Viewport& with) const
184  {
185  Viewport coverage(with);
186  coverage.intersect(*this); // intersection
187  coverage.transform(*this); // in our coordinate system
188 
189  return coverage;
190  }
191 
193  EQFABRIC_API void applyView(const Viewport& segmentVP,
194  const Viewport& viewVP,
195  const PixelViewport& pvp,
196  const Vector4i& overdraw);
197 
198  float x;
199  float y;
200  float w;
201  float h;
202 
203  EQFABRIC_API static const Viewport FULL;
204 };
205 
206 inline std::ostream& operator<<(std::ostream& os, const Viewport& vp)
207 {
208  os << "[ " << vp.x << " " << vp.y << " " << vp.w << " " << vp.h << " ]";
209  return os;
210 }
211 }
212 }
213 
214 #endif // EQFABRIC_VIEWPORT_H
float getArea() const
Definition: viewport.h:131
Defines export visibility macros for library EqualizerFabric.
A fractional viewport with methods for manipulation.
Definition: viewport.h:34
bool operator!=(const Viewport &rhs) const
Definition: viewport.h:109
float getYEnd() const
Definition: viewport.h:135
float y
The Y coordinate.
Definition: viewport.h:199
Viewport getCoverage(const Viewport &with) const
Compute the coverage of another viewport on this viewport.
Definition: viewport.h:183
Viewport(const Vector4f &from)
Construct a fractional viewport from a Vector4f.
Definition: viewport.h:58
float x
The X coordinate.
Definition: viewport.h:198
float getXEnd() const
Definition: viewport.h:133
void intersect(const Viewport &rhs)
Create the intersection of the two viewports.
Definition: viewport.h:137
Viewport()
Construct a full fractional viewport.
Definition: viewport.h:40
void transform(const Viewport &rhs)
Transform this viewport into the rhs viewport space.
Definition: viewport.h:88
void invalidate()
Make the viewport invalid.
Definition: viewport.h:68
bool operator==(const Viewport &rhs) const
Definition: viewport.h:100
Viewport(const float x_, const float y_, const float w_, const float h_)
Construct a fractional viewport with default values.
Definition: viewport.h:49
bool isValid() const
Definition: viewport.h:119
The Equalizer client library.
Definition: eq/agl/types.h:23
static EQFABRIC_API const Viewport FULL
A full viewport.
Definition: viewport.h:203
EQFABRIC_API void applyView(const Viewport &segmentVP, const Viewport &viewVP, const PixelViewport &pvp, const Vector4i &overdraw)
Apply the view coverage to this viewport.
std::ostream & operator<<(std::ostream &os, const AxisEvent &event)
Print the axis event to the given output stream.
Definition: axisEvent.h:49
void apply(const Viewport &rhs)
Apply (accumulate) another viewport.
Definition: viewport.h:77
float h
The height.
Definition: viewport.h:201
bool hasArea() const
Definition: viewport.h:129
Holds a 2D pixel viewport with methods for manipulation.
Definition: pixelViewport.h:34
void unite(const Viewport &rhs)
Create the union of the two viewports.
Definition: viewport.h:169
float w
The width.
Definition: viewport.h:200