def crop_pointcloud(data_crop, x_o, y_o, x_i, y_i, R_o, R_i, z_critical): range_z = 400; 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 转为c++ Eigen,Tensor
时间: 2024-03-20 16:42:20 浏览: 87
Here's the equivalent code in C++ using the Eigen and Tensor libraries:
```
#include <Eigen/Dense>
#include <unsupported/Eigen/CXX11/Tensor>
Eigen::Tensor<double, 3> crop_pointcloud(Eigen::Tensor<double, 3> data_crop, double x_o, double y_o, double x_i, double y_i, double R_o, double R_i, int z_critical) {
int range_z = 400;
double K_o = pow(R_o, 2) / range_z;
double K_i = pow(R_i, 2) / range_z;
for (int z = 0; z < range_z; z++) {
double r_o = sqrt(z * K_o);
Eigen::Tensor<double, 2> data_layer = data_crop.chip(z, 2);
double d_o = sqrt(pow(x_o, 2) + pow(y_o, 2));
double d_i = sqrt(pow(x_i, 2) + pow(y_i, 2));
double r_i = z < z_critical ? 0 : sqrt(z * K_i);
data_crop.chip(z, 2) = (d_o > r_o || d_i <= r_i).select(Eigen::Tensor<double, 2>::Zero(data_layer.rows(), data_layer.cols()), data_layer);
}
return data_crop;
}
```
Note that I assumed the input `data_crop` is a 3D tensor with dimensions (rows, cols, depth), and I used the `chip` method to extract each 2D layer of the tensor as a 2D Eigen tensor. Also, I used the `select` method to apply the cropping condition on each element of the tensor and set it to zero if the condition is true.
阅读全文