Equalizer  1.6.1
viewport.h
1 
2 /* Copyright (c) 2006-2010, 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 <lunchbox/debug.h>
24 
25 #include <iostream>
26 
27 namespace eq
28 {
29 namespace fabric
30 {
31  class PixelViewport;
32  class Viewport;
33  std::ostream& operator << ( std::ostream& os, const Viewport& vp );
34 
36  class Viewport
37  {
38  public:
42  Viewport() : x(0.0f), y(0.0f), w(1.0f), h(1.0f) {}
43 
45  explicit Viewport( const float x_, const float y_,
46  const float w_, const float h_ )
47  : x(x_), y(y_), w(w_), h(h_) {}
48 
50  explicit Viewport( const Vector4f& from )
51  : x( from[0] ), y( from[1] ), w( from[2] ), h( from[3] ) {}
53 
55  void invalidate() { x=0.0f; y=0.0f; w=-1.0f; h=-1.0f; }
56 
58  void apply( const Viewport& rhs )
59  {
60  LBASSERTINFO( isValid(), *this);
61  LBASSERTINFO( rhs.isValid(), rhs );
62  x += rhs.x * w;
63  y += rhs.y * h;
64  w *= rhs.w;
65  h *= rhs.h;
66  }
67 
69  void transform( const Viewport& rhs )
70  {
71  w = w / rhs.w;
72  h = h / rhs.h;
73  x = ( x - rhs.x ) / rhs.w;
74  y = ( y - rhs.y ) / rhs.h;
75  }
76 
81  bool operator == ( const Viewport& rhs ) const
82  {
83  return ( x==rhs.x && y==rhs.y && w==rhs.w && h==rhs.h);
84  }
85 
90  bool operator != ( const Viewport& rhs ) const
91  {
92  return ( x!=rhs.x || y!=rhs.y || w!=rhs.w || h!=rhs.h);
93  }
94 
100  bool isValid() const
101  { return ( x>=0.0f && y>=0.0f && w>=0.0f && h>=0.0f ); }
102 
108  bool hasArea() const { return (w>0.0f && h>0.0f); }
109 
111  float getArea() const { return w*h; }
112 
114  float getXEnd() const { return x+w; }
115 
117  float getYEnd() const { return y+h; }
118 
120  void intersect( const Viewport& rhs )
121  {
122  if( *this == rhs )
123  return;
124 
125  if( !rhs.isValid() || !isValid() )
126  {
127  invalidate();
128  return;
129  }
130 
131  if( !rhs.hasArea() || !hasArea() )
132  {
133  x = 0;
134  y = 0;
135  w = 0;
136  h = 0;
137  return;
138  }
139 
140  const float sEx = static_cast< float >( x + w );
141  const float sEy = static_cast< float >( y + h );
142  const float dEx = static_cast< float >( rhs.x + rhs.w );
143  const float dEy = static_cast< float >( rhs.y + rhs.h );
144 
145  x = LB_MAX( x, rhs.x );
146  y = LB_MAX( y, rhs.y );
147  w = LB_MIN( sEx, dEx ) - x;
148  h = LB_MIN( sEy, dEy ) - y;
149  }
150 
152  void unite( const Viewport& rhs )
153  {
154  const float xEnd = LB_MAX( getXEnd(), rhs.getXEnd( ));
155  const float yEnd = LB_MAX( getYEnd(), rhs.getYEnd( ));
156  x = LB_MIN( x, rhs.x );
157  y = LB_MIN( y, rhs.y );
158  w = xEnd - x;
159  h = yEnd - y;
160  }
161 
166  Viewport getCoverage( const Viewport& with ) const
167  {
168  Viewport coverage( with );
169  coverage.intersect( *this ); // intersection
170  coverage.transform( *this ); // in our coordinate system
171 
172  return coverage;
173  }
174 
176  EQFABRIC_API void applyView( const Viewport& segmentVP,
177  const Viewport& viewVP,
178  const PixelViewport& pvp,
179  const Vector4i& overdraw );
180 
181  float x;
182  float y;
183  float w;
184  float h;
185 
186  EQFABRIC_API static const Viewport FULL;
187  };
188 
189  inline std::ostream& operator << ( std::ostream& os, const Viewport& vp )
190  {
191  os << "[ " << vp.x << " " << vp.y << " " << vp.w << " " << vp.h << " ]";
192  return os;
193  }
194 }
195 }
196 
197 namespace lunchbox
198 {
199 template<> inline void byteswap( eq::fabric::Viewport& value )
200 {
201  byteswap( value.x );
202  byteswap( value.y );
203  byteswap( value.w );
204  byteswap( value.h );
205 }
206 }
207 #endif // EQFABRIC_VIEWPORT_H
static const Viewport FULL
A full viewport.
Definition: viewport.h:186
Viewport(const float x_, const float y_, const float w_, const float h_)
Construct a fractional viewport with default values.
Definition: viewport.h:45
float h
The height.
Definition: viewport.h:184
bool operator==(const Viewport &rhs) const
Definition: viewport.h:81
bool operator!=(const Viewport &rhs) const
Definition: viewport.h:90
Viewport getCoverage(const Viewport &with) const
Compute the coverage of another viewport on this viewport.
Definition: viewport.h:166
vmml::vector< 4, int > Vector4i
A four-component integer vector.
Definition: vmmlib.h:42
void invalidate()
Make the viewport invalid.
Definition: viewport.h:55
bool hasArea() const
Definition: viewport.h:108
void unite(const Viewport &rhs)
Create the union of the two viewports.
Definition: viewport.h:152
void applyView(const Viewport &segmentVP, const Viewport &viewVP, const PixelViewport &pvp, const Vector4i &overdraw)
Apply the view coverage to this viewport.
Viewport(const Vector4f &from)
Construct a fractional viewport from a Vector4f.
Definition: viewport.h:50
void intersect(const Viewport &rhs)
Create the intersection of the two viewports.
Definition: viewport.h:120
float w
The width.
Definition: viewport.h:183
Holds a 2D pixel viewport with methods for manipulation.
Definition: pixelViewport.h:34
float getYEnd() const
Definition: viewport.h:117
void transform(const Viewport &rhs)
Transform this viewport into the rhs viewport space.
Definition: viewport.h:69
Viewport()
Construct a full fractional viewport.
Definition: viewport.h:42
bool isValid() const
Definition: viewport.h:100
float getArea() const
Definition: viewport.h:111
float x
The X coordinate.
Definition: viewport.h:181
void apply(const Viewport &rhs)
Apply (accumulate) another viewport.
Definition: viewport.h:58
vmml::vector< 4, float > Vector4f
A four-component float vector.
Definition: vmmlib.h:47
A fractional viewport with methods for manipulation.
Definition: viewport.h:36
float getXEnd() const
Definition: viewport.h:114
float y
The Y coordinate.
Definition: viewport.h:182