【问题标题】:Get the tighest bounding box获取最紧密的边界框
【发布时间】:2023-02-25 16:09:02
【问题描述】:

我认为代码是不言自明的:

// Set the bbox min and max of the lower node
current_min_bound = lower_get_bbox_min(buf, lower_handle);
current_max_bound = lower_get_bbox_max(buf, lower_handle);
// Set lower node bbox 
if(current_min_bound.x > leaf_node->bbox_min.x) {
    current_min_bound.x = leaf_node->bbox_min.x;
} 
if(current_min_bound.y > leaf_node->bbox_min.y) {
    current_min_bound.y = leaf_node->bbox_min.y;
}
if(current_min_bound.z > leaf_node->bbox_min.z) {
    current_min_bound.z = leaf_node->bbox_min.z;
}
if(current_max_bound.x < leaf_node->origin.x + highest_voxel_coord.x) {
    current_max_bound.x = leaf_node->origin.x + highest_voxel_coord.x;
}
if(current_max_bound.y < leaf_node->origin.y + highest_voxel_coord.y) {
    current_max_bound.y = leaf_node->origin.y + highest_voxel_coord.y;
}
if(current_max_bound.z < leaf_node->origin.z + highest_voxel_coord.z) {
    current_max_bound.z = leaf_node->origin.z + highest_voxel_coord.z;
}
lower_set_bbox_min(buf, lower_handle, current_min_bound);
lower_set_bbox_max(buf, lower_handle, current_max_bound);

所以我想知道是否有更快的方法来比较和获得给定 3D 坐标系统中最紧密的边界框?我认为太多的比较正在扼杀我的 CPU 性能。数据结构是一棵树,但我认为这对这个问题无关紧要。

干杯。

是否可以编写一种更快的方法来计算数学问题。

编辑:

看起来使用三元条件运算符允许编译器优化代码:

                // Set lower node bbox min
                current_min_bound.x = (current_min_bound.x > leaf_node->origin.x + lowest_voxel_coord.x) ? leaf_node->origin.x + lowest_voxel_coord.x : current_min_bound.x;
                current_min_bound.y = (current_min_bound.y > leaf_node->origin.y + lowest_voxel_coord.y) ? leaf_node->origin.y + lowest_voxel_coord.y : current_min_bound.y;
                current_min_bound.z = (current_min_bound.z > leaf_node->origin.z + lowest_voxel_coord.z) ? leaf_node->origin.z + lowest_voxel_coord.z : current_min_bound.z;
                // Set lower node bbox max
                current_max_bound.x = (current_max_bound.x < leaf_node->origin.x + highest_voxel_coord.x) ? leaf_node->origin.x + highest_voxel_coord.x : current_max_bound.x;
                current_max_bound.y = (current_max_bound.y < leaf_node->origin.y + highest_voxel_coord.y) ? leaf_node->origin.y + highest_voxel_coord.y : current_max_bound.y;
                current_max_bound.z = (current_max_bound.z < leaf_node->origin.z + highest_voxel_coord.z) ? leaf_node->origin.z + highest_voxel_coord.z : current_max_bound.z;

迭代次数:27 546

先前代码的相同迭代:0.860708 秒 相同的迭代当前代码:0.717957 秒

【问题讨论】:

  • 如果它没有坏,就不要修理它。首先分析您的代码。
  • 编辑三元条件运算符。谢谢! -“你编译优化,例如-O2”。 - 是的,我愿意。

标签: c compare bounding-box octree


【解决方案1】:

分支预测失败会减慢速度。

您可以尝试“无分支”比较:

static int min( int x, int y ) { return y ^ ((x^y) & -(x<y)); }
static int max( int x, int y ) { return y ^ ((x^y) & -(x>y)); }

也许,将这些转换为宏的任务留给您。

【讨论】:

  • 看起来这段代码和我的编辑部分代码一样快!对于相同的迭代 27 546,它需要 0.717617 秒。谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 2011-07-10
相关资源
最近更新 更多