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
extern crate proc_macro;
use std::collections::HashSet;
use layout::{get_struct_member_layout, StructMemberLayout};
use proc_macro::TokenStream;
use quote::quote;
use syn::DeriveInput;
use syn::{
parse_macro_input, Attribute, Data, Error, Field, Fields, GenericArgument, Ident, Lit,
NestedMeta, PathArguments, Result, Type, TypePath,
};
mod layout;
enum PasturePrimitiveType {
U8,
I8,
U16,
I16,
U32,
I32,
U64,
I64,
F32,
F64,
Bool,
Vec3u8,
Vec3u16,
Vec3f32,
Vec3f64,
Vec4u8,
}
impl PasturePrimitiveType {
fn min_alignment(&self) -> u64 {
match self {
PasturePrimitiveType::U8 => 1,
PasturePrimitiveType::I8 => 1,
PasturePrimitiveType::U16 => 2,
PasturePrimitiveType::I16 => 2,
PasturePrimitiveType::U32 => 4,
PasturePrimitiveType::I32 => 4,
PasturePrimitiveType::U64 => 8,
PasturePrimitiveType::I64 => 8,
PasturePrimitiveType::F32 => 4,
PasturePrimitiveType::F64 => 8,
PasturePrimitiveType::Bool => 1,
PasturePrimitiveType::Vec3u8 => 1,
PasturePrimitiveType::Vec3u16 => 2,
PasturePrimitiveType::Vec3f32 => 4,
PasturePrimitiveType::Vec3f64 => 8,
&PasturePrimitiveType::Vec4u8 => 1,
}
}
fn size(&self) -> u64 {
match self {
PasturePrimitiveType::U8 => 1,
PasturePrimitiveType::I8 => 1,
PasturePrimitiveType::U16 => 2,
PasturePrimitiveType::I16 => 2,
PasturePrimitiveType::U32 => 4,
PasturePrimitiveType::I32 => 4,
PasturePrimitiveType::U64 => 8,
PasturePrimitiveType::I64 => 8,
PasturePrimitiveType::F32 => 4,
PasturePrimitiveType::F64 => 8,
PasturePrimitiveType::Bool => 1,
PasturePrimitiveType::Vec3u8 => 3,
PasturePrimitiveType::Vec3u16 => 6,
PasturePrimitiveType::Vec3f32 => 12,
PasturePrimitiveType::Vec3f64 => 24,
&PasturePrimitiveType::Vec4u8 => 4,
}
}
fn as_token_stream(&self) -> quote::__private::TokenStream {
match self {
PasturePrimitiveType::U8 => quote! {pasture_core::layout::PointAttributeDataType::U8},
PasturePrimitiveType::I8 => quote! {pasture_core::layout::PointAttributeDataType::I8},
PasturePrimitiveType::U16 => quote! {pasture_core::layout::PointAttributeDataType::U16},
PasturePrimitiveType::I16 => quote! {pasture_core::layout::PointAttributeDataType::I16},
PasturePrimitiveType::U32 => quote! {pasture_core::layout::PointAttributeDataType::U32},
PasturePrimitiveType::I32 => quote! {pasture_core::layout::PointAttributeDataType::I32},
PasturePrimitiveType::U64 => quote! {pasture_core::layout::PointAttributeDataType::U64},
PasturePrimitiveType::I64 => quote! {pasture_core::layout::PointAttributeDataType::I64},
PasturePrimitiveType::F32 => quote! {pasture_core::layout::PointAttributeDataType::F32},
PasturePrimitiveType::F64 => quote! {pasture_core::layout::PointAttributeDataType::F64},
PasturePrimitiveType::Bool => {
quote! {pasture_core::layout::PointAttributeDataType::Bool}
}
PasturePrimitiveType::Vec3u8 => {
quote! {pasture_core::layout::PointAttributeDataType::Vec3u8}
}
PasturePrimitiveType::Vec3u16 => {
quote! {pasture_core::layout::PointAttributeDataType::Vec3u16}
}
PasturePrimitiveType::Vec3f32 => {
quote! {pasture_core::layout::PointAttributeDataType::Vec3f32}
}
PasturePrimitiveType::Vec3f64 => {
quote! {pasture_core::layout::PointAttributeDataType::Vec3f64}
}
PasturePrimitiveType::Vec4u8 => {
quote! {pasture_core::layout::PointAttributeDataType::Vec4u8}
}
}
}
}
fn get_primitive_type_for_ident_type(ident: &Ident) -> Result<PasturePrimitiveType> {
let type_name = ident.to_string();
match type_name.as_str() {
"u8" => Ok(PasturePrimitiveType::U8),
"u16" => Ok(PasturePrimitiveType::U16),
"u32" => Ok(PasturePrimitiveType::U32),
"u64" => Ok(PasturePrimitiveType::U64),
"i8" => Ok(PasturePrimitiveType::I8),
"i16" => Ok(PasturePrimitiveType::I16),
"i32" => Ok(PasturePrimitiveType::I32),
"i64" => Ok(PasturePrimitiveType::I64),
"f32" => Ok(PasturePrimitiveType::F32),
"f64" => Ok(PasturePrimitiveType::F64),
"bool" => Ok(PasturePrimitiveType::Bool),
_ => Err(Error::new_spanned(
ident,
format!("Type {} is no valid Pasture primitive type!", type_name),
)),
}
}
fn get_primitive_type_for_non_ident_type(type_path: &TypePath) -> Result<PasturePrimitiveType> {
let valid_idents: HashSet<_> = ["Vector3", "Vector4"].iter().collect();
let path_segment = type_path
.path
.segments
.first()
.ok_or_else(|| Error::new_spanned(&type_path.path, "Invalid type"))?;
if !valid_idents.contains(&path_segment.ident.to_string().as_str()) {
return Err(Error::new_spanned(&path_segment.ident, "Invalid type"));
}
let path_arg = match &path_segment.arguments {
PathArguments::AngleBracketed(arg) => arg,
_ => return Err(Error::new_spanned(&path_segment.arguments, "Invalid type")),
};
let first_generic_arg = path_arg
.args
.first()
.ok_or_else(|| Error::new_spanned(path_arg, "Invalid type arguments"))?;
let type_arg = match first_generic_arg {
GenericArgument::Type(t) => t,
_ => return Err(Error::new_spanned(first_generic_arg, "Invalid type")),
};
let type_path = match type_arg {
Type::Path(p) => p,
_ => return Err(Error::new_spanned(type_arg, "Invalid type")),
};
match type_path.path.get_ident() {
Some(ident) => {
let type_name = ident.to_string();
match path_segment.ident.to_string().as_str() {
"Vector3" => match type_name.as_str() {
"u8" => Ok(PasturePrimitiveType::Vec3u8),
"u16" => Ok(PasturePrimitiveType::Vec3u16),
"f32" => Ok(PasturePrimitiveType::Vec3f32),
"f64" => Ok(PasturePrimitiveType::Vec3f64),
_ => Err(Error::new_spanned(
ident,
format!("Vector3<{}> is no valid Pasture primitive type. Vector3 is supported, but only for generic argument(s) u8, u16, f32 or f64", type_name),
))
},
"Vector4" => match type_name.as_str() {
"u8" => Ok(PasturePrimitiveType::Vec4u8),
_ => Err(Error::new_spanned(
ident,
format!("Vector4<{}> is no valid Pasture primitive type. Vector4 is supported, but only for generic argument(s) u8", type_name),
))
},
_ => Err(Error::new_spanned(ident, format!("Invalid type"))),
}
}
None => Err(Error::new_spanned(&type_path.path, "Invalid type")),
}
}
fn type_path_to_primitive_type(type_path: &TypePath) -> Result<PasturePrimitiveType> {
if type_path.qself.is_some() {
return Err(Error::new_spanned(
type_path,
"Qualified types are illegal in a struct with #[derive(PointType)]",
));
}
let datatype = match type_path.path.get_ident() {
Some(ident) => get_primitive_type_for_ident_type(ident),
None => get_primitive_type_for_non_ident_type(type_path),
}?;
Ok(datatype)
}
fn get_attribute_name_from_field(field: &Field) -> Result<String> {
if field.attrs.len() != 1 {
return Err(Error::new_spanned(
field,
"derive(PointType) requires exactly one #[pasture] attribute per member!",
));
}
let pasture_attribute = &field.attrs[0];
let meta = pasture_attribute.parse_meta()?;
let malformed_field_error_msg = "#[pasture] attribute is malformed. Correct syntax is #[pasture(attribute = \"NAME\")] or #[pasture(BUILTIN_XXX)], where XXX matches any of the builtin attributes in Pasture.";
match &meta {
syn::Meta::List(list) => {
let first_list_entry = list
.nested
.first()
.ok_or_else(|| Error::new_spanned(list, malformed_field_error_msg))?;
let nested_meta = match first_list_entry {
NestedMeta::Meta(nested_meta) => nested_meta,
_ => return Err(Error::new_spanned(list, malformed_field_error_msg)),
};
match nested_meta {
syn::Meta::Path(path) => {
let ident = path
.get_ident()
.ok_or_else(|| Error::new_spanned(path, malformed_field_error_msg))?;
let ident_as_str = ident.to_string();
match ident_as_str.as_str() {
"BUILTIN_POSITION_3D" => Ok("Position3D".into()),
"BUILTIN_INTENSITY" => Ok("Intensity".into()),
"BUILTIN_RETURN_NUMBER" => Ok("ReturnNumber".into()),
"BUILTIN_NUMBER_OF_RETURNS" => Ok("NumberOfReturns".into()),
"BUILTIN_CLASSIFICATION_FLAGS" => Ok("ClassificationFlags".into()),
"BUILTIN_SCANNER_CHANNEL" => Ok("ScannerChannel".into()),
"BUILTIN_SCAN_DIRECTION_FLAG" => Ok("ScanDirectionFlag".into()),
"BUILTIN_EDGE_OF_FLIGHT_LINE" => Ok("EdgeOfFlightLine".into()),
"BUILTIN_CLASSIFICATION" => Ok("Classification".into()),
"BUILTIN_SCAN_ANGLE_RANK" => Ok("ScanAngleRank".into()),
"BUILTIN_SCAN_ANGLE" => Ok("ScanAngle".into()),
"BUILTIN_USER_DATA" => Ok("UserData".into()),
"BUILTIN_POINT_SOURCE_ID" => Ok("PointSourceID".into()),
"BUILTIN_COLOR_RGB" => Ok("ColorRGB".into()),
"BUILTIN_GPS_TIME" => Ok("GpsTime".into()),
"BUILTIN_NIR" => Ok("NIR".into()),
"BUILTIN_WAVE_PACKET_DESCRIPTOR_INDEX" => {
Ok("WavePacketDescriptorIndex".into())
}
"BUILTIN_WAVEFORM_DATA_OFFSET" => Ok("WaveformDataOffset".into()),
"BUILTIN_WAVEFORM_PACKET_SIZE" => Ok("WaveformPacketSize".into()),
"BUILTIN_RETURN_POINT_WAVEFORM_LOCATION" => {
Ok("ReturnPointWaveformLocation".into())
}
"BUILTIN_WAVEFORM_PARAMETERS" => Ok("WaveformParameters".into()),
"BUILTIN_POINT_ID" => Ok("PointID".into()),
"BUILTIN_NORMAL" => Ok("Normal".into()),
_ => {
return Err(Error::new_spanned(
ident,
format!("Unrecognized attribute name {}", ident_as_str),
))
}
}
}
syn::Meta::NameValue(name_value) => name_value
.path
.get_ident()
.and_then(|path| {
if path != "attribute" {
return None;
}
if let Lit::Str(ref attribute_name) = name_value.lit {
Some(attribute_name.value())
} else {
None
}
})
.ok_or_else(|| Error::new_spanned(name_value, malformed_field_error_msg)),
bad => Err(Error::new_spanned(bad, malformed_field_error_msg)),
}
}
bad => Err(Error::new_spanned(bad, malformed_field_error_msg)),
}
}
struct FieldLayoutDescription {
pub attribute_name: String,
pub primitive_type: PasturePrimitiveType,
}
fn get_field_layout_descriptions(fields: &Fields) -> Result<Vec<FieldLayoutDescription>> {
fields
.iter()
.map(|field| match field.ty {
Type::Path(ref type_path) => {
let primitive_type = type_path_to_primitive_type(type_path)?;
let attribute_name = get_attribute_name_from_field(field)?;
Ok(FieldLayoutDescription {
attribute_name,
primitive_type,
})
}
ref bad => Err(Error::new_spanned(
bad,
format!("Invalid type in PointType struct"),
)),
})
.collect::<Result<Vec<FieldLayoutDescription>>>()
}
fn field_parameters(data: &Data, ident: &Ident) -> Result<Vec<FieldLayoutDescription>> {
match data {
Data::Struct(struct_data) => get_field_layout_descriptions(&struct_data.fields),
_ => Err(Error::new_spanned(
ident,
format!("#[derive(PointType)] is only valid for structs"),
)),
}
}
fn calculate_offsets_and_alignment(
fields: &[FieldLayoutDescription],
data: &Data,
ident: &Ident,
type_attributes: &[Attribute],
) -> Result<(Vec<u64>, u64)> {
let struct_data = match data {
Data::Struct(struct_data) => struct_data,
_ => {
return Err(Error::new_spanned(
ident,
format!("#[derive(PointType)] is only valid for structs"),
))
}
};
let struct_layout = get_struct_member_layout(type_attributes, struct_data)?;
let mut current_offset = 0;
let mut max_alignment = 1;
let mut offsets = vec![];
for field in fields {
let min_alignment = match struct_layout {
StructMemberLayout::C => field.primitive_type.min_alignment(),
StructMemberLayout::Packed(max_alignment) => {
std::cmp::min(max_alignment, field.primitive_type.min_alignment())
}
};
max_alignment = std::cmp::max(min_alignment, max_alignment);
let aligned_offset = ((current_offset + min_alignment - 1) / min_alignment) * min_alignment;
offsets.push(aligned_offset);
current_offset = aligned_offset + field.primitive_type.size();
}
Ok((offsets, max_alignment))
}
#[proc_macro_derive(PointType, attributes(pasture))]
pub fn derive_point_type(item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as DeriveInput);
if !input.generics.params.is_empty() {
return Error::new_spanned(input, "derive(PointType) is not valid for generic types")
.to_compile_error()
.into();
}
let name = &input.ident;
let fields = match field_parameters(&input.data, name) {
Ok(inner) => inner,
Err(why) => {
return why.to_compile_error().into();
}
};
let (offsets, type_alignment) =
match calculate_offsets_and_alignment(&fields, &input.data, name, input.attrs.as_slice()) {
Ok(inner) => inner,
Err(why) => {
return why.to_compile_error().into();
}
};
let attribute_descriptions = fields.iter().zip(offsets.iter()).map(|(field, offset)| {
let attribute_name = &field.attribute_name;
let primitive_type = &field.primitive_type.as_token_stream();
quote! {
pasture_core::layout::PointAttributeDefinition::custom(#attribute_name, #primitive_type).at_offset_in_type(#offset)
}
});
let gen = quote! {
impl pasture_core::layout::PointType for #name {
fn layout() -> pasture_core::layout::PointLayout {
pasture_core::layout::PointLayout::from_members_and_alignment(&[
#(#attribute_descriptions ,)*
], #type_alignment)
}
}
};
gen.into()
}