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
use crate::blob::blob_from_node;
use crate::error::Converter;
use crate::xml::{
optional_date_time, optional_string, optional_transform, required_double, required_integer,
required_string,
};
use crate::{Blob, DateTime, Error, Result, Transform};
use roxmltree::{Document, Node};
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Image {
pub guid: String,
pub visual_reference: Option<VisualReference>,
pub representation: Option<Representation>,
pub transform: Option<Transform>,
pub pointcloud_guid: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
pub acquisition: Option<DateTime>,
pub sensor_vendor: Option<String>,
pub sensor_model: Option<String>,
pub sensor_serial: Option<String>,
}
#[derive(Debug, Clone)]
pub enum Representation {
Pinhole(PinholeRepresentation),
Spherical(SphericalRepresentation),
Cylindrical(CylindricalRepresentation),
}
#[derive(Debug, Clone)]
pub enum ImageFormat {
Png,
Jpeg,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ImageBlob {
pub data: Blob,
pub format: ImageFormat,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct VisualReference {
pub blob: ImageBlob,
pub mask: Option<Blob>,
pub width: u32,
pub height: u32,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct PinholeRepresentation {
pub blob: ImageBlob,
pub mask: Option<Blob>,
pub width: u32,
pub height: u32,
pub focal_length: f64,
pub pixel_width: f64,
pub pixel_height: f64,
pub principal_x: f64,
pub principal_y: f64,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct SphericalRepresentation {
pub blob: ImageBlob,
pub mask: Option<Blob>,
pub width: u32,
pub height: u32,
pub pixel_width: f64,
pub pixel_height: f64,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct CylindricalRepresentation {
pub blob: ImageBlob,
pub mask: Option<Blob>,
pub width: u32,
pub height: u32,
pub radius: f64,
pub principal_y: f64,
pub pixel_width: f64,
pub pixel_height: f64,
}
pub fn images_from_document(document: &Document) -> Result<Vec<Image>> {
let images2d_node = document
.descendants()
.find(|n| n.has_tag_name("images2D"))
.invalid_err("Cannot find 'images2D' tag in XML document")?;
let mut images = Vec::new();
for n in images2d_node.children() {
if n.has_tag_name("vectorChild") && n.attribute("type") == Some("Structure") {
let image = image_from_node(&n)?;
images.push(image);
}
}
Ok(images)
}
fn image_from_node(node: &Node) -> Result<Image> {
let guid = required_string(node, "guid")?;
let pointcloud_guid = optional_string(node, "associatedData3DGuid")?;
let transform = optional_transform(node, "pose")?;
let name = optional_string(node, "name")?;
let description = optional_string(node, "description")?;
let sensor_model = optional_string(node, "sensorModel")?;
let sensor_vendor = optional_string(node, "sensorVendor")?;
let sensor_serial = optional_string(node, "sensorSerialNumber")?;
let acquisition = optional_date_time(node, "acquisitionDateTime")?;
let visual_reference_node = node
.children()
.find(|n| n.has_tag_name("visualReferenceRepresentation"));
let visual_reference = if let Some(node) = visual_reference_node {
Some(visual_reference_from_node(&node)?)
} else {
None
};
let representation = extract_representation(node)?;
Ok(Image {
guid,
pointcloud_guid,
transform,
name,
description,
acquisition,
sensor_vendor,
sensor_model,
sensor_serial,
representation,
visual_reference,
})
}
fn extract_representation(parent_node: &Node) -> Result<Option<Representation>> {
let pinhole = parent_node
.children()
.find(|n| n.has_tag_name("pinholeRepresentation"));
if let Some(node) = &pinhole {
return Ok(Some(Representation::Pinhole(pinhole_rep_from_node(node)?)));
}
let spherical = parent_node
.children()
.find(|n| n.has_tag_name("sphericalRepresentation"));
if let Some(node) = &spherical {
return Ok(Some(Representation::Spherical(spherical_rep_from_node(
node,
)?)));
}
let cylindrical = parent_node
.children()
.find(|n| n.has_tag_name("cylindricalRepresentation"));
if let Some(node) = &cylindrical {
return Ok(Some(Representation::Cylindrical(
cylindrical_rep_from_node(node)?,
)));
}
Ok(None)
}
fn visual_reference_from_node(node: &Node) -> Result<VisualReference> {
Ok(VisualReference {
blob: extract_image_blob(node)?,
mask: extract_mask_blob(node)?,
width: required_integer(node, "imageWidth")?,
height: required_integer(node, "imageHeight")?,
})
}
fn pinhole_rep_from_node(node: &Node) -> Result<PinholeRepresentation> {
Ok(PinholeRepresentation {
blob: extract_image_blob(node)?,
mask: extract_mask_blob(node)?,
width: required_integer(node, "imageWidth")?,
height: required_integer(node, "imageHeight")?,
focal_length: required_double(node, "focalLength")?,
pixel_width: required_double(node, "pixelWidth")?,
pixel_height: required_double(node, "pixelHeight")?,
principal_x: required_double(node, "principalPointX")?,
principal_y: required_double(node, "principalPointY")?,
})
}
fn spherical_rep_from_node(node: &Node) -> Result<SphericalRepresentation> {
let blob = extract_image_blob(node)?;
let mask = extract_mask_blob(node)?;
let width = required_integer(node, "imageWidth")?;
let height = required_integer(node, "imageHeight")?;
let pixel_width = required_double(node, "pixelWidth")?;
let pixel_height = required_double(node, "pixelHeight")?;
Ok(SphericalRepresentation {
blob,
mask,
width,
height,
pixel_width,
pixel_height,
})
}
fn cylindrical_rep_from_node(node: &Node) -> Result<CylindricalRepresentation> {
let blob = extract_image_blob(node)?;
let mask = extract_mask_blob(node)?;
let width = required_integer(node, "imageWidth")?;
let height = required_integer(node, "imageHeight")?;
let radius = required_double(node, "radius")?;
let principal_y = required_double(node, "principalPointY")?;
let pixel_width = required_double(node, "pixelWidth")?;
let pixel_height = required_double(node, "pixelHeight")?;
Ok(CylindricalRepresentation {
blob,
mask,
width,
height,
radius,
principal_y,
pixel_width,
pixel_height,
})
}
fn extract_image_blob(parent_node: &Node) -> Result<ImageBlob> {
if let Some(node) = &parent_node.children().find(|n| n.has_tag_name("jpegImage")) {
Ok(ImageBlob {
data: blob_from_node(node)?,
format: ImageFormat::Jpeg,
})
} else if let Some(node) = &parent_node.children().find(|n| n.has_tag_name("pngImage")) {
Ok(ImageBlob {
data: blob_from_node(node)?,
format: ImageFormat::Png,
})
} else {
Error::invalid("Cannot find PNG or JPEG blob")
}
}
fn extract_mask_blob(parent_node: &Node) -> Result<Option<Blob>> {
if let Some(node) = &parent_node.children().find(|n| n.has_tag_name("imageMask")) {
Ok(Some(blob_from_node(node)?))
} else {
Ok(None)
}
}