NCBI C++ ToolKit
glrect.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_OPENGL___GLRECT__HPP
2 #define GUI_OPENGL___GLRECT__HPP
3 
4 /* $Id: glrect.hpp 31696 2014-11-06 18:37:59Z falkrb $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrey Yazhuk
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <gui/opengl/glpoint.hpp>
37 
38 /** @addtogroup GUI_OPENGL
39  *
40  * @{
41  */
42 
44 
45 
46 
47 /// CGlRect reprsents rectangle in the coordinate system with origin located in
48 /// the left bottom corner.
49 
50 
51 template <class T> class CGlRect
52 {
53 public:
54  // Construction
56  : m_Left(0), m_Bottom(0), m_Right(0), m_Top(0) {}
57  CGlRect(T x, T y)
58  : m_Left(x), m_Bottom(y), m_Right(x), m_Top(y) {}
59  CGlRect(T left, T bottom, T right, T top)
60  : m_Left(left), m_Bottom(bottom), m_Right(right), m_Top(top) {}
61 
62  void Init()
63  {
64  m_Left = m_Bottom = m_Right = m_Top = 0;
65  }
66 
67  void Init(T x, T y)
68  {
69  m_Left = m_Right = x;
70  m_Bottom = m_Top = y;
71  }
72 
73  void Init(T left, T bottom, T right, T top)
74  {
75  m_Left = left;
76  m_Bottom = bottom;
77  m_Right = right;
78  m_Top = top;
79  }
80 
81  T Left() const { return m_Left; }
82  T Bottom() const { return m_Bottom; }
83  T Right() const { return m_Right; }
84  T Top() const { return m_Top; }
85 
86  T Width() const
87  {
88  return m_Right - m_Left;
89  }
90  T Height() const
91  {
92  return m_Top - m_Bottom;
93  }
95  {
96  return CGlPoint<T>( (m_Left + m_Right) / 2, (m_Bottom + m_Top) / 2);
97  }
98  // return corners 0..3 starting with lower left and proceeding
99  // counter clockwise, or 0,0 for invalid queries.
101  {
102  switch (idx) {
103  case 0: return CGlPoint<T>( m_Left, m_Bottom);
104  case 1: return CGlPoint<T>( m_Right, m_Bottom);
105  case 2: return CGlPoint<T>( m_Right, m_Top);
106  case 3: return CGlPoint<T>( m_Left, m_Top);
107  }
108 
109  return CGlPoint<T>((T)0, (T)0);
110  }
111 
112  void SetLeft(T left) { m_Left = left; }
113  void SetBottom(T bottom) { m_Bottom = bottom; }
114  void SetRight(T right) { m_Right = right; }
115  void SetTop(T top) { m_Top = top; }
116 
117  void SetHorz(T left, T right)
118  {
119  m_Left = left;
120  m_Right = right;
121  }
122 
123  void SetVert(T bottom, T top)
124  {
125  m_Bottom = bottom;
126  m_Top = top;
127  }
128 
129  void SetSize(T width, T height)
130  {
131  m_Right = m_Left + width;
132  m_Top = m_Bottom + height;
133  }
134 
135  void MoveLeft(T shift) { m_Left += shift; }
136  void MoveRight(T shift) { m_Right += shift; }
137  void MoveBottom(T shift) { m_Bottom += shift; }
138  void MoveTop(T shift) { m_Top += shift; }
139 
140  // tests
141  bool operator==(const CGlRect<T>& rc) const
142  {
143  return m_Left == rc.m_Left && m_Right == rc.m_Right
144  && m_Bottom == rc.m_Bottom && m_Top == rc.m_Top;
145  }
146  bool operator!=(const CGlRect<T>& rc) const
147  {
148  return !(*this == rc);
149  }
150  bool IsEmpty() const
151  {
152  return (m_Left == m_Right) || (m_Bottom == m_Top);
153  }
154  bool PtInRect(T x, T y) const
155  {
156  return ((x >= m_Left && x <= m_Right) || (x <= m_Left && x >= m_Right))
157  && ((y >= m_Bottom && y <= m_Top) || (y <= m_Bottom && y >= m_Top));
158  }
159  bool PtInRect(const CGlPoint<T>& pt) const
160  {
161  return PtInRect(pt.X(), pt.Y());
162  }
163  bool Intersects(const CGlRect& R) const
164  {
165  bool direct_x = (m_Right > m_Left) ||
166  (m_Right == m_Left && R.m_Right > R.m_Left);
167  bool int_x = direct_x ? ! ((R.m_Left > m_Right) || (R.m_Right < m_Left))
168  : ! ((R.m_Left < m_Right) || (R.m_Right > m_Left));
169 
170  bool direct_y = (m_Top > m_Bottom) ||
171  (m_Top == m_Bottom && R.m_Top >= R.m_Bottom);
172  bool int_y = direct_y ? ! ((R.m_Bottom > m_Top) || (R.m_Top < m_Bottom))
173  : ! ((R.m_Bottom < m_Top) || (R.m_Top > m_Bottom));
174  return int_x && int_y;
175  }
176 
177  // operations
178  void Inflate(T d_x, T d_y)
179  {
180  m_Left -= d_x;
181  m_Right += d_x;
182  m_Bottom -= d_y;
183  m_Top += d_y;
184  }
185 
186  void Offset(T d_x, T d_y)
187  {
188  m_Left += d_x;
189  m_Right += d_x;
190  m_Bottom += d_y;
191  m_Top += d_y;
192  }
193 
195  {
196  bool direct_x = (m_Right > m_Left) ||
197  (m_Right == m_Left && r.m_Right > r.m_Left);
198  if(direct_x) {
199  m_Left = max(m_Left, r.m_Left);
200  m_Right = min(m_Right, r.m_Right);
201  if (m_Right < m_Left) m_Right = m_Left;
202  } else {
203  // reversed direction
204  m_Left = min(m_Left, r.m_Left);
205  m_Right = max(m_Right, r.m_Right);
206  if (m_Left < m_Right) m_Left = m_Right;
207  }
208  bool direct_y = (m_Top >= m_Bottom) ||
209  (m_Top == m_Bottom && r.m_Top >= r.m_Bottom);
210  if(direct_y) {
211  m_Bottom = max(m_Bottom, r.m_Bottom);
212  m_Top = min(m_Top, r.m_Top);
213  if (m_Top < m_Bottom) m_Top = m_Bottom;
214  } else {
215  // reversed direction
216  m_Bottom = min(m_Bottom, r.m_Bottom);
217  m_Top = max(m_Top, r.m_Top);
218  if (m_Bottom < m_Top) m_Bottom = m_Top;
219  }
220  return *this;
221  }
222 
224  {
225  bool direct_x = (m_Right > m_Left) ||
226  (m_Right == m_Left && r.m_Right > r.m_Left);
227  if(direct_x) {
228  m_Left = min(m_Left, r.m_Left);
229  m_Right = max(m_Right, r.m_Right);
230  } else {
231  // reversed direction
232  m_Left = max(m_Left, r.m_Left);
233  m_Right = min(m_Right, r.m_Right);
234  }
235  bool direct_y = (m_Top >= m_Bottom) ||
236  (m_Top == m_Bottom && r.m_Top >= r.m_Bottom);
237  if(direct_y) {
238  m_Bottom = min(m_Bottom, r.m_Bottom);
239  m_Top = max(m_Top, r.m_Top);
240  } else {
241  // reversed direction
242  m_Bottom = max(m_Bottom, r.m_Bottom);
243  m_Top = min(m_Top, r.m_Top);
244  }
245  return *this;
246  }
247 
248  inline string ToString() const;
249 private:
254 };
255 
256 
257 template<> inline int CGlRect<int>::Width() const
258 {
259  return m_Right - m_Left + 1;
260 }
261 
262 template<> inline int CGlRect<int>::Height() const
263 {
264  return m_Top - m_Bottom + 1;
265 }
266 
267 
268 template<> inline void CGlRect<int>::SetSize(int width, int height)
269 {
270  m_Right = m_Left + width - 1;
271  m_Top = m_Bottom + height - 1;
272 }
273 
274 
275 template<> inline string CGlRect<int>::ToString() const
276 {
277  string s = "Left " + NStr::IntToString(m_Left);
278  s += ", Right " + NStr::IntToString(m_Right);
279  s += ", Bottom " + NStr::IntToString(m_Bottom);
280  s += ", Top " + NStr::IntToString(m_Top);
281  return s;
282 }
283 
284 
285 template<> inline string CGlRect<double>::ToString() const
286 {
287  string s = "Left " + NStr::DoubleToString(m_Left);
288  s += ", Right " + NStr::DoubleToString(m_Right);
289  s += ", Bottom " + NStr::DoubleToString(m_Bottom);
290  s += ", Top " + NStr::DoubleToString(m_Top);
291  return s;
292 }
293 
295 
296 /* @} */
297 
298 #endif // GUI_OPENGL___GLRECT__HPP
CGlRect reprsents rectangle in the coordinate system with origin located in the left bottom corner.
Definition: glrect.hpp:52
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
CGlRect()
Definition: glrect.hpp:55
CGlRect & IntersectWith(const CGlRect &r)
Definition: glrect.hpp:194
void MoveRight(T shift)
Definition: glrect.hpp:136
void SetRight(T right)
Definition: glrect.hpp:114
T m_Right
Definition: glrect.hpp:252
void SetSize(T width, T height)
Definition: glrect.hpp:129
T X() const
Definition: glpoint.hpp:59
void MoveTop(T shift)
Definition: glrect.hpp:138
T m_Left
Definition: glrect.hpp:250
T Height() const
Definition: glrect.hpp:90
void Init()
Definition: glrect.hpp:62
void SetBottom(T bottom)
Definition: glrect.hpp:113
T Top() const
Definition: glrect.hpp:84
CGlRect & CombineWith(const CGlRect &r)
Definition: glrect.hpp:223
T Bottom() const
Definition: glrect.hpp:82
void Offset(T d_x, T d_y)
Definition: glrect.hpp:186
T Width() const
Definition: glrect.hpp:86
bool IsEmpty() const
Definition: glrect.hpp:150
T Right() const
Definition: glrect.hpp:83
bool operator==(const CGlRect< T > &rc) const
Definition: glrect.hpp:141
CGlPoint< T > CenterPoint() const
Definition: glrect.hpp:94
void Init(T x, T y)
Definition: glrect.hpp:67
T Left() const
Definition: glrect.hpp:81
T Y() const
Definition: glpoint.hpp:60
bool PtInRect(T x, T y) const
Definition: glrect.hpp:154
void Inflate(T d_x, T d_y)
Definition: glrect.hpp:178
bool PtInRect(const CGlPoint< T > &pt) const
Definition: glrect.hpp:159
void MoveBottom(T shift)
Definition: glrect.hpp:137
bool operator!=(const CGlRect< T > &rc) const
Definition: glrect.hpp:146
void SetVert(T bottom, T top)
Definition: glrect.hpp:123
string ToString() const
void MoveLeft(T shift)
Definition: glrect.hpp:135
void SetLeft(T left)
Definition: glrect.hpp:112
CGlPoint< T > GetCorner(int idx)
Definition: glrect.hpp:100
T m_Top
Definition: glrect.hpp:253
CGlRect(T x, T y)
Definition: glrect.hpp:57
void Init(T left, T bottom, T right, T top)
Definition: glrect.hpp:73
CGlRect(T left, T bottom, T right, T top)
Definition: glrect.hpp:59
void SetHorz(T left, T right)
Definition: glrect.hpp:117
bool Intersects(const CGlRect &R) const
Definition: glrect.hpp:163
void SetTop(T top)
Definition: glrect.hpp:115
T m_Bottom
Definition: glrect.hpp:251
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5187
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
T max(T x_, T y_)
T min(T x_, T y_)
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
Modified on Tue Apr 30 06:39:43 2024 by modify_doxy.py rev. 669887