e57 0.11.12

A pure Rust library for reading and writing E57 files with point clouds and related image data.
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
use crate::bs_write::ByteStreamWriteBuffer;
use crate::error::Converter;
use crate::{Error, Result};
use roxmltree::Node;
use std::error::Error as StdError;
use std::fmt::{Debug, Display};
use std::str::FromStr;

/// Describes a record inside a E57 file with name and data type.
#[derive(Clone, Debug)]
pub struct Record {
    pub name: RecordName,
    pub data_type: RecordDataType,
}

/// Basic primitive E57 data types that are used for the different point attributes.
#[derive(Clone, Debug)]
pub enum RecordDataType {
    /// 32-bit IEEE 754-2008 floating point value.
    Single { min: Option<f32>, max: Option<f32> },
    /// 64-bit IEEE 754-2008 floating point value.
    Double { min: Option<f64>, max: Option<f64> },
    /// Signed 64-bit integer scaled with a fixed 64-bit floating point value.
    ScaledInteger {
        min: i64,
        max: i64,
        scale: f64,
        offset: f64,
    },
    /// Signed 64-bit integer value.
    Integer { min: i64, max: i64 },
}

/// Used to describe the prototype records with all attributes that exist in the point cloud.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum RecordName {
    /// Cartesian X coordinate (in meters).
    CartesianX,
    /// Cartesian Y coordinate (in meters).
    CartesianY,
    /// Cartesian Z coordinate (in meters).
    CartesianZ,
    /// Indicates whether the Cartesian coordinate or its magnitude is meaningful.
    /// Can have the value 0 (valid), 1 (XYZ is a direction vector) or 2 (invalid).
    CartesianInvalidState,

    /// Non-negative range (in meters) of the spherical coordinate.
    SphericalRange,
    /// Azimuth angle (in radians between -PI and PI) of the spherical coordinate.
    SphericalAzimuth,
    // Elevation angle (in radians between -PI/2 and PI/2) of the spherical coordinate.
    SphericalElevation,
    /// Indicates whether the spherical coordinate or its range is meaningful.
    /// Can have the value 0 (valid), 1 (range is not meaningful) or 2 (invalid).
    SphericalInvalidState,

    /// Point intensity. Unit is not specified.
    Intensity,
    /// Indicates whether the intensity value is meaningful.
    /// Can have the value 0 (valid) or 1 (invalid).
    IsIntensityInvalid,

    /// Red color value. Unit is not specified.
    ColorRed,
    /// Green color value. Unit is not specified.
    ColorGreen,
    /// Blue color value. Unit is not specified.
    ColorBlue,
    /// Indicates whether the color value is meaningful.
    /// Can have the value 0 (valid) or 1 (invalid).
    IsColorInvalid,

    /// Row number of the point (zero-based). Used for data that is stored in a grid.
    RowIndex,
    /// Column number of the point (zero-based). Used for data that is stored in a grid.
    ColumnIndex,

    /// For multi-return sensors. The total number of returns for the pulse that this point corresponds to.
    ReturnCount,
    /// For multi-return sensors. The number of this return (zero based). That is, 0 is the first, 1 is the second return etc.
    ReturnIndex,

    /// Non-negative time (in seconds) since the start time given by acquisition start in the parent point cloud.
    TimeStamp,
    /// Indicates whether the time stamp value is meaningful.
    /// Can have the value 0 (valid) or 1 (invalid).
    IsTimeStampInvalid,

    /// Unknown point attribute that is not part of the E57 standard.
    /// Files with such attributes are still valid, since any E57 reader must be able to handle unknown extensions.
    /// Most extensions are described on <http://www.libe57.org/extensions.html>, but others might be proprietary.
    Unknown {
        /// XML namespace of the extension that defines this attribute.
        /// Required, but some E57 files out there are omitting it and we still want to be able to read them.
        /// In such cases the namespace will be set to an empty string.
        namespace: String,
        /// Name of the point atribute.
        name: String,
    },
}

/// Represents a raw value of attributes inside a point cloud.
///
/// For scaled integers the record data type with the scale and offset is needed to calculate the actual f64 value.
#[derive(Clone, Debug, PartialEq)]
pub enum RecordValue {
    Single(f32),
    Double(f64),
    ScaledInteger(i64),
    Integer(i64),
}

impl Record {
    pub(crate) fn xml_string(&self) -> String {
        let namespace = self
            .name
            .namespace()
            .map(|n| n.to_owned() + ":")
            .unwrap_or("".to_owned());
        let tag_name = self.name.tag_name();
        let (attrs, value) = serialize_record_type(&self.data_type);
        format!("<{namespace}{tag_name} {attrs}>{value}</{namespace}{tag_name}>\n")
    }
}

impl RecordName {
    pub(crate) fn tag_name(&self) -> &str {
        match self {
            RecordName::CartesianX => "cartesianX",
            RecordName::CartesianY => "cartesianY",
            RecordName::CartesianZ => "cartesianZ",
            RecordName::CartesianInvalidState => "cartesianInvalidState",
            RecordName::SphericalRange => "sphericalRange",
            RecordName::SphericalAzimuth => "sphericalAzimuth",
            RecordName::SphericalElevation => "sphericalElevation",
            RecordName::SphericalInvalidState => "sphericalInvalidState",
            RecordName::Intensity => "intensity",
            RecordName::IsIntensityInvalid => "isIntensityInvalid",
            RecordName::ColorRed => "colorRed",
            RecordName::ColorGreen => "colorGreen",
            RecordName::ColorBlue => "colorBlue",
            RecordName::IsColorInvalid => "isColorInvalid",
            RecordName::RowIndex => "rowIndex",
            RecordName::ColumnIndex => "columnIndex",
            RecordName::ReturnCount => "returnCount",
            RecordName::ReturnIndex => "returnIndex",
            RecordName::TimeStamp => "timeStamp",
            RecordName::IsTimeStampInvalid => "isTimeStampInvalid",
            RecordName::Unknown { name, .. } => name,
        }
    }

    pub(crate) fn namespace(&self) -> Option<&str> {
        match self {
            RecordName::Unknown { namespace, .. } => Some(namespace),
            _ => None,
        }
    }

    pub(crate) fn from_namespace_and_tag_name(
        namespace: Option<&str>,
        tag_name: &str,
    ) -> Result<Self> {
        Ok(match tag_name {
            "cartesianX" => RecordName::CartesianX,
            "cartesianY" => RecordName::CartesianY,
            "cartesianZ" => RecordName::CartesianZ,
            "cartesianInvalidState" => RecordName::CartesianInvalidState,
            "sphericalRange" => RecordName::SphericalRange,
            "sphericalAzimuth" => RecordName::SphericalAzimuth,
            "sphericalElevation" => RecordName::SphericalElevation,
            "sphericalInvalidState" => RecordName::SphericalInvalidState,
            "intensity" => RecordName::Intensity,
            "isIntensityInvalid" => RecordName::IsIntensityInvalid,
            "colorRed" => RecordName::ColorRed,
            "colorGreen" => RecordName::ColorGreen,
            "colorBlue" => RecordName::ColorBlue,
            "isColorInvalid" => RecordName::IsColorInvalid,
            "rowIndex" => RecordName::RowIndex,
            "columnIndex" => RecordName::ColumnIndex,
            "returnCount" => RecordName::ReturnCount,
            "returnIndex" => RecordName::ReturnIndex,
            "timeStamp" => RecordName::TimeStamp,
            "isTimeStampInvalid" => RecordName::IsTimeStampInvalid,
            _ => RecordName::Unknown {
                namespace: namespace.unwrap_or_default().to_owned(), // Missing namespace becomes empty string
                name: tag_name.to_owned(),
            },
        })
    }
}

impl RecordDataType {
    pub(crate) fn from_node(node: &Node) -> Result<Self> {
        let tag_name = node.tag_name().name();
        let type_name = node
            .attribute("type")
            .invalid_err(format!("Missing type attribute for XML tag '{tag_name}'"))?;
        Ok(match type_name {
            "Float" => {
                let precision = node.attribute("precision").unwrap_or("double");
                if precision == "double" {
                    let min = optional_attribute(node, "minimum", tag_name, type_name)?;
                    let max = optional_attribute(node, "maximum", tag_name, type_name)?;
                    RecordDataType::Double { min, max }
                } else if precision == "single" {
                    let min = optional_attribute(node, "minimum", tag_name, type_name)?;
                    let max = optional_attribute(node, "maximum", tag_name, type_name)?;
                    RecordDataType::Single { min, max }
                } else {
                    Error::invalid(format!(
                        "Float 'precision' attribute value '{precision}' for 'Float' type is unknown"
                    ))?
                }
            }
            "Integer" => {
                let min =
                    optional_attribute(node, "minimum", tag_name, type_name)?.unwrap_or(i64::MIN);
                let max =
                    optional_attribute(node, "maximum", tag_name, type_name)?.unwrap_or(i64::MAX);
                if max < min {
                    Error::invalid(format!(
                        "Maximum value '{max}' and minimum value '{min}' of type '{type_name}' in XML tag '{tag_name}' are invalid"
                    ))?
                }
                RecordDataType::Integer { min, max }
            }
            "ScaledInteger" => {
                let min =
                    optional_attribute(node, "minimum", tag_name, type_name)?.unwrap_or(i64::MIN);
                let max =
                    optional_attribute(node, "maximum", tag_name, type_name)?.unwrap_or(i64::MAX);
                if max < min {
                    Error::invalid(format!(
                        "Maximum value '{max}' and minimum value '{min}' of type '{type_name}' in XML tag '{tag_name}' are invalid"
                    ))?
                }
                let scale = optional_attribute(node, "scale", tag_name, type_name)?.unwrap_or(1.0);
                let offset =
                    optional_attribute(node, "offset", tag_name, type_name)?.unwrap_or(0.0);
                RecordDataType::ScaledInteger {
                    min,
                    max,
                    scale,
                    offset,
                }
            }
            _ => Error::not_implemented(format!(
                "Unsupported type '{type_name}' in XML tag '{tag_name}' detected"
            ))?,
        })
    }

    pub(crate) fn bit_size(&self) -> usize {
        match self {
            RecordDataType::Single { .. } => std::mem::size_of::<f32>() * 8,
            RecordDataType::Double { .. } => std::mem::size_of::<f64>() * 8,
            RecordDataType::ScaledInteger { min, max, .. } => integer_bits(*min, *max),
            RecordDataType::Integer { min, max } => integer_bits(*min, *max),
        }
    }

    pub(crate) fn write(
        &self,
        value: &RecordValue,
        buffer: &mut ByteStreamWriteBuffer,
    ) -> Result<()> {
        match self {
            RecordDataType::Single { .. } => {
                if let RecordValue::Single(float) = value {
                    let bytes = float.to_le_bytes();
                    buffer.add_bytes(&bytes);
                } else {
                    Error::invalid("Data type single only supports single values")?
                }
            }
            RecordDataType::Double { .. } => {
                if let RecordValue::Double(double) = value {
                    let bytes = double.to_le_bytes();
                    buffer.add_bytes(&bytes);
                } else {
                    Error::invalid("Data type double only supports double values")?
                }
            }
            RecordDataType::ScaledInteger { min, max, .. } => {
                if let RecordValue::ScaledInteger(int) = value {
                    serialize_integer(*int, *min, *max, buffer);
                } else {
                    Error::invalid("Data type scaled integer only supports scaled integer values")?
                }
            }
            RecordDataType::Integer { min, max } => {
                if let RecordValue::Integer(int) = value {
                    serialize_integer(*int, *min, *max, buffer);
                } else {
                    Error::invalid("Data type integer only supports integer values")?
                }
            }
        };
        Ok(())
    }

    pub(crate) fn limits(&self) -> (Option<RecordValue>, Option<RecordValue>) {
        match self {
            RecordDataType::Single { min, max } => {
                (min.map(RecordValue::Single), max.map(RecordValue::Single))
            }
            RecordDataType::Double { min, max } => {
                (min.map(RecordValue::Double), max.map(RecordValue::Double))
            }
            RecordDataType::ScaledInteger { min, max, .. } => (
                Some(RecordValue::ScaledInteger(*min)),
                Some(RecordValue::ScaledInteger(*max)),
            ),
            RecordDataType::Integer { min, max } => (
                Some(RecordValue::Integer(*min)),
                Some(RecordValue::Integer(*max)),
            ),
        }
    }
}

impl RecordValue {
    pub fn to_f64(&self, dt: &RecordDataType) -> Result<f64> {
        match self {
            RecordValue::Single(s) => Ok(*s as f64),
            RecordValue::Double(d) => Ok(*d),
            RecordValue::ScaledInteger(i) => {
                if let RecordDataType::ScaledInteger { scale, offset, .. } = dt {
                    Ok(*i as f64 * *scale + offset)
                } else {
                    Error::internal("Tried to convert scaled integer value with wrong data type")
                }
            }
            RecordValue::Integer(i) => Ok(*i as f64),
        }
    }

    pub fn to_u8(&self, dt: &RecordDataType) -> Result<u8> {
        if let (RecordValue::Integer(i), RecordDataType::Integer { min, max }) = (self, dt) {
            if *min >= 0 && *max <= 255 {
                Ok(*i as u8)
            } else {
                Error::internal("Integer range is too big for u8")
            }
        } else {
            Error::internal("Tried to convert value to u8 with unsupported value or data type")
        }
    }

    pub fn to_i64(&self, dt: &RecordDataType) -> Result<i64> {
        // Does not make much sense to convert float to int,
        // but some files generated by PDAL use floats for invalid states...
        // See also here: https://github.com/cry-inc/e57/issues/18

        if let (RecordValue::Integer(i), RecordDataType::Integer { .. }) = (self, dt) {
            Ok(*i)
        } else if let (
            RecordValue::ScaledInteger(si),
            RecordDataType::ScaledInteger { scale, offset, .. },
        ) = (self, dt)
        {
            Ok((*si as f64 * *scale + offset) as i64)
        } else if let (RecordValue::Single(s), RecordDataType::Single { .. }) = (self, dt) {
            Ok(*s as i64)
        } else if let (RecordValue::Double(d), RecordDataType::Double { .. }) = (self, dt) {
            Ok(*d as i64)
        } else {
            Error::internal("Tried to convert value to i64 with unsupported data type")
        }
    }
}

impl Display for RecordValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RecordValue::Single(v) => write!(f, "{v}"),
            RecordValue::Double(v) => write!(f, "{v}"),
            RecordValue::ScaledInteger(v) => write!(f, "{v}"),
            RecordValue::Integer(v) => write!(f, "{v}"),
        }
    }
}

#[inline]
fn serialize_integer(value: i64, min: i64, max: i64, buffer: &mut ByteStreamWriteBuffer) {
    let uint = (value - min) as u64;
    let data = uint.to_le_bytes();
    let bits = integer_bits(min, max);
    buffer.add_bits(&data, bits);
}

#[inline]
fn integer_bits(min: i64, max: i64) -> usize {
    let range = max as i128 - min as i128;
    if range > 0 {
        range.ilog2() as usize + 1
    } else {
        0
    }
}

fn optional_attribute<T>(
    node: &Node,
    attribute: &str,
    tag_name: &str,
    type_name: &str,
) -> Result<Option<T>>
where
    T: FromStr,
    T::Err: StdError + Send + Sync + 'static,
{
    Ok(if let Some(attr) = node.attribute(attribute) {
        let parsed = attr.parse::<T>();
        Some(parsed.invalid_err(format!(
            "Failed to parse attribute '{attribute}' for type '{type_name}' in XML tag '{tag_name}'"
        ))?)
    } else {
        None
    })
}

fn serialize_record_type(rt: &RecordDataType) -> (String, String) {
    match rt {
        RecordDataType::Single { min, max } => {
            let mut str = String::from("type=\"Float\" precision=\"single\"");
            if let Some(min) = min {
                str += &format!(" minimum=\"{min}\"");
            }
            if let Some(max) = max {
                str += &format!(" maximum=\"{max}\"");
            }
            let value = min.unwrap_or(0.0).to_string();
            (str, value)
        }
        RecordDataType::Double { min, max } => {
            let mut str = String::from("type=\"Float\"");
            if let Some(min) = min {
                str += &format!(" minimum=\"{min}\"");
            }
            if let Some(max) = max {
                str += &format!(" maximum=\"{max}\"");
            }
            let value = min.unwrap_or(0.0).to_string();
            (str, value)
        }
        RecordDataType::ScaledInteger { min, max, scale, offset } => (
            format!(
                "type=\"ScaledInteger\" minimum=\"{min}\" maximum=\"{max}\" scale=\"{scale}\" offset=\"{offset}\""
            ),
            min.to_string(),
        ),
        RecordDataType::Integer { min, max } => (
            format!("type=\"Integer\" minimum=\"{min}\" maximum=\"{max}\""),
            min.to_string(),
        ),
    }
}

impl RecordDataType {
    pub const F32: RecordDataType = RecordDataType::Single {
        min: None,
        max: None,
    };

    pub const UNIT_F32: RecordDataType = RecordDataType::Single {
        min: Some(0.0),
        max: Some(1.0),
    };

    pub const F64: RecordDataType = RecordDataType::Double {
        min: None,
        max: None,
    };

    pub const U8: RecordDataType = RecordDataType::Integer {
        min: 0,
        max: u8::MAX as i64,
    };

    pub const U16: RecordDataType = RecordDataType::Integer {
        min: 0,
        max: u16::MAX as i64,
    };
}

impl Record {
    pub const CARTESIAN_X_F32: Record = Record {
        name: RecordName::CartesianX,
        data_type: RecordDataType::F32,
    };

    pub const CARTESIAN_Y_F32: Record = Record {
        name: RecordName::CartesianY,
        data_type: RecordDataType::F32,
    };

    pub const CARTESIAN_Z_F32: Record = Record {
        name: RecordName::CartesianZ,
        data_type: RecordDataType::F32,
    };

    pub const CARTESIAN_X_F64: Record = Record {
        name: RecordName::CartesianX,
        data_type: RecordDataType::F64,
    };

    pub const CARTESIAN_Y_F64: Record = Record {
        name: RecordName::CartesianY,
        data_type: RecordDataType::F64,
    };

    pub const CARTESIAN_Z_F64: Record = Record {
        name: RecordName::CartesianZ,
        data_type: RecordDataType::F64,
    };

    pub const CARTESIAN_INVALID_STATE: Record = Record {
        name: RecordName::CartesianInvalidState,
        data_type: RecordDataType::Integer { min: 0, max: 2 },
    };

    pub const SPHERICAL_AZIMUTH_F32: Record = Record {
        name: RecordName::SphericalAzimuth,
        data_type: RecordDataType::F32,
    };

    pub const SPHERICAL_ELEVATION_F32: Record = Record {
        name: RecordName::SphericalElevation,
        data_type: RecordDataType::F32,
    };

    pub const SPHERICAL_RANGE_F32: Record = Record {
        name: RecordName::SphericalRange,
        data_type: RecordDataType::F32,
    };

    pub const SPHERICAL_AZIMUTH_F64: Record = Record {
        name: RecordName::SphericalAzimuth,
        data_type: RecordDataType::F64,
    };

    pub const SPHERICAL_ELEVATION_F64: Record = Record {
        name: RecordName::SphericalElevation,
        data_type: RecordDataType::F64,
    };

    pub const SPHERICAL_RANGE_F64: Record = Record {
        name: RecordName::SphericalRange,
        data_type: RecordDataType::F64,
    };

    pub const SPHERICAL_INVALID_STATE: Record = Record {
        name: RecordName::SphericalInvalidState,
        data_type: RecordDataType::Integer { min: 0, max: 2 },
    };

    pub const COLOR_RED_U8: Record = Record {
        name: RecordName::ColorRed,
        data_type: RecordDataType::U8,
    };

    pub const COLOR_GREEN_U8: Record = Record {
        name: RecordName::ColorGreen,
        data_type: RecordDataType::U8,
    };

    pub const COLOR_BLUE_U8: Record = Record {
        name: RecordName::ColorBlue,
        data_type: RecordDataType::U8,
    };

    pub const INTENSITY_U16: Record = Record {
        name: RecordName::Intensity,
        data_type: RecordDataType::U16,
    };

    pub const COLOR_RED_UNIT_F32: Record = Record {
        name: RecordName::ColorRed,
        data_type: RecordDataType::UNIT_F32,
    };

    pub const COLOR_GREEN_UNIT_F32: Record = Record {
        name: RecordName::ColorGreen,
        data_type: RecordDataType::UNIT_F32,
    };

    pub const COLOR_BLUE_UNIT_F32: Record = Record {
        name: RecordName::ColorBlue,
        data_type: RecordDataType::UNIT_F32,
    };

    pub const INTENSITY_UNIT_F32: Record = Record {
        name: RecordName::Intensity,
        data_type: RecordDataType::UNIT_F32,
    };
}