SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SearchGridT.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 /// @todo Documentation Core/GeometryPrimitives/SearchGridT.h
30 
31 #ifndef CORE_DATATYPES_SEARCHGRIDT_H
32 #define CORE_DATATYPES_SEARCHGRIDT_H 1
33 
38 
39 #include <algorithm>
40 #include <vector>
41 
43 
44 namespace SCIRun {
45 
46 template<class INDEX>
48 {
49  public:
50  /// Include the types defined in Types into this class
53  typedef typename std::vector<INDEX>::iterator iterator;
54 
56  const Core::Geometry::Point &min, const Core::Geometry::Point &max) :
57  ni_(x), nj_(y), nk_(z)
58  {
59  transform_.pre_scale(Core::Geometry::Vector(1.0 / x, 1.0 / y, 1.0 / z));
60  transform_.pre_scale(max - min);
61 
62  transform_.pre_translate(Core::Geometry::Vector(min));
63  transform_.compute_imat();
64  bin_.resize(x*y*z);
65  }
66 
67  inline void transform(const Core::Geometry::Transform &t)
68  { transform_.pre_trans(t);}
69 
71  { return transform_; }
72 
74  { transform_ = trans; return transform_; }
75 
76 
77  /// Get the size of the search grid
78  inline size_type get_ni() const { return ni_; }
79  inline size_type get_nj() const { return nj_; }
80  inline size_type get_nk() const { return nk_; }
81 
82  inline bool locate(index_type &i, index_type &j,
83  index_type &k, const Core::Geometry::Point &p) const
84  {
85  const Core::Geometry::Point r = transform_.unproject(p);
86 
87  const double rx = floor(r.x());
88  const double ry = floor(r.y());
89  const double rz = floor(r.z());
90 
91  // Clamp in double space to avoid overflow errors.
92  if (rx < 0.0 || ry < 0.0 || rz < 0.0 ||
93  rx >= ni_ || ry >= nj_ || rz >= nk_ )
94  {
95  return (false);
96  }
97 
98  i = static_cast<index_type>(rx);
99  j = static_cast<index_type>(ry);
100  k = static_cast<index_type>(rz);
101  return (true);
102  }
103 
104 
105  inline bool locate_clamp(index_type &i, index_type &j,
106  index_type &k, const Core::Geometry::Point &p) const
107  {
108  const Core::Geometry::Point r = transform_.unproject(p);
109 
110  double rx = floor(r.x());
111  double ry = floor(r.y());
112  double rz = floor(r.z());
113 
114  // Clamp in double space to avoid overflow errors.
115  if (rx < 0.0) rx = 0.0;
116  if (ry < 0.0) ry = 0.0;
117  if (rz < 0.0) rz = 0.0;
118 
119  if (rx >= ni_) rx = ni_;
120  if (ry >= nj_) ry = nj_;
121  if (rz >= nk_) rz = nk_;
122 
123  i = static_cast<index_type>(rx);
124  j = static_cast<index_type>(ry);
125  k = static_cast<index_type>(rz);
126  return (true);
127  }
128 
129  inline void unsafe_locate(index_type &i, index_type &j,
130  index_type &k, const Core::Geometry::Point &p) const
131  {
133  transform_.unproject(p, r);
134 
135  i = static_cast<index_type>(r.x());
136  j = static_cast<index_type>(r.y());
137  k = static_cast<index_type>(r.z());
138  }
139 
140  void insert(INDEX val, const Core::Geometry::BBox &bbox)
141  {
142  index_type mini=0, minj=0, mink=0, maxi=0, maxj=0, maxk=0;
143 
144  locate(mini, minj, mink, bbox.min());
145  locate(maxi, maxj, maxk, bbox.max());
146 
147  for (index_type i = mini; i <= maxi; i++)
148  {
149  for (index_type j = minj; j <= maxj; j++)
150  {
151  for (index_type k = mink; k <= maxk; k++)
152  {
153  bin_[linearize(i, j, k)].push_back(val);
154  }
155  }
156  }
157  }
158 
159  void remove(INDEX val, const Core::Geometry::BBox &bbox)
160  {
161  index_type mini, minj, mink, maxi, maxj, maxk;
162 
163  unsafe_locate(mini, minj, mink, bbox.min());
164  unsafe_locate(maxi, maxj, maxk, bbox.max());
165 
166  for (index_type i = mini; i <= maxi; i++)
167  {
168  for (index_type j = minj; j <= maxj; j++)
169  {
170  for (index_type k = mink; k <= maxk; k++)
171  {
172  index_type q = linearize(i, j, k);
173  std::remove(bin_[q].begin(),bin_[q].end(),val);
174  }
175  }
176  }
177  }
178 
179  void insert(INDEX val, const Core::Geometry::Point &point)
180  {
181  index_type i, j, k;
182  unsafe_locate(i, j, k, point);
183  bin_[linearize(i, j, k)].push_back(val);
184  }
185 
186  void remove(INDEX val, const Core::Geometry::Point &point)
187  {
188  index_type i, j, k;
189  unsafe_locate(i, j, k, point);
190  index_type q = linearize(i, j, k);
191  std::remove(bin_[q].begin(),bin_[q].end(),val);
192  }
193 
194  inline bool lookup(iterator &begin, iterator &end, const Core::Geometry::Point &p)
195  {
196  index_type i, j, k;
197  if (locate(i, j, k, p))
198  {
199  index_type q = linearize(i, j, k);
200  begin = bin_[q].begin();
201  end = bin_[q].end();
202  return (true);
203  }
204  return (false);
205  }
206 
207  inline void lookup_ijk(iterator &begin, iterator &end, size_type i, size_type j,
208  size_type k)
209  {
210  index_type q = linearize(i, j, k);
211  begin = bin_[q].begin();
212  end = bin_[q].end();
213  }
214 
215 
217  size_type j, size_type k) const
218  {
220  transform_.unproject(p, r);
221 
222  // Splat the point onto the cell.
223  if (r.x() < i) { r.x(i); }
224  else if (r.x() > i+1) { r.x(i+1); }
225 
226  if (r.y() < j) { r.y(j); }
227  else if (r.y() > j+1) { r.y(j+1); }
228 
229  if (r.z() < k) { r.z(k); }
230  else if (r.z() > k+1) { r.z(k+1); }
231 
232  // Project the cell intersection back to world space.
234  transform_.project(r, q);
235 
236  // Return distance from point to projected cell point.
237  return (p - q).length2();
238  }
239 
240  private:
241  index_type linearize(index_type i, index_type j, index_type k) const
242  { return (((i * nj_) + j) * nk_ + k); }
243 
244 
245  private:
246  /// Size of the search grid
247  index_type ni_, nj_, nk_;
248  /// Transformation to unitary coordinate system
249  Core::Geometry::Transform transform_;
250 
251  /// Where to store the lookup table
252  std::vector<std::vector<INDEX> > bin_;
253 };
254 
255 
256 } // namespace SCIRun
257 
258 #endif
void insert(INDEX val, const Core::Geometry::BBox &bbox)
Definition: SearchGridT.h:140
Core::Geometry::Transform & set_transform(const Core::Geometry::Transform &trans)
Definition: SearchGridT.h:73
Vector project(const Vector &p) const
Definition: Transform.cc:425
size_type get_nj() const
Definition: SearchGridT.h:79
Point max() const
Definition: BBox.h:195
void pre_scale(const Vector &)
Definition: Transform.cc:193
Point unproject(const Point &p) const
Definition: Transform.cc:483
void transform(const Core::Geometry::Transform &t)
Definition: SearchGridT.h:67
void y(const double)
Definition: Point.h:135
void lookup_ijk(iterator &begin, iterator &end, size_type i, size_type j, size_type k)
Definition: SearchGridT.h:207
Definition: Point.h:49
const Core::Geometry::Transform & get_transform() const
Definition: SearchGridT.h:70
Definition: BBox.h:46
void compute_imat() const
Definition: Transform.cc:687
SCIRun::index_type index_type
Include the types defined in Types into this class.
Definition: SearchGridT.h:51
Definition: Vector.h:63
void z(const double)
Definition: Point.h:145
double length2() const
Definition: Vector.h:248
Definition: ParallelLinearAlgebraTests.cc:358
Definition: SearchGridT.h:47
double min_distance_squared(const Core::Geometry::Point &p, size_type i, size_type j, size_type k) const
Definition: SearchGridT.h:216
void x(const double)
Definition: Point.h:125
SearchGridT(size_type x, size_type y, size_type z, const Core::Geometry::Point &min, const Core::Geometry::Point &max)
Definition: SearchGridT.h:55
long long size_type
Definition: Types.h:40
void pre_trans(const Transform &)
Definition: Transform.cc:150
bool lookup(iterator &begin, iterator &end, const Core::Geometry::Point &p)
Definition: SearchGridT.h:194
void unsafe_locate(index_type &i, index_type &j, index_type &k, const Core::Geometry::Point &p) const
Definition: SearchGridT.h:129
std::vector< INDEX >::iterator iterator
Definition: SearchGridT.h:53
Point min() const
Definition: BBox.h:192
Definition: Transform.h:53
SCIRun::size_type size_type
Definition: SearchGridT.h:52
void pre_translate(const Vector &)
Definition: Transform.cc:278
void insert(INDEX val, const Core::Geometry::Point &point)
Definition: SearchGridT.h:179
long long index_type
Definition: Types.h:39
bool locate_clamp(index_type &i, index_type &j, index_type &k, const Core::Geometry::Point &p) const
Definition: SearchGridT.h:105
bool locate(index_type &i, index_type &j, index_type &k, const Core::Geometry::Point &p) const
Definition: SearchGridT.h:82
size_type get_ni() const
Get the size of the search grid.
Definition: SearchGridT.h:78
size_type get_nk() const
Definition: SearchGridT.h:80