DIY  3.0
data-parallel out-of-core C++ library
 All Classes Namespaces Functions Typedefs Groups Pages
pick.hpp
1 #ifndef DIY_PICK_HPP
2 #define DIY_PICK_HPP
3 
4 #include "link.hpp"
5 
6 namespace diy
7 {
8  template<class Bounds, class Point, class OutIter>
9  void near(const RegularLink<Bounds>& link, const Point& p, float r, OutIter out,
10  const Bounds& domain);
11 
12  template<class Bounds, class Point, class OutIter>
13  void in(const RegularLink<Bounds>& link, const Point& p, OutIter out, const Bounds& domain);
14 
15  template<class Point, class Bounds>
16  float distance(int dim, const Bounds& bounds, const Point& p);
17 
18  template<class Bounds>
19  inline
20  float distance(int dim, const Bounds& bounds1, const Bounds& bounds2);
21 
22  template<class Bounds>
23  void wrap_bounds(Bounds& bounds, Direction wrap_dir, const Bounds& domain, int dim);
24 }
25 
27 template<class Bounds, class Point, class OutIter>
28 void
29 diy::
31  const Point& p,
32  float r,
33  OutIter out,
34  const Bounds& domain)
35 {
36  Bounds neigh_bounds; // neighbor block bounds
37 
38  // for all neighbors of this block
39  for (int n = 0; n < link.size(); n++)
40  {
41  // wrap neighbor bounds, if necessary, otherwise bounds will be unchanged
42  neigh_bounds = link.bounds(n);
43  wrap_bounds(neigh_bounds, link.wrap(n), domain, link.dimension());
44 
45  if (distance(link.dimension(), neigh_bounds, p) <= r)
46  *out++ = n;
47  } // for all neighbors
48 }
49 
51 template<class Point, class Bounds>
52 float
53 diy::
54 distance(int dim, const Bounds& bounds, const Point& p)
55 {
56  float res = 0;
57  for (int i = 0; i < dim; ++i)
58  {
59  // avoids all the annoying case logic by finding
60  // diff = max(bounds.min[i] - p[i], 0, p[i] - bounds.max[i])
61  float diff = 0, d;
62 
63  d = bounds.min[i] - p[i];
64  if (d > diff) diff = d;
65  d = p[i] - bounds.max[i];
66  if (d > diff) diff = d;
67 
68  res += diff*diff;
69  }
70  return sqrt(res);
71 }
72 
73 template<class Bounds>
74 float
75 diy::
76 distance(int dim, const Bounds& bounds1, const Bounds& bounds2)
77 {
78  float res = 0;
79  for (int i = 0; i < dim; ++i)
80  {
81  float diff = 0, d;
82 
83  float d1 = bounds1.max[i] - bounds2.min[i];
84  float d2 = bounds2.max[i] - bounds1.min[i];
85 
86  if (d1 > 0 && d2 > 0)
87  diff = 0;
88  else if (d1 <= 0)
89  diff = -d1;
90  else if (d2 <= 0)
91  diff = -d2;
92 
93  res += diff*diff;
94  }
95  return sqrt(res);
96 }
97 
99 template<class Bounds, class Point, class OutIter>
100 void
101 diy::
102 in(const RegularLink<Bounds>& link,
103  const Point& p,
104  OutIter out,
105  const Bounds& domain)
106 {
107  Bounds neigh_bounds; // neighbor block bounds
108 
109  // for all neighbors of this block
110  for (int n = 0; n < link.size(); n++)
111  {
112  // wrap neighbor bounds, if necessary, otherwise bounds will be unchanged
113  neigh_bounds = link.bounds(n);
114  wrap_bounds(neigh_bounds, link.wrap(n), domain, link.dimension());
115 
116  if (distance(link.dimension(), neigh_bounds, p) == 0)
117  *out++ = n;
118  } // for all neighbors
119 }
120 
121 // wraps block bounds
122 // wrap dir is the wrapping direction from original block to wrapped neighbor block
123 // overall domain bounds and dimensionality are also needed
124 template<class Bounds>
125 void
126 diy::
127 wrap_bounds(Bounds& bounds, Direction wrap_dir, const Bounds& domain, int dim)
128 {
129  for (int i = 0; i < dim; ++i)
130  {
131  bounds.min[i] += wrap_dir[i] * (domain.max[i] - domain.min[i]);
132  bounds.max[i] += wrap_dir[i] * (domain.max[i] - domain.min[i]);
133  }
134 }
135 
136 
137 #endif
float distance(int dim, const Bounds &bounds, const Point &p)
Find the distance between point p and box bounds.
Definition: pick.hpp:54
void in(const RegularLink< Bounds > &link, const Point &p, OutIter out, const Bounds &domain)
Finds the neighbor(s) containing the target point.
Definition: pick.hpp:102
Definition: point.hpp:15
Definition: types.hpp:16
void near(const RegularLink< Bounds > &link, const Point &p, float r, OutIter out, const Bounds &domain)
Finds the neighbors within radius r of a target point.
Definition: pick.hpp:30