LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Decomposer.h
Go to the documentation of this file.
1 
10 #ifndef LARCOREALG_GEOMETRY_DECOMPOSER_H
11 #define LARCOREALG_GEOMETRY_DECOMPOSER_H
12 
13 // LArSoft libraries
14 #include "larcorealg/Geometry/geo_vectors_utils.h" // geo::vect
15 
16 // C/C++ standard libraries
17 #include <cmath> // std::abs()
18 #include <type_traits> // std::declval()
19 #include <utility> // std::move()
20 
21 namespace geo {
22 
23  // --- BEGIN Decomposition objects -------------------------------------------
27 
37  template <typename Vector>
38  class PlaneBase {
39  public:
40  using Vector_t = Vector;
41 
43  static constexpr double RoundingTol = 1e-4;
44 
46  PlaneBase(Vector_t const& main, Vector_t const& secondary)
47  : fMain(PastorizeUnitVector(main))
48  , fSecondary(PastorizeUnitVector(secondary))
50  {}
51 
53  Vector_t const& MainDir() const { return fMain; }
54 
56  Vector_t const& SecondaryDir() const { return fSecondary; }
57 
59  Vector_t const& NormalDir() const { return fNormal; }
60 
62  void SetMainDir(Vector_t const& dir)
63  {
64  fMain = dir;
65  ResetNormal();
66  }
67 
70  {
71  fSecondary = dir;
72  ResetNormal();
73  }
74 
77  {
78  return geo::vect::rounded01(geo::vect::normalize(dir), RoundingTol);
79  }
80 
81  private:
85 
88 
90  void ResetNormal() { fNormal = ComputeNormal(); }
91 
92  }; // class PlaneBase<>
93 
97  template <typename Vector, typename Point = Vector>
100 
101  public:
102  using Vector_t = typename PlaneBase_t::Vector_t;
103  using Point_t = Point;
104 
106  AffinePlaneBase(Point_t const& origin, Vector_t const& main, Vector_t const& secondary)
107  : fOrigin(origin), fBase(main, secondary)
108  {}
109 
111  Vector_t const& MainDir() const { return fBase.MainDir(); }
112 
114  Vector_t const& SecondaryDir() const { return fBase.SecondaryDir(); }
115 
117  Vector_t const& NormalDir() const { return fBase.NormalDir(); }
118 
120  Point_t Origin() const { return fOrigin; }
121 
123  Vector_t ToVector(Point_t const& point) const { return point - Origin(); }
124 
126  void SetOrigin(Point_t const& point) { fOrigin = point; }
127 
129  void SetMainDir(Vector_t const& dir) { fBase.SetMainDir(dir); }
130 
132  void SetSecondaryDir(Vector_t const& dir) { fBase.SetSecondaryDir(dir); }
133 
134  private:
137 
138  }; // class AffinePlaneBase<>
139 
142  template <typename ProjVector>
144 
145  using Projection_t = ProjVector;
146 
148  using Distance_t = decltype(geo::vect::mag2(std::declval<Projection_t>()));
149 
152 
153  DecomposedVector() = default;
154 
155  DecomposedVector(Projection_t const& projection, Distance_t distance)
156  : projection(projection), distance(distance)
157  {}
158 
159  DecomposedVector(Distance_t distance, Projection_t const& projection)
160  : projection(projection), distance(distance)
161  {}
162 
163  }; // struct DecomposedVector
164 
176  template <typename Vector, typename Point, typename ProjVector>
178 
179  public:
181 
184 
187 
190 
193 
196 
199  : fPlaneBase({0.0, 0.0, 0.0}, // origin
200  {1.0, 0.0, 0.0}, // x axis
201  {0.0, 1.0, 0.0} // y axis
202  )
203  {}
204 
206  PlaneDecomposer(AffinePlaneBase_t&& base) : fPlaneBase(std::move(base)) {}
207 
209  PlaneDecomposer(AffinePlaneBase_t const& base) : fPlaneBase(base) {}
210 
213 
215  void SetBase(AffinePlaneBase_t&& base) { fPlaneBase = std::move(base); }
216 
218  void SetBase(AffinePlaneBase_t const& base) { fPlaneBase = base; }
219 
221  void SetOrigin(Point_t const& point) { fPlaneBase.SetOrigin(point); }
222 
224  void SetMainDir(Vector_t const& dir) { fPlaneBase.SetMainDir(dir); }
225 
227  void SetSecondaryDir(Vector_t const& dir) { fPlaneBase.SetSecondaryDir(dir); }
228 
230 
233 
235  Point_t ReferencePoint() const { return Base().Origin(); }
236 
238  Vector_t const& MainDir() const { return Base().MainDir(); }
239 
241  Vector_t const& SecondaryDir() const { return Base().SecondaryDir(); }
242 
244  AffinePlaneBase_t const& Base() const { return fPlaneBase; }
245 
247 
253 
255  auto MainComponent(Projection_t const& v) const { return v.X(); }
256 
258  auto SecondaryComponent(Projection_t const& v) const { return v.Y(); }
259 
261 
264 
266  auto PointMainComponent(Point_t const& point) const
267  {
268  return VectorMainComponent(Base().ToVector(point));
269  }
270 
272  auto PointSecondaryComponent(Point_t const& point) const
273  {
274  return VectorSecondaryComponent(Base().ToVector(point));
275  }
276 
289  {
290  return VectorProjection(Base().ToVector(point));
291  }
292 
294  auto VectorMainComponent(Vector_t const& v) const { return geo::vect::dot(v, MainDir()); }
295 
297  auto VectorSecondaryComponent(Vector_t const& v) const
298  {
299  return geo::vect::dot(v, SecondaryDir());
300  }
301 
313  {
314  return {VectorMainComponent(v), VectorSecondaryComponent(v)};
315  }
316 
328  double Angle(Vector_t const& v) const
329  {
330  double const a = std::atan2(VectorSecondaryComponent(v), VectorMainComponent(v));
331  return (a >= M_PI) ? -M_PI : a;
332  }
333 
335 
338 
349  Vector_t ComposeVector(Projection_t const& projection) const
350  {
351  return MainComponent(projection) * MainDir() +
352  SecondaryComponent(projection) * SecondaryDir();
353  }
354 
366  Point_t ComposePoint(Projection_t const& projection) const
367  {
368  return ReferencePoint() + ComposeVector(projection);
369  }
370 
372 
373  private:
375 
376  }; // class PlaneDecomposer<>
377 
387  template <typename Vector, typename Point, typename ProjVector>
388  class Decomposer {
389 
391 
393 
395  PlaneDecomposer_t const& Plane() const { return fPlaneDecomp; }
396 
397  public:
399  using Point_t = typename PlaneDecomposer_t::Point_t;
400 
403 
406 
409 
412 
415 
417  Decomposer() = default;
418 
420  Decomposer(AffinePlaneBase_t&& base) : fPlaneDecomp(std::move(base)) {}
421 
423  Decomposer(AffinePlaneBase_t const& base) : fPlaneDecomp(base) {}
424 
427 
429  void SetBase(AffinePlaneBase_t&& base) { fPlaneDecomp.SetBase(std::move(base)); }
430 
432  void SetBase(AffinePlaneBase_t const& base) { fPlaneDecomp.SetBase(base); }
433 
435  void SetOrigin(Point_t const& point) { fPlaneDecomp.SetOrigin(point); }
436 
438  void SetMainDir(Vector_t const& dir) { fPlaneDecomp.SetMainDir(dir); }
439 
441  void SetSecondaryDir(Vector_t const& dir) { fPlaneDecomp.SetSecondaryDir(dir); }
442 
444 
447 
449  Point_t ReferencePoint() const { return Plane().ReferencePoint(); }
450 
452  AffinePlaneBase_t const& Base() const { return Plane().Base(); }
453 
455  Vector_t const& MainDir() const { return Plane().MainDir(); }
456 
458  Vector_t const& SecondaryDir() const { return Plane().SecondaryDir(); }
459 
461  Vector_t const& NormalDir() const { return Base().NormalDir(); }
462 
464 
467 
469  auto PointMainComponent(Point_t const& point) const
470  {
471  return VectorMainComponent(Base().ToVector(point));
472  }
473 
475  auto PointSecondaryComponent(Point_t const& point) const
476  {
477  return VectorSecondaryComponent(Base().ToVector(point));
478  }
479 
481  auto PointNormalComponent(Point_t const& point) const
482  {
483  return VectorNormalComponent(Base().ToVector(point));
484  }
485 
498  {
499  return Plane().PointProjection(point);
500  }
501 
517  {
518  return DecomposeVector(Base().ToVector(point));
519  }
520 
522 
525 
527  auto VectorMainComponent(Vector_t const& v) const { return Plane().VectorMainComponent(v); }
528 
530  auto VectorSecondaryComponent(Vector_t const& v) const
531  {
532  return Plane().VectorSecondaryComponent(v);
533  }
534 
536  auto VectorNormalComponent(Vector_t const& v) const { return geo::vect::dot(v, NormalDir()); }
537 
550  {
551  return Plane().VectorProjection(v);
552  }
553 
569  {
570  return {VectorNormalComponent(v), ProjectVectorOnPlane(v)};
571  }
572 
584  double Angle(Vector_t const& v) const { return Plane().Angle(v); }
585 
587 
590 
592  auto MainComponent(Projection_t const& v) const { return Plane().MainComponent(v); }
593 
595  auto SecondaryComponent(Projection_t const& v) const { return Plane().SecondaryComponent(v); }
596 
598 
601 
611  {
612  return ComposePoint(decomp.distance, decomp.projection);
613  }
614 
633  Point_t ComposePoint(double distance, Projection_t const& proj) const
634  {
635  return ReferencePoint() + ComposeVector(distance, proj);
636  }
637 
639 
642 
652  {
653  return ComposeVector(decomp.distance, decomp.projection);
654  }
655 
674  Vector_t ComposeVector(double distance, Projection_t const& proj) const
675  {
676  return Plane().ComposeVector(proj) + distance * NormalDir();
677  }
678 
680 
681  }; // class Decomposer<>
682 
684  // --- END Decomposition objects ---------------------------------------------
685 
686 } // namespace geo
687 
688 #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:610
void SetBase(AffinePlaneBase_t const &base)
Change projection base.
Definition: Decomposer.h:218
auto VectorSecondaryComponent(Vector_t const &v) const
Returns the secondary component of a vector.
Definition: Decomposer.h:530
Vector_t fSecondary
Secondary axis on the plane.
Definition: Decomposer.h:83
Projection_t projection
Projection of the vector on the plane.
Definition: Decomposer.h:150
typename PlaneDecomposer_t::Distance_t Distance_t
Type representing the signed distance from the projection plane.
Definition: Decomposer.h:408
Distance_t distance
Distance of the vector from the plane.
Definition: Decomposer.h:151
auto PointNormalComponent(Point_t const &point) const
Returns the secondary component of a point.
Definition: Decomposer.h:481
Point_t ComposePoint(double distance, Projection_t const &proj) const
Returns the 3D point from composition of projection and distance.
Definition: Decomposer.h:633
Vector_t fNormal
Axis normal to the plane.
Definition: Decomposer.h:84
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:441
int main()
Definition: example_main.cc:17
Vector_t const & SecondaryDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:114
auto VectorMainComponent(Vector_t const &v) const
Returns the main component of a projection vector.
Definition: Decomposer.h:294
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:106
Vector_t const & NormalDir() const
Returns the plane normal axis direction.
Definition: Decomposer.h:461
PlaneBase(Vector_t const &main, Vector_t const &secondary)
Constructor: assigns the axes.
Definition: Decomposer.h:46
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:224
Vector_t ToVector(Point_t const &point) const
Returns the vector representing the specified point in the affine space.
Definition: Decomposer.h:123
auto SecondaryComponent(Projection_t const &v) const
Returns the secondary component of a projection vector.
Definition: Decomposer.h:258
Vector_t const & NormalDir() const
Returns the axis normal to the plane.
Definition: Decomposer.h:59
Point_t fOrigin
Origin of the coordinate system.
Definition: Decomposer.h:135
PlaneBase_t fBase
Base.
Definition: Decomposer.h:136
Vector_t ComposeVector(DecomposedVector_t const &decomp) const
Returns the 3D vector from composition of projection and distance.
Definition: Decomposer.h:651
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:227
auto PointMainComponent(Point_t const &point) const
Returns the main component of a point.
Definition: Decomposer.h:469
Point_t Origin() const
Returns the origin of the coordinate system in world coordinates.
Definition: Decomposer.h:120
Class with methods to decompose and compose back vectors.
Definition: Decomposer.h:388
DecomposedVector_t DecomposeVector(Vector_t const &v) const
Decomposes a 3D vector in two components.
Definition: Decomposer.h:568
STL namespace.
Vector_t const & MainDir() const
Returns the plane main axis direction.
Definition: Decomposer.h:238
Projection_t VectorProjection(Vector_t const &v) const
Returns the projection of the specified vector on the plane.
Definition: Decomposer.h:312
typename AffinePlaneBase_t::Point_t Point_t
Type for a point.
Definition: Decomposer.h:186
Vector_t const & MainDir() const
Returns the main axis direction.
Definition: Decomposer.h:53
void SetBase(AffinePlaneBase_t &&base)
Change projection base.
Definition: Decomposer.h:429
PlaneDecomposer(AffinePlaneBase_t &&base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:206
DecomposedVector(Distance_t distance, Projection_t const &projection)
Definition: Decomposer.h:159
auto VectorNormalComponent(Vector_t const &v) const
Returns the secondary component of a vector.
Definition: Decomposer.h:536
Projection_t PointProjection(Point_t const &point) const
Returns the projection of the specified point on the plane.
Definition: Decomposer.h:288
recob::tracking::Point_t Point_t
static Vector_t PastorizeUnitVector(Vector_t dir)
Normalizes and rounds a direction vector.
Definition: Decomposer.h:76
Projection_t ProjectPointOnPlane(Point_t const &point) const
Returns the projection of the specified point on the plane.
Definition: Decomposer.h:497
PlaneDecomposer_t const & Plane() const
Returns the plane decomposer.
Definition: Decomposer.h:395
void SetOrigin(Point_t const &point)
Change the 3D point of the reference frame origin.
Definition: Decomposer.h:126
typename PlaneDecomposer_t::Projection_t Projection_t
Type representing the projection vector.
Definition: Decomposer.h:405
typename PlaneBase_t::Vector_t Vector_t
Vector in space.
Definition: Decomposer.h:102
Vector_t ComposeVector(double distance, Projection_t const &proj) const
Returns the 3D vector from composition of projection and distance.
Definition: Decomposer.h:674
auto MainComponent(Projection_t const &v) const
Returns the main component of a projection vector.
Definition: Decomposer.h:255
auto SecondaryComponent(Projection_t const &v) const
Returns the secondary component of a projection vector.
Definition: Decomposer.h:595
AffinePlaneBase_t const & Base() const
Returns the complete base representation.
Definition: Decomposer.h:244
auto PointSecondaryComponent(Point_t const &point) const
Returns the secondary component of a 3D point.
Definition: Decomposer.h:272
Decomposer(AffinePlaneBase_t &&base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:420
typename PlaneDecomposer_t::AffinePlaneBase_t AffinePlaneBase_t
Type of vector base for the space.
Definition: Decomposer.h:414
AffinePlaneBase_t const & Base() const
Returns the base of the decomposition.
Definition: Decomposer.h:452
PlaneDecomposer()
Default constructor: projection on (x,y) with origin (0, 0, 0)
Definition: Decomposer.h:198
auto MainComponent(Projection_t const &v) const
Returns the main component of a projection vector.
Definition: Decomposer.h:592
DecomposedVector(Projection_t const &projection, Distance_t distance)
Definition: Decomposer.h:155
Utilities to extend the interface of geometry vectors.
decltype(geo::vect::mag2(std::declval< Projection_t >())) Distance_t
Type for distance from plane.
Definition: Decomposer.h:148
DecomposedVector_t DecomposePoint(Point_t const &point) const
Decomposes a 3D point in two components.
Definition: Decomposer.h:516
double Angle(Vector_t const &v) const
Returns the angle of the projection from main direction.
Definition: Decomposer.h:328
void SetBase(AffinePlaneBase_t const &base)
Change projection base.
Definition: Decomposer.h:432
void SetOrigin(Point_t const &point)
Change the 3D point of the reference frame origin.
Definition: Decomposer.h:221
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:349
void ResetNormal()
Reset normal to the plane.
Definition: Decomposer.h:90
double Angle(Vector_t const &v) const
Returns the angle of the projection from main direction.
Definition: Decomposer.h:584
typename DecomposedVector_t::Projection_t Projection_t
Type representing the projection vector.
Definition: Decomposer.h:192
Point_t ReferencePoint() const
Returns the reference point for the plane coordinate, as a 3D point.
Definition: Decomposer.h:449
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:411
typename AffinePlaneBase_t::Vector_t Vector_t
Type for a vector.
Definition: Decomposer.h:189
auto VectorMainComponent(Vector_t const &v) const
Returns the main component of a vector.
Definition: Decomposer.h:527
void SetOrigin(Point_t const &point)
Change the 3D point of the reference frame origin.
Definition: Decomposer.h:435
Vector_t const & SecondaryDir() const
Returns the plane secondary axis direction.
Definition: Decomposer.h:458
Vector_t const & MainDir() const
Returns the plane main axis direction.
Definition: Decomposer.h:455
PlaneDecomposer_t fPlaneDecomp
Manages the projection on the plane.
Definition: Decomposer.h:392
void SetBase(AffinePlaneBase_t &&base)
Change projection base.
Definition: Decomposer.h:215
auto PointSecondaryComponent(Point_t const &point) const
Returns the secondary component of a point.
Definition: Decomposer.h:475
Projection_t ProjectVectorOnPlane(Vector_t const &v) const
Returns the projection of the specified vector on the plane.
Definition: Decomposer.h:549
auto VectorSecondaryComponent(Vector_t const &v) const
Returns the secondary component of a projection vector.
Definition: Decomposer.h:297
TDirectory * dir
Definition: macro.C:5
Class with methods for projection of vectors on a plane.
Definition: Decomposer.h:177
Point_t ReferencePoint() const
Returns the reference point for the plane coordinate, as a 3D point.
Definition: Decomposer.h:235
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:43
geo::Vector_t Vector_t
Type for the vector in space.
Definition: Decomposer.h:40
AffinePlaneBase_t fPlaneBase
Reference base.
Definition: Decomposer.h:374
auto PointMainComponent(Point_t const &point) const
Returns the main component of a 3D point.
Definition: Decomposer.h:266
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:129
A base for a plane in space.
Definition: Decomposer.h:38
Point_t ComposePoint(Projection_t const &projection) const
Returns the 3D point from the specified projection.
Definition: Decomposer.h:366
ProjVector Projection_t
Type for 2D projection.
Definition: Decomposer.h:145
Vector_t ComputeNormal() const
Computes the normal to the plane.
Definition: Decomposer.h:87
Vector_t const & NormalDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:117
PlaneDecomposer(AffinePlaneBase_t const &base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:209
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:69
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:401
Vector_t const & SecondaryDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:56
Namespace collecting geometry-related classes utilities.
Vector_t fMain
Main axis on the plane.
Definition: Decomposer.h:82
recob::tracking::Vector_t Vector_t
Vector_t const & MainDir() const
Returns the main axis direction.
Definition: Decomposer.h:111
Vector_t const & SecondaryDir() const
Returns the plane secondary axis direction.
Definition: Decomposer.h:241
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:402
Decomposer(AffinePlaneBase_t const &base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:423
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:132
typename DecomposedVector_t::Distance_t Distance_t
Type representing the signed distance from the projection plane.
Definition: Decomposer.h:195
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:62
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:438