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 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 = 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实现 ,data_crop的数据类型为Eigen::Tensor<uint8_t, 3, Eigen::RowMajor>
时间: 2024-03-19 22:44:45 浏览: 140
以下是使用Eigen::Tensor库实现的代码,其中使用了Eigen::TensorMap和Eigen::array类来实现张量的切片和元素访问:
```
#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>
#include <cmath>
using namespace Eigen;
template<typename T>
using Tensor3 = Tensor<T, 3, RowMajor>;
template<typename T>
Tensor3<T> crop_pointcloud(Tensor3<T>& data_crop, const Tensor<int, 2>& x_o, const Tensor<int, 2>& y_o,
const Tensor<int, 2>& x_i, const Tensor<int, 2>& y_i, const T& R_o, const T& R_i, const int z_critical) {
const int range_z = data_crop.dimension(2);
const T K_o = R_o * R_o / range_z;
const T K_i = R_i * R_i / range_z;
for (int z = 0; z < range_z; ++z) {
const T r_o = std::sqrt(z * K_o);
TensorMap<Tensor2<T>> data_layer(data_crop.data() + z * data_crop.dimension(0) * data_crop.dimension(1), data_crop.dimension(0), data_crop.dimension(1));
const Tensor<T, 2> d_o = (x_o * x_o + y_o * y_o).sqrt();
const Tensor<T, 2> d_i = (x_i * x_i + y_i * y_i).sqrt();
const T r_i = (z < z_critical) ? 0 : std::sqrt(z * K_i);
data_layer = (d_o > r_o).select(T(0), (d_i <= r_i).select(T(0), data_layer));
}
return data_crop;
}
int main() {
const int range_x = 100;
const int range_y = 100;
const int range_z = 200;
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;
}
}
const Tensor<int, 2> x_o = x.array() - range_x / 2;
const Tensor<int, 2> y_o = y.array() - range_y / 2;
const Tensor<int, 2> x_i = x.array() - 1;
const Tensor<int, 2> y_i = y.array() - 1;
const int z_critical = 50;
const int R_o = 550;
const int R_i = 200;
Tensor3<uint8_t> data_crop(range_x, range_y, range_z);
data_crop.setRandom();
data_crop = crop_pointcloud(data_crop, x_o, y_o, x_i, y_i, R_o, R_i, z_critical);
data_crop = data_crop.slice(IndexList<0, 1, 2>(), IndexList<0, 0, 10>());
return 0;
}
```
这段代码中,crop_pointcloud函数的输入和输出都是三维张量,数据类型为uint8_t,其中x_o、y_o、x_i、y_i均为二维张量,R_o、R_i、z_critical均为标量。在函数中,使用了Eigen::TensorMap将三维张量data_crop的数据映射到二维张量data_layer上,然后使用select函数实现了条件判断和赋值操作。
阅读全文