C语言 获取最紧密的边界框

wr98u20j  于 2023-03-01  发布在  其他
关注(0)|答案(1)|浏览(95)

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

// 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秒

t2a7ltrp

t2a7ltrp1#

分支预测失败可能会减慢速度。
你可以尝试“无分支”比较:

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)); }

也许让您将这些转换为宏。

相关问题