copp 0.2.0

Convex-objective path parameterization for robotic trajectory planning.
Documentation
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
485
486
//! C ABI wrappers for interpolation utilities.

use crate::copp::InterpolationMode;
use crate::copp::copp2::stable::basic::{a_to_b_topp2, s_to_t_topp2, t_to_s_topp2};
use crate::copp::copp3::stable::basic::{s_to_t_topp3, t_to_s_topp3};
use crate::copp::copp3::stable::topp3_lp::force_positive_a;
use crate::diag::CoppError;
use crate::ffi::c::core::status::panic_to_status;
use crate::ffi::c::{CoppSliceF64, CoppSliceMutF64, CoppStatus, CoppVecF64};
use std::panic::{AssertUnwindSafe, catch_unwind};

/// Compute TOPP2 segment profile `b` from node profile `a`.
///
/// On success, `out_b` owns a COPP-allocated vector and must be released with
/// `copp_vec_f64_free`.
///
/// # Safety
/// `s.data` and `a.data` must be valid for `len` reads when their lengths are
/// non-zero. `out_b` must be valid for one `CoppVecF64` write.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn copp_a_to_b_2nd(
    s: CoppSliceF64,
    a: CoppSliceF64,
    out_b: *mut CoppVecF64,
) -> CoppStatus {
    crate::ffi::c::core::status::clear_last_error();
    let out_b = match CoppVecF64::out_ptr(out_b) {
        Ok(out_b) => out_b,
        Err(status) => return status,
    };
    // SAFETY: `out_b` was checked for null above; caller must provide a valid
    // output location as part of the C ABI contract.
    unsafe {
        CoppVecF64::write_empty_to(out_b);
    }

    match catch_unwind(AssertUnwindSafe(|| {
        // SAFETY: The C ABI contract requires non-empty input slices to point
        // to valid contiguous `double` arrays for the duration of this call.
        let s = unsafe { s.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let a = unsafe { a.as_slice()? };
        let b = a_to_b_topp2(s, a).map_err(interpolation_status)?;
        // SAFETY: Same checked output location as above.
        unsafe {
            CoppVecF64::write_vec_to(out_b, b);
        }
        Ok(CoppStatus::Ok)
    })) {
        Ok(Ok(status)) | Ok(Err(status)) => status.into_ffi_status(),
        Err(payload) => panic_to_status(payload).into_ffi_status(),
    }
}

/// Compute cumulative TOPP2 time profile `t(s)` from node profile `a(s)`.
///
/// On success, `*out_t_final` receives the final time and `out_t_s` owns a
/// COPP-allocated vector that must be released with `copp_vec_f64_free`.
///
/// # Safety
/// `s.data` and `a.data` must be valid for `len` reads when their lengths are
/// non-zero. `out_t_final` must be valid for one `double` write and `out_t_s`
/// must be valid for one `CoppVecF64` write. C callers can pass the address of
/// ordinary stack variables; heap allocation is not required for these outputs.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn copp_s_to_t_2nd(
    s: CoppSliceF64,
    a: CoppSliceF64,
    t0: f64,
    out_t_final: *mut f64,
    out_t_s: *mut CoppVecF64,
) -> CoppStatus {
    crate::ffi::c::core::status::clear_last_error();
    if out_t_final.is_null() || out_t_s.is_null() {
        return CoppStatus::NullPointer.into_ffi_status();
    }
    let out_t_s = match CoppVecF64::out_ptr(out_t_s) {
        Ok(out_t_s) => out_t_s,
        Err(status) => return status,
    };
    // SAFETY: `out_t_s` was checked for null above; caller must provide a valid
    // output location as part of the C ABI contract.
    unsafe {
        CoppVecF64::write_empty_to(out_t_s);
    }

    match catch_unwind(AssertUnwindSafe(|| {
        // SAFETY: The C ABI contract requires non-empty input slices to point
        // to valid contiguous `double` arrays for the duration of this call.
        let s = unsafe { s.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let a = unsafe { a.as_slice()? };
        let (t_final, t_s) = s_to_t_topp2(s, a, t0).map_err(interpolation_status)?;

        // SAFETY: `out_t_final` is checked for null above and is expected to be
        // valid for writes by the C ABI contract.
        unsafe {
            out_t_final.write(t_final);
        }
        // SAFETY: Same checked output location as above.
        unsafe {
            CoppVecF64::write_vec_to(out_t_s, t_s);
        }
        Ok(CoppStatus::Ok)
    })) {
        Ok(Ok(status)) | Ok(Err(status)) => status.into_ffi_status(),
        Err(payload) => panic_to_status(payload).into_ffi_status(),
    }
}

/// Interpolate `s(t)` from TOPP2 profiles using a uniform time grid.
///
/// On success, `out_s_t` owns a COPP-allocated vector and must be released with
/// `copp_vec_f64_free`.
///
/// # Safety
/// `s.data`, `a.data`, and `t_s.data` must be valid for `len` reads when their
/// lengths are non-zero. `out_s_t` must be valid for one `CoppVecF64` write.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn copp_t_to_s_uniform_2nd(
    s: CoppSliceF64,
    a: CoppSliceF64,
    t_s: CoppSliceF64,
    t0: f64,
    dt: f64,
    include_final: bool,
    out_s_t: *mut CoppVecF64,
) -> CoppStatus {
    crate::ffi::c::core::status::clear_last_error();
    topp2_t_to_s_common(s, a, t_s, out_s_t, |s, a, t_s| {
        t_to_s_topp2(
            s,
            a,
            t_s,
            InterpolationMode::UniformTimeGrid(t0, dt, include_final),
        )
        .map_err(interpolation_status)
    })
}

/// Interpolate `s(t)` from TOPP2 profiles using caller-provided time samples.
///
/// On success, `out_s_t` owns a COPP-allocated vector and must be released with
/// `copp_vec_f64_free`.
///
/// # Safety
/// `s.data`, `a.data`, `t_s.data`, and `t_sample.data` must be valid for `len`
/// reads when their lengths are non-zero. `out_s_t` must be valid for one
/// `CoppVecF64` write.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn copp_t_to_s_non_uniform_2nd(
    s: CoppSliceF64,
    a: CoppSliceF64,
    t_s: CoppSliceF64,
    t_sample: CoppSliceF64,
    out_s_t: *mut CoppVecF64,
) -> CoppStatus {
    crate::ffi::c::core::status::clear_last_error();
    topp2_t_to_s_common(s, a, t_s, out_s_t, |s, a, t_s| {
        // SAFETY: The C ABI contract requires non-empty input slices to point
        // to valid contiguous `double` arrays for the duration of this call.
        let t_sample = unsafe { t_sample.as_slice()? };
        t_to_s_topp2(s, a, t_s, InterpolationMode::NonUniformTimeGrid(t_sample))
            .map_err(interpolation_status)
    })
}

/// Compute cumulative TOPP3/COPP3 time profile `t(s)` from node profiles
/// `a(s) = dot{s}^2` and `b(s) = ddot{s}`.
///
/// C callers pass profile parts explicitly instead of constructing a
/// `Topp3ProfileRef`: `a` and `b` are node-based arrays with length
/// `s.len`, and `num_stationary_*` are the effective stationary interval
/// counts at the start and end.
///
/// On success, `*out_t_final` receives the final time and `out_t_s` owns a
/// COPP-allocated vector that must be released with `copp_vec_f64_free`.
///
/// # Safety
/// `s.data`, `a.data`, and `b.data` must be valid for reads when their lengths
/// are non-zero. `out_t_final` must be valid for one `double` write and
/// `out_t_s` must be valid for one `CoppVecF64` write.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn copp_s_to_t_3rd(
    s: CoppSliceF64,
    a: CoppSliceF64,
    b: CoppSliceF64,
    num_stationary_start: usize,
    num_stationary_end: usize,
    t0: f64,
    out_t_final: *mut f64,
    out_t_s: *mut CoppVecF64,
) -> CoppStatus {
    crate::ffi::c::core::status::clear_last_error();
    if out_t_final.is_null() || out_t_s.is_null() {
        return CoppStatus::NullPointer.into_ffi_status();
    }
    let out_t_s = match CoppVecF64::out_ptr(out_t_s) {
        Ok(out_t_s) => out_t_s,
        Err(status) => return status,
    };
    // SAFETY: `out_t_s` was checked for null above; caller must provide a valid
    // output location as part of the C ABI contract.
    unsafe {
        CoppVecF64::write_empty_to(out_t_s);
    }

    match catch_unwind(AssertUnwindSafe(|| {
        // SAFETY: The C ABI contract requires non-empty input slices to point
        // to valid contiguous `double` arrays for the duration of this call.
        let s = unsafe { s.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let a = unsafe { a.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let b = unsafe { b.as_slice()? };
        let (t_final, t_s) =
            s_to_t_topp3(s, (a, b, (num_stationary_start, num_stationary_end)), t0)
                .map_err(interpolation_status)?;

        // SAFETY: `out_t_final` is checked for null above and is expected to
        // be valid for writes by the C ABI contract.
        unsafe {
            out_t_final.write(t_final);
        }
        // SAFETY: Same checked output location as above.
        unsafe {
            CoppVecF64::write_vec_to(out_t_s, t_s);
        }
        Ok(CoppStatus::Ok)
    })) {
        Ok(Ok(status)) | Ok(Err(status)) => status.into_ffi_status(),
        Err(payload) => panic_to_status(payload).into_ffi_status(),
    }
}

/// Interpolate `s(t)` from TOPP3/COPP3 profiles using a uniform time grid.
///
/// C callers pass profile parts explicitly instead of constructing a
/// `Topp3ProfileRef`. On success, `out_s_t` owns a COPP-allocated vector and
/// must be released with `copp_vec_f64_free`.
///
/// # Safety
/// `s.data`, `a.data`, `b.data`, and `t_s.data` must be valid for reads when
/// their lengths are non-zero. `out_s_t` must be valid for one `CoppVecF64`
/// write.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn copp_t_to_s_uniform_3rd(
    s: CoppSliceF64,
    a: CoppSliceF64,
    b: CoppSliceF64,
    num_stationary_start: usize,
    num_stationary_end: usize,
    t_s: CoppSliceF64,
    t0: f64,
    dt: f64,
    include_final: bool,
    out_s_t: *mut CoppVecF64,
) -> CoppStatus {
    crate::ffi::c::core::status::clear_last_error();
    topp3_t_to_s_common(
        s,
        a,
        b,
        (num_stationary_start, num_stationary_end),
        t_s,
        out_s_t,
        |s, a, b, num_stationary, t_s| {
            t_to_s_topp3(
                s,
                (a, b, num_stationary),
                t_s,
                InterpolationMode::UniformTimeGrid(t0, dt, include_final),
            )
            .map_err(interpolation_status)
        },
    )
}

/// Interpolate `s(t)` from TOPP3/COPP3 profiles using caller-provided time
/// samples.
///
/// C callers pass profile parts explicitly instead of constructing a
/// `Topp3ProfileRef`. On success, `out_s_t` owns a COPP-allocated vector and
/// must be released with `copp_vec_f64_free`.
///
/// # Safety
/// `s.data`, `a.data`, `b.data`, `t_s.data`, and `t_sample.data` must be valid
/// for reads when their lengths are non-zero. `out_s_t` must be valid for one
/// `CoppVecF64` write.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn copp_t_to_s_non_uniform_3rd(
    s: CoppSliceF64,
    a: CoppSliceF64,
    b: CoppSliceF64,
    num_stationary_start: usize,
    num_stationary_end: usize,
    t_s: CoppSliceF64,
    t_sample: CoppSliceF64,
    out_s_t: *mut CoppVecF64,
) -> CoppStatus {
    crate::ffi::c::core::status::clear_last_error();
    topp3_t_to_s_common(
        s,
        a,
        b,
        (num_stationary_start, num_stationary_end),
        t_s,
        out_s_t,
        |s, a, b, num_stationary, t_s| {
            // SAFETY: The C ABI contract requires non-empty input slices to
            // point to valid contiguous `double` arrays for the duration of
            // this call.
            let t_sample = unsafe { t_sample.as_slice()? };
            t_to_s_topp3(
                s,
                (a, b, num_stationary),
                t_s,
                InterpolationMode::NonUniformTimeGrid(t_sample),
            )
            .map_err(interpolation_status)
        },
    )
}

/// Adjust mutable TOPP3/COPP3 `a` and `b` node profiles in place so
/// interpolated `a(s)` stays strictly positive per interval.
///
/// C callers pass profile parts explicitly instead of constructing a
/// `Topp3ProfileMut`. `a` and `b` must be mutable node-based arrays with length
/// `s.len`. On success, `*out_succeed` receives the operation result flag; the
/// `a.data` and `b.data` buffers may have been modified.
///
/// # Safety
/// `s.data` must be valid for reads when non-empty. `a.data` and `b.data` must
/// be valid for unique mutable access when non-empty and must not alias each
/// other. `out_succeed` must be valid for one `bool` write.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn copp_force_positive_a_3rd(
    s: CoppSliceF64,
    a: CoppSliceMutF64,
    b: CoppSliceMutF64,
    num_stationary_start: usize,
    num_stationary_end: usize,
    a_min: f64,
    out_succeed: *mut bool,
) -> CoppStatus {
    crate::ffi::c::core::status::clear_last_error();
    if out_succeed.is_null() {
        return CoppStatus::NullPointer.into_ffi_status();
    }
    // SAFETY: `out_succeed` was checked for null above and is expected to be
    // valid for one write by the C ABI contract.
    unsafe {
        out_succeed.write(false);
    }

    match catch_unwind(AssertUnwindSafe(|| {
        // SAFETY: The C ABI contract requires non-empty input slices to point
        // to valid contiguous `double` arrays for the duration of this call.
        let s = unsafe { s.as_slice()? };
        // SAFETY: The C ABI contract requires non-empty mutable slices to be
        // valid and uniquely borrowed for the duration of this call.
        let a = unsafe { a.as_mut_slice()? };
        // SAFETY: Same mutable-slice contract as above.
        let b = unsafe { b.as_mut_slice()? };
        let succeed =
            force_positive_a((a, b, (num_stationary_start, num_stationary_end)), s, a_min)
                .map_err(interpolation_status)?;

        // SAFETY: Same checked output location as above.
        unsafe {
            out_succeed.write(succeed);
        }
        Ok(CoppStatus::Ok)
    })) {
        Ok(Ok(status)) | Ok(Err(status)) => status.into_ffi_status(),
        Err(payload) => panic_to_status(payload).into_ffi_status(),
    }
}

/// Shared implementation for TOPP2 inverse interpolation C wrappers.
///
/// This helper centralizes output initialization, panic fencing, common input
/// slice conversion, error mapping, and COPP-allocated vector transfer.
fn topp2_t_to_s_common(
    s: CoppSliceF64,
    a: CoppSliceF64,
    t_s: CoppSliceF64,
    out_s_t: *mut CoppVecF64,
    interpolate: impl FnOnce(&[f64], &[f64], &[f64]) -> Result<Vec<f64>, CoppStatus>,
) -> CoppStatus {
    let out_s_t = match CoppVecF64::out_ptr(out_s_t) {
        Ok(out_s_t) => out_s_t,
        Err(status) => return status,
    };
    // SAFETY: `out_s_t` was checked for null above; caller must provide a valid
    // output location as part of the C ABI contract.
    unsafe {
        CoppVecF64::write_empty_to(out_s_t);
    }

    match catch_unwind(AssertUnwindSafe(|| {
        // SAFETY: The C ABI contract requires non-empty input slices to point
        // to valid contiguous `double` arrays for the duration of this call.
        let s = unsafe { s.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let a = unsafe { a.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let t_s = unsafe { t_s.as_slice()? };

        let s_t = interpolate(s, a, t_s)?;
        // SAFETY: Same checked output location as above.
        unsafe {
            CoppVecF64::write_vec_to(out_s_t, s_t);
        }
        Ok(CoppStatus::Ok)
    })) {
        Ok(Ok(status)) | Ok(Err(status)) => status.into_ffi_status(),
        Err(payload) => panic_to_status(payload).into_ffi_status(),
    }
}

/// Shared implementation for TOPP3 inverse interpolation C wrappers.
///
/// This helper centralizes output initialization, panic fencing, common input
/// slice conversion, error mapping, and COPP-allocated vector transfer.
fn topp3_t_to_s_common(
    s: CoppSliceF64,
    a: CoppSliceF64,
    b: CoppSliceF64,
    num_stationary: (usize, usize),
    t_s: CoppSliceF64,
    out_s_t: *mut CoppVecF64,
    interpolate: impl FnOnce(
        &[f64],
        &[f64],
        &[f64],
        (usize, usize),
        &[f64],
    ) -> Result<Vec<f64>, CoppStatus>,
) -> CoppStatus {
    let out_s_t = match CoppVecF64::out_ptr(out_s_t) {
        Ok(out_s_t) => out_s_t,
        Err(status) => return status,
    };
    // SAFETY: `out_s_t` was checked for null above; caller must provide a valid
    // output location as part of the C ABI contract.
    unsafe {
        CoppVecF64::write_empty_to(out_s_t);
    }

    let (num_stationary_start, num_stationary_end) = num_stationary;

    match catch_unwind(AssertUnwindSafe(|| {
        // SAFETY: The C ABI contract requires non-empty input slices to point
        // to valid contiguous `double` arrays for the duration of this call.
        let s = unsafe { s.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let a = unsafe { a.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let b = unsafe { b.as_slice()? };
        // SAFETY: Same input-slice contract as above.
        let t_s = unsafe { t_s.as_slice()? };

        let s_t = interpolate(s, a, b, (num_stationary_start, num_stationary_end), t_s)?;
        // SAFETY: Same checked output location as above.
        unsafe {
            CoppVecF64::write_vec_to(out_s_t, s_t);
        }
        Ok(CoppStatus::Ok)
    })) {
        Ok(Ok(status)) | Ok(Err(status)) => status.into_ffi_status(),
        Err(payload) => panic_to_status(payload).into_ffi_status(),
    }
}

/// Map interpolation-layer errors to C ABI status codes.
///
/// Interpolation input checks report `CoppError::InvalidInput`, but they are not
/// solver failures. The C ABI therefore exposes them as invalid C arguments.
fn interpolation_status(error: CoppError) -> CoppStatus {
    match error {
        CoppError::InvalidInput(_, _) => CoppStatus::InvalidArgument,
        error => CoppStatus::from(&error),
    }
}