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
use std::ptr;
use crate::error::Result;
use crate::ffi;
use crate::handle::ObjectHandle;
use crate::object::Object;
use crate::texture::Texture;
use crate::types::{BoundingBox, CameraInfo, CameraProjection, StereoscopicCameraInfo};
use crate::util::{parse_json, required_handle};
#[derive(Debug, Clone)]
/// Wraps the corresponding Model I/O camera counterpart.
pub struct Camera {
handle: ObjectHandle,
}
impl Camera {
/// Builds this wrapper from the retained handle of the wrapped Model I/O camera counterpart.
pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
Self { handle }
}
/// Wraps the corresponding Model I/O initializer for the wrapped Model I/O camera counterpart.
pub fn new() -> Result<Self> {
let mut out_camera = 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_camera_new(&mut out_camera, &mut out_error) };
crate::util::status_result(status, out_error)?;
Ok(Self::from_handle(required_handle(out_camera, "MDLCamera")?))
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn info(&self) -> Result<CameraInfo> {
parse_json(
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_camera_info_json(self.handle.as_ptr()) },
"MDLCamera",
)
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn set_projection(&self, projection: CameraProjection) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_camera_set_projection(self.handle.as_ptr(), projection.as_raw()) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn set_near_visibility_distance(&self, value: f32) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_camera_set_near_visibility_distance(self.handle.as_ptr(), value) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn set_far_visibility_distance(&self, value: f32) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_camera_set_far_visibility_distance(self.handle.as_ptr(), value) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn set_world_to_meters_conversion_scale(&self, value: f32) {
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_camera_set_world_to_meters_conversion_scale(self.handle.as_ptr(), value);
};
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn set_focal_length(&self, value: f32) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_camera_set_focal_length(self.handle.as_ptr(), value) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn set_focus_distance(&self, value: f32) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_camera_set_focus_distance(self.handle.as_ptr(), value) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn set_field_of_view(&self, value: f32) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_camera_set_field_of_view(self.handle.as_ptr(), value) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn look_at(&self, focus_position: [f32; 3]) {
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_camera_look_at(
self.handle.as_ptr(),
focus_position[0],
focus_position[1],
focus_position[2],
);
};
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn look_at_from(&self, focus_position: [f32; 3], camera_position: [f32; 3]) {
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_camera_look_at_from(
self.handle.as_ptr(),
focus_position[0],
focus_position[1],
focus_position[2],
camera_position[0],
camera_position[1],
camera_position[2],
);
};
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn frame_bounding_box(&self, bounding_box: BoundingBox, set_near_and_far: bool) {
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_camera_frame_bounding_box(
self.handle.as_ptr(),
bounding_box.min[0],
bounding_box.min[1],
bounding_box.min[2],
bounding_box.max[0],
bounding_box.max[1],
bounding_box.max[2],
i32::from(set_near_and_far),
);
};
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn ray_to(&self, pixel: [i32; 2], viewport: [i32; 2]) -> [f32; 3] {
let mut ray = [0.0_f32; 3];
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_camera_ray_to(
self.handle.as_ptr(),
pixel[0],
pixel[1],
viewport[0],
viewport[1],
&mut ray[0],
&mut ray[1],
&mut ray[2],
);
};
ray
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn bokeh_kernel(&self, size: [i32; 2]) -> Option<Texture> {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
let ptr = unsafe { ffi::mdl_camera_bokeh_kernel(self.handle.as_ptr(), size[0], size[1]) };
// SAFETY: The unsafe operation is valid in this context.
unsafe { ObjectHandle::from_retained_ptr(ptr) }.map(Texture::from_handle)
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O camera counterpart.
pub fn as_object(&self) -> Object {
Object::from_handle(self.handle.clone())
}
}
#[derive(Debug, Clone)]
/// Wraps the corresponding Model I/O stereoscopic camera counterpart.
pub struct StereoscopicCamera {
handle: ObjectHandle,
}
impl StereoscopicCamera {
/// Builds this wrapper from the retained handle of the wrapped Model I/O stereoscopic camera counterpart.
pub(crate) fn from_handle(handle: ObjectHandle) -> Self {
Self { handle }
}
/// Wraps the corresponding Model I/O initializer for the wrapped Model I/O stereoscopic camera counterpart.
pub fn new() -> Result<Self> {
let mut out_camera = 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_stereoscopic_camera_new(&mut out_camera, &mut out_error) };
crate::util::status_result(status, out_error)?;
Ok(Self::from_handle(required_handle(
out_camera,
"MDLStereoscopicCamera",
)?))
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O stereoscopic camera counterpart.
pub fn info(&self) -> Result<StereoscopicCameraInfo> {
parse_json(
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_stereoscopic_camera_info_json(self.handle.as_ptr()) },
"MDLStereoscopicCamera",
)
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O stereoscopic camera counterpart.
pub fn set_inter_pupillary_distance(&self, value: f32) {
// SAFETY: The unsafe operation is valid in this context.
unsafe {
ffi::mdl_stereoscopic_camera_set_inter_pupillary_distance(self.handle.as_ptr(), value);
};
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O stereoscopic camera counterpart.
pub fn set_left_vergence(&self, value: f32) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_stereoscopic_camera_set_left_vergence(self.handle.as_ptr(), value) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O stereoscopic camera counterpart.
pub fn set_right_vergence(&self, value: f32) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_stereoscopic_camera_set_right_vergence(self.handle.as_ptr(), value) };
}
/// Calls the corresponding Model I/O method on the wrapped Model I/O stereoscopic camera counterpart.
pub fn set_overlap(&self, value: f32) {
// SAFETY: ObjectHandle wraps a valid opaque pointer from Swift; FFI function accepts it safely.
unsafe { ffi::mdl_stereoscopic_camera_set_overlap(self.handle.as_ptr(), value) };
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O stereoscopic camera counterpart.
pub fn as_camera(&self) -> Camera {
Camera::from_handle(self.handle.clone())
}
#[must_use]
/// Calls the corresponding Model I/O method on the wrapped Model I/O stereoscopic camera counterpart.
pub fn as_object(&self) -> Object {
Object::from_handle(self.handle.clone())
}
}