Equalizer  1.13.0
Parallel Rendering Framework
viewport.h
1 
2 /* Copyright (c) 2006-2016, 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 <lunchbox/debug.h>
25 #include <iostream>
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:
40  Viewport() : x(0.0f), y(0.0f), w(1.0f), h(1.0f) {}
41 
43  Viewport( const float x_, const float y_,
44  const float w_, const float h_ )
45  : x( x_ ), y( y_ ), w( w_ ), h( h_ ) {}
46 
48  explicit Viewport( const Vector4f& from )
49  : x( from[0] ), y( from[1] ), w( from[2] ), h( from[3] ) {}
51 
53  void invalidate() { x=0.0f; y=0.0f; w=-1.0f; h=-1.0f; }
54 
56  void apply( const Viewport& rhs )
57  {
58  LBASSERTINFO( isValid(), *this);
59  LBASSERTINFO( rhs.isValid(), rhs );
60  x += rhs.x * w;
61  y += rhs.y * h;
62  w *= rhs.w;
63  h *= rhs.h;
64  }
65 
67  void transform( const Viewport& rhs )
68  {
69  w = w / rhs.w;
70  h = h / rhs.h;
71  x = ( x - rhs.x ) / rhs.w;
72  y = ( y - rhs.y ) / rhs.h;
73  }
74 
79  bool operator == ( const Viewport& rhs ) const
80  {
81  return ( x==rhs.x && y==rhs.y && w==rhs.w && h==rhs.h);
82  }
83 
88  bool operator != ( const Viewport& rhs ) const
89  {
90  return ( x!=rhs.x || y!=rhs.y || w!=rhs.w || h!=rhs.h);
91  }
92 
98  bool isValid() const
99  { return ( x>=0.0f && y>=0.0f && w>=0.0f && h>=0.0f ); }
100 
106  bool hasArea() const { return (w>0.0f && h>0.0f); }
107 
109  float getArea() const { return w*h; }
110 
112  float getXEnd() const { return x+w; }
113 
115  float getYEnd() const { return y+h; }
116 
118  void intersect( const Viewport& rhs )
119  {
120  if( *this == rhs )
121  return;
122 
123  if( !rhs.isValid() || !isValid() )
124  {
125  invalidate();
126  return;
127  }
128 
129  if( !rhs.hasArea() || !hasArea() )
130  {
131  x = 0;
132  y = 0;
133  w = 0;
134  h = 0;
135  return;
136  }
137 
138  const float sEx = static_cast< float >( x + w );
139  const float sEy = static_cast< float >( y + h );
140  const float dEx = static_cast< float >( rhs.x + rhs.w );
141  const float dEy = static_cast< float >( rhs.y + rhs.h );
142 
143  x = LB_MAX( x, rhs.x );
144  y = LB_MAX( y, rhs.y );
145  w = LB_MIN( sEx, dEx ) - x;
146  h = LB_MIN( sEy, dEy ) - y;
147  }
148 
150  void unite( const Viewport& rhs )
151  {
152  const float xEnd = LB_MAX( getXEnd(), rhs.getXEnd( ));
153  const float yEnd = LB_MAX( getYEnd(), rhs.getYEnd( ));
154  x = LB_MIN( x, rhs.x );
155  y = LB_MIN( y, rhs.y );
156  w = xEnd - x;
157  h = yEnd - y;
158  }
159 
164  Viewport getCoverage( const Viewport& with ) const
165  {
166  Viewport coverage( with );
167  coverage.intersect( *this ); // intersection
168  coverage.transform( *this ); // in our coordinate system
169 
170  return coverage;
171  }
172 
174  EQFABRIC_API void applyView( const Viewport& segmentVP,
175  const Viewport& viewVP,
176  const PixelViewport& pvp,
177  const Vector4i& overdraw );
178 
179  float x;
180  float y;
181  float w;
182  float h;
183 
184  EQFABRIC_API static const Viewport FULL;
185 };
186 
187 inline std::ostream& operator << ( std::ostream& os, const Viewport& vp )
188 {
189  os << "[ " << vp.x << " " << vp.y << " " << vp.w << " " << vp.h << " ]";
190  return os;
191 }
192 }
193 }
194 
195 namespace lunchbox
196 {
197 template<> inline void byteswap( eq::fabric::Viewport& value )
198 {
199  byteswap( value.x );
200  byteswap( value.y );
201  byteswap( value.w );
202  byteswap( value.h );
203 }
204 }
205 #endif // EQFABRIC_VIEWPORT_H
float getArea() const
Definition: viewport.h:109
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:88
float getYEnd() const
Definition: viewport.h:115
float y
The Y coordinate.
Definition: viewport.h:180
Viewport getCoverage(const Viewport &with) const
Compute the coverage of another viewport on this viewport.
Definition: viewport.h:164
Viewport(const Vector4f &from)
Construct a fractional viewport from a Vector4f.
Definition: viewport.h:48
float x
The X coordinate.
Definition: viewport.h:179
float getXEnd() const
Definition: viewport.h:112
void intersect(const Viewport &rhs)
Create the intersection of the two viewports.
Definition: viewport.h:118
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:67
void invalidate()
Make the viewport invalid.
Definition: viewport.h:53
bool operator==(const Viewport &rhs) const
Definition: viewport.h:79
Viewport(const float x_, const float y_, const float w_, const float h_)
Construct a fractional viewport with default values.
Definition: viewport.h:43
bool isValid() const
Definition: viewport.h:98
The Equalizer client library.
Definition: eq/agl/types.h:23
static EQFABRIC_API const Viewport FULL
A full viewport.
Definition: viewport.h:184
EQFABRIC_API void applyView(const Viewport &segmentVP, const Viewport &viewVP, const PixelViewport &pvp, const Vector4i &overdraw)
Apply the view coverage to this viewport.
void apply(const Viewport &rhs)
Apply (accumulate) another viewport.
Definition: viewport.h:56
float h
The height.
Definition: viewport.h:182
bool hasArea() const
Definition: viewport.h:106
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:150
float w
The width.
Definition: viewport.h:181