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

Go to the SVN repository for this file.

1 #ifndef GUI_UTILS__BBOX_HPP
2 #define GUI_UTILS__BBOX_HPP
3 
4 /* $Id: bbox.hpp 27010 2012-12-07 16:37:16Z 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: Peter Meric
30  *
31  * File Description:
32  * CBBox - represents (and calculates) a bounding box
33  *
34  */
35 
36 /** @addtogroup GUI_UTILS
37 *
38 * @{
39 */
40 
41 #include <corelib/ncbistd.hpp>
42 
43 #include <gui/utils/vect2.hpp>
44 #include <gui/utils/vect3.hpp>
45 
47 
48 // BBox represents a simple, rectangular bounding box
49 //
50 template <int N>
51 class CBBox
52 {
53 public:
54  enum { X, Y, Z };
55 
57  : m_IsSet(false)
58  {
59  for (unsigned int i = 0; i < N; ++i) {
60  lower[i] = upper[i] = 0.0f;
61  }
62  }
63 
64 
65  virtual ~CBBox()
66  {
67  }
68 
69 
70  void Add(const CBBox<N>& bbox)
71  {
72  Add(bbox.lower);
73  Add(bbox.upper);
74  }
75 
76  void Add(const CVect2<float>& pt)
77  {
78  unsigned int dim = 2;
79  if (N < 2)
80  dim = N;
81 
82  if (!m_IsSet) {
83  for (unsigned int i = 0; i < dim; ++i) {
84  lower[i] = upper[i] = pt[i];
85  }
86  m_IsSet = true;
87  }
88  else {
89  for (unsigned int i = 0; i < dim; ++i) {
90  if (pt[i] < lower[i]) {
91  lower[i] = pt[i];
92  }
93  else if (pt[i] > upper[i]) {
94  upper[i] = pt[i];
95  }
96  }
97  }
98  }
99 
100  void Add(const CVect3<float>& pt)
101  {
102  unsigned int dim = 3;
103  if (N < 3)
104  dim = N;
105 
106  if (!m_IsSet) {
107  for (unsigned int i = 0; i < dim; ++i) {
108  lower[i] = upper[i] = pt[i];
109  }
110  m_IsSet = true;
111  }
112  else {
113  for (unsigned int i = 0; i < dim; ++i) {
114  if (pt[i] < lower[i]) {
115  lower[i] = pt[i];
116  }
117  else if (pt[i] > upper[i]) {
118  upper[i] = pt[i];
119  }
120  }
121  }
122  }
123 
124 
125  //
126  // NumSets is the number of sets of points
127  // Stride is the distance between the start of one set and the next
128  //
129  void Add(const float vals[N], int num_sets = 1, int stride = 0)
130  {
131  unsigned int offset = 0;
132 
133  while (num_sets-- > 0) {
134  if (!m_IsSet) {
135  for (unsigned int i = 0; i < N; ++i) {
136  unsigned int idx = i + offset;
137  lower[i] = upper[i] = vals[idx];
138  }
139  m_IsSet = true;
140  }
141  else {
142  for (unsigned int i = 0; i < N; ++i) {
143  unsigned int idx = i + offset;
144  if (vals[idx] < lower[i]) {
145  lower[i] = vals[idx];
146  }
147  else if (vals[idx] > upper[i]) {
148  upper[i] = vals[idx];
149  }
150  }
151  }
152 
153  offset += stride;
154  }
155  }
156 
157 
158  pair<float, float> GetNthRange(unsigned int n) const
159  {
160  return make_pair(lower[n], upper[n]);
161  }
162 
163 
164  virtual void PrintTo(CNcbiOstream& strm) const
165  {
166  strm << '[';
167 
168  for (unsigned int i = 0; i < N; ++i) {
169  if (i > 0) {
170  strm << ", ";
171  }
172  strm << '(' << lower[i] << ", " << upper[i] << ')';
173  }
174  strm << ']';
175  }
176 
177 
178 private:
179  bool m_IsSet;
180  float lower[N], upper[N];
181 };
182 
183 
185 {
186  bb.PrintTo(strm);
187  return strm;
188 }
189 
190 
192 
193 /* @} */
194 
195 #endif // GUI_UTILS__BBOX_HPP
Definition: bbox.hpp:52
Include a standard set of the NCBI C++ Toolkit most basic headers.
static ulg bb
#define false
Definition: bool.h:36
int offset
Definition: replacements.h:160
virtual ~CBBox()
Definition: bbox.hpp:65
float upper[N]
Definition: bbox.hpp:180
CBBox()
Definition: bbox.hpp:56
void Add(const CBBox< N > &bbox)
Definition: bbox.hpp:70
void Add(const CVect2< float > &pt)
Definition: bbox.hpp:76
void Add(const CVect3< float > &pt)
Definition: bbox.hpp:100
CNcbiOstream & operator<<(CNcbiOstream &strm, const CBBox< 3 > &bb)
Definition: bbox.hpp:184
void Add(const float vals[N], int num_sets=1, int stride=0)
Definition: bbox.hpp:129
virtual void PrintTo(CNcbiOstream &strm) const
Definition: bbox.hpp:164
bool m_IsSet
Definition: bbox.hpp:179
float lower[N]
Definition: bbox.hpp:180
pair< float, float > GetNthRange(unsigned int n) const
Definition: bbox.hpp:158
@ X
Definition: bbox.hpp:54
@ Y
Definition: bbox.hpp:54
@ Z
Definition: bbox.hpp:54
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
int i
yy_size_t n
#define N
Definition: crc32.c:57
Modified on Fri Sep 20 14:57:02 2024 by modify_doxy.py rev. 669887