simple_intersect_for_tests.h
Go to the documentation of this file.
1 /*
2 This file is part of the Ristra portage project.
3 Please see the license file at the root of this repository, or at:
4  https://github.com/laristra/portage/blob/master/LICENSE
5 */
6 
7 #include <vector>
9 #include "wonton/support/Point.h"
10 
11 
12 namespace BOX_INTERSECT {
13 
14 template <int D>
15 void bounding_box(std::vector<Wonton::Point<D>> coords,
16  Wonton::Point<D> *pmin, Wonton::Point<D> *pmax) {
17  *pmin = coords[0];
18  *pmax = coords[0];
19  for (auto pcoord : coords) {
20  for (int d = 0; d < D; d++) {
21  if (pcoord[d] < (*pmin)[d])
22  (*pmin)[d] = pcoord[d];
23  if (pcoord[d] > (*pmax)[d])
24  (*pmax)[d] = pcoord[d];
25  }
26  }
27 }
28 
29 template <int D>
30 bool intersect_boxes(Wonton::Point<D> min1, Wonton::Point<D> max1,
31  Wonton::Point<D> min2, Wonton::Point<D> max2,
32  std::vector<double> *xsect_moments) {
33 
34  Wonton::Point<D> intmin, intmax;
35 
36  for (int d = 0; d < D; ++d) {
37  // check for non-intersection in this dimension
38 
39  if (min1[d] > max2[d]) return false;
40  if (min2[d] > max1[d]) return false;
41 
42  // sort the min max vals in this dimension
43  double val[4];
44  val[0] = min1[d]; val[1] = max1[d]; val[2] = min2[d]; val[3] = max2[d];
45  for (int i = 0; i < 3; i++)
46  for (int j = i+1; j < 4; j++)
47  if (val[i] > val[j]) {
48  double tmp = val[i];
49  val[i] = val[j];
50  val[j] = tmp;
51  }
52 
53  // pick the middle two as the min max coordinates of intersection
54  // box in this dimension
55 
56  intmin[d] = val[1]; intmax[d] = val[2];
57  }
58 
59  // Calculate the volume
60 
61  double vol = 1.0;
62  for (int d = 0; d < D; d++)
63  vol *= intmax[d]-intmin[d];
64 
65  // Sanity check
66 
67  assert(vol >= 0.0);
68 
69  if (vol == 0.0) return false;
70 
71  // Calculate the centroid
72 
73  Wonton::Point<D> centroid = (intmin+intmax)/2.0;
74 
75  // moments
76 
77  xsect_moments->clear();
78  xsect_moments->push_back(vol);
79  for (int d = 0; d < D; d++)
80  xsect_moments->push_back(centroid[d]*vol);
81 
82  return true;
83 }
84 
85 template <int D>
86 void intersection_moments(std::vector<Wonton::Point<D>> cell_xyz,
87  std::vector<std::vector<Wonton::Point<D>>> candidate_cells_xyz,
88  std::vector<int> *xcells,
89  std::vector<std::vector<double>> *xwts) {
90 
91  int num_candidates = candidate_cells_xyz.size();
92 
93  xwts->clear();
94 
95  Wonton::Point<D> cmin, cmax;
96  bounding_box<D>(cell_xyz, &cmin, &cmax);
97 
98  for (int c = 0; c < num_candidates; ++c) {
99  Wonton::Point<D> cmin2, cmax2;
100  bounding_box<D>(candidate_cells_xyz[c], &cmin2, &cmax2);
101 
102  std::vector<double> xsect_moments;
103  if (intersect_boxes<D>(cmin, cmax, cmin2, cmax2, &xsect_moments)) {
104  xwts->push_back(xsect_moments);
105  xcells->push_back(c);
106  }
107  }
108 }
109 
110 
111 } // namespace BOX_INTERSECT
112 
bool intersect_boxes(Wonton::Point< D > min1, Wonton::Point< D > max1, Wonton::Point< D > min2, Wonton::Point< D > max2, std::vector< double > *xsect_moments)
Definition: simple_intersect_for_tests.h:30
Definition: simple_intersect_for_tests.h:12
void intersection_moments(std::vector< Wonton::Point< D >> cell_xyz, std::vector< std::vector< Wonton::Point< D >>> candidate_cells_xyz, std::vector< int > *xcells, std::vector< std::vector< double >> *xwts)
Definition: simple_intersect_for_tests.h:86
std::vector< T > vector
Definition: portage.h:238
Wonton::Point< OP::dim > centroid(const typename OP::points_t &apts)
Definition: operator.h:616
void bounding_box(std::vector< Wonton::Point< D >> coords, Wonton::Point< D > *pmin, Wonton::Point< D > *pmax)
Definition: simple_intersect_for_tests.h:15