Intersection.cxx
Go to the documentation of this file.
2 
3 #include <set>
4 using namespace std;
5 
6 using namespace WireCell;
7 
8 int WireCell::hit_square(int axis0, const Ray& bounds,
9  const Point& point, const Vector& dir,
10  Ray& hits)
11 {
12  const Point& bmin = bounds.first;
13  const Point& bmax = bounds.second;
14 
15  double hit1[3] = {0}, hit2[3] = {0};
16 
17  int hitmask = 0;
18  if (0 == dir[axis0]) {
19  return hitmask;
20  }
21 
22  int axis1 = (axis0 + 1)%3;
23  int axis2 = (axis1 + 1)%3;
24 
25  { // toward the min intercept
26  double intercept = bmin[axis0];
27  double scale = (intercept - point[axis0])/dir[axis0];
28 
29  double one = point[axis1] + scale*dir[axis1];
30  double two = point[axis2] + scale*dir[axis2];
31 
32  if (bmin[axis1] <= one && one <= bmax[axis1] &&
33  bmin[axis2] <= two && two <= bmax[axis2]) {
34  hitmask |= 1;
35  hit1[axis0] = intercept;
36  hit1[axis1] = one;
37  hit1[axis2] = two;
38  }
39  }
40 
41  { // toward the max intercept
42  double intercept = bmax[axis0];
43  double scale = (intercept - point[axis0])/dir[axis0];
44 
45  double one = point[axis1] + scale*dir[axis1];
46  double two = point[axis2] + scale*dir[axis2];
47 
48  if (bmin[axis1] <= one && one <= bmax[axis1] &&
49  bmin[axis2] <= two && two <= bmax[axis2]) {
50  hitmask |= 2;
51  hit2[axis0] = intercept;
52  hit2[axis1] = one;
53  hit2[axis2] = two;
54  }
55  }
56 
57  hits = Ray(Point(hit1), Point(hit2));
58  return hitmask;
59 }
60 
61 int WireCell::box_intersection(const Ray& bounds, const Ray& ray, Ray& hits)
62 {
63  PointSet results;
64  const Point& point = ray.first;
65  const Point dir = (ray.second - ray.first).norm();
66 
67  // check each projection
68  for (int axis=0; axis<3; ++axis) {
69  Ray res;
70  int got = hit_square(axis, bounds, point, dir, res);
71 
72  if (got&1) {
73  //pair<PointSet::iterator, bool> what =
74  results.insert(res.first);
75  }
76  if (got&2) {
77  //pair<PointSet::iterator, bool> what =
78  results.insert(res.second);
79  }
80  }
81 
82  if (results.size() > 2) {
83  return -1;
84  }
85 
86  int hitmask = 0;
87  for (auto hitit = results.begin(); hitit != results.end(); ++hitit) {
88  const Point& hit = *hitit;
89  Vector hitdir = hit - point;
90  double dot = hitdir.norm().dot(dir);
91 
92  if (dot > 0) { // actually should be closer to +/-1 w/in tolerance
93  hits.first = hit;
94  hitmask |= 1;
95  }
96  else {
97  hits.second = hit;
98  hitmask |= 2;
99  }
100  }
101 
102  return hitmask;
103 }
104 
std::pair< Point, Point > Ray
A line segment running from a first (tail) to a second (head) point.
Definition: Point.h:21
STL namespace.
string dir
int hit_square(int axis0, const Ray &bounds, const Point &point, const Vector &dir, Ray &hits)
Definition: Intersection.cxx:8
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
Definition: DCEL.h:34
D3Vector norm() const
Return a normalized vector in the direction of this vector.
Definition: D3Vector.h:91
std::set< Point, ComparePoints > PointSet
Definition: Point.h:46
BoundingBox bounds(int x, int y, int w, int h)
Definition: main.cpp:37
auto norm(Vector const &v)
Return norm of the specified vector.
Detector simulation of raw signals on wires.
Definition: Main.h:22
void scale(Sequence< Val > &seq, Val scalar)
Scale (multiply) sequence values by scalar.
Definition: Waveform.h:146
T dot(const D3Vector &rhs) const
Return the dot product of this vector and the other.
Definition: D3Vector.h:80
int box_intersection(const Ray &bounds, const Ray &ray, Ray &hits)