Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
apple_utils.hpp
1 // Copyright (c) 2026 Dhruv Patel
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NAV2_MPPI_CONTROLLER__TOOLS__APPLE_UTILS_HPP_
16 #define NAV2_MPPI_CONTROLLER__TOOLS__APPLE_UTILS_HPP_
17 
18 #include <xtensor/xtensor.hpp>
19 
20 namespace mppi::utils
21 {
25 template<typename E>
26 xt::xtensor<float, 1> manual_cumsum_1d(const E & expression)
27 {
28  auto shape = expression.shape();
29  xt::xtensor<float, 1> result = xt::zeros<float>(shape);
30  float sum = 0.0f;
31  for (size_t i = 0; i < shape[0]; ++i) {
32  sum += static_cast<float>(expression(i));
33  result(i) = sum;
34  }
35  return result;
36 }
37 
41 template<typename E>
42 xt::xtensor<float, 2> manual_cumsum_2d(const E & expression, int axis)
43 {
44  auto shape = expression.shape();
45  xt::xtensor<float, 2> result = xt::zeros<float>(shape);
46  if (axis == 1) {
47  for (size_t i = 0; i < shape[0]; ++i) {
48  float row_sum = 0.0f;
49  for (size_t j = 0; j < shape[1]; ++j) {
50  row_sum += static_cast<float>(expression(i, j));
51  result(i, j) = row_sum;
52  }
53  }
54  } else {
55  for (size_t j = 0; j < shape[1]; ++j) {
56  float col_sum = 0.0f;
57  for (size_t i = 0; i < shape[0]; ++i) {
58  col_sum += static_cast<float>(expression(i, j));
59  result(i, j) = col_sum;
60  }
61  }
62  }
63  return result;
64 }
65 } // namespace mppi::utils
66 
67 #endif // NAV2_MPPI_CONTROLLER__TOOLS__APPLE_UTILS_HPP_