copp 0.2.2

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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include "copp/path.hpp"

// Path facade implementation.
//
// This file is the C++ side of the private cxx bridge. Public headers expose
// `copp::Path` and callback types; this translation unit validates C++ views,
// converts them into bridge slices/descriptors, and copies Rust path evaluation
// results back into owned `copp::Matrix` objects.
//
// The key ownership rule is simple: user input buffers only need to live for the
// duration of the public call, while returned `Path` objects own their Rust-side
// path handles through RAII.

#include <cmath>
#include <stdexcept>
#include <string>
#include <vector>

#include "rust/cxx.h"
#include "copp/detail/path_bridge.hpp"
#include "copp/src/ffi/cpp/mod.rs.h"

namespace
{

    rust::Slice<const double> to_rust_slice(copp::Span<const double> values) noexcept
    {
        return rust::Slice<const double>(values.data(), values.size());
    }

    rust::Slice<const double> to_rust_slice(copp::MatrixView values) noexcept
    {
        return rust::Slice<const double>(values.data(), values.storage_size());
    }

    copp::MatrixView optional_state_view(const std::optional<copp::Matrix> &matrix) noexcept
    {
        if (!matrix)
        {
            return {};
        }
        return matrix->view();
    }

    std::string to_std_string(const rust::Error &error)
    {
        return std::string(error.what());
    }

    uint8_t to_bridge(copp::OutOfRangeMode mode)
    {
        switch (mode)
        {
        case copp::OutOfRangeMode::Error:
            return 0;
        case copp::OutOfRangeMode::Clamp:
            return 1;
        }
        throw copp::Error(copp::Status::invalid_input, "unsupported OutOfRangeMode");
    }

    copp::Matrix copy_to_column_major(copp::MatrixView view)
    {
        copp::Matrix matrix(view.rows(), view.cols());
        if (view.layout() == copp::MatrixLayout::ColumnMajor)
        {
            for (std::size_t col = 0; col < view.cols(); ++col)
            {
                const double *src = view.data() + col * view.leading_dim();
                for (std::size_t row = 0; row < view.rows(); ++row)
                {
                    matrix(row, col) = src[row];
                }
            }
            return matrix;
        }

        for (std::size_t row = 0; row < view.rows(); ++row)
        {
            const double *src = view.data() + row * view.leading_dim();
            for (std::size_t col = 0; col < view.cols(); ++col)
            {
                matrix(row, col) = src[col];
            }
        }
        return matrix;
    }

    void check_matrix_view(copp::MatrixView view, const char *name)
    {
        if (view.rows() == 0 || view.cols() == 0)
        {
            throw copp::Error(copp::Status::invalid_input, std::string(name) + " must be non-empty");
        }
        if (view.data() == nullptr && view.storage_size() != 0)
        {
            throw copp::Error(copp::Status::invalid_input, std::string(name) + " data is null");
        }
        if (view.layout() == copp::MatrixLayout::ColumnMajor && view.leading_dim() < view.rows())
        {
            throw copp::Error(
                copp::Status::invalid_input,
                std::string(name) + " leading dimension is smaller than rows");
        }
        if (view.layout() == copp::MatrixLayout::RowMajor && view.leading_dim() < view.cols())
        {
            throw copp::Error(
                copp::Status::invalid_input,
                std::string(name) + " leading dimension is smaller than cols");
        }
    }

    void check_s_range(double s_min, double s_max, const char *name)
    {
        if (!std::isfinite(s_min) || !std::isfinite(s_max) || s_max <= s_min)
        {
            throw copp::Error(
                copp::Status::invalid_input,
                std::string(name) + " requires finite s_min < s_max");
        }
    }

    copp::Matrix make_matrix(std::size_t rows, std::size_t cols, rust::Slice<const double> data)
    {
        return copp::Matrix(rows, cols, std::vector<double>(data.begin(), data.end()));
    }

} // namespace

namespace copp
{

    namespace bridge
    {

        CppPathEvaluator2nd::CppPathEvaluator2nd(
            std::size_t dim,
            copp::PathEvaluator2nd evaluator)
            : dim_(dim), evaluator_(std::move(evaluator))
        {
            if (dim_ == 0)
            {
                throw copp::Error(copp::Status::invalid_input, "path evaluator dimension must be positive");
            }
            if (!evaluator_)
            {
                throw copp::Error(copp::Status::invalid_input, "path evaluator callback is empty");
            }
        }

        std::size_t CppPathEvaluator2nd::dim() const noexcept
        {
            return dim_;
        }

        void CppPathEvaluator2nd::evaluate_up_to_2nd(
            rust::Slice<const double> s,
            rust::Slice<double> q,
            rust::Slice<double> dq,
            rust::Slice<double> ddq) const
        {
            const std::size_t expected_len = dim_ * s.size();
            if (q.size() != expected_len || dq.size() != expected_len || ddq.size() != expected_len)
            {
                throw copp::Error(copp::Status::invalid_input, "path evaluator output buffer shape mismatch");
            }

            std::lock_guard<std::mutex> lock(mutex_);
            evaluator_(
                copp::Span<const double>(s.data(), s.size()),
                copp::MatrixRef::column_major(q.data(), dim_, s.size()),
                copp::MatrixRef::column_major(dq.data(), dim_, s.size()),
                copp::MatrixRef::column_major(ddq.data(), dim_, s.size()));
        }

        CppPathEvaluator3rd::CppPathEvaluator3rd(
            std::size_t dim,
            copp::PathEvaluator3rd evaluator)
            : dim_(dim), evaluator_(std::move(evaluator))
        {
            if (dim_ == 0)
            {
                throw copp::Error(copp::Status::invalid_input, "path evaluator dimension must be positive");
            }
            if (!evaluator_)
            {
                throw copp::Error(copp::Status::invalid_input, "path evaluator callback is empty");
            }
        }

        std::size_t CppPathEvaluator3rd::dim() const noexcept
        {
            return dim_;
        }

        void CppPathEvaluator3rd::evaluate_up_to_2nd(
            rust::Slice<const double> s,
            rust::Slice<double> q,
            rust::Slice<double> dq,
            rust::Slice<double> ddq) const
        {
            const std::size_t expected_len = dim_ * s.size();
            if (q.size() != expected_len || dq.size() != expected_len || ddq.size() != expected_len)
            {
                throw copp::Error(copp::Status::invalid_input, "path evaluator output buffer shape mismatch");
            }

            std::vector<double> dddq(expected_len);
            std::lock_guard<std::mutex> lock(mutex_);
            evaluator_(
                copp::Span<const double>(s.data(), s.size()),
                copp::MatrixRef::column_major(q.data(), dim_, s.size()),
                copp::MatrixRef::column_major(dq.data(), dim_, s.size()),
                copp::MatrixRef::column_major(ddq.data(), dim_, s.size()),
                copp::MatrixRef::column_major(dddq.data(), dim_, s.size()));
        }

        void CppPathEvaluator3rd::evaluate_up_to_3rd(
            rust::Slice<const double> s,
            rust::Slice<double> q,
            rust::Slice<double> dq,
            rust::Slice<double> ddq,
            rust::Slice<double> dddq) const
        {
            const std::size_t expected_len = dim_ * s.size();
            if (q.size() != expected_len || dq.size() != expected_len || ddq.size() != expected_len ||
                dddq.size() != expected_len)
            {
                throw copp::Error(copp::Status::invalid_input, "path evaluator output buffer shape mismatch");
            }

            std::lock_guard<std::mutex> lock(mutex_);
            evaluator_(
                copp::Span<const double>(s.data(), s.size()),
                copp::MatrixRef::column_major(q.data(), dim_, s.size()),
                copp::MatrixRef::column_major(dq.data(), dim_, s.size()),
                copp::MatrixRef::column_major(ddq.data(), dim_, s.size()),
                copp::MatrixRef::column_major(dddq.data(), dim_, s.size()));
        }

    } // namespace bridge

    struct Path::Impl
    {
        explicit Impl(rust::Box<bridge::PathHandle> &&path) : path(std::move(path)) {}

        rust::Box<bridge::PathHandle> path;
    };

    Path::Path(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}

    Path::Path(Path &&) noexcept = default;

    Path &Path::operator=(Path &&) noexcept = default;

    Path::~Path() = default;

    namespace detail
    {

        const void *path_handle(const Path &path) noexcept
        {
            if (!path.impl_)
            {
                return nullptr;
            }
            return &*path.impl_->path;
        }

    } // namespace detail

    Path Path::from_waypoints(
        std::initializer_list<std::initializer_list<double>> waypoints,
        SplineConfig config)
    {
        auto matrix = Matrix::from_columns(waypoints);
        return Path::from_waypoints(matrix.view(), config);
    }

    Path Path::from_parametric(
        PathParametric parametric,
        double s_min,
        double s_max)
    {
        if (!parametric)
        {
            throw Error(Status::invalid_input, "parametric path callback is empty");
        }
        check_s_range(s_min, s_max, "Path::from_parametric");

        const double s_probe = 0.5 * (s_min + s_max);
        std::vector<Jet3> probe;
        try
        {
            probe = parametric(Jet3::constant(s_probe));
        }
        catch (const Error &)
        {
            throw;
        }
        catch (const std::exception &error)
        {
            throw Error(
                Status::invalid_input,
                std::string("parametric path callback failed during dimension probe: ") + error.what());
        }
        const auto dim = probe.size();
        if (dim == 0)
        {
            throw Error(Status::invalid_input, "parametric path dimension must be positive");
        }

        return Path::from_parametric(
            dim,
            s_min,
            s_max,
            [parametric = std::move(parametric), dim](Jet3 s, Span<Jet3> q) {
                const auto values = parametric(s);
                if (values.size() != dim)
                {
                    throw Error(Status::invalid_input, "parametric path callback returned inconsistent dimension");
                }
                for (std::size_t i = 0; i < dim; ++i)
                {
                    q[i] = values[i];
                }
            });
    }

    Path Path::from_parametric(
        std::size_t dim,
        double s_min,
        double s_max,
        PathParametricWriter parametric)
    {
        if (dim == 0)
        {
            throw Error(Status::invalid_input, "parametric path dimension must be positive");
        }
        if (!parametric)
        {
            throw Error(Status::invalid_input, "parametric path callback is empty");
        }
        check_s_range(s_min, s_max, "Path::from_parametric");

        return Path::from_evaluator_3rd(
            dim,
            s_min,
            s_max,
            [dim, parametric = std::move(parametric)](Span<const double> samples,
                                                      MatrixRef q,
                                                      MatrixRef dq,
                                                      MatrixRef ddq,
                                                      MatrixRef dddq) {
                std::vector<Jet3> values(dim);
                for (std::size_t j = 0; j < samples.size(); ++j)
                {
                    parametric(Jet3::seed(samples[j]), Span<Jet3>(values));
                    for (std::size_t i = 0; i < dim; ++i)
                    {
                        const auto value = values[i];
                        q(i, j) = value.v;
                        dq(i, j) = value.d1;
                        ddq(i, j) = value.d2;
                        dddq(i, j) = value.d3;
                    }
                }
            });
    }

    Path Path::from_evaluator_2nd(
        std::size_t dim,
        double s_min,
        double s_max,
        PathEvaluator2nd evaluator)
    {
        try
        {
            auto handle = bridge::path_from_evaluator_2nd(
                std::make_unique<bridge::CppPathEvaluator2nd>(dim, std::move(evaluator)),
                s_min,
                s_max);
            return Path(std::make_unique<Impl>(std::move(handle)));
        }
        catch (const rust::Error &error)
        {
            throw Error(Status::invalid_input, to_std_string(error));
        }
    }

    Path Path::from_evaluator_3rd(
        std::size_t dim,
        double s_min,
        double s_max,
        PathEvaluator3rd evaluator)
    {
        try
        {
            auto handle = bridge::path_from_evaluator_3rd(
                std::make_unique<bridge::CppPathEvaluator3rd>(dim, std::move(evaluator)),
                s_min,
                s_max);
            return Path(std::make_unique<Impl>(std::move(handle)));
        }
        catch (const rust::Error &error)
        {
            throw Error(Status::invalid_input, to_std_string(error));
        }
    }

    Path Path::from_waypoints(MatrixView waypoints, SplineConfig config)
    {
        check_matrix_view(waypoints, "waypoints");

        Matrix owned;
        MatrixView bridge_view = waypoints;
        if (waypoints.layout() == MatrixLayout::RowMajor)
        {
            owned = copy_to_column_major(waypoints);
            bridge_view = owned.view();
        }

        try
        {
            const auto start_state = optional_state_view(config.start_state);
            const auto end_state = optional_state_view(config.end_state);
            auto handle = bridge::path_from_waypoints(
                to_rust_slice(bridge_view),
                bridge_view.rows(),
                bridge_view.cols(),
                bridge_view.leading_dim(),
                config.order,
                config.s_min,
                config.s_max,
                to_bridge(config.out_of_range),
                to_rust_slice(start_state),
                start_state.rows(),
                start_state.cols(),
                start_state.leading_dim(),
                to_rust_slice(end_state),
                end_state.rows(),
                end_state.cols(),
                end_state.leading_dim());
            return Path(std::make_unique<Impl>(std::move(handle)));
        }
        catch (const rust::Error &error)
        {
            throw Error(Status::invalid_input, to_std_string(error));
        }
    }

    std::size_t Path::dim() const
    {
        return impl_->path->dim();
    }

    std::pair<double, double> Path::s_range() const
    {
        return {impl_->path->s_min(), impl_->path->s_max()};
    }

    PathDerivatives Path::evaluate_q(Span<const double> s) const
    {
        try
        {
            auto result = impl_->path->evaluate_q(to_rust_slice(s));
            return PathDerivatives{
                make_matrix(dim(), s.size(), result->q()),
                std::nullopt,
                std::nullopt,
                std::nullopt,
            };
        }
        catch (const rust::Error &error)
        {
            throw Error(Status::invalid_input, to_std_string(error));
        }
    }

    PathDerivatives Path::evaluate_up_to_2nd(Span<const double> s) const
    {
        try
        {
            auto result = impl_->path->evaluate_up_to_2nd(to_rust_slice(s));
            const auto rows = dim();
            const auto cols = s.size();
            return PathDerivatives{
                make_matrix(rows, cols, result->q()),
                make_matrix(rows, cols, result->dq()),
                make_matrix(rows, cols, result->ddq()),
                std::nullopt,
            };
        }
        catch (const rust::Error &error)
        {
            throw Error(Status::invalid_input, to_std_string(error));
        }
    }

    PathDerivatives Path::evaluate_up_to_3rd(Span<const double> s) const
    {
        try
        {
            auto result = impl_->path->evaluate_up_to_3rd(to_rust_slice(s));
            const auto rows = dim();
            const auto cols = s.size();
            return PathDerivatives{
                make_matrix(rows, cols, result->q()),
                make_matrix(rows, cols, result->dq()),
                make_matrix(rows, cols, result->ddq()),
                make_matrix(rows, cols, result->dddq()),
            };
        }
        catch (const rust::Error &error)
        {
            throw Error(Status::invalid_input, to_std_string(error));
        }
    }

} // namespace copp