LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GeoTrajectory.cxx
Go to the documentation of this file.
3 
4 #include <sstream>
5 
6 namespace geoalgo {
7 
8  Trajectory::Trajectory(size_t npoints, size_t ndimension)
9  : std::vector<geoalgo::Point_t>(npoints, Point_t(ndimension))
10  {}
11 
12  Trajectory::Trajectory(const std::vector<std::vector<double>>& obj)
13  {
14  this->reserve(obj.size());
15  for (auto const& p : obj)
16  this->push_back(Point_t(p));
17  }
18 
19  Trajectory::Trajectory(const std::vector<geoalgo::Point_t>& obj)
20  {
21  this->reserve(obj.size());
22  for (auto const& p : obj)
23  this->push_back(p);
24  }
25 
26  double Trajectory::Length(size_t start_step, size_t end_step) const
27  {
28 
29  if (end_step == 0)
30  end_step = size() - 1; // By default end_step is 0. Then consider the whole trajectory()
31 
32  // Sanity checks
33  if (start_step >= end_step) throw GeoAlgoException("Cannot have start step >= end step!");
34 
35  if (end_step >= size()) throw GeoAlgoException("Requested step index bigger than size!");
36 
37  // if length < 2, no length
38  if (size() < 2) return 0;
39 
40  double length = 0;
41  for (size_t i = start_step; i < end_step; ++i)
42 
43  length += (*this)[i]._Dist_((*this)[i + 1]);
44 
45  return length;
46  }
47 
48  bool Trajectory::IsLonger(double ref) const
49  {
50 
51  if (size() < 2) return false;
52 
53  double length = 0;
54  for (size_t i = 0; i < size() - 1; ++i) {
55 
56  length += (*this)[i]._Dist_((*this)[i + 1]);
57 
58  if (length > ref) return true;
59  }
60 
61  return false;
62  }
63 
64  void Trajectory::push_back(const Point_t& obj)
65  {
66  compat(obj);
67  if (!(size() && obj == (*rbegin()))) std::vector<geoalgo::Point_t>::push_back(obj);
68  }
69 
70  void Trajectory::compat(const Point_t& obj) const
71  {
72 
73  if (!size()) return;
74  if ((*(this->begin())).size() != obj.size()) {
75 
76  std::ostringstream msg;
77  msg << "<<" << __FUNCTION__ << ">>"
78  << " size mismatch: " << (*(this->begin())).size() << " != " << obj.size() << std::endl;
79  throw GeoAlgoException(msg.str());
80  }
81  }
82 
83  void Trajectory::compat(const Trajectory& obj) const
84  {
85 
86  if (!size() || !(obj.size())) return;
87 
88  if ((*(this->begin())).size() != (*obj.begin()).size()) {
89 
90  std::ostringstream msg;
91  msg << "<<" << __FUNCTION__ << ">>"
92  << " size mismatch: " << (*(this->begin())).size() << " != " << (*obj.begin()).size()
93  << std::endl;
94  throw GeoAlgoException(msg.str());
95  }
96  }
97 
98  Vector Trajectory::Dir(size_t i) const
99  {
100 
101  if (size() < (i + 2)) {
102  std::ostringstream msg;
103  msg << "<<" << __FUNCTION__ << ">>"
104  << " length=" << size() << " is too short to find a direction @ index=" << i << std::endl;
105  throw GeoAlgoException(msg.str());
106  }
107  return _Dir_(i);
108  }
109 
110  Vector Trajectory::_Dir_(size_t i) const
111  {
112  return ((*this)[i + 1] - (*this)[i]);
113  }
114 }
void compat(const Point_t &obj) const
Dimensionality check function w/ Trajectory.
Class def header for a class GeoAlgoException.
STL namespace.
Trajectory(size_t npoints=0, size_t ndimension=0)
Default ctor to specify # points and dimension of each point.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
void push_back(const Point_t &obj)
push_back overrie w/ dimensionality check
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
Vector Point_t
Definition: GeoVector.h:204
double Length(size_t start_step=0, size_t end_step=0) const
The summed-length along all trajectory points.
Vector Dir(size_t i=0) const
The direction at a specified trajectory point.
bool IsLonger(double) const
Check if the trajectory is longer than specified value.
Class def header for a class Trajectory.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
Vector _Dir_(size_t i) const
Returns a direction vector at a specified trajectory point w/o size check.