LocalTransformation.h
Go to the documentation of this file.
1 /**
2  * @file larcorealg/Geometry/LocalTransformation.h
3  * @brief Class containing local-to-world transformations
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date November 30, 2016
6  * @ingroup Geometry
7  *
8  */
9 
10 #ifndef LARCOREALG_GEOMETRY_LOCALTRANSFORMATION_H
11 #define LARCOREALG_GEOMETRY_LOCALTRANSFORMATION_H
12 
13 
14 // ROOT libraries
15 // (none)
16 
17 // C/C++ standard libraries
18 #include <vector>
19 #include <utility> // std::move(), std::forward()
20 #include <type_traits> // std::enable_if_t<>, std::decay_t<>
21 #include <cstdlib> // std::size_t
22 
23 
24 // forward declarations
25 class TGeoNode;
26 
27 
28 namespace geo {
29 
30  namespace details {
31  template <typename Dest, typename Src>
33  } // namespace details
34 
35 
37 
38 
39  /// Builds a matrix to go from local to world coordinates in one step
40  template <typename StoredMatrix, typename ITER>
41  static StoredMatrix transformationFromPath(ITER begin, ITER end);
42 
43  /// Builds a matrix to go from local to world coordinates in one step
44  template <typename StoredMatrix>
45  static StoredMatrix transformationFromPath(std::vector<TGeoNode const*> const& path, size_t depth);
46  // { return transformationFromPath(path.begin(), path.begin() + depth); }
47 
48 
49  /**
50  * @brief Class to transform between world and local coordinates
51  * @tparam StoredMatrix type of transformation matrix internally stored
52  * @ingroup Geometry
53  *
54  * This class provides two directions of transformations (world to local and
55  * the other way around), for points and for vectors.
56  * The vector version of the transformation does not apply translation.
57  *
58  * @note In the class method examples, the following definition is assumed:
59  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
60  * using LocalTransformation_t = geo::LocalTransformation<TGeoHMatrix>;
61  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62  */
63  template <typename StoredMatrix>
65  public:
66 
67  /// Type of transformation matrix
68  using TransformationMatrix_t = StoredMatrix;
69 
70  //@{
71  /**
72  * @brief Constructor: uses the specified local-to-world transformation.
73  * @param matrix the transformation matrix to be used
74  *
75  * The transformation `matrix` is used to transform vectors from the "local"
76  * to the "world" frame, while its inverse is used for transformations from
77  * the "world" to the "local" frame.
78  *
79  * The specified matrix is copied into a local copy unless a R-value
80  * reference argument is specified (e.g. with `std::move()`).
81  */
83  : fGeoMatrix(matrix) {}
85  : fGeoMatrix(std::move(matrix)) {}
86  //@}
87 
88  /**
89  * @brief Constructor: chains the transformations from the specified nodes.
90  * @param path the path of ROOT geometry nodes
91  * @param depth the index in the path of the last node to be considered
92  *
93  * The resulting transformation is the sequence of transformations from
94  * `depth` nodes from the first on.
95  */
96  LocalTransformation(std::vector<TGeoNode const*> const& path, size_t depth)
97  : fGeoMatrix
98  (transformationFromPath<StoredMatrix>(path.begin(), path.begin() + depth + 1))
99  {}
100 
101 
102  /**
103  * @brief Constructor: chains the transformations from all specified nodes.
104  * @param path the path of ROOT geometry nodes
105  *
106  * The resulting transformation is the sequence of transformations from
107  * the first to the last node of the path.
108  */
109  LocalTransformation(std::vector<TGeoNode const*> const& path)
110  : LocalTransformation(path, path.size()) {}
111 
112  /**
113  * @brief Constructor: sequence of transformations from a node path.
114  * @tparam ITER type of iterator to node pointers
115  * @param begin the begin iterator of the path of ROOT geometry nodes
116  * @param end the end iterator of the path of ROOT geometry nodes
117  *
118  * The resulting transformation is the sequence of transformations from
119  * the one pointed by `begin` to the one before `end`.
120  */
121  template <typename ITER>
123  : fGeoMatrix(transformationFromPath<StoredMatrix>(begin, end)) {}
124 
125 
126  /**
127  * @brief Transforms a point from local frame to world frame
128  * @param local local coordinates: [0] x, [1] y, [2] z [cm]
129  * @param world (output) corresponding world coordinates [cm]
130  *
131  * The full transformation is applied. Fox example:
132  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133  * LocalTransformation_t trans( ... ); // with proper initialisation
134  *
135  * std::array<double, 3U> origin, center;
136  * origin.fill(0.);
137  * trans.LocalToWorld(origin.data(), center.data());
138  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139  * `center` will contain the world coordinates of the center of the volume,
140  * which is usually represented by the origin in the local coordinates.
141  *
142  * In-place replacement is *not* supported: `world` and `local` buffers are
143  * assumed not to, and must not, overlap.
144  */
145  void LocalToWorld(double const* local, double* world) const;
146 
147 
148  // @{
149  /**
150  * @brief Transforms a point from local frame to world frame
151  * @tparam SrcPoint type of the input (local) vector
152  * @tparam DestPoint type of the output (world) vector (default: as `Point`)
153  * @param local local coordinates [cm]
154  * @return corresponding world coordinates [cm]
155  *
156  * The full transformation is applied. Fox example:
157  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158  * LocalTransformation_t trans( ... ); // with proper initialisation
159  *
160  * auto center = trans.LocalToWorld(TVector3());
161  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162  * `center` will be a `TVector3` containing the world coordinates of the
163  * center of the volume, which is usually represented by the origin in the
164  * local coordinates (a TVector3 is by default constructed to point to the
165  * origin).
166  */
167  template <
168  typename DestPoint, typename SrcPoint,
170  >
171  DestPoint LocalToWorld(SrcPoint const& local) const
172  { return LocalToWorldImpl<DestPoint>(local); }
173  template <typename Point>
174  Point LocalToWorld(Point const& local) const
175  { return LocalToWorldImpl<Point>(local); }
176  // @}
177 
178  /**
179  * @brief Transforms a vector from local frame to world frame
180  * @param local local coordinates: [0] x, [1] y, [2] z [cm]
181  * @param world (output) corresponding world coordinates [cm]
182  *
183  * The translation is not applied, since the argument is supposed to be a
184  * vector, relative difference between two points.
185  *
186  * In-place replacement is *not* supported: `world` and `local` buffers are
187  * assumed not to, and must not, overlap.
188  */
189  void LocalToWorldVect(double const* local, double* world) const;
190 
191  //@{
192  /**
193  * @brief Transforms a vector from local frame to world frame
194  * @tparam SrcVector type of the input (local) vector
195  * @tparam DestVector type of output (world) vector (default: as `Vector`)
196  * @param local local coordinates [cm]
197  * @return corresponding world coordinates [cm]
198  *
199  * The translation is not applied, since the argument is supposed to be a
200  * vector, relative difference between two points.
201  */
202  template <
203  typename DestVector, typename SrcVector,
205  >
206  DestVector LocalToWorldVect(SrcVector const& local) const
207  { return LocalToWorldVectImpl<DestVector>(local); }
208  template <typename Vector>
209  Vector LocalToWorldVect(Vector const& local) const
210  { return LocalToWorldVectImpl<Vector>(local); }
211  //@}
212 
213 
214  /**
215  * @brief Transforms a point from world frame to local frame
216  * @param world world coordinates: [0] x, [1] y, [2] z [cm]
217  * @param local (output) corresponding local coordinates [cm]
218  *
219  * The full transformation is applied. Fox example:
220  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221  * LocalTransformation_t trans( ... ); // with proper initialisation
222  *
223  * std::array<double, 3U> world{ 4.0, 5.0, -2.5 }, local;
224  * trans.WorldToLocal(world.data(), local.data());
225  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226  * `local` will contain the local coordinates of the specified point.
227  *
228  * In-place replacement is *not* supported: `world` and `local` buffers are
229  * assumed not to, and must not, overlap.
230  */
231  void WorldToLocal(double const* world, double* local) const;
232 
233  //@{
234  /**
235  * @brief Transforms a point from world frame to local frame
236  * @tparam SrcPoint type of the input (local) vector
237  * @tparam DestPoint type of the output (world) vector (default: as `Point`)
238  * @param world world coordinates [cm]
239  * @return corresponding local coordinates [cm]
240  *
241  * The full transformation is applied. Fox example:
242  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243  * LocalTransformation_t trans( ... ); // with proper initialisation
244  *
245  * auto local = trans.WorldToLocal(TVector3(4.0, 5.0, -2.5));
246  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247  * `local` will be a `TVector3` containing the local coordinates of the
248  * specified point.
249  */
250  template <
251  typename DestPoint, typename SrcPoint,
253  >
254  DestPoint WorldToLocal(SrcPoint const& world) const
255  { return WorldToLocalImpl<DestPoint>(world); }
256  template <typename Point>
257  Point WorldToLocal(Point const& world) const
258  { return WorldToLocalImpl<Point>(world); }
259  //@}
260 
261  /**
262  * @brief Transforms a vector from world frame to local frame
263  * @param world world coordinates: [0] x, [1] y, [2] z [cm]
264  * @param local (output) corresponding local coordinates [cm]
265  *
266  * The translation is not applied, since the argument is supposed to be a
267  * vector, relative difference between two points.
268  *
269  * In-place replacement is *not* supported: `world` and `local` buffers are
270  * assumed not to, and must not, overlap.
271  */
272  void WorldToLocalVect(const double* world, double* local) const;
273 
274 
275  //@{
276  /**
277  * @brief Transforms a vector from world frame to local frame
278  * @tparam SrcVector type of the input (local) vector
279  * @tparam DestVector type of output (world) vector (default: as `Vector`)
280  * @param world coordinates [cm]
281  * @return corresponding world coordinates [cm]
282  *
283  * The translation is not applied, since the argument is supposed to be a
284  * vector, relative difference between two points.
285  */
286  template <
287  typename DestVector, typename SrcVector,
289  >
290  DestVector WorldToLocalVect(SrcVector const& world) const
291  { return WorldToLocalVectImpl<DestVector>(world); }
292  template <typename Vector>
293  Vector WorldToLocalVect(Vector const& world) const
294  { return WorldToLocalVectImpl<Vector>(world); }
295  //@}
296 
297 
298  /// Direct access to the transformation matrix
299  TransformationMatrix_t const& Matrix() const { return fGeoMatrix; }
300 
301  protected:
302 
303  TransformationMatrix_t fGeoMatrix; ///< local to world transform
304 
305 
306  template <typename DestPoint, typename SrcPoint>
307  DestPoint LocalToWorldImpl(SrcPoint const& local) const;
308 
309  template <typename DestVector, typename SrcVector>
310  DestVector LocalToWorldVectImpl(SrcVector const& local) const;
311 
312  template <typename DestPoint, typename SrcPoint>
313  DestPoint WorldToLocalImpl(SrcPoint const& world) const;
314 
315  template <typename DestVector, typename SrcVector>
316  DestVector WorldToLocalVectImpl(SrcVector const& world) const;
317 
318  }; // class LocalTransformation<>
319 
320 
321  /// Converts a transformation matrix into `Dest` format.
322  template <typename Dest, typename Src>
323  decltype(auto) convertTransformationMatrix(Src&& trans)
324  {
326  <std::decay_t<Dest>, std::decay_t<Src>>::convert
327  (std::forward<Src>(trans));
328  }
329 
330 
331 } // namespace geo
332 
333 
334 //------------------------------------------------------------------------------
335 // template implementation
336 
337 #include "LocalTransformation.tcc"
338 
339 //------------------------------------------------------------------------------
340 
341 
342 #endif // LARCOREALG_GEOMETRY_LOCALTRANSFORMATION_H
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
DestPoint WorldToLocal(SrcPoint const &world) const
Transforms a point from world frame to local frame.
decltype(auto) convertTransformationMatrix(Src &&trans)
Converts a transformation matrix into Dest format.
geo::TransformationMatrix TransformationMatrix_t
Type of transformation matrix.
TransformationMatrix_t const & Matrix() const
Direct access to the transformation matrix.
STL namespace.
DestVector LocalToWorldVect(SrcVector const &local) const
Transforms a vector from local frame to world frame.
intermediate_table::const_iterator const_iterator
Vector LocalToWorldVect(Vector const &local) const
LocalTransformation(std::vector< TGeoNode const * > const &path)
Constructor: chains the transformations from all specified nodes.
DestVector WorldToLocalVect(SrcVector const &world) const
Transforms a vector from world frame to local frame.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
LocalTransformation(std::vector< TGeoNode const * > const &path, size_t depth)
Constructor: chains the transformations from the specified nodes.
Class to transform between world and local coordinates.
def move(depos, offset)
Definition: depos.py:107
def convert(inputfile, outputfile="wire-cell-garfield-fine-response.json.bz2", average=False, shaped=False)
Definition: garfield.py:262
TransformationMatrix_t fGeoMatrix
local to world transform
LocalTransformation(TransformationMatrix_t const &matrix)
Constructor: uses the specified local-to-world transformation.
LocalTransformation(ITER begin, ITER end)
Constructor: sequence of transformations from a node path.
static StoredMatrix transformationFromPath(ITER begin, ITER end)
Builds a matrix to go from local to world coordinates in one step.
DestPoint LocalToWorld(SrcPoint const &local) const
Transforms a point from local frame to world frame.
Vector WorldToLocalVect(Vector const &world) const
Point LocalToWorld(Point const &local) const
Point WorldToLocal(Point const &world) const
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
LocalTransformation(TransformationMatrix_t &&matrix)
LArSoft geometry interface.
Definition: ChannelGeo.h:16
std::vector< TGeoNode const * >::const_iterator GeoNodeIterator_t