Skip to content

Commit 5ed30ea

Browse files
authored
Merge pull request #35 from MOLAorg/feat/mm2txt-ignore-fields
mm2txt and mm2ply: Add new option --ignore-missing-fields
2 parents 0d52dbf + fd627a2 commit 5ed30ea

4 files changed

Lines changed: 64 additions & 11 deletions

File tree

apps/mm2ply/main.cpp

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ TCLAP::ValueArg<std::string> argExportFields(
4646
"the specified order.",
4747
false, "", "field1,field2,...", cmd);
4848

49+
TCLAP::SwitchArg argIgnoreMissingFields(
50+
"", "ignore-missing-fields",
51+
"If defined, the lack of any of the --export-fields in the map will be considered a warning "
52+
"instead of an error, and that column will be padded with zeros.",
53+
cmd);
54+
4955
// ----------------------------------------------------------------
5056
// PLY Export Logic using CPointsMap field-generic API
5157
// ----------------------------------------------------------------
5258
void saveToPly(
5359
const mrpt::maps::CPointsMap& pc, const std::string& filename, bool binary,
54-
const std::vector<std::string>& selectedFields = {})
60+
const std::vector<std::string>& selectedFields = {}, bool ignoreMissingFields = false)
5561
{
5662
std::ofstream f;
5763
f.open(filename, binary ? std::ios::binary : std::ios::out);
@@ -264,7 +270,12 @@ void saveToPly(
264270
#endif
265271
if (!found)
266272
{
267-
throw std::runtime_error("Field not found: " + fieldName);
273+
if (!ignoreMissingFields)
274+
{
275+
throw std::runtime_error("Field not found: " + fieldName);
276+
}
277+
// Mark this accessor as invalid (nullptr) - will output zeros
278+
acc.bufPtr = nullptr;
268279
}
269280
}
270281
accessors.push_back(acc);
@@ -287,6 +298,33 @@ void saveToPly(
287298
{
288299
for (const auto& acc : accessors)
289300
{
301+
// Handle missing fields (when ignoreMissingFields is true)
302+
if (acc.bufPtr == nullptr)
303+
{
304+
// Output zero for missing fields
305+
switch (acc.type)
306+
{
307+
case FieldAccessor::FLOAT:
308+
write_val(0.0f, "%.8e");
309+
break;
310+
#if MRPT_VERSION >= 0x020f03
311+
case FieldAccessor::DOUBLE:
312+
write_val(0.0, "%.16le");
313+
break;
314+
case FieldAccessor::UINT16:
315+
write_val(static_cast<uint16_t>(0), "%u");
316+
break;
317+
case FieldAccessor::UINT8:
318+
write_val(static_cast<uint8_t>(0), "%i");
319+
break;
320+
#endif
321+
default:
322+
write_val(0.0f, "%.8e");
323+
break;
324+
}
325+
continue;
326+
}
327+
290328
switch (acc.type)
291329
{
292330
default:
@@ -472,16 +510,20 @@ int main(int argc, char** argv)
472510
}
473511
#endif
474512
std::cerr << std::endl;
475-
throw std::runtime_error(
476-
"Field '" + field +
477-
"' specified in --export-fields not found in layer '" + name + "'");
513+
if (!argIgnoreMissingFields.isSet())
514+
{
515+
throw std::runtime_error(
516+
"Field '" + field +
517+
"' specified in --export-fields not found in layer '" + name + "'");
518+
}
478519
}
479520
}
480521
}
481522

482523
std::string out = prefix + "_" + name + ".ply";
483524
std::cout << "Exporting '" << name << "' to " << out << "..." << std::endl;
484-
saveToPly(*pts, out, arg_binary.getValue(), selectedFields);
525+
saveToPly(
526+
*pts, out, arg_binary.getValue(), selectedFields, argIgnoreMissingFields.isSet());
485527
}
486528
}
487529
catch (const std::exception& e)

apps/mm2txt/main.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ TCLAP::ValueArg<std::string> argExportFields(
5454
"the specified order.",
5555
false, "", "field1,field2,...", cmd);
5656

57+
TCLAP::SwitchArg argIgnoreMissingFields(
58+
"", "ignore-missing-fields",
59+
"If defined, the lack of any of the --export-fields in the map will be considered a warning "
60+
"instead of an error, and that column will be padded with zeros.",
61+
cmd);
62+
5763
bool saveToTxt(
5864
const mrpt::maps::CGenericPointsMap& pts, const std::string& fileName, bool printHeader,
5965
const std::vector<std::string>& selectedFields = {})
@@ -332,9 +338,12 @@ void run_mm2txt()
332338
}
333339
#endif
334340
std::cerr << std::endl;
335-
THROW_EXCEPTION_FMT(
336-
"Field '%s' specified in --export-fields not found in layer '%s'",
337-
field.c_str(), name.c_str());
341+
if (!argIgnoreMissingFields.isSet())
342+
{
343+
THROW_EXCEPTION_FMT(
344+
"Field '%s' specified in --export-fields not found in layer '%s'",
345+
field.c_str(), name.c_str());
346+
}
338347
}
339348
}
340349
}

docs/source/app_mm2ply.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Usage
2424

2525
.. code-block:: bash
2626
27-
mm2ply -i <input.mm> [-o <output_prefix>] [-b] [--export-fields <field1,field2,...>]
27+
mm2ply -i <input.mm> [-o <output_prefix>] [-b] [--export-fields <field1,field2,...>] [--ignore-missing-fields]
2828
2929
Arguments
3030
^^^^^^^^^
@@ -33,6 +33,7 @@ Arguments
3333
- ``-o, --output <prefix>`` (optional): Prefix for output PLY files. If not specified, uses the input filename without extension
3434
- ``-b, --binary`` (optional): Export in binary format instead of ASCII (default: ASCII)
3535
- ``--export-fields <field1,field2,...>`` (optional): Comma-separated list of fields to export in the specified order. If not provided, all available fields will be exported. Spaces around commas are allowed
36+
- ``--ignore-missing-fields`` (optional): If defined, the lack of any of the ``--export-fields`` in the map will be considered a warning instead of an error, and that column will be padded with zeros.
3637

3738
Examples
3839
^^^^^^^^

docs/source/app_mm2txt.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ Usage
2525

2626
.. code-block:: bash
2727
28-
mm2txt <input.mm> [-l <layer_name>] [--export-fields <field1,field2,...>]
28+
mm2txt <input.mm> [-l <layer_name>] [--export-fields <field1,field2,...>] [--ignore-missing-fields]
2929
3030
Arguments
3131
^^^^^^^^^
3232

3333
- ``input`` (required): Input metric map file (\*.mm)
3434
- ``-l, --layer <name>`` (optional): Layer to export. If not provided, all layers will be exported. This argument can appear multiple times to export specific layers
3535
- ``--export-fields <field1,field2,...>`` (optional): Comma-separated list of fields to export in the specified order. If not provided, all available fields will be exported. Spaces around commas are allowed
36+
- ``--ignore-missing-fields`` (optional): If defined, the lack of any of the ``--export-fields`` in the map will be considered a warning instead of an error, and that column will be padded with zeros.
3637

3738
Examples
3839
^^^^^^^^

0 commit comments

Comments
 (0)