SheafSystem  0.0.0.0
d_tree_point_locator.impl.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
19 
20 #ifndef D_TREE_POINT_LOCATOR_IMPL_H
21 #define D_TREE_POINT_LOCATOR_IMPL_H
22 
23 #ifndef SHEAF_DLL_SPEC_H
24 #include "SheafSystem/sheaf_dll_spec.h"
25 #endif
26 
27 #ifndef D_TREE_POINT_LOCATOR_H
28 #include "SheafSystem/d_tree_point_locator.h"
29 #endif
30 
31 #ifndef D_BIN_POINT_LOCATOR_IMPL_H
32 #include "SheafSystem/d_bin_point_locator.impl.h"
33 #endif
34 
35 #ifndef D_TREE_POINT_LOCATOR_PATH_H
36 #include "SheafSystem/d_tree_point_locator_path.h"
37 #endif
38 
39 #ifndef STD_UTILITY_H
40 #include "SheafSystem/std_utility.h"
41 #endif
42 
43 #ifndef STD_ALGORITHM_H
44 #include "SheafSystem/std_algorithm.h"
45 #endif
46 
47 //#undef DIAGNOSTIC_OUTPUT
48 //#define DIAGNOSTIC_OUTPUT
49 
50 namespace geometry
51 {
52 
53 // ===========================================================
54 // D_TREE_POINT_LOCATOR FACET
55 // ===========================================================
56 
57 // PUBLIC MEMBER FUNCTIONS
58 
59 template <int DC, int DB>
62  : d_bin_point_locator<DC, DB>::d_bin_point_locator(xcoords)
63 {
64  // Preconditions:
65 
66  require(xcoords.state_is_read_accessible());
67  require(xcoords.schema().df() == DC);
68  require(xdepth <= max_depth());
69 
70  // Body:
71 
72  _depth = xdepth;
73 
74  this->update();
75 
76  // Postconditions:
77 
78 
79  // Exit:
80 
81  return;
82 }
83 
84 template <int DC, int DB>
87  : d_bin_point_locator<DC, DB>::d_bin_point_locator(xcoords)
88 {
89  // Preconditions:
90 
91  require(xcoords.state_is_read_accessible());
92  require(xcoords.schema().df() == DC);
93 
94  // Body:
95 
96  // Set the depth to log base 2^DC of number of evaluation members.
97 
98  double leval_ct = static_cast<double>(xcoords.schema().evaluation_ct());
99  size_type ldepth =
100  static_cast<size_type>((log(leval_ct)/log(static_cast<double>(1<<DC))));
101 
102  // Make depth is at least 1.
103 
104  _depth = ldepth > 1 ? ldepth : 1;
105 
106  this->update();
107 
108  // Postconditions:
109 
110 
111  // Exit:
112 
113  return;
114 }
115 
116 template <int DC, int DB>
119 {
120  // Preconditions:
121 
122  // Body:
123 
124  // Nothing to do.
125 
126  // Postconditions:
127 
128  // Exit:
129 
130  return;
131 }
132 
133 template <int DC, int DB>
136 root() const
137 {
138  // Preconditions:
139 
140  // Body:
141 
143 
144  // Postconditions:
145 
146  ensure(invariant());
147 
148  return result;
149 }
150 
151 template <int DC, int DB>
152 size_type
154 depth() const
155 {
156  size_type result;
157 
158  // Preconditions:
159 
160 
161  // Body:
162 
163  result = _depth;
164 
165  // Postconditions:
166 
167  ensure(result > 0);
168 
169  // Exit:
170 
171  return result;
172 }
173 
174 template <int DC, int DB>
175 size_type
178 {
179  // Preconditions:
180 
181 
182  // Body:
183 
185 
186  // Postconditions:
187 
188  ensure(result > 0);
189 
190  // Exit:
191 
192  return result;
193 }
194 
195 template <int DC, int DB>
196 size_type
199 {
200  size_type result;
201 
202  // Preconditions:
203 
204 
205  // Body:
206 
207  // Root is not in the pool, so size is pool size + 1.
208 
209  result = node_pool().allocated_size() + 1;
210 
211  // Postconditions:
212 
213 
214  // Exit:
215 
216  return result;
217 }
218 
219 template <int DC, int DB>
220 size_type
223 {
224  size_type result;
225 
226  // Preconditions:
227 
228 
229  // Body:
230 
231  // Root is not in the pool, so capacity is pool capacity + 1.
232 
233  result = node_pool().capacity();
234 
235  // Postconditions:
236 
237 
238  // Exit:
239 
240  return result;
241 }
242 
243 
244 
245 template <int DC, int DB>
246 size_type&
249 {
250 
251  // Preconditions:
252 
253 
254  // Body:
255 
256  static size_type result = 5;
257 
258  // Postconditions:
259 
260 
261  // Exit:
262 
263  return result;
264 }
265 
266 
267 // PROTECTED MEMBER FUNCTIONS
268 
269 template <int DC, int DB>
270 void
273 {
274  // Preconditions:
275 
276  require(this->is_empty());
277 
278  // Body:
279 
280  // Choose a reasonable initial capacity for the node pool.
281 
282  node_pool().clear();
283  node_pool().reserve(1024);
284 
285  // Compute the bin size of the smallest leaves in the tree.
286  // All paths have leading bit == 0, so number of bins in
287  // each dimension is pow(2, depth-1) rather than pow(2, depth).
288 
289  sec_vd_value_type bin_ct = pow(2.0, static_cast<sec_vd_value_type>(_depth-1));
290  for(int i=0; i<DC; i++)
291  {
292  this->_bin_ub[i] = static_cast<size_type>(bin_ct);
293  this->_bin_size[i] = (this->_ub[i] - this->_lb[i])/bin_ct;
294  }
295 
296  // Compute the reciprocal of the bin size of the smallest representable leaves.
297  // Used for efficiency in relative_position_pa().
298 
300  for(int i=0; i<DC; i++)
301  {
302  this->_one_over_min_bin_size[i] = lcoord_ub/(this->_ub[i] - this->_lb[i]);
303  }
304 
305  // Postconditions:
306 
307  // Exit:
308 
309  return;
310 }
311 
312 template <int DC, int DB>
316 {
317  // Preconditions:
318 
319  // Body:
320 
322 
323  // Postconditions:
324 
325 
326  // Exit:
327 
328  return result;
329 }
330 
331 
332 // ===========================================================
333 // D_BIN_POINT_LOCATOR FACET
334 // ===========================================================
335 
336 // PUBLIC MEMBER FUNCTIONS
337 
338 template <int DC, int DB>
339 void
342 {
343  // Preconditions:
344 
345  require(xbox != 0);
346 
347  // Body:
348 
349  define_old_variable(size_type old_box_ct = this->_box_ct);
350 
352  _root.insert_box(xbox, lpath);
353  this->_box_ct++;
354 
355  // Postconditions:
356 
357  ensure(contains_box(xbox));
358  ensure(this->box_ct() == old_box_ct + 1);
359 
360  // Exit:
361 
362  return;
363 }
364 
365 template <int DC, int DB>
366 void
369 {
370  // Preconditions:
371 
372  require(xbox != 0);
373  require(contains_box(xbox));
374 
375  // Body:
376 
377  define_old_variable(size_type old_box_ct = this->_box_ct);
378 
380  _root.remove_box(xbox, lpath);
381  this->_box_ct--;
382 
383  // Postconditions:
384 
385  ensure(!contains_box(xbox));
386  ensure(this->box_ct() == old_box_ct - 1);
387 
388  // Exit:
389 
390  return;
391 }
392 
393 template <int DC, int DB>
397 {
398  // Preconditions:
399 
400  require(xpt != 0);
401  require(xpt_ub >= this->dc());
402  require(this->domain_contains(xpt, xpt_ub));
403 
404  // Body:
405 
407  this->relative_position_pa(xpt, xpt_ub, rel_pt);
408 
409  d_tree_point_locator_path<DC, DB> lpath(rel_pt, this);
410  const box_list_type& result = _root.box_list(lpath);
411 
412  // Postconditions:
413 
414 
415  // Exit:
416 
417  return result;
418 }
419 
420 
421 template <int DC, int DB>
422 bool
425 {
426  bool result = true;
427 
428  // Preconditions:
429 
430  require(xbox != 0);
431 
432  // Body:
433 
435  result = _root.contains_box(xbox, lpath);
436 
437  // Postconditions:
438 
439 
440  // Exit:
441 
442  return result;
443 }
444 
445 template <int DC, int DB>
446 void
449 {
450  // Preconditions:
451 
452  // Body:
453 
454  _root.clear();
455  node_pool().clear();
456  this->_box_ct = 0;
457 
458  // Postconditions:
459 
460  ensure(this->is_empty());
461 
462  // Exit:
463 
464  return;
465 }
466 
467 
468 // ===========================================================
469 // POINT_LOCATOR FACET
470 // ===========================================================
471 
472 // PUBLIC MEMBER FUNCTIONS
473 
474 template <int DC, int DB>
475 bool
477 invariant() const
478 {
479  bool result = true;
480 
481  invariance(depth() > 0);
482  invariance(this->coordinates().schema().df() == DC);
483 
484 
485  return result;
486 }
487 
488 
489 //==============================================================================
490 // NON-MEMBER FUNCTIONS
491 //==============================================================================
492 
493 template <int DC, int DB>
494 std::ostream& operator<<(std::ostream& xos, const d_tree_point_locator<DC, DB>& xtree)
495 {
496  // Preconditions:
497 
498  // Body:
499 
500  using namespace std;
501 
502  cout << "lb =";
503  for(int i=0; i< xtree.dc(); i++)
504  {
505  cout << " " << xtree.lb()[i];
506  }
507  cout << endl;
508 
509  cout << "ub =";
510  for(int i=0; i< xtree.dc(); i++)
511  {
512  cout << " " << xtree.ub()[i];
513  }
514  cout << endl;
515 
516  cout << "depth = " << xtree.depth() << endl;
517  cout << "root = " << xtree.root() << endl;
518 
519  // Postconditions:
520 
521  // Exit:
522 
523  return xos;
524 }
525 
526 } // namespace geometry
527 
528 #endif // D_TREE_POINT_LOCATOR_IMPL_H
virtual void update()
Updates the search structure to the current values of coordinates(); synonym for update(true);.
size_type depth() const
The number of levels; the length of the longest path from root() to a leaf.
size_type size()
The number of nodes in the tree.
virtual void remove_box(d_bounding_box< DC, DB > *xbox)
Remove xbox from the tree.
virtual bool invariant() const
Class invariant.
sec_ed & coordinates() const
The coordinate section this inverts.
virtual const box_list_type & box_list(sec_vd_value_type *xpt, size_type xpt_ub) const
The list of bounding boxes which may contain xpt.
block< sec_vd_value_type > _one_over_min_bin_size
Reciprocal of the dimensions of the smallest bins.
size_type capacity()
The nodes allocated for use by the tree.
bool state_is_read_accessible() const
True if this is attached and if the state is accessible for read or access control is disabled...
bool is_empty() const
True if this contains no bounding boxes.
A reallocatable pool of objects of type T. Objects in the pool are either "allocated" or linked toget...
STL namespace.
d_tree_point_locator_node< DC, DB > _root
The root node of the tree.
static size_type leftmost_bit_id()
The index of the leftmost bit.
static size_type & default_depth()
The default depth; the depth of a instance created with the default constructor.
Fixed point relative coordinates for a tree domain.
static int_type ub()
The upper bound for components.
ptr_linked_pool< d_tree_point_locator_node< DC, DB > > & node_pool()
Pool for efficiently allocating and deallocating nodes.
A section of a fiber bundle with a d-dimensional Euclidean vector space fiber.
Definition: sec_ed.h:47
static size_type max_depth()
The maximum representable number of levels.
block< sec_vd_value_type > _lb
The lower bound of the domain.
block< size_type > _bin_ub
The upper bound for the bin index.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
A bounding box that can be strung together into a list.
void update_bins()
Updates the tree bin parameters.
size_type box_ct() const
The number of bounding boxes stored in the search structure.
SHEAF_DLL_SPEC void log(const sec_at0 &x0, sec_at0 &xresult, bool xauto_access)
Compute log of x0 (log(x0)) (pre-allocated version).
Definition: sec_at0.cc:1434
d_tree_point_locator()
Default constructor; disabled.
size_type _depth
The number of levels in the tree; the length of the longest path from the root to a leaf...
ptr_linked_pool< d_tree_point_locator_node< DC, DB > > _node_pool
Pool for efficiently allocating and deallocating nodes.
A path in an d_tree_point_locator search structure.
virtual void insert_box(d_bounding_box< DC, DB > *xbox)
Insert xbox into the tree.
void relative_position_pa(sec_vd_value_type *xpt, size_type xpt_ub, d_bin_coordinates< DC, DB > &xresult) const
The position of xpt relative to the lower bound, in integer coordinates; pre-allocated version...
size_type _box_ct
The number of bounding boxes stored in the search structure.
virtual section_space_schema_member & schema()
The restricted schema for this (mutable version).
virtual void clear()
Clear the tree of all bounding boxes and all nodes except the root.
int dc() const
The spatial dimension of the domain; the dimension of the global coordinates.
int evaluation_ct() const
The number of members in the intersection of the evaluation subposet and the down set of the base spa...
const d_tree_point_locator_node< DC, DB > & root() const
The root node.
block< sec_vd_value_type > _ub
The upper bound of the domain.
int df() const
The dimension of the fiber space component.
virtual bool contains_box(d_bounding_box< DC, DB > *xbox) const
True if xbox is in the box list of some bin.
A node in a d_tree_point_locator search structure.
Namespace for geometry component of sheaf system.
Definition: field_vd.h:54
SHEAF_DLL_SPEC void pow(const sec_at0 &x0, const vd_value_type &xexponent, sec_at0 &xresult, bool xauto_access)
Compute x0 to power xexponent (pow(x0, xexponent)) (pre-allocated version).
Definition: sec_at0.cc:1495
vd_value_type sec_vd_value_type
The type of component in the value of a section at a point.
Definition: fiber_bundle.h:73
bool domain_contains(sec_vd_value_type *xpt, size_type xpt_ub) const
True if the domain contains xpt.
An abstract point location query in domains with global coordinate dimension dc and local coordinate ...
block< sec_vd_value_type > _bin_size
The dimensions of the smallest bins.
Wrapper class for forward_list or slist depending on compiler. The class replicates the minimum subse...