17 #ifndef GARSOFT_UTILITIES_GEO_VECTORS_UTILS_H 18 #define GARSOFT_UTILITIES_GEO_VECTORS_UTILS_H 26 #include <type_traits> 45 template <
typename Op,
typename...
T>
49 <
typename Op,
typename First,
typename Second,
typename... Others>
51 static auto compute(Op op, First&&
a, Second&&
b, Others&&... others)
57 (op, std::forward<Second>(
b), std::forward<Others>(others)...)
62 template <
typename Op,
typename T>
64 static auto compute(Op,
T&& v) -> decltype(
auto)
65 {
return std::forward<T>(v); }
68 template <
typename Op,
typename...
T>
73 template <
typename...
T>
76 auto and_op = [](
auto&&
a,
auto&&
b) {
return a &&
b; };
93 template <
typename Vector>
96 template <
typename Vector>
101 template <
typename Vector>
105 template<
typename Class>
106 static constexpr
bool TestX(decltype(std::declval<Class>().
X())*) {
return true; }
107 template<
typename Class>
108 static constexpr
bool TestX(...) {
return false; }
109 template<
typename Class>
110 static constexpr
bool TestY(decltype(std::declval<Class>().
Y())*) {
return true; }
111 template<
typename Class>
112 static constexpr
bool TestY(...) {
return false; }
113 template<
typename Class>
114 static constexpr
bool TestZ(decltype(std::declval<Class>().
Z())*) {
return true; }
115 template<
typename Class>
116 static constexpr
bool TestZ(...) {
return false; }
117 template<
typename Class>
118 static constexpr
bool TestT(decltype(std::declval<Class>().
T())*) {
return true; }
119 template<
typename Class>
120 static constexpr
bool TestT(...) {
return false; }
123 static constexpr
bool X = TestX<Vector>(
nullptr);
124 static constexpr
bool Y = TestY<Vector>(
nullptr);
125 static constexpr
bool Z = TestZ<Vector>(
nullptr);
126 static constexpr
bool T = TestT<Vector>(
nullptr);
129 template <
typename Vector>
131 template <
typename Vector>
133 template <
typename Vector>
135 template <
typename Vector>
138 template <
typename Vector>
140 return HasT<Vector>()? 4U
149 template <
typename Vector>
151 = std::array<VectorScalar_t<Vector>, dimension<Vector>()>;
154 template <
typename T>
159 =
typename decltype(std::mem_fn(std::declval<T>()))::result_type;
162 template <
typename T>
165 template <
typename T>
168 template <
typename Class,
typename Func>
171 template <
typename T>
175 template <
typename Vector,
typename SetterType =
void>
178 template <
typename Vector>
185 template <
typename Vector,
typename SetterType>
198 "Invalid setter type" 203 template <
typename Vector>
215 template <
typename Vector>
238 template <
typename Getter>
241 using Vector_t = std::remove_reference_t<MemberFuncClass_t<Getter>>;
246 template <
typename Vector,
typename SetterType>
259 template <
typename Vector,
typename SetterType>
272 :
Base_t(getter), fSetter(setter) {}
297 template <
typename Getter,
typename Setter>
300 using Vector_t = std::remove_reference_t<MemberFuncClass_t<Getter>>;
306 template <
typename CoordHelper,
typename StoredVector>
319 : fCoord(coordManager), fVector(v) {}
323 : fCoord(getter), fVector(v) {}
344 template <
typename CoordHelper,
typename StoredVector>
361 :
Base_t(v, coordManager) {}
402 template <
typename T>
404 if (
std::abs(value) < tol)
return 0.;
568 template <
typename Vector>
569 constexpr
unsigned int dimension() {
return details::dimension<Vector>(); }
570 template <
typename Vector>
576 template <
typename Vector>
577 constexpr std::array<std::size_t, geo::vect::dimension<Vector>()>
indices();
578 template <
typename Vector>
579 constexpr
auto indices(
Vector const&) -> decltype(indices<Vector>());
583 template <
typename Vector>
606 template <
typename Vector,
typename Coords>
610 template <
typename Vector>
615 template <
typename Vector>
640 template <
typename Vector>
665 template <
typename Vector>
671 template <
typename Vector>
677 template <
typename Vector>
683 template <
typename Vector>
689 template <
typename Vector>
695 template <
typename Vector>
701 template <
typename Vector>
746 template <
typename Vector>
760 template <
typename Vector>
766 template <
typename Vector>
768 template <
typename Vector>
774 template <
typename Vector>
776 template <
typename Vector>
782 template <
typename Vector>
791 template <
typename Vector>
794 {
return { v, helper }; }
807 template <
typename Vector>
809 {
return bindCoord(v, XcoordManager<Vector>); }
821 template <
typename Vector>
823 {
return bindCoord<Vector>(v, YcoordManager<Vector>); }
835 template <
typename Vector>
837 {
return bindCoord<Vector>(v, ZcoordManager<Vector>); }
849 template <
typename Vector>
851 {
return bindCoord<Vector>(v, TcoordManager<Vector>); }
864 template <
typename Vector>
870 template <
typename Vector>
875 template <
typename Vector>
889 template <
typename Dest,
typename Source>
908 template <
typename Dest,
typename Source>
909 std::vector<Dest>
convertCollTo(std::vector<Source>
const& coll);
923 template <
typename Vector,
typename Pred>
963 template <
typename Vector,
typename Scalar>
971 template <
typename Vector,
typename Scalar>
976 template <
typename Vector>
980 template <
typename Vector>
984 template <
typename Vector>
988 template <
typename Vector>
992 template <
typename Vector>
996 template <
typename Vector>
1001 template <
typename Vector>
1040 template <
unsigned int N = 3U>
1042 static constexpr
unsigned int Dim =
N;
1058 template <
typename BeginIter,
typename EndIter>
1061 {
add(begin, end); }
1069 bool empty()
const {
return fW == 0.0; }
1085 template <
typename Po
int>
1088 {
return makeWeightedPoint<Point>(1.0 / fW); }
1093 {
return middlePointAs<geo::Point_t>(); }
1110 template <
typename Po
int>
1113 std::size_t ic = 0U;
1124 template <
typename Po
int>
1127 std::size_t ic = 0U;
1129 fSums[ic++] += weight *
c();
1142 template <
typename BeginIter,
typename EndIter>
1144 { std::for_each(begin, end, [
this](
auto const&
p){ this->
add(
p); }); }
1147 void clear() { fSums.fill(0.); fW = 0.0; }
1155 template <
typename Point, std::size_t...
I>
1157 {
return { fSums.operator[](
I)... }; }
1159 template <
typename Point, std::size_t...
I>
1161 {
return { (fSums.operator[](
I) * w)... }; }
1164 template <
typename Po
int>
1166 {
return geo::vect::makeFromCoords<Point>(fSums); }
1170 template <
typename Po
int>
1209 template <
typename Po
int,
typename BeginIter,
typename EndIter>
1212 constexpr
auto Dim = geo::vect::dimension<Point>();
1214 .
template middlePointAs<Point>();
1238 template <
typename BeginIter,
typename EndIter>
1240 {
return middlePointAs<geo::Point_t>(
begin,
end); }
1258 template <
typename Po
int>
1261 constexpr
auto Dim = geo::vect::dimension<Point>();
1263 .
template middlePointAs<Point>();
1279 template <
typename Po
int>
1281 {
return geo::vect::convertTo<::geo::Point_t>(
p); }
1284 template <
typename Vector>
1286 {
return geo::vect::convertTo<::geo::Vector_t>(v); }
1291 template <
typename Po
int>
1293 (std::vector<Point>
const& coll)
1294 {
return convertCollTo<geo::Point_t>(coll); }
1298 template <
typename Vector>
1300 (std::vector<Vector>
const& coll)
1301 {
return convertCollTo<geo::Vector_t>(coll); }
1305 template <
typename Coords>
1307 {
return makeFromCoords<::geo::Point_t>(std::forward<Coords>(coords)); }
1310 template <
typename Coords>
1312 {
return makeFromCoords<::geo::Vector_t>(std::forward<Coords>(coords)); }
1354 template <
typename T>
1356 {
return static_cast<T&&
>(
t); }
1358 template<
typename T>
1361 static_assert(!std::is_lvalue_reference<T>(),
1362 "template argument substituting T is an lvalue reference type");
1363 return static_cast<T&&
>(
t);
1368 template <
typename Vector>
1371 template <
typename Vector>
1374 template <
typename Vector>
1376 {
return makeVectorIndices<Vector>(); }
1379 template <
typename T, T... Indices>
1383 {
return std::array<
T,
sizeof...(Indices)>{{ Indices... }}; }
1386 template <
typename T, T N>
1388 {
return makeIndexSeqImpl<T>(std::make_integer_sequence<T, N>{}); }
1392 template <std::
size_t I,
typename Data>
1395 template <
typename Vector,
typename Coords, std::size_t... Indices>
1397 (Coords&& coords, std::index_sequence<Indices...>)
1400 { accessElement<Indices>(constexpr_forward<Coords>(coords))... };
1405 template <
typename Vector>
1408 template <
typename Vector,
unsigned int Dim = dimension<Vector>()>
1411 template <
typename Vector>
1413 static auto get(
unsigned int n) noexcept
1414 {
return (n == 0)? XcoordManager<Vector>: NoCoordManager<Vector>; }
1417 template <
typename Vector>
1419 static auto get(
unsigned int n) noexcept
1426 template <
typename Vector>
1428 static auto get(
unsigned int n) noexcept
1435 template <
typename Vector>
1437 static auto get(
unsigned int n) noexcept
1447 template <
typename Vector,
unsigned int N>
1449 static constexpr
unsigned int Dim =
N;
1454 static_assert(dimension<Vector>() == Dim,
"Inconsistent vector size.");
1457 template <
typename Vector,
unsigned int N>
1460 template <
typename Vector>
1471 XcoordManager<Vector>
1472 , YcoordManager<Vector>
1477 template <
typename Vector>
1488 XcoordManager<Vector>
1489 , YcoordManager<Vector>
1490 , ZcoordManager<Vector>
1495 template <
typename Vector>
1506 XcoordManager<Vector>
1507 , YcoordManager<Vector>
1508 , ZcoordManager<Vector>
1509 , TcoordManager<Vector>
1516 template <
typename Vector,
unsigned int N>
1518 static constexpr
unsigned int Dim =
N;
1523 static_assert(dimension<Vector>() == Dim,
"Inconsistent vector size.");
1526 template <
typename Vector,
unsigned int N>
1529 template <
typename Vector>
1546 template <
typename Vector>
1564 template <
typename Vector>
1585 template<
typename Dest,
typename Source>
1587 static_assert(dimension<Source>() == dimension<Dest>(),
1588 "Source and destination vectors must have the same dimension.");
1593 template <
typename Dest,
typename Source,
unsigned int Dim>
1598 "This vector dimensionality is not implemented yet." 1602 template <
typename Dest,
typename Source>
1610 template <
typename Dest,
typename Source>
1618 template <
typename Dest,
typename Source>
1628 <
typename Dest,
typename Source,
unsigned int Dim = dimension<Source>()>
1632 template <
typename Vector,
unsigned int Dim>
1635 (Dim == dimension<Vector>(),
"Inconsistent vector dimension");
1641 template <
typename Point, std::size_t...
I>
1653 template <
typename Vector>
1654 constexpr std::array<std::size_t, geo::vect::dimension<Vector>()>
1657 return details::makeIndexSeq<std::size_t, dimension<Vector>()>();
1660 template <
typename Vector>
1662 {
return indices<Vector>(); }
1666 template <
typename Vector,
typename Coords>
1669 return makeFromCoordsImpl<Vector>
1670 (constexpr_forward<Coords>(coords), makeVectorIndices<Vector>());
1675 template <
typename Vector>
1681 template <
typename Vector>
1683 return vect::bindCoord<Vector>(v, coordManager<Vector>(
n));
1688 template <
typename Vector>
1690 using PlainVector = std::remove_reference_t<Vector>;
1695 template <
typename Vector>
1697 {
return coordManagers<Vector>(); }
1700 template <
typename Vector>
1702 using ConstVector = std::add_const_t<std::remove_reference_t<Vector>>;
1707 template <
typename Vector>
1709 {
return coordReaders<Vector>(); }
1713 template <
typename Vector>
1719 template <
typename Vector>
1721 using ConstVector = std::add_const_t<std::remove_reference_t<Vector>>;
1728 template <
typename Dest,
typename Source>
1734 template <
typename Dest,
typename Source>
1737 std::vector<Dest>
dest;
1738 dest.reserve(coll.size());
1739 std::transform(coll.begin(), coll.end(), std::back_inserter(dest),
1740 geo::vect::convertTo<Dest, Source>);
1747 template <
typename Vector,
typename Pred>
1752 return makeFromCoords<Vector>(
values);
1757 template <
typename Vector>
1764 #endif // GARSOFT_UTILITIES_GEO_VECTORS_UTILS_H
void operator()(Vector_t &v, Scalar_t c) const
Setter: assigns a value to the bound coordinate of specified vector.
constexpr auto coordManager(unsigned int n)
Returns an object that can be bound to a vector to manage one of its coordinates. ...
static constexpr bool TestZ(...)
void round01(Vector &v, Scalar tol)
Returns a vector with all components rounded if close to 0, -1 or +1.
static constexpr auto XcoordManager< Vector const >
Object that can be bound to a vector to access its X coordinate.
Stored_t & fVector
The vector to manage the coordinate of.
typename BaseTypes_t::Scalar_t Scalar_t
BoundCoordManager(Stored_t &v, CoordHelper_t coordManager)
Constructor: manage the specified coordinate of specified vector.
typename Traits_t::Getter_t Getter_t
void clear()
Resets the status of the object to no accumulated points.
typename CoordHelper_t::Getter_t Getter_t
constexpr auto dot(Vector const &a, Vector const &b)
Return cross product of two vectors.
Point makePoint() const
Converts the internal sums into a Point.
constexpr auto accessElement(Data &&data)
static auto compute(Op op, First &&a, Second &&b, Others &&...others) -> decltype(auto)
std::array< Manager_t, Dim > Return_t
Point middlePointAs() const
Returns the middle point, NaN components if no point.
auto Zcoord(Vector &v)
Returns an object to manage the coordinate Z of the vector v.
static constexpr bool TestX(...)
constexpr auto makeIndexSeq()
constexpr auto makeVectorIndices()
static constexpr auto XcoordManager
Object that can be bound to a vector to manage its X coordinate.
Point makePointImpl(std::index_sequence< I... >) const
std::vector< Dest > convertCollTo(std::vector< Source > const &coll)
Returns a vector of type Dest with the same content as a Src.
void mult(Vector_t &v, Scalar_t f) const
Multiplies the coordinate by the specified amount.
DoubleProduct & operator+=(DoubleProduct &left, DoubleProduct const &right)
static constexpr Vector convert(Vector const &v)
std::array< Manager_t, Dim > Return_t
decltype(XcoordManager< Vector >) Manager_t
static constexpr auto YcoordManager< Vector const >
static constexpr auto const YcoordManager
typename BaseTypes_t::Vector_t Vector_t
GENVECTOR_CONSTEXPR::geo::Vector_t makeVectorFromCoords(Coords &&coords)
Creates a geo::Vector_t from its coordinates (see makeFromCoords()).
auto mixedProduct(Vector const &a, Vector const &b, Vector const &c)
auto coord(Vector &v, unsigned int n) noexcept
Returns an object to manage the coordinate n of a vector.
constexpr auto bindCoordReaders(Vector const &v)
typename Vector::Scalar type
Helper class to compute the middle point in a point set.
constexpr auto makeCoordReader(Getter getter)
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >> Point_t
typename Traits_t::Setter_t Setter_t
std::decay_t< Vector > Vector_t
constexpr auto makeIndexSeqImpl(std::integer_sequence< T, Indices... >)
typename MemberFuncReturnType< T >::type MemberFuncReturn_t
Coord add(Coord c1, Coord c2)
double weight() const
Returns the total weight (number of points if all have weight 1).
::geo::Point_t toPoint(Point const &p)
Convert the specified point into a geo::Point_t.
::geo::Vector_t toVector(Vector const &v)
Convert the specified vector into a geo::Vector_t.
static constexpr auto TcoordManager
std::vector< geo::Vector_t > convertCollToVector(std::vector< Vector > const &coll)
constexpr auto bindCoord(Vector const &v, CoordReader_t< Vector > helper)
Binds the specified constant vector to the coordinate reader.
BoundCoordManager(Stored_t &v, Getter_t getter, Setter_t setter)
Constructor: manage the specified vector with specified methods.
static constexpr auto ZcoordManager
decltype(Xcoord(std::declval< Vector >())) Manager_t
static Return_t bind(Vector &v)
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space.
typename BaseTypes_t::Getter_t Getter_t
typename BaseTypes_t::Setter_t Setter_t
constexpr auto coordManagers()
Returns an array with all coordinate managers for a type of vector.
static Return_t bind(Vector &v)
Helper class for read/write of a single vector coordinate.
typename CoordHelper_t::Vector_t Vector_t
typename MemberFuncClassType< T >::type MemberFuncClass_t
BoundCoordGetter(Stored_t &v, Getter_t getter)
Constructor: manage the specified vector with specified methods.
void add(Point const &p)
Accumulates a point.
static constexpr auto ZcoordManager< Vector const >
GENVECTOR_CONSTEXPR::geo::Point_t makePointFromCoords(Coords &&coords)
Creates a geo::Point_t from its coordinates (see makeFromCoords()).
typename BaseTypes_t::Scalar_t Scalar_t
Point middlePointAs(BeginIter begin, EndIter end)
Returns the middle of the specified points.
void add(Point const &p, double weight)
Accumulates a point.
static constexpr bool TestX(decltype(std::declval< Class >().X())*)
typename VectorScalar< Vector >::type VectorScalar_t
typename Traits_t::Vector_t Vector_t
constexpr unsigned int dimension()
static Dest convert(Source const &v)
constexpr T && constexpr_forward(std::remove_reference_t< T > &t)
std::array< Manager_t, Dim > Return_t
constexpr auto coordReaders()
Returns an array with all coordinate readers for a type of vector.
void decr(Vector_t &v, Scalar_t c) const
Decrements the coordinate by the specified amount.
CoordHelper CoordHelper_t
bool isfinite(Vector const &v)
Returns whether all components of the vector are finite.
typename BaseTypes_t::Vector_t Vector_t
typename Traits_t::Scalar_t Scalar_t
Getter_t fGetter
Member function returning the coordinate value.
void add(BeginIter begin, EndIter end)
Adds a sequence of points.
bool empty() const
Returns whether the total weight is zero (usually means no points).
typename BaseTypes_t::Vector_t Vector_t
void incr(Vector_t &v, Scalar_t c) const
Increments the coordinate by the specified amount.
constexpr Vector makeFromCoordsImpl(Coords &&coords, std::index_sequence< Indices... >)
constexpr CoordGetter(Getter_t getter)
Constructor: sets getter and setter functions.
typename BaseTypes_t::Scalar_t Scalar_t
std::tuple< double, double, const reco::ClusterHit3D * > Point
Definitions used by the VoronoiDiagram algorithm.
static Dest convert(Source const &v)
MiddlePointAccumulatorDim(BeginIter begin, EndIter end)
Constructor: starts with accumulating a sequence of points.
def convert(inputfile, outputfile="wire-cell-garfield-fine-response.json.bz2", average=False, shaped=False)
typename CoordHelper_t::Setter_t Setter_t
decltype(auto) begin(ROOT::Math::LorentzVector< CoordSystem > const &v)
typename CoordHelper_t::Scalar_t Scalar_t
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
MemberFuncReturn_t< Setter_t > SetterReturn_t
constexpr auto makeCoordManager(Getter getter, Setter setter)
static Return_t bind(Vector &v)
Point makeWeightedPoint(double w) const
std::array< Manager_t, Dim > Return_t
auto Ycoord(Vector &v)
Returns an object to manage the coordinate Y of the vector v.
typename decltype(std::mem_fn(std::declval< T >()))::result_type type
static constexpr bool TestY(...)
bool isfiniteImpl(Point const &point, std::index_sequence< I... >)
Stored_t & vector() const
Vector transformCoords(Vector const &v, Pred &&pred)
Returns a new vector applying a predicate to each component.
static auto compute(Op, T &&v) -> decltype(auto)
CoordHelper_t fCoord
Helper to manage a specific coordinate.
auto norm(Vector const &v)
Return norm of the specified vector.
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.
std::array< Manager_t, Dim > Return_t
auto Xcoord(Vector &v)
Returns an object to manage the coordinate X of the vector v.
typename BaseTypes_t::Getter_t Getter_t
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< double >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space.
static Dest convert(Source const &v)
Point makeWeightedPointImpl(double w, std::index_sequence< I... >) const
BoundCoordGetter(Stored_t &v, CoordHelper_t coordManager)
Constructor: manage the specified coordinate of specified vector.
std::array< Manager_t, Dim > Return_t
auto array(Array const &a)
Returns a manipulator which will print the specified array.
auto Tcoord(Vector &v)
Returns an object to manage the coordinate T of the vector v.
geo::Point_t middlePoint() const
CoordHelper_t const & manager() const
auto extended_accumulate(Op op, T &&...args)
typename BaseTypes_t::Getter_t Getter_t
auto extended_and(T...args) -> decltype(auto)
std::vector< geo::Point_t > convertCollToPoint(std::vector< Point > const &coll)
std::make_index_sequence< Dim > IndexSequence_t
static constexpr auto TcoordManager< Vector const >
std::array< VectorScalar_t< Vector >, dimension< Vector >()> CoordinateArray_t
A STL array suitable to contain all coordinate values of a Vector.
decltype(details::makeCoordReader(&Vector::X)) CoordReader_t
Type of a coordinate reader for a vector type.
decltype(auto) end(ROOT::Math::LorentzVector< CoordSystem > const &v)
constexpr auto bindCoordManagers(Vector &v)
constexpr CoordManager(Getter_t getter, Setter_t setter)
Constructor: sets getter and setter functions.
std::array< Manager_t, Dim > Return_t
std::array< Manager_t, Dim > Return_t
Setter_t fSetter
Member function setting the coordinate value.
MiddlePointAccumulatorDim()
Default constructor: starts with no accumulated point.
Helper class for read of a single vector coordinate.
std::array< Length_t, Dim > fSums
Sum of each of the point components.
details::VectorScalar_t< Vector > coordinate_t
Type of coordinate of the specified vector type.
VectorScalar_t< Vector > Scalar_t
constexpr Vector makeFromCoords(Coords &&coords)
Creates a Vector object with coordinates from coords.
static constexpr bool TestT(...)
std::make_index_sequence< dimension< Vector >()> VectorIndices_t
Type of sequence of indices up to Vector size.
Vector cross(Vector const &a, Vector const &b)
Return cross product of two vectors.
Vector normalize(Vector const &v)
Returns a vector parallel to v and with norm 1.
Dest convertTo(Source const &v)
Returns a vector of type Dest with the same content as a Src.
auto const & get(AssnsNode< L, R, D > const &r)
void div(Vector_t &v, Scalar_t f) const
Divides the coordinate by the specified amount.
decltype(details::makeCoordManager(&Vector::X,&Vector::SetX)) CoordManager_t
Type of a coordinate manager for a vector type.
LArSoft geometry interface.
recob::tracking::Vector_t Vector_t
geo::Point_t middlePoint(BeginIter begin, EndIter end)
Returns the middle of the specified points.
Scalar_t(Vector_t::*)() const Getter_t
constexpr CoordManager_t< Vector > NoCoordManager
static constexpr bool TestY(decltype(std::declval< Class >().Y())*)
static constexpr bool TestZ(decltype(std::declval< Class >().Z())*)
static constexpr bool TestT(decltype(std::declval< Class >().T())*)