LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
Decomposer.h
Go to the documentation of this file.
1 
8 #ifndef LARCOREALG_GEOMETRY_DECOMPOSER_H
9 #define LARCOREALG_GEOMETRY_DECOMPOSER_H
10 
11 // LArSoft libraries
12 #include "larcorealg/Geometry/geo_vectors_utils.h" // geo::vect
13 
14 // C/C++ standard libraries
15 #include <cmath> // std::abs()
16 #include <type_traits> // std::declval()
17 #include <utility> // std::move()
18 
19 namespace geo {
20 
21  // --- BEGIN Decomposition objects -------------------------------------------
25 
35  template <typename Vector>
36  class PlaneBase {
37  public:
38  using Vector_t = Vector;
39 
41  static constexpr double RoundingTol = 1e-4;
42 
44  PlaneBase(Vector_t const& main, Vector_t const& secondary)
45  : fMain(PastorizeUnitVector(main))
46  , fSecondary(PastorizeUnitVector(secondary))
48  {}
49 
51  Vector_t const& MainDir() const { return fMain; }
52 
54  Vector_t const& SecondaryDir() const { return fSecondary; }
55 
57  Vector_t const& NormalDir() const { return fNormal; }
58 
60  void SetMainDir(Vector_t const& dir)
61  {
62  fMain = dir;
63  ResetNormal();
64  }
65 
68  {
69  fSecondary = dir;
70  ResetNormal();
71  }
72 
75  {
76  return vect::rounded01(vect::normalize(dir), RoundingTol);
77  }
78 
79  private:
83 
86 
88  void ResetNormal() { fNormal = ComputeNormal(); }
89 
90  }; // class PlaneBase<>
91 
95  template <typename Vector, typename Point = Vector>
98 
99  public:
100  using Vector_t = typename PlaneBase_t::Vector_t;
101  using Point_t = Point;
102 
104  AffinePlaneBase(Point_t const& origin, Vector_t const& main, Vector_t const& secondary)
105  : fOrigin(origin), fBase(main, secondary)
106  {}
107 
109  Vector_t const& MainDir() const { return fBase.MainDir(); }
110 
112  Vector_t const& SecondaryDir() const { return fBase.SecondaryDir(); }
113 
115  Vector_t const& NormalDir() const { return fBase.NormalDir(); }
116 
118  Point_t Origin() const { return fOrigin; }
119 
121  Vector_t ToVector(Point_t const& point) const { return point - Origin(); }
122 
124  void SetOrigin(Point_t const& point) { fOrigin = point; }
125 
127  void SetMainDir(Vector_t const& dir) { fBase.SetMainDir(dir); }
128 
130  void SetSecondaryDir(Vector_t const& dir) { fBase.SetSecondaryDir(dir); }
131 
132  private:
135 
136  }; // class AffinePlaneBase<>
137 
140  template <typename ProjVector>
142 
143  using Projection_t = ProjVector;
144 
146  using Distance_t = decltype(vect::mag2(std::declval<Projection_t>()));
147 
150 
151  DecomposedVector() = default;
152 
153  DecomposedVector(Projection_t const& projection, Distance_t distance)
154  : projection(projection), distance(distance)
155  {}
156 
157  DecomposedVector(Distance_t distance, Projection_t const& projection)
158  : projection(projection), distance(distance)
159  {}
160 
161  }; // struct DecomposedVector
162 
174  template <typename Vector, typename Point, typename ProjVector>
176 
177  public:
179 
182 
185 
188 
191 
194 
197  : fPlaneBase({0.0, 0.0, 0.0}, // origin
198  {1.0, 0.0, 0.0}, // x axis
199  {0.0, 1.0, 0.0} // y axis
200  )
201  {}
202 
204  PlaneDecomposer(AffinePlaneBase_t&& base) : fPlaneBase(std::move(base)) {}
205 
207  PlaneDecomposer(AffinePlaneBase_t const& base) : fPlaneBase(base) {}
208 
211 
213  void SetBase(AffinePlaneBase_t&& base) { fPlaneBase = std::move(base); }
214 
216  void SetBase(AffinePlaneBase_t const& base) { fPlaneBase = base; }
217 
219  void SetOrigin(Point_t const& point) { fPlaneBase.SetOrigin(point); }
220 
222  void SetMainDir(Vector_t const& dir) { fPlaneBase.SetMainDir(dir); }
223 
225  void SetSecondaryDir(Vector_t const& dir) { fPlaneBase.SetSecondaryDir(dir); }
226 
228 
231 
233  Point_t ReferencePoint() const { return Base().Origin(); }
234 
236  Vector_t const& MainDir() const { return Base().MainDir(); }
237 
239  Vector_t const& SecondaryDir() const { return Base().SecondaryDir(); }
240 
242  AffinePlaneBase_t const& Base() const { return fPlaneBase; }
243 
245 
251 
253  auto MainComponent(Projection_t const& v) const { return v.X(); }
254 
256  auto SecondaryComponent(Projection_t const& v) const { return v.Y(); }
257 
259 
262 
264  auto PointMainComponent(Point_t const& point) const
265  {
266  return VectorMainComponent(Base().ToVector(point));
267  }
268 
270  auto PointSecondaryComponent(Point_t const& point) const
271  {
272  return VectorSecondaryComponent(Base().ToVector(point));
273  }
274 
287  {
288  return VectorProjection(Base().ToVector(point));
289  }
290 
292  auto VectorMainComponent(Vector_t const& v) const { return vect::dot(v, MainDir()); }
293 
295  auto VectorSecondaryComponent(Vector_t const& v) const { return vect::dot(v, SecondaryDir()); }
296 
308  {
309  return {VectorMainComponent(v), VectorSecondaryComponent(v)};
310  }
311 
323  double Angle(Vector_t const& v) const
324  {
325  double const a = std::atan2(VectorSecondaryComponent(v), VectorMainComponent(v));
326  return (a >= M_PI) ? -M_PI : a;
327  }
328 
330 
333 
344  Vector_t ComposeVector(Projection_t const& projection) const
345  {
346  return MainComponent(projection) * MainDir() +
347  SecondaryComponent(projection) * SecondaryDir();
348  }
349 
361  Point_t ComposePoint(Projection_t const& projection) const
362  {
363  return ReferencePoint() + ComposeVector(projection);
364  }
365 
367 
368  private:
370 
371  }; // class PlaneDecomposer<>
372 
382  template <typename Vector, typename Point, typename ProjVector>
383  class Decomposer {
384 
386 
388 
390  PlaneDecomposer_t const& Plane() const { return fPlaneDecomp; }
391 
392  public:
394  using Point_t = typename PlaneDecomposer_t::Point_t;
395 
398 
401 
404 
407 
410 
412  Decomposer() = default;
413 
415  Decomposer(AffinePlaneBase_t&& base) : fPlaneDecomp(std::move(base)) {}
416 
418  Decomposer(AffinePlaneBase_t const& base) : fPlaneDecomp(base) {}
419 
422 
424  void SetBase(AffinePlaneBase_t&& base) { fPlaneDecomp.SetBase(std::move(base)); }
425 
427  void SetBase(AffinePlaneBase_t const& base) { fPlaneDecomp.SetBase(base); }
428 
430  void SetOrigin(Point_t const& point) { fPlaneDecomp.SetOrigin(point); }
431 
433  void SetMainDir(Vector_t const& dir) { fPlaneDecomp.SetMainDir(dir); }
434 
436  void SetSecondaryDir(Vector_t const& dir) { fPlaneDecomp.SetSecondaryDir(dir); }
437 
439 
442 
444  Point_t ReferencePoint() const { return Plane().ReferencePoint(); }
445 
447  AffinePlaneBase_t const& Base() const { return Plane().Base(); }
448 
450  Vector_t const& MainDir() const { return Plane().MainDir(); }
451 
453  Vector_t const& SecondaryDir() const { return Plane().SecondaryDir(); }
454 
456  Vector_t const& NormalDir() const { return Base().NormalDir(); }
457 
459 
462 
464  auto PointMainComponent(Point_t const& point) const
465  {
466  return VectorMainComponent(Base().ToVector(point));
467  }
468 
470  auto PointSecondaryComponent(Point_t const& point) const
471  {
472  return VectorSecondaryComponent(Base().ToVector(point));
473  }
474 
476  auto PointNormalComponent(Point_t const& point) const
477  {
478  return VectorNormalComponent(Base().ToVector(point));
479  }
480 
493  {
494  return Plane().PointProjection(point);
495  }
496 
512  {
513  return DecomposeVector(Base().ToVector(point));
514  }
515 
517 
520 
522  auto VectorMainComponent(Vector_t const& v) const { return Plane().VectorMainComponent(v); }
523 
525  auto VectorSecondaryComponent(Vector_t const& v) const
526  {
527  return Plane().VectorSecondaryComponent(v);
528  }
529 
531  auto VectorNormalComponent(Vector_t const& v) const { return vect::dot(v, NormalDir()); }
532 
545  {
546  return Plane().VectorProjection(v);
547  }
548 
564  {
565  return {VectorNormalComponent(v), ProjectVectorOnPlane(v)};
566  }
567 
579  double Angle(Vector_t const& v) const { return Plane().Angle(v); }
580 
582 
585 
587  auto MainComponent(Projection_t const& v) const { return Plane().MainComponent(v); }
588 
590  auto SecondaryComponent(Projection_t const& v) const { return Plane().SecondaryComponent(v); }
591 
593 
596 
606  {
607  return ComposePoint(decomp.distance, decomp.projection);
608  }
609 
628  Point_t ComposePoint(double distance, Projection_t const& proj) const
629  {
630  return ReferencePoint() + ComposeVector(distance, proj);
631  }
632 
634 
637 
647  {
648  return ComposeVector(decomp.distance, decomp.projection);
649  }
650 
669  Vector_t ComposeVector(double distance, Projection_t const& proj) const
670  {
671  return Plane().ComposeVector(proj) + distance * NormalDir();
672  }
673 
675 
676  }; // class Decomposer<>
677 
679  // --- END Decomposition objects ---------------------------------------------
680 
681 } // namespace geo
682 
683 #endif // LARCOREALG_GEOMETRY_DECOMPOSER_H
Point_t ComposePoint(DecomposedVector_t const &decomp) const
Returns the 3D point from composition of projection and distance.
Definition: Decomposer.h:605
void SetBase(AffinePlaneBase_t const &base)
Change projection base.
Definition: Decomposer.h:216
auto VectorSecondaryComponent(Vector_t const &v) const
Returns the secondary component of a vector.
Definition: Decomposer.h:525
Vector_t fSecondary
Secondary axis on the plane.
Definition: Decomposer.h:81
Projection_t projection
Projection of the vector on the plane.
Definition: Decomposer.h:148
typename PlaneDecomposer_t::Distance_t Distance_t
Type representing the signed distance from the projection plane.
Definition: Decomposer.h:403
Distance_t distance
Distance of the vector from the plane.
Definition: Decomposer.h:149
auto PointNormalComponent(Point_t const &point) const
Returns the secondary component of a point.
Definition: Decomposer.h:476
Point_t ComposePoint(double distance, Projection_t const &proj) const
Returns the 3D point from composition of projection and distance.
Definition: Decomposer.h:628
Vector_t fNormal
Axis normal to the plane.
Definition: Decomposer.h:82
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:436
int main()
Definition: example_main.cc:17
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
Definition: geo_vectors.h:160
Vector_t const & SecondaryDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:112
auto VectorMainComponent(Vector_t const &v) const
Returns the main component of a projection vector.
Definition: Decomposer.h:292
AffinePlaneBase(Point_t const &origin, Vector_t const &main, Vector_t const &secondary)
Constructor: assigns the origin of the system and the axes.
Definition: Decomposer.h:104
Vector_t const & NormalDir() const
Returns the plane normal axis direction.
Definition: Decomposer.h:456
PlaneBase(Vector_t const &main, Vector_t const &secondary)
Constructor: assigns the axes.
Definition: Decomposer.h:44
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:222
Vector_t ToVector(Point_t const &point) const
Returns the vector representing the specified point in the affine space.
Definition: Decomposer.h:121
auto SecondaryComponent(Projection_t const &v) const
Returns the secondary component of a projection vector.
Definition: Decomposer.h:256
Vector_t const & NormalDir() const
Returns the axis normal to the plane.
Definition: Decomposer.h:57
Point_t fOrigin
Origin of the coordinate system.
Definition: Decomposer.h:133
PlaneBase_t fBase
Base.
Definition: Decomposer.h:134
Vector_t ComposeVector(DecomposedVector_t const &decomp) const
Returns the 3D vector from composition of projection and distance.
Definition: Decomposer.h:646
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:225
auto PointMainComponent(Point_t const &point) const
Returns the main component of a point.
Definition: Decomposer.h:464
Point_t Origin() const
Returns the origin of the coordinate system in world coordinates.
Definition: Decomposer.h:118
Class with methods to decompose and compose back vectors.
Definition: Decomposer.h:383
DecomposedVector_t DecomposeVector(Vector_t const &v) const
Decomposes a 3D vector in two components.
Definition: Decomposer.h:563
STL namespace.
Vector_t const & MainDir() const
Returns the plane main axis direction.
Definition: Decomposer.h:236
Projection_t VectorProjection(Vector_t const &v) const
Returns the projection of the specified vector on the plane.
Definition: Decomposer.h:307
typename AffinePlaneBase_t::Point_t Point_t
Type for a point.
Definition: Decomposer.h:184
Vector_t const & MainDir() const
Returns the main axis direction.
Definition: Decomposer.h:51
void SetBase(AffinePlaneBase_t &&base)
Change projection base.
Definition: Decomposer.h:424
PlaneDecomposer(AffinePlaneBase_t &&base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:204
DecomposedVector(Distance_t distance, Projection_t const &projection)
Definition: Decomposer.h:157
auto VectorNormalComponent(Vector_t const &v) const
Returns the secondary component of a vector.
Definition: Decomposer.h:531
Projection_t PointProjection(Point_t const &point) const
Returns the projection of the specified point on the plane.
Definition: Decomposer.h:286
recob::tracking::Point_t Point_t
static Vector_t PastorizeUnitVector(Vector_t dir)
Normalizes and rounds a direction vector.
Definition: Decomposer.h:74
Projection_t ProjectPointOnPlane(Point_t const &point) const
Returns the projection of the specified point on the plane.
Definition: Decomposer.h:492
PlaneDecomposer_t const & Plane() const
Returns the plane decomposer.
Definition: Decomposer.h:390
void SetOrigin(Point_t const &point)
Change the 3D point of the reference frame origin.
Definition: Decomposer.h:124
typename PlaneDecomposer_t::Projection_t Projection_t
Type representing the projection vector.
Definition: Decomposer.h:400
typename PlaneBase_t::Vector_t Vector_t
Vector in space.
Definition: Decomposer.h:100
Vector_t ComposeVector(double distance, Projection_t const &proj) const
Returns the 3D vector from composition of projection and distance.
Definition: Decomposer.h:669
auto MainComponent(Projection_t const &v) const
Returns the main component of a projection vector.
Definition: Decomposer.h:253
auto SecondaryComponent(Projection_t const &v) const
Returns the secondary component of a projection vector.
Definition: Decomposer.h:590
AffinePlaneBase_t const & Base() const
Returns the complete base representation.
Definition: Decomposer.h:242
auto PointSecondaryComponent(Point_t const &point) const
Returns the secondary component of a 3D point.
Definition: Decomposer.h:270
Decomposer(AffinePlaneBase_t &&base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:415
typename PlaneDecomposer_t::AffinePlaneBase_t AffinePlaneBase_t
Type of vector base for the space.
Definition: Decomposer.h:409
AffinePlaneBase_t const & Base() const
Returns the base of the decomposition.
Definition: Decomposer.h:447
PlaneDecomposer()
Default constructor: projection on (x,y) with origin (0, 0, 0)
Definition: Decomposer.h:196
auto MainComponent(Projection_t const &v) const
Returns the main component of a projection vector.
Definition: Decomposer.h:587
DecomposedVector(Projection_t const &projection, Distance_t distance)
Definition: Decomposer.h:153
Utilities to extend the interface of geometry vectors.This library provides facilities that can be us...
DecomposedVector_t DecomposePoint(Point_t const &point) const
Decomposes a 3D point in two components.
Definition: Decomposer.h:511
double Angle(Vector_t const &v) const
Returns the angle of the projection from main direction.
Definition: Decomposer.h:323
void SetBase(AffinePlaneBase_t const &base)
Change projection base.
Definition: Decomposer.h:427
void SetOrigin(Point_t const &point)
Change the 3D point of the reference frame origin.
Definition: Decomposer.h:219
constexpr auto dot(Vector const &a, OtherVector const &b)
Return cross product of two vectors.
Vector_t ComposeVector(Projection_t const &projection) const
Returns the 3D vector from the specified projection.
Definition: Decomposer.h:344
void ResetNormal()
Reset normal to the plane.
Definition: Decomposer.h:88
double Angle(Vector_t const &v) const
Returns the angle of the projection from main direction.
Definition: Decomposer.h:579
typename DecomposedVector_t::Projection_t Projection_t
Type representing the projection vector.
Definition: Decomposer.h:190
Point_t ReferencePoint() const
Returns the reference point for the plane coordinate, as a 3D point.
Definition: Decomposer.h:444
Vector rounded01(Vector const &v, Scalar tol)
Returns a vector with all components rounded if close to 0, -1 or +1.
auto mag2(Vector const &v)
Return norm of the specified vector.
typename PlaneDecomposer_t::DecomposedVector_t DecomposedVector_t
Type representing a decomposition on the plane.
Definition: Decomposer.h:406
typename AffinePlaneBase_t::Vector_t Vector_t
Type for a vector.
Definition: Decomposer.h:187
auto VectorMainComponent(Vector_t const &v) const
Returns the main component of a vector.
Definition: Decomposer.h:522
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
Definition: geo_vectors.h:180
void SetOrigin(Point_t const &point)
Change the 3D point of the reference frame origin.
Definition: Decomposer.h:430
Vector_t const & SecondaryDir() const
Returns the plane secondary axis direction.
Definition: Decomposer.h:453
Vector_t const & MainDir() const
Returns the plane main axis direction.
Definition: Decomposer.h:450
PlaneDecomposer_t fPlaneDecomp
Manages the projection on the plane.
Definition: Decomposer.h:387
void SetBase(AffinePlaneBase_t &&base)
Change projection base.
Definition: Decomposer.h:213
auto PointSecondaryComponent(Point_t const &point) const
Returns the secondary component of a point.
Definition: Decomposer.h:470
Projection_t ProjectVectorOnPlane(Vector_t const &v) const
Returns the projection of the specified vector on the plane.
Definition: Decomposer.h:544
auto VectorSecondaryComponent(Vector_t const &v) const
Returns the secondary component of a projection vector.
Definition: Decomposer.h:295
TDirectory * dir
Definition: macro.C:5
Class with methods for projection of vectors on a plane.
Definition: Decomposer.h:175
Point_t ReferencePoint() const
Returns the reference point for the plane coordinate, as a 3D point.
Definition: Decomposer.h:233
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
Definition: DCEL.h:42
Float_t proj
Definition: plot.C:35
static constexpr double RoundingTol
Rounding threshold for vectors.
Definition: Decomposer.h:41
AffinePlaneBase_t fPlaneBase
Reference base.
Definition: Decomposer.h:369
auto PointMainComponent(Point_t const &point) const
Returns the main component of a 3D point.
Definition: Decomposer.h:264
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:127
A base for a plane in space.
Definition: Decomposer.h:36
Point_t ComposePoint(Projection_t const &projection) const
Returns the 3D point from the specified projection.
Definition: Decomposer.h:361
ProjVector Projection_t
Type for 2D projection.
Definition: Decomposer.h:143
Vector_t ComputeNormal() const
Computes the normal to the plane.
Definition: Decomposer.h:85
Vector_t const & NormalDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:115
PlaneDecomposer(AffinePlaneBase_t const &base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:207
Vector normalize(Vector const &v)
Returns a vector parallel to v and with norm 1.
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:67
decltype(vect::mag2(std::declval< Projection_t >())) Distance_t
Type for distance from plane.
Definition: Decomposer.h:146
Float_t e
Definition: plot.C:35
recob::tracking::Plane Plane
Definition: TrackState.h:17
typename PlaneDecomposer_t::Point_t Point_t
Type for a vector.
Definition: Decomposer.h:396
Vector_t const & SecondaryDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:54
ROOT libraries.
Vector_t fMain
Main axis on the plane.
Definition: Decomposer.h:80
recob::tracking::Vector_t Vector_t
Vector_t const & MainDir() const
Returns the main axis direction.
Definition: Decomposer.h:109
Vector_t const & SecondaryDir() const
Returns the plane secondary axis direction.
Definition: Decomposer.h:239
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:229
typename PlaneDecomposer_t::Vector_t Vector_t
Definition: Decomposer.h:397
Decomposer(AffinePlaneBase_t const &base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:418
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:130
typename DecomposedVector_t::Distance_t Distance_t
Type representing the signed distance from the projection plane.
Definition: Decomposer.h:193
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:60
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:433