MOLA keyframe metadata¶
Simple-maps (.simplemap files, mrpt::maps::CSimpleMap) store a sequence of keyframes, each one a
pose and the observations taken there. Tools that build or process a map often need facts about each
keyframe that are not observations: the bounding box of its scan, the velocities used to de-skew it, the place
it belongs to… The MOLA keyframe metadata is where those facts are kept, inside the keyframe itself, so
they follow it through every tool that cuts, joins, levels or transforms the map.
This page describes version 1 of the format.
Where it is stored¶
Each keyframe holds at most one
mrpt::obs::CObservationCommentwithsensorLabelequal tometadata(if a keyframe has several, the first one counts). Itstimestampis the reference time of the keyframe (for LiDAR odometry, the scan time the keyframe pose refers to).Its
textis a YAML map, written in block style. Each top-level key holds one fact about that keyframe.
Example, from a keyframe saved by MOLA-LO and partitioned into places afterwards (abridged: the velocity buffer holds many more samples, and numbers are written with up to 17 significant digits):
frame_bbox_min: '[-1.983328 -15.201783 -0.989350]'
frame_bbox_max: '[5.140738 2.812885 1.424790]'
local_velocity_buffer:
parameters:
max_time_window: 0.5
state:
reference_zero_time: 1649764542.0114455
linear_velocities:
1649764541.6109204: '[0.701129 -0.173613 -0.070121]'
angular_velocities:
1649764541.5612061: '[0.107190 -0.262536 0.041762]'
deskew_twist:
- 0.70112
- -0.17361
- -0.07012
- 0.10719
- -0.26254
- 0.04176
place:
id: 3
flags:
- closed
params_hash: 2873211234
Versioning¶
The optional top-level key
version(an integer) gives the format version. A missingversionmeans 1: that is what MOLA-LO has always written (the legacy format is version 1). Version 1 writers do not write it.Adding new keys does not change the version. It only increases if the meaning or layout of an existing key changes in a way old readers would misread.
Readers check it (
keyframe_metadata_version()in C++). A reader that finds a version higher than the one it knows should not trust the keys it reads, and warn.A writer that finds a higher version keeps the
versionkey (never lowers it) and does not change that metadata: the meaning of its own key may have changed too. The C++ helpers refuse such changes (also changing or removingversionitself), andsm-cli joinkeeps such keyframes as they are, without reading their place (it warns, once, if one of them has aplacekey, whose id then may clash with other maps).
Rules¶
Ownership of keys. Each top-level key belongs to one producer, the only tool that writes or changes it.
Existing keys keep their names. Two exceptions, both for place: any partition of the map writes it the same
way, and the last one replaces it; and sm-cli join renumbers its id (see below), the only change a tool
other than a producer makes to a key.
New keys. A tool that needs a new key picks a descriptive snake_case name, prefixed with the tool’s name if
it could clash (e.g. mytool_score), and adds it to the table below with a pull request to this page.
Keep what you do not know. Every tool that rewrites a keyframe (to add its own key, to move its pose, to copy it into another map…) keeps all the keys it does not own, with their values. The C++ helpers below keep even the text of those entries, since parsing and writing YAML again may change how numbers and strings are written. Tools in other languages may parse and write the map again, as long as every unknown key reads back the same.
Never change the comment in place. Keyframes of different maps (copies of a CSimpleMap, the central map of
a running system and its readers) often share their observation objects. To change the metadata, replace the
comment observation in the keyframe’s CSensoryFrame with a modified copy (the C++ helpers do), and change the
CSensoryFrame of the map being written, not one shared with other maps.
Per-keyframe facts only. Nothing about the whole map goes into the metadata of one keyframe (e.g. a list of places in the first keyframe): cutting the map, or joining it with another one, would break it. Map-level views, such as the list of places with their keyframes, are rebuilt from the keyframes when a map is loaded.
Do not assume a key is there. A keyframe may carry the keys of some tools and not of others (a map built with another front end, a keyframe added by hand…), and may have no metadata at all.
Known keys¶
Key |
Producer |
Contents |
Readers |
|---|---|---|---|
|
Bounding box of the keyframe’s points in the vehicle frame, as a quoted string |
Simple-map loop closure (submap bounding boxes). |
|
|
MOLA-LO |
Samples around the scan time, to de-skew the scan again: linear and angular velocities and linear
accelerations in the vehicle frame, and attitude samples ( |
sm2mm (de-skew filters), MOLA-LO (local map rebuilds). |
|
MOLA-LO |
|
MOLA-LO (local map rebuilds). |
|
MOLA-LO (map gravity estimation) |
Written on every keyframe after the odometry re-leveled its map frame during the run (a quoted pose string): the poses of those keyframes are already expressed in the re-leveled frame, so the map is not comparable, pose by pose, with a run without that change. |
Informational. |
|
|
The place of the keyframe, see below. |
|
|
Any writer of a version other than 1 |
Format version (see above). |
All readers. |
The place key. A place is a group of keyframes that observe the same part of the environment,
whenever they were visited (a room, a corridor, a floor). Its fields:
id: an unsigned 32-bit integer, unique within the map but not necessarily consecutive. Keyframes with the same id belong to the same place.flags(optional, a sequence of strings):unpartitionedwhen the partition found no clear boundary inside that part and kept it whole (e.g. a path in the open: it may span what one would call several places), andclosedwhen the producer stopped changing that place (e.g. it was not observed for a while). Unknown flags, of any type, are ignored.params_hash(optional): a 32-bit FNV-1a hash of the options that build the graph between keyframes (node sampling, signatures, the points pipeline), 0 if unknown.sm-cli level-walls --by-submapbuilds that graph again, and says so when its own options give another hash.
What the map tools do with it¶
sm-cli keeps the metadata of every keyframe it copies:
cutandtrimcopy keyframes with their metadata unchanged. A place may then be only partly in the output; its keyframes keep their id.joinkeeps the metadata, but renumbers place ids: those of each input map are moved past the largest id of the maps before it, so places of different maps never share an id (not for metadata of a newer version).level,level-wallsandtfchange poses only, and leave the metadata as it was. Bounding boxes, velocities, accelerations and twists are in the vehicle frame and stay valid; the attitude samples oflocal_velocity_bufferandmap_frame_relevel_rotationkeep describing the map frame of the original run.partitionwritesplace(replacing an older one) and keeps every other key.
C++ API¶
The helpers are in mp2p_icp’s mp2p_icp_map library (#include <mp2p_icp/keyframe_metadata.h>, namespace
mp2p_icp, feature macro MP2P_ICP_HAS_KEYFRAME_METADATA; code that must also build with older mp2p_icp
versions can guard the include with __has_include and the code with the macro):
#include <mp2p_icp/keyframe_metadata.h>
#include <mrpt/maps/CSimpleMap.h>
#include <iostream>
#include <stdexcept>
mrpt::maps::CSimpleMap sm;
if (!sm.loadFromFile("map.simplemap"))
{
throw std::runtime_error("Cannot read map.simplemap");
}
for (auto& kf : sm)
{
if (!kf.sf)
{
continue;
}
try
{
// Read: the YAML map, or nothing if the keyframe has no metadata
const auto md = mp2p_icp::get_keyframe_metadata(*kf.sf);
if (md && mp2p_icp::keyframe_metadata_version(*md) != 1)
{
continue; // a newer format: neither read nor changed here
}
if (md && md->has("place"))
{
const auto placeId = (*md)["place"]["id"].as<uint32_t>();
std::cout << "place " << placeId << "\n";
}
// Write a key of your own on a copy of the keyframe's sensory frame
// (other maps may share it); the other entries are kept.
mrpt::containers::yaml value = mrpt::containers::yaml::Map();
value["score"] = 0.8;
auto sf = std::make_shared<mrpt::obs::CSensoryFrame>(*kf.sf);
mp2p_icp::set_keyframe_metadata(*sf, "mytool_score", value);
kf.sf = sf;
}
catch (const std::exception& e)
{
// A malformed entry, or a comment that is not a YAML map: that
// keyframe is left as it was
std::cerr << "Keyframe not changed: " << e.what() << "\n";
}
}
if (!sm.saveToFile("map_out.simplemap"))
{
throw std::runtime_error("Cannot write map_out.simplemap");
}
What the helpers do:
get_keyframe_metadata()returns the YAML map of the first metadata comment, or nothing if the keyframe has no metadata or its text is not a YAML map.find_keyframe_metadata_obs()returns the comment itself (e.g. for its timestamp), andkeyframe_metadata_version()its version.set_keyframe_metadata(sf, key, value)sets one key (a null value removes it); an overload takes a YAML map and sets several keys at once. It changessf: the comment observation is replaced by a modified copy, or, if the keyframe has none, a new one is added, stamped with the time given or else with the timestamp of the keyframe’s first observation that has one (pass the keyframe reference time when you know it).The other entries keep their text when the comment is a block-style map with plain or simply quoted keys (what every producer writes); anything else is parsed and written again whole, and its entries read back the same.
Strings: a string that is already a quoted YAML scalar (e.g.
"'[1 2 3]'", as MOLA-LO writes its bounding boxes) stands for its contents. Before MRPT 3.7, MRPT’s YAML writer writes strings as they are: such strings are written as is, and any other string is quoted when it would not read back as the same string. From MRPT 3.7 on, the writer quotes strings itself, and the helpers give it pre-quoted strings unquoted. Either way the value read back is the same; a string that reads as a number, boolean or null may be refused, depending on the MRPT version.The new text is parsed once: if the key would not read back as given, or the metadata is not a YAML map or is of a newer version, nothing changes and an exception is thrown (unlike
get_keyframe_metadata(), which returns nothing for metadata that is not a map).
For places (mp2p_icp_filters, #include <mp2p_icp_filters/simplemap_partition.h>, feature macro
MP2P_ICP_HAS_KEYFRAME_PLACES), KeyframePlace::FromKeyframe() reads the place of a keyframe (key
KEYFRAME_PLACE_KEY), and write_keyframe_places() / simplemap_partition_from_places() write a whole
partition into a map and read it back.
Writing metadata while mapping¶
Inside a running MOLA system, modules give per-keyframe values to the central map with
mola::SharedKeyframeMap::set_keyframe_metadata(key, values, lastValues) (feature macro
MOLA_KERNEL_SHARED_KEYFRAME_MAP_HAS_METADATA), where values is a
std::map<KeyFrameID, mrpt::containers::yaml>. mola_mapper merges the values current when
it saves the simple-map into each keyframe’s comment; a key the keyframe’s front end already wrote is kept as it
was. Later values replace earlier ones, and a null value withdraws the value this call set for that keyframe.
Register the key early: a call with no values and
lastValues = falseas soon as the module finds the map, before the map becomes final. The mapper waits only for keys registered before it starts saving, and refuses every call from then on (false), so a writer that registers late loses its values.Send
lastValues = truewhen the module will send nothing more for that key: once its values are final (after it seesKeyframeMapSnapshot::is_finalif they depend on the final poses, or at once if they do not), and also whenever it stops for any other reason (an error, shutdown).At the end of the run, before saving, the mapper waits until every registered key got its last values, or until no write came for its
keyframe_metadata_timeoutparameter (30 s by default; every write restarts it, 0 means no wait); then it saves the values it has (after a timeout, with a warning that names the keys without last values). It does not wait if it saves no simple-map.The call returns
false, and stores nothing, if the map keeps no keyframe metadata (the default of the interface), is not ready yet, the key is not valid (empty, not made of letters, digits and_, orversion), an id is not a keyframe of the map, or the mapper has already started saving at the end of the run. Writers must check it.