BoundBox.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 // Original Author: Paul Henning //
7 // TSM, ASCI Problem Setup Team //
8 // Applied Physics Division //
9 // Los Alamos National Laboratory //
10 // phenning@lanl.gov //
11 // //
12 // Modified for GK by: Brian Jean //
13 // TSM, ASCI Problem Setup Team //
14 // Applied Physics Division //
15 // Los Alamos National Laboratory //
16 // 505.665.6374 //
17 // baj@lanl.gov //
18 // //
19 // Modified for Portage by: Rao Garimella //
20 // rao@lanl.gov //
22 
23 #ifndef PORTAGE_SEARCH_BOUNDBOX_H_
24 #define PORTAGE_SEARCH_BOUNDBOX_H_
25 
26 #include <iostream>
27 #include <cassert>
28 #include <limits>
29 
30 #include "wonton/support/Point.h"
31 #include "wonton/support/Vector.h"
33 
34 namespace Portage {
35  using Wonton::Point;
36  using Wonton::Vector;
37 
38  const double real_max = std::numeric_limits<double>::max();
39  const double real_min = std::numeric_limits<double>::min();
40 
46  template<int D> class IsotheticBBox
47  {
48  public:
49  IsotheticBBox() { clearBox(); }
50 
52  void clear() { clearBox(); }
53 
55  bool empty() const { return min[0] == real_max; }
56 
57  // Functions to add new elements to the box
59  void add(const Point<D>& p) {
60  if(empty()) {
61  min = p; max = p;
62  }
63  else {
64  for(int i = 0; i < D; i++) {
65  if(p[i] < min[i]) min[i] = p[i];
66  else if(p[i] > max[i]) max[i] = p[i];
67  }
68  }
69  }
70 
72  void add(const IsotheticBBox<D>& box) {
73  add(box.getMin());
74  add(box.getMax());
75  }
76 
78  void bulge(double epsilon) {
79  for(int i = 0; i < D; i++) {
80  max[i] += epsilon;
81  min[i] -= epsilon;
82  }
83  }
84 
85  // Calculate box feature points
87  Point<D> center() const { return Point<D>((min.asV() + max.asV())/2); }
88 
90  double center(int axis) const { return (min[axis] + max[axis])/2; }
91 
93  double radius(bool doSqrt = true) const {
94  const Vector<D> r((max-min)/2);
95  return r.norm(doSqrt);
96  }
97 
99  const Point<D>& getMin() const { return min; }
100 
102  double getMin(int i) const { assert(i < D); return min[i]; }
103 
105  const Point<D>& getMax() const { return max; }
106 
108  double getMax(int i) const { assert(i < D); return max[i]; }
109 
111  int longAxis() const {
112  // Find a longest axis
113  const Vector<D> diff = max-min;
114  double maxVal = diff[0];
115  int maxIdx = 0;
116  for(int i = 1; i < D; i++)
117  if(diff[i] > maxVal) {
118  maxVal = diff[i];
119  maxIdx = i;
120  }
121  return maxIdx;
122  }
123 
125  double volume() const {
126  double result = 1.0;
127  for(int i = 0; i < D; i++) result *= max[i] - min[i];
128  return result;
129  }
130 
131  // Intersection predicates
133  bool intersect(const Point<D>& p) const {
134  for(int i = 0; i < D; i++)
135  if(p[i] > max[i] || p[i] < min[i]) return false;
136  return true;
137  }
138 
140  bool intersect(const IsotheticBBox<D>& b) const {
141  for(int i = 0; i < D; i++)
142  if(b.max[i] < min[i] || b.min[i] > max[i]) return false;
143  return true;
144  }
145 
146  private:
147  Point<D> min,max;
148  void clearBox() {
149  for (size_t i=0;i<D;++i) {
150  min[i] = real_max;
151  max[i] = real_min;
152  }
153  }
154 
155  };
156 
157 
159 
164  template <int D> bool approxEq(const IsotheticBBox<D>& box1,
165  const IsotheticBBox<D>& box2,
166  const double& tol)
167  {
168  const Point<D>& min1 = box1.getMin();
169  const Point<D>& max1 = box1.getMax();
170  const Point<D>& min2 = box2.getMin();
171  const Point<D>& max2 = box2.getMax();
172 
173  return (approxEq(min1,min2,tol) && approxEq(max1,max2,tol));
174  }
175 
176  template <int D> bool interval(const IsotheticBBox<D>& box,
177  const Point<D>& orig,
178  const Vector<D>& magdir,
179  double& a, double& b)
180  {
181  a = 0;
182  b = 1;
183 
184  for(int i = 0; i < D; i++) {
185  if(magdir[i] == 0) {
186  if(orig[i] < box.getMin(i) || orig[i] > box.getMax(i)) return false;
187  } else {
188  const double dMin = (box.getMin(i)-orig[i])/magdir[i];
189  const double dMax = (box.getMax(i)-orig[i])/magdir[i];
190  if(dMax < dMin) {
191  if(b < dMax || a > dMin) return false;
192  if(a < dMax) a = dMax;
193  if(b > dMin) b = dMin;
194  } else {
195  if(b < dMin || a > dMax) return false;
196  if(a < dMin) a = dMin;
197  if(b > dMax) b = dMax;
198  }
199  }
200  }
201  return true;
202  }
203 
206 
207 
208  template<int D> inline std::ostream& operator<<(std::ostream& os,
209  const Portage::IsotheticBBox<D>& box)
210  {
211  if(box.empty())
212  os << "(empty)";
213  else
214  os << "min=" << box.getMin() << " max=" << box.getMax();
215  return os;
216  }
217 
218 } // namaespace Portage
219 
220 #endif // PORTAGE_SEARCH_BOUNDBOX_H_
std::ostream & operator<<(std::ostream &os, const Portage::IsotheticBBox< D > &box)
Definition: BoundBox.h:208
void clear()
Re-initialize box limits to default values.
Definition: BoundBox.h:52
const Point< D > & getMax() const
Return the maximum of the bounding box.
Definition: BoundBox.h:105
bool intersect(const Point< D > &p) const
Determine if the Point p is within the bounding box.
Definition: BoundBox.h:133
bool intersect(const IsotheticBBox< D > &b) const
Determine if the IsotheticBBox b is within the bounding box.
Definition: BoundBox.h:140
const double real_min
Definition: BoundBox.h:39
IsotheticBBox< 3 > IsotheticBBox3
Definition: BoundBox.h:205
double getMin(int i) const
Return the minimum of the bonding box along axis i.
Definition: BoundBox.h:102
bool interval(const IsotheticBBox< D > &box, const Point< D > &orig, const Vector< D > &magdir, double &a, double &b)
Definition: BoundBox.h:176
int longAxis() const
Find which axis is the longest of the bounding box.
Definition: BoundBox.h:111
bool empty() const
Tests if the bounding box contains space.
Definition: BoundBox.h:55
double center(int axis) const
Calculate the center aint the axis axis.
Definition: BoundBox.h:90
IsotheticBBox()
Definition: BoundBox.h:49
double volume() const
Calculate the volume of the bounding box.
Definition: BoundBox.h:125
double const epsilon
Numerical tolerance.
Definition: weight.h:34
const double real_max
Definition: BoundBox.h:38
void bulge(double epsilon)
Expand the box by an additive constant.
Definition: BoundBox.h:78
bool approxEq(const IsotheticBBox< D > &box1, const IsotheticBBox< D > &box2, const double &tol)
Determine if two IsotheticBBox elements are coincident in space.
Definition: BoundBox.h:164
double radius(bool doSqrt=true) const
Calculate distance from the origin to the center of the bounding box.
Definition: BoundBox.h:93
const Point< D > & getMin() const
Return the minimum of the bounding box.
Definition: BoundBox.h:99
void add(const Point< D > &p)
Update the bounding box by adding an additional Point.
Definition: BoundBox.h:59
Definition: coredriver.h:42
double getMax(int i) const
Return the maximum of the bounding box along axis i.
Definition: BoundBox.h:108
An isothetic (axis-aligned) N-dimensional bounding box.
Definition: BoundBox.h:46
IsotheticBBox< 2 > IsotheticBBox2
Definition: BoundBox.h:204
void add(const IsotheticBBox< D > &box)
Update the bounding box by adding the extents of another IsotheticBBox.
Definition: BoundBox.h:72
Point< D > center() const
Calculate the Point of the center of the bounding box.
Definition: BoundBox.h:87