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
use std::ptr;
use crate::camera::Camera;
use crate::error::Result;
use crate::ffi;
use crate::handle::ObjectHandle;
use crate::light::Light;
use crate::mesh::Mesh;
use crate::physically_plausible_light::PhysicallyPlausibleLight;
use crate::protocols::{Component, Named, ObjectContainerComponent};
use crate::skeleton::Skeleton;
use crate::types::{BoundingBox, ObjectInfo, ObjectKind};
use crate::util::{c_string, parse_json, required_handle, take_string};
use crate::voxel_array::VoxelArray;
use crate::PackedJointAnimation;
#[derive(Debug, Clone)]
/// Wraps the corresponding Model I/O object counterpart.
pub struct Object {
handle: ObjectHandle,
}
impl Named for Object {
fn name(&self) -> Option<String> {
self.name()
}
fn set_name(&self, name: &str) -> Result<()> {
self.set_name(name)
}
}
impl Object {
/// Builds this wrapper from the retained handle of the wrapped Model I/O object counterpart.
pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
Self { handle }
}
/// Returns the opaque pointer used to call the wrapped Model I/O object counterpart.
pub(crate) fn as_ptr(&self) -> *mut core::ffi::c_void {
self.handle.as_ptr()
}
/// Wraps the corresponding Model I/O initializer for the wrapped Model I/O object counterpart.
pub fn new() -> Result<Self> {
let mut out_object = ptr::null_mut();
let mut out_error = ptr::null_mut();
// SAFETY: Output pointers are initialized and managed; FFI function is called safely.
let status = unsafe { ffi::mdl_object_new(&mut out_object, &mut out_error) };
crate::util::status_result(status, out_error)?;
Ok(Self::from_handle(required_handle(out_object, "MDLObject")?))
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn info(&self) -> Result<ObjectInfo> {
parse_json(
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_info_json(self.handle.as_ptr()) },
"MDLObject",
)
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn kind(&self) -> ObjectKind {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
ObjectKind::from_raw(unsafe { ffi::mdl_object_kind(self.handle.as_ptr()) })
.unwrap_or(ObjectKind::Unknown)
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn name(&self) -> Option<String> {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
take_string(unsafe { ffi::mdl_object_name_string(self.handle.as_ptr()) })
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn set_name(&self, name: &str) -> Result<()> {
let name = c_string(name)?;
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_set_name(self.handle.as_ptr(), name.as_ptr()) };
Ok(())
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn path(&self) -> Option<String> {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
take_string(unsafe { ffi::mdl_object_path_string(self.handle.as_ptr()) })
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn hidden(&self) -> bool {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_hidden(self.handle.as_ptr()) != 0 }
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn set_hidden(&self, hidden: bool) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_set_hidden(self.handle.as_ptr(), i32::from(hidden)) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn add_child(&self, child: &Self) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_add_child(self.handle.as_ptr(), child.handle.as_ptr()) };
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn children_container(&self) -> Option<ObjectContainer> {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
let ptr = unsafe { ffi::mdl_object_children_container(self.handle.as_ptr()) };
// SAFETY: The unsafe operation is valid in this context.
unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(ObjectContainer::from_handle)
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn set_children_container(&self, container: Option<&ObjectContainer>) {
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_object_set_children_container(
self.handle.as_ptr(),
container.map_or(ptr::null_mut(), ObjectContainer::as_ptr),
);
};
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn child_count(&self) -> usize {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_child_count(self.handle.as_ptr()) as usize }
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn child_at(&self, index: usize) -> Option<Self> {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
let ptr = unsafe { ffi::mdl_object_child_at(self.handle.as_ptr(), index as u64) };
// SAFETY: The unsafe operation is valid in this context.
unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(Self::from_handle)
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn children(&self) -> Vec<Self> {
(0..self.child_count())
.filter_map(|index| self.child_at(index))
.collect()
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn at_path(&self, path: &str) -> Result<Option<Self>> {
let path = c_string(path)?;
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
let ptr = unsafe { ffi::mdl_object_at_path(self.handle.as_ptr(), path.as_ptr()) };
// SAFETY: The unsafe operation is valid in this context.
Ok(unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(Self::from_handle))
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn bounding_box_at_time(&self, time: f64) -> BoundingBox {
let mut min = [0.0_f32; 3];
let mut max = [0.0_f32; 3];
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_object_bounding_box_at_time(
self.handle.as_ptr(),
time,
&mut min[0],
&mut min[1],
&mut min[2],
&mut max[0],
&mut max[1],
&mut max[2],
);
}
BoundingBox { min, max }
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn as_mesh(&self) -> Option<Mesh> {
(self.kind() == ObjectKind::Mesh).then(|| Mesh::from_handle(self.handle.clone()))
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn as_light(&self) -> Option<Light> {
matches!(
self.kind(),
ObjectKind::Light | ObjectKind::PhysicallyPlausibleLight
)
.then(|| Light::from_handle(self.handle.clone()))
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn as_physically_plausible_light(&self) -> Option<PhysicallyPlausibleLight> {
(self.kind() == ObjectKind::PhysicallyPlausibleLight)
.then(|| PhysicallyPlausibleLight::from_handle(self.handle.clone()))
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn as_camera(&self) -> Option<Camera> {
(self.kind() == ObjectKind::Camera).then(|| Camera::from_handle(self.handle.clone()))
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn as_voxel_array(&self) -> Option<VoxelArray> {
(self.kind() == ObjectKind::VoxelArray)
.then(|| VoxelArray::from_handle(self.handle.clone()))
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn as_skeleton(&self) -> Option<Skeleton> {
(self.kind() == ObjectKind::Skeleton).then(|| Skeleton::from_handle(self.handle.clone()))
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object counterpart.
pub fn as_packed_joint_animation(&self) -> Option<PackedJointAnimation> {
(self.kind() == ObjectKind::PackedJointAnimation)
.then(|| PackedJointAnimation::from_handle(self.handle.clone()))
}
}
#[derive(Debug, Clone)]
/// Wraps the corresponding Model I/O object container counterpart.
pub struct ObjectContainer {
handle: ObjectHandle,
}
impl Component for ObjectContainer {}
impl ObjectContainerComponent for ObjectContainer {
fn count(&self) -> usize {
self.count()
}
fn object_at(&self, index: usize) -> Option<Object> {
self.object_at(index)
}
fn add_object(&self, object: &Object) {
self.add_object(object);
}
fn remove_object(&self, object: &Object) {
self.remove_object(object);
}
}
impl ObjectContainer {
/// Builds this wrapper from the retained handle of the wrapped Model I/O object container counterpart.
pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
Self { handle }
}
/// Returns the opaque pointer used to call the wrapped Model I/O object container counterpart.
pub(crate) fn as_ptr(&self) -> *mut core::ffi::c_void {
self.handle.as_ptr()
}
/// Wraps the corresponding Model I/O initializer for the wrapped Model I/O object container counterpart.
pub fn new() -> Result<Self> {
let mut out_container = ptr::null_mut();
let mut out_error = ptr::null_mut();
// SAFETY: Output pointers are initialized and managed; FFI function is called safely.
let status = unsafe { ffi::mdl_object_container_new(&mut out_container, &mut out_error) };
crate::util::status_result(status, out_error)?;
Ok(Self::from_handle(required_handle(
out_container,
"MDLObjectContainer",
)?))
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object container counterpart.
pub fn count(&self) -> usize {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_container_count(self.handle.as_ptr()) as usize }
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object container counterpart.
pub fn object_at(&self, index: usize) -> Option<Object> {
let ptr =
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_container_object_at(self.handle.as_ptr(), index as u64) };
// SAFETY: The unsafe operation is valid in this context.
unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(Object::from_handle)
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O object container counterpart.
pub fn objects(&self) -> Vec<Object> {
(0..self.count())
.filter_map(|index| self.object_at(index))
.collect()
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O object container counterpart.
pub fn add_object(&self, object: &Object) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_container_add_object(self.handle.as_ptr(), object.as_ptr()) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O object container counterpart.
pub fn remove_object(&self, object: &Object) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_object_container_remove_object(self.handle.as_ptr(), object.as_ptr()) };
}
}