a/math: Add a "get latest" to relation history as well.

This commit is contained in:
Ryan Pavlik 2021-12-10 10:34:59 -06:00 committed by Jakob Bornecrantz
parent aff7079dca
commit ef4550c268
3 changed files with 49 additions and 3 deletions

View file

@ -84,7 +84,7 @@ m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation
}
enum m_relation_history_result
m_relation_history_get( struct m_relation_history *rh, uint64_t at_timestamp_ns, struct xrt_space_relation *out_relation)
m_relation_history_get(struct m_relation_history *rh, uint64_t at_timestamp_ns, struct xrt_space_relation *out_relation)
{
XRT_TRACE_MARKER();
os_mutex_lock(&rh->mutex);
@ -228,6 +228,22 @@ end:
return ret;
}
bool
m_relation_history_get_latest(struct m_relation_history *rh,
uint64_t *out_time_ns,
struct xrt_space_relation *out_relation)
{
os_mutex_lock(&rh->mutex);
if (rh->impl.length() == 0) {
os_mutex_unlock(&rh->mutex);
return false;
}
*out_relation = rh->impl[0]->relation;
*out_time_ns = rh->impl[0]->timestamp;
os_mutex_unlock(&rh->mutex);
return true;
}
uint32_t
m_relation_history_get_size(const struct m_relation_history *rh)
{

View file

@ -67,6 +67,22 @@ m_relation_history_push(struct m_relation_history *rh, struct xrt_space_relation
enum m_relation_history_result
m_relation_history_get(struct m_relation_history *rh, uint64_t at_time_ns, struct xrt_space_relation *out_relation);
/*!
* @brief Get the latest report in the buffer, if any.
*
* @param rh self
* @param[out] out_time_ns Populated with the latest report time, if any
* @param[out] out_relation Populated with the latest relation, if any
*
* @return false if the history is empty.
*
* @public @memberof m_relation_history
*/
bool
m_relation_history_get_latest(struct m_relation_history *rh,
uint64_t *out_time_ns,
struct xrt_space_relation *out_relation);
/*!
* @brief Returns the number of items in the history.
*

View file

@ -40,22 +40,36 @@ TEST_CASE("m_relation_history")
// two seconds after T0
constexpr auto T2 = T1 + (uint64_t)U_TIME_1S_IN_NS;
xrt_space_relation out_relation = XRT_SPACE_RELATION_ZERO;
uint64_t out_time = 0;
CHECK(m_relation_history_get_size(rh) == 0);
CHECK_FALSE(m_relation_history_get_latest(rh, &out_time, &out_relation));
CHECK(m_relation_history_push(rh, &relation, T0));
CHECK(m_relation_history_get_size(rh) == 1);
CHECK(m_relation_history_get_latest(rh, &out_time, &out_relation));
CHECK(out_time == T0);
relation.pose.position.x = 1.f;
CHECK(m_relation_history_push(rh, &relation, T1));
CHECK(m_relation_history_get_size(rh) == 2);
CHECK(m_relation_history_get_latest(rh, &out_time, &out_relation));
CHECK(out_time == T1);
relation.pose.position.x = 2.f;
CHECK(m_relation_history_push(rh, &relation, T2));
CHECK(m_relation_history_get_size(rh) == 3);
CHECK(m_relation_history_get_latest(rh, &out_time, &out_relation));
CHECK(out_time == T2);
// Try going back in time: should fail
// Try going back in time: should fail to push, leave state the same
CHECK_FALSE(m_relation_history_push(rh, &relation, T1));
CHECK(m_relation_history_get_size(rh) == 3);
CHECK(m_relation_history_get_latest(rh, &out_time, &out_relation));
CHECK(out_time == T2);
xrt_space_relation out_relation = XRT_SPACE_RELATION_ZERO;
CHECK(m_relation_history_get(rh, 0, &out_relation) == M_RELATION_HISTORY_RESULT_INVALID);
CHECK(m_relation_history_get(rh, T0, &out_relation) == M_RELATION_HISTORY_RESULT_EXACT);