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++ data_crop的类型为Eigen::Tensor<uint8_t, 3, Eigen::RowMajor>
时间: 2024-03-20 07:39:56 浏览: 117
numpy.meshgrid()理解(小结)
5星 · 资源好评率100%
以下是将给定的 Python 代码转换为 C++ 代码后的结果,其中包括了对于 Eigen Tensor 类型的支持:
```
#include <Eigen/Core>
#include <Eigen/Tensor>
void crop_pointcloud(Eigen::Tensor<uint8_t, 3, Eigen::RowMajor>& data_crop,
Eigen::ArrayXXf& x_o, Eigen::ArrayXXf& y_o,
Eigen::ArrayXXf& x_i, Eigen::ArrayXXf& y_i,
const float R_o, const float R_i, const int z_critical) {
const int range_x = x_o.rows();
const int range_y = y_o.cols();
const int range_z = data_crop.dimension(2);
const float range_z_float = static_cast<float>(range_z);
const float K_o = R_o * R_o / range_z_float;
const float K_i = R_i * R_i / range_z_float;
for (int z = 0; z < range_z; ++z) {
const float r_o = std::sqrt(z * K_o);
Eigen::Tensor<uint8_t, 2, Eigen::RowMajor> data_layer = data_crop.chip(z, 2);
const Eigen::ArrayXXf d_o = (x_o * x_o + y_o * y_o).sqrt();
const Eigen::ArrayXXf d_i = (x_i * x_i + y_i * y_i).sqrt();
const float 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(0, data_layer);
}
}
int main() {
const int range_x = ...; // 请填入具体数值
const int range_y = ...; // 请填入具体数值
const int range_z = ...; // 请填入具体数值
const int z_critical = 50;
const float R_o = 550.0f;
const float R_i = 200.0f;
Eigen::Tensor<uint8_t, 3, Eigen::RowMajor> data_crop(range_x, range_y, range_z);
Eigen::ArrayXXf x_o, y_o, x_i, y_i;
x_o.resize(range_x, range_y);
y_o.resize(range_x, range_y);
x_i.resize(range_x, range_y);
y_i.resize(range_x, range_y);
for (int i = 0; i < range_x; ++i) {
for (int j = 0; j < range_y; ++j) {
x_o(i, j) = static_cast<float>(i) - range_x / 2.0f;
y_o(i, j) = static_cast<float>(j) - range_y / 2.0f;
x_i(i, j) = static_cast<float>(i) - dx;
y_i(i, j) = static_cast<float>(j) - dy;
}
}
crop_pointcloud(data_crop, x_o, y_o, x_i, y_i, R_o, R_i, z_critical);
data_crop = data_crop.slice(Eigen::array<int, 3>({0, 0, 10}), Eigen::array<int, 3>({range_x, range_y, range_z - 10}));
return 0;
}
```
请根据您的具体需求对代码进行修改和适配。
阅读全文