LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 <utility> // std::move()
19 #include <type_traits> // std::declval()
20 
21 
22 namespace geo {
23 
24  // --- BEGIN Decomposition objects -------------------------------------------
28 
38  template <typename Vector>
39  class PlaneBase {
40  public:
41  using Vector_t = Vector;
42 
44  static constexpr double RoundingTol = 1e-4;
45 
47  PlaneBase(Vector_t const& main, Vector_t const& secondary)
48  : fMain(PastorizeUnitVector(main))
49  , fSecondary(PastorizeUnitVector(secondary))
51  {}
52 
54  Vector_t const& MainDir() const { return fMain; }
55 
57  Vector_t const& SecondaryDir() const { return fSecondary; }
58 
60  Vector_t const& NormalDir() const { return fNormal; }
61 
63  void SetMainDir(Vector_t const& dir) { fMain = dir; ResetNormal(); }
64 
67  { fSecondary = dir; ResetNormal(); }
68 
69 
72  { return geo::vect::rounded01(geo::vect::normalize(dir), RoundingTol); }
73 
74  private:
78 
81  { return PastorizeUnitVector(MainDir().Cross(SecondaryDir())); }
82 
84  void ResetNormal() { fNormal = ComputeNormal(); }
85 
86  }; // class PlaneBase<>
87 
88 
92  template <typename Vector, typename Point = Vector>
95 
96  public:
97  using Vector_t = typename PlaneBase_t::Vector_t;
98  using Point_t = Point;
99 
102  (Point_t const& origin, Vector_t const& main, Vector_t const& secondary)
103  : fOrigin(origin)
104  , fBase(main, secondary)
105  {}
106 
108  Vector_t const& MainDir() const { return fBase.MainDir(); }
109 
111  Vector_t const& SecondaryDir() const { return fBase.SecondaryDir(); }
112 
114  Vector_t const& NormalDir() const { return fBase.NormalDir(); }
115 
117  Point_t Origin() const { return fOrigin; }
118 
120  Vector_t ToVector(Point_t const& point) const { return point - Origin(); }
121 
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:
133 
136 
137  }; // class AffinePlaneBase<>
138 
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 
150 
153 
154 
155  DecomposedVector() = default;
156 
157  DecomposedVector(Projection_t const& projection, Distance_t distance)
158  : projection(projection), distance(distance) {}
159 
160  DecomposedVector(Distance_t distance, Projection_t const& projection)
161  : projection(projection), distance(distance) {}
162 
163  }; // struct DecomposedVector
164 
165 
166 
178  template <typename Vector, typename Point, typename ProjVector>
180 
181  public:
182 
184 
187 
190 
193 
196 
199 
200 
203  : fPlaneBase(
204  { 0.0, 0.0, 0.0 }, // origin
205  { 1.0, 0.0, 0.0 }, // x axis
206  { 0.0, 1.0, 0.0 } // y axis
207  )
208  {}
209 
211  PlaneDecomposer(AffinePlaneBase_t&& base): fPlaneBase(std::move(base)) {}
212 
214  PlaneDecomposer(AffinePlaneBase_t const& base): fPlaneBase(base) {}
215 
218 
220  void SetBase(AffinePlaneBase_t&& base) { fPlaneBase = std::move(base); }
221 
223  void SetBase(AffinePlaneBase_t const& base) { fPlaneBase = base; }
224 
226  void SetOrigin(Point_t const& point) { fPlaneBase.SetOrigin(point); }
227 
229  void SetMainDir(Vector_t const& dir) { fPlaneBase.SetMainDir(dir); }
230 
233  { fPlaneBase.SetSecondaryDir(dir); }
234 
236 
239 
241  Point_t ReferencePoint() const { return Base().Origin(); }
242 
244  Vector_t const& MainDir() const { return Base().MainDir(); }
245 
247  Vector_t const& SecondaryDir() const { return Base().SecondaryDir(); }
248 
250  AffinePlaneBase_t const& Base() const { return fPlaneBase; }
251 
253 
254 
260 
262  auto MainComponent(Projection_t const& v) const { return v.X(); }
263 
265  auto SecondaryComponent(Projection_t const& v) const { return v.Y(); }
266 
268 
269 
272 
274  auto PointMainComponent(Point_t const& point) const
275  { return VectorMainComponent(Base().ToVector(point)); }
276 
278  auto PointSecondaryComponent(Point_t const& point) const
279  { return VectorSecondaryComponent(Base().ToVector(point)); }
280 
293  { return VectorProjection(Base().ToVector(point)); }
294 
295 
297  auto VectorMainComponent(Vector_t const& v) const
298  { return geo::vect::dot(v, MainDir()); }
299 
301  auto VectorSecondaryComponent(Vector_t const& v) const
302  { return geo::vect::dot(v, SecondaryDir()); }
303 
315  { return { VectorMainComponent(v), VectorSecondaryComponent(v) }; }
316 
328  double Angle(Vector_t const& v) const
329  {
330  double const a
331  = std::atan2(VectorSecondaryComponent(v), VectorMainComponent(v));
332  return (a >= M_PI)? -M_PI: a;
333  }
334 
336 
337 
340 
351  Vector_t ComposeVector(Projection_t const& projection) const
352  {
353  return MainComponent(projection) * MainDir()
354  + SecondaryComponent(projection) * SecondaryDir()
355  ;
356  }
357 
369  Point_t ComposePoint(Projection_t const& projection) const
370  { return ReferencePoint() + ComposeVector(projection); }
371 
373 
374  private:
376 
377  }; // class PlaneDecomposer<>
378 
379 
380 
390  template <typename Vector, typename Point, typename ProjVector>
391  class Decomposer {
392 
394 
396 
398  PlaneDecomposer_t const& Plane() const { return fPlaneDecomp; }
399 
400  public:
402  using Point_t = typename PlaneDecomposer_t::Point_t;
403 
406 
409 
412 
415 
418 
419 
421  Decomposer() = default;
422 
424  Decomposer(AffinePlaneBase_t&& base): fPlaneDecomp(std::move(base)) {}
425 
427  Decomposer(AffinePlaneBase_t const& base): fPlaneDecomp(base) {}
428 
431 
434  { fPlaneDecomp.SetBase(std::move(base)); }
435 
437  void SetBase(AffinePlaneBase_t const& base)
438  { fPlaneDecomp.SetBase(base); }
439 
441  void SetOrigin(Point_t const& point) { fPlaneDecomp.SetOrigin(point); }
442 
444  void SetMainDir(Vector_t const& dir) { fPlaneDecomp.SetMainDir(dir); }
445 
448  { fPlaneDecomp.SetSecondaryDir(dir); }
449 
451 
452 
455 
457  Point_t ReferencePoint() const { return Plane().ReferencePoint(); }
458 
460  AffinePlaneBase_t const& Base() const { return Plane().Base(); }
461 
463  Vector_t const& MainDir() const { return Plane().MainDir(); }
464 
466  Vector_t const& SecondaryDir() const { return Plane().SecondaryDir(); }
467 
469  Vector_t const& NormalDir() const { return Base().NormalDir(); }
470 
472 
473 
476 
478  auto PointMainComponent(Point_t const& point) const
479  { return VectorMainComponent(Base().ToVector(point)); }
480 
482  auto PointSecondaryComponent(Point_t const& point) const
483  { return VectorSecondaryComponent(Base().ToVector(point)); }
484 
486  auto PointNormalComponent(Point_t const& point) const
487  { return VectorNormalComponent(Base().ToVector(point)); }
488 
501  { return Plane().PointProjection(point); }
502 
518  { return DecomposeVector(Base().ToVector(point)); }
519 
521 
522 
525 
527  auto VectorMainComponent(Vector_t const& v) const
528  { return Plane().VectorMainComponent(v); }
529 
531  auto VectorSecondaryComponent(Vector_t const& v) const
532  { return Plane().VectorSecondaryComponent(v); }
533 
535  auto VectorNormalComponent(Vector_t const& v) const
536  { return geo::vect::dot(v, NormalDir()); }
537 
550  { return Plane().VectorProjection(v); }
551 
567  { return { VectorNormalComponent(v), ProjectVectorOnPlane(v) }; }
568 
580  double Angle(Vector_t const& v) const
581  { return Plane().Angle(v); }
582 
584 
585 
588 
590  auto MainComponent(Projection_t const& v) const
591  { return Plane().MainComponent(v); }
592 
594  auto SecondaryComponent(Projection_t const& v) const
595  { return Plane().SecondaryComponent(v); }
596 
598 
599 
602 
612  { return ComposePoint(decomp.distance, decomp.projection); }
613 
632  Point_t ComposePoint(double distance, Projection_t const& proj) const
633  { return ReferencePoint() + ComposeVector(distance, proj); }
634 
636 
639 
649  { return ComposeVector(decomp.distance, decomp.projection); }
650 
669  Vector_t ComposeVector(double distance, Projection_t const& proj) const
670  { return Plane().ComposeVector(proj) + distance * NormalDir(); }
671 
673 
674 
675  }; // class Decomposer<>
676 
678  // --- END Decomposition objects ---------------------------------------------
679 
680 } // namespace geo
681 
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:611
void SetBase(AffinePlaneBase_t const &base)
Change projection base.
Definition: Decomposer.h:223
auto VectorSecondaryComponent(Vector_t const &v) const
Returns the secondary component of a vector.
Definition: Decomposer.h:531
Vector_t fSecondary
Secondary axis on the plane.
Definition: Decomposer.h:76
Projection_t projection
Projection of the vector on the plane.
Definition: Decomposer.h:151
typename PlaneDecomposer_t::Distance_t Distance_t
Type representing the signed distance from the projection plane.
Definition: Decomposer.h:411
constexpr auto dot(Vector const &a, Vector const &b)
Return cross product of two vectors.
Distance_t distance
Distance of the vector from the plane.
Definition: Decomposer.h:152
auto PointNormalComponent(Point_t const &point) const
Returns the secondary component of a point.
Definition: Decomposer.h:486
Point_t ComposePoint(double distance, Projection_t const &proj) const
Returns the 3D point from composition of projection and distance.
Definition: Decomposer.h:632
Vector_t fNormal
Axis normal to the plane.
Definition: Decomposer.h:77
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:447
Vector_t const & SecondaryDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:111
auto VectorMainComponent(Vector_t const &v) const
Returns the main component of a projection vector.
Definition: Decomposer.h:297
Vector_t const & NormalDir() const
Returns the plane normal axis direction.
Definition: Decomposer.h:469
PlaneBase(Vector_t const &main, Vector_t const &secondary)
Constructor: assigns the axes.
Definition: Decomposer.h:47
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:229
Vector_t ToVector(Point_t const &point) const
Returns the vector representing the specified point in the affine space.
Definition: Decomposer.h:120
int main(int argc, char **argv)
Definition: main.cpp:72
auto SecondaryComponent(Projection_t const &v) const
Returns the secondary component of a projection vector.
Definition: Decomposer.h:265
Vector_t const & NormalDir() const
Returns the axis normal to the plane.
Definition: Decomposer.h:60
Point_t fOrigin
Origin of the coordinate system.
Definition: Decomposer.h:134
PlaneBase_t fBase
Base.
Definition: Decomposer.h:135
Vector_t ComposeVector(DecomposedVector_t const &decomp) const
Returns the 3D vector from composition of projection and distance.
Definition: Decomposer.h:648
void SetSecondaryDir(Vector_t const &dir)
Change the secondary direction of the projection base.
Definition: Decomposer.h:232
auto PointMainComponent(Point_t const &point) const
Returns the main component of a point.
Definition: Decomposer.h:478
Point_t Origin() const
Returns the origin of the coordinate system in world coordinates.
Definition: Decomposer.h:117
Class with methods to decompose and compose back vectors.
Definition: Decomposer.h:391
DecomposedVector_t DecomposeVector(Vector_t const &v) const
Decomposes a 3D vector in two components.
Definition: Decomposer.h:566
STL namespace.
Vector_t const & MainDir() const
Returns the plane main axis direction.
Definition: Decomposer.h:244
Projection_t VectorProjection(Vector_t const &v) const
Returns the projection of the specified vector on the plane.
Definition: Decomposer.h:314
typename AffinePlaneBase_t::Point_t Point_t
Type for a point.
Definition: Decomposer.h:189
Vector_t const & MainDir() const
Returns the main axis direction.
Definition: Decomposer.h:54
void SetBase(AffinePlaneBase_t &&base)
Change projection base.
Definition: Decomposer.h:433
PlaneDecomposer(AffinePlaneBase_t &&base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:211
DecomposedVector(Distance_t distance, Projection_t const &projection)
Definition: Decomposer.h:160
auto VectorNormalComponent(Vector_t const &v) const
Returns the secondary component of a vector.
Definition: Decomposer.h:535
Projection_t PointProjection(Point_t const &point) const
Returns the projection of the specified point on the plane.
Definition: Decomposer.h:292
recob::tracking::Point_t Point_t
static Vector_t PastorizeUnitVector(Vector_t dir)
Normalizes and rounds a direction vector.
Definition: Decomposer.h:71
Projection_t ProjectPointOnPlane(Point_t const &point) const
Returns the projection of the specified point on the plane.
Definition: Decomposer.h:500
PlaneDecomposer_t const & Plane() const
Returns the plane decomposer.
Definition: Decomposer.h:398
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:408
typename PlaneBase_t::Vector_t Vector_t
Vector in space.
Definition: Decomposer.h:97
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:262
auto SecondaryComponent(Projection_t const &v) const
Returns the secondary component of a projection vector.
Definition: Decomposer.h:594
AffinePlaneBase_t const & Base() const
Returns the complete base representation.
Definition: Decomposer.h:250
auto PointSecondaryComponent(Point_t const &point) const
Returns the secondary component of a 3D point.
Definition: Decomposer.h:278
Decomposer(AffinePlaneBase_t &&base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:424
typename PlaneDecomposer_t::AffinePlaneBase_t AffinePlaneBase_t
Type of vector base for the space.
Definition: Decomposer.h:417
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
Definition: DCEL.h:34
AffinePlaneBase_t const & Base() const
Returns the base of the decomposition.
Definition: Decomposer.h:460
PlaneDecomposer()
Default constructor: projection on (x,y) with origin (0, 0, 0)
Definition: Decomposer.h:202
auto MainComponent(Projection_t const &v) const
Returns the main component of a projection vector.
Definition: Decomposer.h:590
DecomposedVector(Projection_t const &projection, Distance_t distance)
Definition: Decomposer.h:157
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:517
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:437
void SetOrigin(Point_t const &point)
Change the 3D point of the reference frame origin.
Definition: Decomposer.h:226
Vector_t ComposeVector(Projection_t const &projection) const
Returns the 3D vector from the specified projection.
Definition: Decomposer.h:351
void ResetNormal()
Reset normal to the plane.
Definition: Decomposer.h:84
double Angle(Vector_t const &v) const
Returns the angle of the projection from main direction.
Definition: Decomposer.h:580
typename DecomposedVector_t::Projection_t Projection_t
Type representing the projection vector.
Definition: Decomposer.h:195
Point_t ReferencePoint() const
Returns the reference point for the plane coordinate, as a 3D point.
Definition: Decomposer.h:457
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:414
typename AffinePlaneBase_t::Vector_t Vector_t
Type for a vector.
Definition: Decomposer.h:192
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:441
Vector_t const & SecondaryDir() const
Returns the plane secondary axis direction.
Definition: Decomposer.h:466
Vector_t const & MainDir() const
Returns the plane main axis direction.
Definition: Decomposer.h:463
PlaneDecomposer_t fPlaneDecomp
Manages the projection on the plane.
Definition: Decomposer.h:395
void SetBase(AffinePlaneBase_t &&base)
Change projection base.
Definition: Decomposer.h:220
auto PointSecondaryComponent(Point_t const &point) const
Returns the secondary component of a point.
Definition: Decomposer.h:482
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:301
TDirectory * dir
Definition: macro.C:5
Class with methods for projection of vectors on a plane.
Definition: Decomposer.h:179
Point_t ReferencePoint() const
Returns the reference point for the plane coordinate, as a 3D point.
Definition: Decomposer.h:241
Float_t proj
Definition: plot.C:34
static constexpr double RoundingTol
Rounding threshold for vectors.
Definition: Decomposer.h:44
geo::Vector_t Vector_t
Type for the vector in space.
Definition: Decomposer.h:41
AffinePlaneBase_t fPlaneBase
Reference base.
Definition: Decomposer.h:375
auto PointMainComponent(Point_t const &point) const
Returns the main component of a 3D point.
Definition: Decomposer.h:274
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:39
Point_t ComposePoint(Projection_t const &projection) const
Returns the 3D point from the specified projection.
Definition: Decomposer.h:369
ProjVector Projection_t
Type for 2D projection.
Definition: Decomposer.h:145
Vector_t ComputeNormal() const
Computes the normal to the plane.
Definition: Decomposer.h:80
Vector_t const & NormalDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:114
PlaneDecomposer(AffinePlaneBase_t const &base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:214
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:66
Float_t e
Definition: plot.C:34
recob::tracking::Plane Plane
Definition: TrackState.h:17
typename PlaneDecomposer_t::Point_t Point_t
Type for a vector.
Definition: Decomposer.h:404
Vector_t const & SecondaryDir() const
Returns the secondary axis direction.
Definition: Decomposer.h:57
Namespace collecting geometry-related classes utilities.
Vector_t fMain
Main axis on the plane.
Definition: Decomposer.h:75
recob::tracking::Vector_t Vector_t
Vector_t const & MainDir() const
Returns the main axis direction.
Definition: Decomposer.h:108
Vector_t const & SecondaryDir() const
Returns the plane secondary axis direction.
Definition: Decomposer.h:247
constexpr Point origin()
Returns a origin position with a point of the specified type.
Definition: geo_vectors.h:230
typename PlaneDecomposer_t::Vector_t Vector_t
Definition: Decomposer.h:405
Decomposer(AffinePlaneBase_t const &base)
Constructor: specifies a base (an origin and two direction vectors)
Definition: Decomposer.h:427
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:198
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:63
void SetMainDir(Vector_t const &dir)
Change the main direction of the projection base.
Definition: Decomposer.h:444