COPP C++ API
C++ interface for COPP trajectory optimization
Loading...
Searching...
No Matches
path_from_evaluator_2nd.cpp
Go to the documentation of this file.
1// Tutorial example: construct a path from a batch second-order evaluator.
2//
3// Use this form when derivatives come from an external model, table, or custom
4// code rather than from `Jet3` automatic differentiation. The callback receives
5// all query samples in one batch and fills column-major output buffers.
6
7#include <cmath>
8#include <cstddef>
9#include <iostream>
10#include <vector>
11
12#include <copp/copp.hpp>
13
14int main()
15{
16 constexpr std::size_t dim = 2;
17
18 // Step 1: Declare the path range and a callback that fills q, dq, and ddq.
19 // Output matrix shape is `(dim x s.size())`.
21 dim,
22 0.0,
23 1.0,
27 copp::MatrixRef ddq) {
28 for (std::size_t j = 0; j < s.size(); ++j)
29 {
30 const double x = s[j];
31 q(0, j) = x;
32 dq(0, j) = 1.0;
33 ddq(0, j) = 0.0;
34
35 q(1, j) = std::sin(x);
36 dq(1, j) = std::cos(x);
37 ddq(1, j) = -std::sin(x);
38 }
39 });
40
41 // Step 2: Evaluate with a batch of samples. The bridge crosses into C++
42 // once for the full batch instead of once per sample.
43 std::vector<double> samples{0.0, 0.5, 1.0};
44 auto out = path.evaluate_up_to_2nd(samples);
45
46 // Step 3: Read the sine dimension at the middle sample.
47 std::cout << "dim = " << path.dim() << "\n";
48 std::cout << "q(1, 1) = " << out.q(1, 1) << "\n";
49 std::cout << "dq(1, 1) = " << out.dq.value()(1, 1) << "\n";
50 std::cout << "ddq(1, 1) = " << out.ddq.value()(1, 1) << "\n";
51}
Mutable column-major matrix view used by callback APIs.
Definition core.hpp:288
static Path from_evaluator_2nd(std::size_t dim, double s_min, double s_max, PathEvaluator2nd evaluator)
Build a path from a batch evaluator with explicit derivatives up to second order.
PathDerivatives evaluate_up_to_2nd(Span< const double > s) const
Evaluate position, first derivative, and second derivative.
Non-owning view of a contiguous one-dimensional array.
Definition core.hpp:139
constexpr std::size_t size() const noexcept
Definition core.hpp:169