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
use crate::{
decode::{Decoder, DecoderBuffer},
encode::{Encoder, EncoderBuffer},
prelude::*,
};
use autocxx::prelude::*;
pub type PointCloudBuilder = WrappedDracoObject<ffi::draco::PointCloudBuilder>;
impl PointCloudBuilder {
pub fn new(num: u32) -> Self {
let mut builder = ffi::draco::PointCloudBuilder::new().within_unique_ptr();
builder.pin_mut().Start(num);
Self(builder)
}
pub fn add_attribute(
&mut self,
attribute_type: ffi::draco::GeometryAttribute_Type,
num_components: i8,
data_type: ffi::draco::DataType,
) -> AttrId {
self.0
.pin_mut()
.AddAttribute(attribute_type, num_components, data_type)
.into()
}
/// Adds the data from the provided slice as the attribute value for a specific point.
///
/// # Safety
///
/// The `point` slice must point to valid memory with a lifetime at least as long as the `PointCloudBuilder` instance.
/// The size and type of the data in the slice must be compatible with the attribute `attr_id`.
pub fn add_point<T>(
&mut self,
attr_id: AttrId,
point_index: impl Into<ffi::draco::PointIndex>,
point: &[T],
) {
unsafe { self.add_point_with_ptr(attr_id, point_index, point.as_ptr() as *const c_void) }
}
/// Adds the data pointed to by the raw pointer as the attribute value for a specific point.
///
/// # Safety
///
/// This function is unsafe because it takes a raw pointer `ptr` to the point data.
/// It is the caller's responsibility to ensure that:
/// - `ptr` is valid and points to memory that is properly aligned for the attribute type.
/// - The memory pointed to by `ptr` has a lifetime at least as long as the `PointCloudBuilder` instance.
/// - The size of the data pointed to by `ptr` is sufficient for the attribute type associated with `attr_id`.
pub unsafe fn add_point_with_ptr(
&mut self,
attr_id: AttrId,
point_index: impl Into<ffi::draco::PointIndex>,
ptr: *const c_void,
) {
self.0
.pin_mut()
.SetAttributeValueForPoint(attr_id.into(), point_index.into(), ptr);
}
pub fn build(mut self, deduplicate_points: bool) -> PointCloud {
PointCloud {
0: self.0.pin_mut().Finalize(deduplicate_points),
}
}
}
pub type PointCloud = WrappedDracoObject<ffi::draco::PointCloud>;
impl Default for PointCloud {
fn default() -> Self {
Self::new()
}
}
impl PointCloud {
pub fn new() -> Self {
let pc = ffi::draco::PointCloud::new().within_unique_ptr();
Self(pc)
}
// This function returns the attribute id of the attribute with the given name
// It stores the value in-place
pub fn get_point<T>(
&mut self,
attr_id: AttrId,
point_index: impl Into<ffi::draco::PointIndex>,
point_container: &mut [T],
) where
T: Default + Copy,
{
let pa_ptr = self.0.pin_mut().GetAttributeByUniqueId(attr_id.as_u32());
unsafe {
(*pa_ptr).GetMappedValue(
point_index.into(),
point_container.as_mut_ptr() as *mut c_void,
);
};
}
// This function allocates a new array of type T and fills it with the point data
pub fn get_point_alloc<T, const N: usize>(
&mut self,
attr_id: AttrId,
point_index: impl Into<ffi::draco::PointIndex>,
) -> [T; N]
where
T: Default + Copy,
{
let mut point = [T::default(); N];
self.get_point(attr_id, point_index, &mut point);
point
}
// Returns the number of named attributes of a given type.
pub fn num_named_attributes(&self, attr_type: ffi::draco::GeometryAttribute_Type) -> i32 {
self.0.NumNamedAttributes(attr_type)
}
// Returns the id of the i-th named attribute of a given type.
pub fn get_named_attribute_id(
&self,
attr_type: ffi::draco::GeometryAttribute_Type,
i: i32,
) -> Option<AttrId> {
let id = self.0.GetNamedAttributeId1(attr_type, i.into());
if id < 0 {
None
} else {
Some(AttrId(id))
}
}
// // Returns the i-th named attribute of a given type.
// pub fn get_named_attribute(
// &self,
// attr_type: ffi::draco::GeometryAttribute_Type,
// i: i32,
// ) -> Option<NonOwningPointAttribute> {
// let attr = self.0.GetNamedAttribute1(attr_type, i.into());
// if attr.is_null() {
// None
// } else {
// Some(NonOwningPointAttribute { ptr: attr })
// }
// }
// // Returns the named attribute of a given unique id.
// pub fn get_named_attribute_by_unique_id(
// &self,
// attr_type: ffi::draco::GeometryAttribute_Type,
// id: u32,
// ) -> Option<NonOwningPointAttribute> {
// let attr = self.0.GetNamedAttributeByUniqueId(attr_type, id);
// if attr.is_null() {
// None
// } else {
// Some(NonOwningPointAttribute { ptr: attr })
// }
// }
pub fn num_points(&self) -> u32 {
self.0.num_points()
}
pub fn len(&self) -> u32 {
self.0.num_points()
}
pub fn is_empty(&self) -> bool {
self.0.num_points() == 0
}
/// Encode the point cloud to an encoder buffer
pub fn to_buffer(&self, encoder: &mut Encoder) -> DracoStatusType<EncoderBuffer> {
let mut buffer = EncoderBuffer::new();
let status = unsafe {
encoder
.0
.pin_mut()
.EncodePointCloudToBuffer(self.0.as_ref().unwrap(), buffer.as_mut_ptr())
.within_unique_ptr()
};
if status.ok() {
Ok(buffer)
} else {
Err(status.into())
}
}
/// Decode a point cloud from a decoder buffer
///
/// # Safety
///
/// The decoder buffer must contains valid memory
pub fn from_buffer(decoder: &mut Decoder, buffer: &mut DecoderBuffer) -> DracoStatusType<Self> {
let mut status_or = unsafe {
decoder
.decoder
.pin_mut()
.DecodePointCloudFromBuffer(buffer.0.as_mut_ptr())
};
if status_or.ok() {
Ok(Self(status_or.pin_mut().value()))
} else {
Err(status_or.status().within_unique_ptr().into())
}
}
}