absgeo.cxx
Go to the documentation of this file.
1 /* absgeo.cxx: This is the abstract geometry file which contains functions
2  * that are fully divorced from cells and planes. */
3 
4 #include <vector>
5 #include <cfloat>
6 #include <math.h>
7 
8 // Return the distance from a point to a line segment if the point on
9 // the segment closest to the point is not one of the ends. Otherwise
10 // return FLT_MAX.
11 static float point_to_line(const int x, const int y,
12  const std::pair<int, int> & p1,
13  const std::pair<int, int> & p2)
14 {
15  std::pair<float, float> eten;
16  {
17  std::pair<float, float> ete;
18  ete.first = p1.first - p2.first;
19  ete.second = p1.second - p2.second;
20 
21  eten.first = ete.first /sqrt(ete.first*ete.first + ete.second*ete.second);
22  eten.second = ete.second/sqrt(ete.first*ete.first + ete.second*ete.second);
23  }
24 
25  std::pair<float, float> etp;
26  etp.first = p1.first - x;
27  etp.second = p1.second - y;
28 
29  const double detca = fabs(eten.first*etp.first + eten.second*etp.second);
30 
31  std::pair<float, float> closest_app;
32 
33  closest_app.first = p1.first - eten.first *detca;
34  closest_app.second = p1.second - eten.second*detca;
35 
36  const bool between =
37  (p1.first < closest_app.first && closest_app.first < p2.first) ||
38  (p1.first > closest_app.first && closest_app.first > p2.first);
39 
40  if(between) return sqrt(pow(closest_app.first - x, 2) +
41  pow(closest_app.second - y, 2));
42  else return FLT_MAX;
43 }
44 
45 static float min(const float a, const float b, const float c)
46 {
47  if(a < b && a < c) return a;
48  if(b < a && b < c) return b;
49  return c;
50 }
51 
52 static float point_to_line_segment(const int x, const int y,
53  const std::pair<int, int> & p1,
54  const std::pair<int, int> & p2)
55 {
56  const float dist_to_inf_line = point_to_line(x, y, p1, p2);
57  const float dist_to_p1 = sqrt(pow(x - p1.first, 2) + pow(y - p1.second, 2));
58  const float dist_to_p2 = sqrt(pow(x - p2.first, 2) + pow(y - p2.second, 2));
59  return min(dist_to_inf_line, dist_to_p1, dist_to_p2);
60 }
61 
62 float screen_dist_to_track(const int x, const int y,
63  const std::vector< std::pair<int, int> > & track)
64 {
65  float mindist = FLT_MAX;
66  for(unsigned int i = 0; i < track.size()-1; i++){
67  const float dist = point_to_line_segment(x, y, track[i], track[i+1]);
68  if(dist < mindist) mindist = dist;
69  }
70  return mindist;
71 }
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
T sqrt(T number)
Definition: d0nt_math.hpp:156
constexpr T pow(T x)
Definition: pow.h:75
Definition: event.h:19
double dist
Definition: runWimpSim.h:113
const double a
static float point_to_line(const int x, const int y, const std::pair< int, int > &p1, const std::pair< int, int > &p2)
Definition: absgeo.cxx:11
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
static float point_to_line_segment(const int x, const int y, const std::pair< int, int > &p1, const std::pair< int, int > &p2)
Definition: absgeo.cxx:52
const hit & b
Definition: hits.cxx:21
float screen_dist_to_track(const int x, const int y, const std::vector< std::pair< int, int > > &track)
Definition: absgeo.cxx:62