CEDRIC  Revision_backup-2009-02
ClipRect.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 2008 Boris Iven, Max Hermann
4  *
5  * This file is part of the "CEDRIC Event Display" application.
6  *
7  *****************************************************************************/
8 
9 #ifndef CLIPRECT_H
10 #define CLIPRECT_H
11 
12 #include <ostream>
13 
14 #define CLIPRECT_SUPPORT_OSG
15 #define CLIPRECT_SUPPORT_QT
16 
17 #ifdef CLIPRECT_SUPPORT_OSG
18 #include <osg/BoundingBox>
19 #include <osg/BoundingSphere>
20 #include <osg/Vec2>
21 #endif
22 
23 #ifdef CLIPRECT_SUPPORT_QT
24 #include <QRect>
25 #endif
26 
28 struct ClipRect
29 {
31  : left(0),right(0),bottom(0),top(0)
32  {}
33 
34  ClipRect( double left_, double right_, double bottom_, double top_ )
35  : left(left_), right(right_), bottom(bottom_), top(top_)
36  {}
37 
38  double left, right, bottom, top;
39 
41  void sort()
42  {
43  if( left > right ) std::swap( left, right );
44  if( bottom > top ) std::swap( top, bottom );
45  }
46 
48  inline double center_x() const
49  {
50  return left + (right-left)*.5;
51  }
52 
54  inline double center_y() const
55  {
56  return bottom + (top-bottom)*.5;
57  }
58 
60  inline double width() const
61  {
62  return fabs(right-left);
63  }
64 
66  inline double height() const
67  {
68  return fabs(bottom-top);
69  }
70 
72  void scale_x( float s )
73  {
74  float l = center_x() - .5*s*(right-left);
75  float r = center_x() + .5*s*(right-left);
76 
77  left = l;
78  right = r;
79  }
80 
82  void scale_y( float s )
83  {
84  float b = center_y() - .5*s*(top-bottom);
85  float t = center_y() + .5*s*(top-bottom);
86 
87  bottom = b;
88  top = t;
89  }
90 
92  double aspect() const
93  {
94  return width()/height();
95  }
96 
98  void translate( double x, double y )
99  {
100  float l = (center_x() + x) - .5*(right-left);
101  float r = (center_x() + x) + .5*(right-left);
102  float b = (center_y() + y) - .5*(top-bottom);
103  float t = (center_y() + y) + .5*(top-bottom);
104 
105  top = t;
106  left = l;
107  right = r;
108  bottom = b;
109  }
110 
111 #ifdef CLIPRECT_SUPPORT_OSG
112  ClipRect( osg::Vec2 min, osg::Vec2 max )
113  {
114  top = max.y();
115  left = min.x();
116  right = max.x();
117  bottom = min.y();
118  }
119 
120  ClipRect( const osg::BoundingSphere& bs )
121  {
122  fromBoundingSphere( bs );
123  }
124 
125  void fromBoundingSphere( const osg::BoundingSphere& bs )
126  {
127  left = bottom = -bs._radius;
128  right = top = +bs._radius;
129  }
130 #endif
131 
132 #ifdef CLIPRECT_SUPPORT_QT
133  ClipRect( const QRect& qr )
134  {
135  fromQRect( qr );
136  }
137 
138  void fromQRect( const QRect& qr )
139  {
140  top = qr.top();
141  left = qr.left();
142  right = qr.right();
143  bottom = qr.bottom();
144  }
145 #endif
146 };
147 
149 std::ostream& operator << ( std::ostream& os, const ClipRect& rect );
151 
152 #endif
Simple rectangle structure (e.g. usefull for saving ortho clipplanes)
Definition: ClipRect.h:28
double bottom
Definition: ClipRect.h:38
ClipRect()
Definition: ClipRect.h:30
double right
Definition: ClipRect.h:38
double center_x() const
Returns horizontal center.
Definition: ClipRect.h:48
void translate(double x, double y)
Translate center by x and y.
Definition: ClipRect.h:98
double height() const
Returns bottom-top (absolute value)
Definition: ClipRect.h:66
ClipRect(const osg::BoundingSphere &bs)
Definition: ClipRect.h:120
void scale_y(float s)
Scale vertical by extending bottom and top border equally.
Definition: ClipRect.h:82
double left
Definition: ClipRect.h:38
void sort()
Swap values such that (left <= right) && (bottom <= top)
Definition: ClipRect.h:41
double top
Definition: ClipRect.h:38
double center_y() const
Returns vertical center.
Definition: ClipRect.h:54
double width() const
Returns right-left (absolute value)
Definition: ClipRect.h:60
void fromBoundingSphere(const osg::BoundingSphere &bs)
Definition: ClipRect.h:125
std::ostream & operator<<(std::ostream &os, const ClipRect &rect)
Definition: ClipRect.cpp:11
ClipRect(const QRect &qr)
Definition: ClipRect.h:133
void scale_x(float s)
Scale horizontal by extending left and right border equally.
Definition: ClipRect.h:72
void fromQRect(const QRect &qr)
Definition: ClipRect.h:138
ClipRect(double left_, double right_, double bottom_, double top_)
Definition: ClipRect.h:34
ClipRect(osg::Vec2 min, osg::Vec2 max)
Definition: ClipRect.h:112
double aspect() const
Returns aspect width()/height()
Definition: ClipRect.h:92