point-formats 0.1.0

Dependency-light LiDAR/point-cloud/mesh format conversion crate with explicit adapters for heavyweight formats.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Native readers and writers.

pub mod delimited;
pub mod obj;
pub mod pcd;
pub mod ply;
pub mod pts;
pub mod ptx;
pub mod stl;

#[cfg(feature = "copc")]
pub mod copc;
#[cfg(feature = "e57")]
pub mod e57;
#[cfg(feature = "geospatial")]
pub mod geojson;
#[cfg(feature = "geospatial")]
pub mod geotiff;
#[cfg(feature = "las")]
pub mod las;

pub mod asciigrid;
#[cfg(feature = "dxf")]
pub mod dxf;
#[cfg(feature = "gltf")]
pub mod gltf;
#[cfg(feature = "gpkg")]
pub mod gpkg;
#[cfg(feature = "robotics")]
pub mod robotics;
#[cfg(feature = "sensor")]
pub mod sensor;
#[cfg(feature = "shapefile")]
pub mod shapefile;

use crate::error::{Error, Result};
use crate::format::Format;
use crate::types::Geometry;
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::Path;

/// Delimiter used by delimited text point files.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Delimiter {
    Auto,
    Whitespace,
    Comma,
    Tab,
    Semicolon,
}

impl Delimiter {
    #[inline]
    pub(crate) fn split_into<'a>(self, line: &'a str, fields: &mut Vec<&'a str>) {
        fields.clear();
        match self {
            Self::Auto => Self::detect(line).split_into(line, fields),
            Self::Whitespace => fields.extend(line.split_whitespace()),
            Self::Comma => fields.extend(line.split(',').map(str::trim)),
            Self::Tab => fields.extend(line.split('\t').map(str::trim)),
            Self::Semicolon => fields.extend(line.split(';').map(str::trim)),
        }
    }

    #[inline]
    pub(crate) fn detect(line: &str) -> Self {
        if line.contains(',') {
            Self::Comma
        } else if line.contains(';') {
            Self::Semicolon
        } else if line.contains('\t') {
            Self::Tab
        } else {
            Self::Whitespace
        }
    }

    #[inline]
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Auto | Self::Whitespace => " ",
            Self::Comma => ",",
            Self::Tab => "\t",
            Self::Semicolon => ";",
        }
    }
}

/// Column positions for delimited text files. Missing optional columns are `None`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnMapping {
    pub x: usize,
    pub y: usize,
    pub z: usize,
    pub intensity: Option<usize>,
    pub red: Option<usize>,
    pub green: Option<usize>,
    pub blue: Option<usize>,
    pub classification: Option<usize>,
    pub gps_time: Option<usize>,
    pub normal_x: Option<usize>,
    pub normal_y: Option<usize>,
    pub normal_z: Option<usize>,
}

impl Default for ColumnMapping {
    fn default() -> Self {
        Self {
            x: 0,
            y: 1,
            z: 2,
            intensity: Some(3),
            red: Some(4),
            green: Some(5),
            blue: Some(6),
            classification: Some(7),
            gps_time: Some(8),
            normal_x: Some(9),
            normal_y: Some(10),
            normal_z: Some(11),
        }
    }
}

impl ColumnMapping {
    pub(crate) fn from_header(header: &[&str]) -> Option<Self> {
        fn find(header: &[&str], names: &[&str]) -> Option<usize> {
            header.iter().position(|value| {
                let normalized = value
                    .trim()
                    .trim_matches('"')
                    .trim_matches('\'')
                    .to_ascii_lowercase()
                    .replace([' ', '-', '.'], "_");
                names.iter().any(|candidate| normalized == *candidate)
            })
        }

        let x = find(header, &["x", "easting", "east", "lon", "longitude"])?;
        let y = find(header, &["y", "northing", "north", "lat", "latitude"])?;
        let z = find(header, &["z", "elevation", "height", "altitude"])?;
        Some(Self {
            x,
            y,
            z,
            intensity: find(header, &["intensity", "i"]),
            red: find(header, &["red", "r"]),
            green: find(header, &["green", "g"]),
            blue: find(header, &["blue", "b"]),
            classification: find(header, &["classification", "class", "label"]),
            gps_time: find(header, &["gps_time", "gpstime", "time", "timestamp"]),
            normal_x: find(header, &["normal_x", "nx", "n_x"]),
            normal_y: find(header, &["normal_y", "ny", "n_y"]),
            normal_z: find(header, &["normal_z", "nz", "n_z"]),
        })
    }
}

/// Options for XYZ/TXT/CSV-style formats.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DelimitedOptions {
    pub delimiter: Delimiter,
    /// `None` means autodetect by trying to parse the first non-comment line.
    pub has_header: Option<bool>,
    pub columns: ColumnMapping,
    pub write_header: bool,
    pub precision: usize,
}

impl Default for DelimitedOptions {
    fn default() -> Self {
        Self {
            delimiter: Delimiter::Auto,
            has_header: None,
            columns: ColumnMapping::default(),
            write_header: false,
            precision: 6,
        }
    }
}

/// PLY encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlyEncoding {
    Ascii,
    BinaryLittleEndian,
}

/// PLY writer options.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlyOptions {
    pub encoding: PlyEncoding,
    pub precision: usize,
}

impl Default for PlyOptions {
    fn default() -> Self {
        Self {
            encoding: PlyEncoding::Ascii,
            precision: 6,
        }
    }
}

/// PCD encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PcdEncoding {
    Ascii,
    Binary,
}

/// PCD writer options.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PcdOptions {
    pub encoding: PcdEncoding,
    pub precision: usize,
}

impl Default for PcdOptions {
    fn default() -> Self {
        Self {
            encoding: PcdEncoding::Ascii,
            precision: 6,
        }
    }
}

/// STL writer options.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StlOptions {
    pub binary: bool,
}

impl Default for StlOptions {
    fn default() -> Self {
        Self { binary: true }
    }
}

/// Native reader/writer options.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct NativeOptions {
    pub delimited: DelimitedOptions,
    pub ply: PlyOptions,
    pub pcd: PcdOptions,
    pub stl: StlOptions,
}

pub fn read_path(
    path: impl AsRef<Path>,
    format: Format,
    options: &NativeOptions,
) -> Result<Geometry> {
    let path = path.as_ref();

    #[cfg(feature = "shapefile")]
    if matches!(format, Format::Shapefile) {
        return shapefile::read(path);
    }

    #[cfg(feature = "gltf")]
    if matches!(format, Format::Gltf | Format::Glb) {
        return gltf::read(path);
    }

    #[cfg(feature = "gpkg")]
    if matches!(format, Format::Gpkg) {
        return gpkg::read(path);
    }

    #[cfg(feature = "robotics")]
    if matches!(format, Format::RosBag) {
        return robotics::read_rosbag(path);
    }

    #[cfg(feature = "robotics")]
    if matches!(format, Format::Ros2Bag) {
        return robotics::read_ros2bag(path);
    }

    #[cfg(feature = "robotics")]
    if matches!(format, Format::PointCloud2) {
        return robotics::read_pc2(path);
    }

    #[cfg(feature = "sensor")]
    if matches!(format, Format::Pcap) {
        return sensor::read_pcap(path);
    }

    #[cfg(feature = "sensor")]
    if matches!(format, Format::UdpPackets) {
        return sensor::read_udppackets(path);
    }

    #[cfg(feature = "sensor")]
    if matches!(format, Format::VendorRaw) {
        return sensor::read_vendorraw(path);
    }

    let file = File::open(path)?;
    let mut reader = BufReader::new(file);
    match format {
        Format::Xyz | Format::Txt | Format::Csv => {
            let mut opts = options.delimited.clone();
            if matches!(format, Format::Csv) && matches!(opts.delimiter, Delimiter::Auto) {
                opts.delimiter = Delimiter::Comma;
                opts.write_header = true;
            }
            delimited::read(&mut reader, format, &opts).map(Geometry::PointCloud)
        }
        Format::Pts => pts::read(&mut reader).map(Geometry::PointCloud),
        Format::Ptx => ptx::read(&mut reader).map(Geometry::PointCloud),
        Format::Ply => ply::read(&mut reader),
        Format::Pcd => pcd::read(&mut reader),
        Format::Obj => obj::read(&mut reader),
        Format::Stl => stl::read(&mut reader),
        Format::AsciiGrid => asciigrid::read(&mut reader),

        #[cfg(feature = "dxf")]
        Format::Dxf => dxf::read(&mut reader),

        #[cfg(feature = "las")]
        Format::Las | Format::Laz => las::read(reader),

        #[cfg(feature = "copc")]
        Format::Copc => copc::read(&mut reader),

        #[cfg(feature = "e57")]
        Format::E57 => e57::read(&mut reader),

        #[cfg(feature = "geospatial")]
        Format::GeoTiff | Format::Cog => geotiff::read(reader),

        #[cfg(feature = "geospatial")]
        Format::GeoJson => geojson::read(reader),

        _ => Err(Error::unsupported(format, "read", format.adapter_hint())),
    }
}

pub fn write_path(
    path: impl AsRef<Path>,
    format: Format,
    geometry: &Geometry,
    options: &NativeOptions,
) -> Result<()> {
    let path = path.as_ref();

    #[cfg(feature = "e57")]
    if matches!(format, Format::E57) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return e57::write_to_path(path, cloud);
    }

    #[cfg(feature = "shapefile")]
    if matches!(format, Format::Shapefile) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return shapefile::write(path, cloud);
    }

    #[cfg(feature = "gltf")]
    if matches!(format, Format::Gltf) {
        return gltf::write_gltf(path, geometry);
    }

    #[cfg(feature = "gltf")]
    if matches!(format, Format::Glb) {
        return gltf::write_glb(path, geometry);
    }

    #[cfg(feature = "gpkg")]
    if matches!(format, Format::Gpkg) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return gpkg::write(path, cloud);
    }

    #[cfg(feature = "robotics")]
    if matches!(format, Format::RosBag) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return robotics::write_rosbag(path, cloud);
    }

    #[cfg(feature = "robotics")]
    if matches!(format, Format::Ros2Bag) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return robotics::write_ros2bag(path, cloud);
    }

    #[cfg(feature = "robotics")]
    if matches!(format, Format::PointCloud2) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return robotics::write_pc2(path, cloud);
    }

    #[cfg(feature = "sensor")]
    if matches!(format, Format::Pcap) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return sensor::write_pcap(path, cloud);
    }

    #[cfg(feature = "sensor")]
    if matches!(format, Format::UdpPackets) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return sensor::write_udppackets(path, cloud);
    }

    #[cfg(feature = "sensor")]
    if matches!(format, Format::VendorRaw) {
        let cloud = as_cloud_for_point_format(geometry, format)?;
        return sensor::write_vendorraw(path, cloud);
    }

    let file = File::create(path)?;
    let mut writer = BufWriter::new(file);
    match format {
        Format::Xyz | Format::Txt | Format::Csv => {
            let cloud = as_cloud_for_point_format(geometry, format)?;
            let mut opts = options.delimited.clone();
            if matches!(format, Format::Csv) {
                if matches!(opts.delimiter, Delimiter::Auto) {
                    opts.delimiter = Delimiter::Comma;
                }
                opts.write_header = true;
            }
            delimited::write(&mut writer, format, cloud, &opts)
        }
        Format::Pts => pts::write(&mut writer, as_cloud_for_point_format(geometry, format)?),
        Format::Ptx => ptx::write(&mut writer, as_cloud_for_point_format(geometry, format)?),
        Format::Ply => ply::write(&mut writer, geometry, &options.ply),
        Format::Pcd => pcd::write(
            &mut writer,
            as_cloud_for_point_format(geometry, format)?,
            &options.pcd,
        ),
        Format::Obj => obj::write(&mut writer, geometry),
        Format::Stl => stl::write(&mut writer, geometry, &options.stl),
        Format::AsciiGrid => {
            let cloud = as_cloud_for_point_format(geometry, format)?;
            asciigrid::write(&mut writer, cloud)
        }

        #[cfg(feature = "dxf")]
        Format::Dxf => dxf::write(&mut writer, geometry),

        #[cfg(feature = "las")]
        Format::Las | Format::Laz => {
            let cloud = as_cloud_for_point_format(geometry, format)?;
            las::write(writer, cloud)
        }

        #[cfg(feature = "geospatial")]
        Format::GeoTiff | Format::Cog => {
            let cloud = as_cloud_for_point_format(geometry, format)?;
            geotiff::write(writer, cloud)
        }

        #[cfg(feature = "geospatial")]
        Format::GeoJson => {
            let cloud = as_cloud_for_point_format(geometry, format)?;
            geojson::write(writer, cloud)
        }

        _ => Err(Error::unsupported(format, "write", format.adapter_hint())),
    }
}

fn as_cloud_for_point_format(
    geometry: &Geometry,
    format: Format,
) -> Result<&crate::types::PointCloud> {
    match geometry {
        Geometry::PointCloud(cloud) => Ok(cloud),
        Geometry::Mesh(_) => Err(Error::LossyConversionBlocked {
            from: "mesh",
            to: format,
            reason: "the destination is a point-cloud format and cannot preserve faces".to_string(),
        }),
    }
}

#[inline]
pub(crate) fn parse_f64(format: Format, line: usize, name: &str, value: &str) -> Result<f64> {
    value.parse::<f64>().map_err(|_| {
        Error::parse(
            format,
            line,
            format!("expected numeric {name}, got '{value}'"),
        )
    })
}

#[inline]
pub(crate) fn parse_f32(format: Format, line: usize, name: &str, value: &str) -> Result<f32> {
    value.parse::<f32>().map_err(|_| {
        Error::parse(
            format,
            line,
            format!("expected numeric {name}, got '{value}'"),
        )
    })
}

#[inline]
pub(crate) fn parse_u8(format: Format, line: usize, name: &str, value: &str) -> Result<u8> {
    if let Ok(v) = value.parse::<u8>() {
        return Ok(v);
    }
    let as_float = parse_f64(format, line, name, value)?;
    if as_float.fract() == 0.0 && (0.0..=u8::MAX as f64).contains(&as_float) {
        Ok(as_float as u8)
    } else {
        Err(Error::parse(
            format,
            line,
            format!("expected {name} in range 0..255, got '{value}'"),
        ))
    }
}

#[inline]
pub(crate) fn parse_u16(format: Format, line: usize, name: &str, value: &str) -> Result<u16> {
    if let Ok(v) = value.parse::<u16>() {
        return Ok(v);
    }
    let as_float = parse_f64(format, line, name, value)?;
    if as_float.fract() == 0.0 && (0.0..=u16::MAX as f64).contains(&as_float) {
        Ok(as_float as u16)
    } else {
        Err(Error::parse(
            format,
            line,
            format!("expected {name} in range 0..65535, got '{value}'"),
        ))
    }
}

#[inline]
pub(crate) fn fmt_f64(value: f64, precision: usize) -> String {
    if value == 0.0 {
        // Avoid writing -0.000000 after transforms/triangulation.
        return format!("{:.*}", precision, 0.0);
    }
    format!("{:.*}", precision, value)
}

#[inline]
pub(crate) fn write_f32_le<W: std::io::Write>(writer: &mut W, value: f32) -> Result<()> {
    writer.write_all(&value.to_le_bytes())?;
    Ok(())
}

#[inline]
pub(crate) fn write_f64_le<W: std::io::Write>(writer: &mut W, value: f64) -> Result<()> {
    writer.write_all(&value.to_le_bytes())?;
    Ok(())
}

#[inline]
pub(crate) fn write_u16_le<W: std::io::Write>(writer: &mut W, value: u16) -> Result<()> {
    writer.write_all(&value.to_le_bytes())?;
    Ok(())
}

#[inline]
pub(crate) fn write_u32_le<W: std::io::Write>(writer: &mut W, value: u32) -> Result<()> {
    writer.write_all(&value.to_le_bytes())?;
    Ok(())
}

#[inline]
pub(crate) fn read_exact<const N: usize, R: std::io::Read>(reader: &mut R) -> Result<[u8; N]> {
    let mut bytes = [0_u8; N];
    reader.read_exact(&mut bytes)?;
    Ok(bytes)
}

#[inline]
pub(crate) fn read_f32_le<R: std::io::Read>(reader: &mut R) -> Result<f32> {
    Ok(f32::from_le_bytes(read_exact(reader)?))
}

#[inline]
pub(crate) fn read_f64_le<R: std::io::Read>(reader: &mut R) -> Result<f64> {
    Ok(f64::from_le_bytes(read_exact(reader)?))
}

#[inline]
pub(crate) fn read_u16_le<R: std::io::Read>(reader: &mut R) -> Result<u16> {
    Ok(u16::from_le_bytes(read_exact(reader)?))
}

#[inline]
pub(crate) fn read_u32_le<R: std::io::Read>(reader: &mut R) -> Result<u32> {
    Ok(u32::from_le_bytes(read_exact(reader)?))
}