def crop_pointcloud(data_crop, x_o, y_o, x_i, y_i, R_o, R_i, z_critical): K_o = R_o ** 2 / range_z K_i = R_i ** 2 / range_z for z in range(range_z): r_o = np.sqrt(z * K_o) data_layer = data_crop[:, :, z] d_o = np.sqrt(x_o ** 2 + y_o ** 2) d_i = np.sqrt(x_i ** 2 + y_i ** 2) if z < z_critical: r_i = 0 else: r_i = np.sqrt(z * K_i) data_crop[:, :, z] = np.where((d_o > r_o) | (d_i <= r_i), 0, data_layer) return data_crop data_crop = data[:, :, :400] range_x, range_y, range_z = data_crop.shape dx = int((dx1 + dx2) / 2) dy = int((dy1 + dy2) / 2) x, y = np.meshgrid(np.arange(range_x), np.arange(range_y)) # np.savetxt('reshape_data.txt', x, delimiter=' ', fmt="%i") x_o = x - range_x / 2 y_o = y - range_y / 2 x_i = x - dx y_i = y - dy z_critical = 50 R_o = 550 R_i = 200 data_crop = crop_pointcloud(data_crop, x_o, y_o, x_i, y_i, R_o, R_i, z_critical) data_crop = data_crop[:, :, 10:] 转C++ Eigen Tensor
时间: 2024-03-21 18:38:22 浏览: 61
一个用于点云数据的精益c++库- kzampog/cilantro
以下是将上述 Python 代码转换为 C++ Eigen Tensor 的示例代码:
```
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/CXX11/Tensor>
using namespace Eigen;
template<typename T>
void crop_pointcloud(Tensor<T, 3>& data_crop, int x_o, int y_o, int x_i, int y_i, T R_o, T R_i, int z_critical) {
int range_z = data_crop.dimension(2);
T K_o = R_o * R_o / range_z;
T K_i = R_i * R_i / range_z;
for (int z = 0; z < range_z; ++z) {
T r_o = std::sqrt(z * K_o);
Tensor<T, 2> data_layer = data_crop.chip(z, 2);
T d_o = std::sqrt(x_o * x_o + y_o * y_o);
T d_i = std::sqrt(x_i * x_i + y_i * y_i);
T r_i = z < z_critical ? 0 : std::sqrt(z * K_i);
data_crop.chip(z, 2) = (d_o > r_o || d_i <= r_i).select(T(0), data_layer);
}
}
int main() {
int range_x = 100, range_y = 100, range_z = 400;
typedef float T;
Tensor<T, 3> data_crop(range_x, range_y, range_z);
int dx1 = 50, dx2 = 60, dy1 = 70, dy2 = 80;
int dx = (dx1 + dx2) / 2, dy = (dy1 + dy2) / 2;
Tensor<int, 2> x(range_x, range_y), y(range_x, range_y);
for (int i = 0; i < range_x; ++i)
for (int j = 0; j < range_y; ++j)
x(i, j) = i, y(i, j) = j;
Tensor<T, 2> x_o = x.cast<T>() - range_x / 2, y_o = y.cast<T>() - range_y / 2;
Tensor<T, 2> x_i = x.cast<T>() - dx, y_i = y.cast<T>() - dy;
int z_critical = 50;
T R_o = 550, R_i = 200;
crop_pointcloud(data_crop, x_o, y_o, x_i, y_i, R_o, R_i, z_critical);
data_crop = data_crop.slice(Slice(0, range_x), Slice(0, range_y), Slice(10, range_z));
std::cout << data_crop << std::endl;
return 0;
}
```
需要注意的是,要使用 `Eigen/CXX11/Tensor` 头文件来支持三维张量。此外,由于 C++ 不支持按位或和按位与运算符 `|` 和 `&` 作用于浮点数,因此需要使用 `select` 函数来进行条件选择。
阅读全文