Ocean
SmoothedTransformation.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_TRACKING_SMOOTH_TRANSFORMATION_H
9 #define META_OCEAN_TRACKING_SMOOTH_TRANSFORMATION_H
10 
12 
13 #include "ocean/base/Lock.h"
14 #include "ocean/base/Timestamp.h"
15 
17 #include "ocean/math/Numeric.h"
18 
19 namespace Ocean
20 {
21 
22 namespace Tracking
23 {
24 
25 /**
26  * This class implements a smoother for a 12-DOF transformations (3D translation, 3D rotation, 3D scale, and 3D shear).
27  * The transformation can be updated as often as necessary, while an internal smoothing interval is used to create a smooth transition between the current transformation and the given transformation.
28  * <pre>
29  * new transformation
30  * old transformation new transformation + smoothing interval
31  * | | |
32  * V V V
33  * timeline: ------------------------------------------------------------------------------------------------------------
34  * the old transformation | the interpolated transformation | the new transformation
35  * will be reported | will be reported | will be reported
36  * </pre>
37  *
38  * The class is thread-safe.
39  * @ingroup tracking
40  */
41 class OCEAN_TRACKING_EXPORT SmoothedTransformation
42 {
43  public:
44 
45  /**
46  * Creates a new transformation object.
47  * @param smoothingInterval The time interval in which the old transformation will be merged/inerpolated into the new transformation, in seconds, with range (0, infinity)
48  */
49  explicit SmoothedTransformation(const double smoothingInterval = 1.0);
50 
51  /**
52  * Sets or updates a new transformation.
53  * @param transformation The new (e.g., the latest) 12-DOF transformation to be set, must be valid
54  * @param timestamp The timestamp of the given transformation, must be valid, with range [timestamp(), infinity)
55  */
56  void setTransformation(const HomogenousMatrix4& transformation, const Timestamp& timestamp);
57 
58  /**
59  * Sets a new smoothing interval.
60  * @param smoothingInterval The time interval in which the old transformation will be merged/inerpolated into the new transformation, in seconds, with range (0, infinity)
61  */
62  inline void setSmoothingInterval(const double smoothingInterval);
63 
64  /**
65  * Returns the smoothed 12-DOF transformation of this object.
66  * @param timestamp The timestamp for which the smoothed transformation is requested, with range (-infinity, infinity), must be valid
67  * @return The smoothed 12-DOF transformation, invalid if no transformation was set
68  */
69  HomogenousMatrix4 transformation(const Timestamp& timestamp) const;
70 
71  /**
72  * Returns the timestamp of the latest transformation.
73  * @param transformation Optional resulting transformation associated with the latest transformation, invalid if the resulting timestamp is invalid
74  * @return The latest timestamp, may be invalid
75  */
76  inline Timestamp timestamp(HomogenousMatrix4* transformation = nullptr) const;
77 
78  /**
79  * Resets this transformation object and removes all previously set transformations.
80  * The smoothing interval is untouched.
81  */
82  void reset();
83 
84  /**
85  * Returns whether this object holds a valid transformation.
86  * @return True, if so
87  */
88  inline bool isValid() const;
89 
90  private:
91 
92  /// The time interval in which the old transformation will be merged/inerpolated into the new transformation, in seconds, with range (0, infinity).
94 
95  /// The old 12-DOF transformation connected with the old timestamp.
97 
98  /// The new 12-DOF transformation connected with the new timestamp.
100 
101  /// The timestamp of the old transformation.
103 
104  /// The timestamp of the new transformation.
106 
107  /// The lock of this transformation object.
108  mutable Lock lock_;
109 };
110 
111 inline void SmoothedTransformation::setSmoothingInterval(const double smoothingInterval)
112 {
113  ocean_assert(smoothingInterval > NumericD::eps());
114 
115  const ScopedLock scopedLock(lock_);
116 
117  smoothingInterval_ = smoothingInterval;
118 }
119 
121 {
122  const ScopedLock scopedLock(lock_);
123 
124  ocean_assert(oldTransformation_.isValid() == oldTimestamp_.isValid());
125  ocean_assert(newTransformation_.isValid() == newTimestamp_.isValid());
126 
127  if (newTimestamp_.isValid())
128  {
129  if (transformation != nullptr)
130  {
132  }
133 
134  return newTimestamp_;
135  }
136 
137  if (transformation != nullptr)
138  {
140  }
141 
142  return oldTimestamp_;
143 }
144 
146 {
147  const ScopedLock scopedLock(lock_);
148 
149  return oldTransformation_.isValid();
150 }
151 
152 }
153 
154 }
155 
156 #endif // META_OCEAN_TRACKING_SMOOTH_TRANSFORMATION_H
bool isValid() const
Returns whether this matrix is a valid homogeneous transformation.
Definition: HomogenousMatrix4.h:1806
This class implements a recursive lock object.
Definition: Lock.h:31
static constexpr T eps()
Returns a small epsilon.
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
This class implements a timestamp.
Definition: Timestamp.h:36
bool isValid() const
Returns whether the timestamp holds a valid time.
Definition: Timestamp.h:303
This class implements a smoother for a 12-DOF transformations (3D translation, 3D rotation,...
Definition: SmoothedTransformation.h:42
HomogenousMatrix4 oldTransformation_
The old 12-DOF transformation connected with the old timestamp.
Definition: SmoothedTransformation.h:96
void setSmoothingInterval(const double smoothingInterval)
Sets a new smoothing interval.
Definition: SmoothedTransformation.h:111
HomogenousMatrix4 newTransformation_
The new 12-DOF transformation connected with the new timestamp.
Definition: SmoothedTransformation.h:99
double smoothingInterval_
The time interval in which the old transformation will be merged/inerpolated into the new transformat...
Definition: SmoothedTransformation.h:93
HomogenousMatrix4 transformation(const Timestamp &timestamp) const
Returns the smoothed 12-DOF transformation of this object.
Timestamp oldTimestamp_
The timestamp of the old transformation.
Definition: SmoothedTransformation.h:102
SmoothedTransformation(const double smoothingInterval=1.0)
Creates a new transformation object.
Lock lock_
The lock of this transformation object.
Definition: SmoothedTransformation.h:108
void setTransformation(const HomogenousMatrix4 &transformation, const Timestamp &timestamp)
Sets or updates a new transformation.
void reset()
Resets this transformation object and removes all previously set transformations.
Timestamp timestamp(HomogenousMatrix4 *transformation=nullptr) const
Returns the timestamp of the latest transformation.
Definition: SmoothedTransformation.h:120
Timestamp newTimestamp_
The timestamp of the new transformation.
Definition: SmoothedTransformation.h:105
bool isValid() const
Returns whether this object holds a valid transformation.
Definition: SmoothedTransformation.h:145
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15