23

我正在寻找一种好的光线八叉树交集算法,它可以让我以迭代的方式获得光线穿过的叶子。我计划在 CPU 上实现它,因为我还不想深入研究 CUDA :)

目前,我的 Voxel raycaster 只是在 XxYxZ 体素的非分层阵列上执行 3D DDA(Amanatides/Woo 版本)。您可以想象,当有很多空白空间时,这会非常昂贵,如下图所示(更亮的红色 = 更多工作 :)):

哑 3D DDA 的工作量 - 红色 = 更多工作

我已经发现这个任务有两种算法:bottom-up,从叶子向上工作,top-down,基本上是深度优先搜索。

我已经找到了 Revelles 从 2000 年开始的算法,称为An Effective parametric algorithm for octree traversal,看起来很有趣,但已经很老了。这是一种自上而下的算法。

最流行的自下而上方法似乎是K. Sung,A DDA Octtree Traversal Algorithm for Ray Tracing,Eurographics'91,North Holland-Elsevier,ISBN 0444 89096 3,p。73-85。问题是大多数 DDA 八叉树遍历算法都期望八叉树具有相同的深度,这是我不想要的 - 空子树应该只是一个空指针或类似的东西。

在最近关于稀疏体素八叉树的文献中,我设法通读了(最值得注意的是莱恩关于 SVO 的工作,它们似乎都基于某种 GPU 实现的 DDA 版本(Amanatides/Woo 风格)。

现在,这是我的问题:有没有人有实现基本的、简洁的光线八叉树相交算法的经验?你会推荐什么?

4

2 回答 2

13

作为记录,这是我最终使用的 Revelles 论文的实现:

#include "octree_traversal.h"

using namespace std;

unsigned char a; // because an unsigned char is 8 bits

int first_node(double tx0, double ty0, double tz0, double txm, double tym, double tzm){
unsigned char answer = 0;   // initialize to 00000000
// select the entry plane and set bits
if(tx0 > ty0){
    if(tx0 > tz0){ // PLANE YZ
        if(tym < tx0) answer|=2;    // set bit at position 1
        if(tzm < tx0) answer|=1;    // set bit at position 0
        return (int) answer;
    }
}
else {
    if(ty0 > tz0){ // PLANE XZ
        if(txm < ty0) answer|=4;    // set bit at position 2
        if(tzm < ty0) answer|=1;    // set bit at position 0
        return (int) answer;
    }
}
// PLANE XY
if(txm < tz0) answer|=4;    // set bit at position 2
if(tym < tz0) answer|=2;    // set bit at position 1
return (int) answer;
}

int new_node(double txm, int x, double tym, int y, double tzm, int z){
if(txm < tym){
    if(txm < tzm){return x;}  // YZ plane
}
else{
    if(tym < tzm){return y;} // XZ plane
}
return z; // XY plane;
}

void proc_subtree (double tx0, double ty0, double tz0, double tx1, double ty1, double tz1, Node* node){
float txm, tym, tzm;
int currNode;

if(tx1 < 0 || ty1 < 0 || tz1 < 0) return;
if(node->terminal){
    cout << "Reached leaf node " << node->debug_ID << endl;
    return;
}
else{ cout << "Reached node " << node->debug_ID << endl;}

txm = 0.5*(tx0 + tx1);
tym = 0.5*(ty0 + ty1);
tzm = 0.5*(tz0 + tz1);

currNode = first_node(tx0,ty0,tz0,txm,tym,tzm);
do{
    switch (currNode)
    {
    case 0: { 
        proc_subtree(tx0,ty0,tz0,txm,tym,tzm,node->children[a]);
        currNode = new_node(txm,4,tym,2,tzm,1);
        break;}
    case 1: { 
        proc_subtree(tx0,ty0,tzm,txm,tym,tz1,node->children[1^a]);
        currNode = new_node(txm,5,tym,3,tz1,8);
        break;}
    case 2: { 
        proc_subtree(tx0,tym,tz0,txm,ty1,tzm,node->children[2^a]);
        currNode = new_node(txm,6,ty1,8,tzm,3);
        break;}
    case 3: { 
        proc_subtree(tx0,tym,tzm,txm,ty1,tz1,node->children[3^a]);
        currNode = new_node(txm,7,ty1,8,tz1,8);
        break;}
    case 4: { 
        proc_subtree(txm,ty0,tz0,tx1,tym,tzm,node->children[4^a]);
        currNode = new_node(tx1,8,tym,6,tzm,5);
        break;}
    case 5: { 
        proc_subtree(txm,ty0,tzm,tx1,tym,tz1,node->children[5^a]);
        currNode = new_node(tx1,8,tym,7,tz1,8);
        break;}
    case 6: { 
        proc_subtree(txm,tym,tz0,tx1,ty1,tzm,node->children[6^a]);
        currNode = new_node(tx1,8,ty1,8,tzm,7);
        break;}
    case 7: { 
        proc_subtree(txm,tym,tzm,tx1,ty1,tz1,node->children[7^a]);
        currNode = 8;
        break;}
    }
} while (currNode<8);
}

void ray_octree_traversal(Octree* octree, Ray ray){
a = 0;

// fixes for rays with negative direction
if(ray.direction[0] < 0){
    ray.origin[0] = octree->center[0] * 2 - ray.origin[0];//camera origin fix
    ray.direction[0] = - ray.direction[0];
    a |= 4 ; //bitwise OR (latest bits are XYZ)
}
if(ray.direction[1] < 0){
    ray.origin[1] = octree->center[1] * 2 - ray.origin[1];
    ray.direction[1] = - ray.direction[1];
    a |= 2 ; 
}
if(ray.direction[2] < 0){
    ray.origin[2] = octree->center[2] * 2 - ray.origin[2];
    ray.direction[2] = - ray.direction[2];
    a |= 1 ; 
}

double divx = 1 / ray.direction[0]; // IEEE stability fix
double divy = 1 / ray.direction[1];
double divz = 1 / ray.direction[2];

double tx0 = (octree->min[0] - ray.origin[0]) * divx;
double tx1 = (octree->max[0] - ray.origin[0]) * divx;
double ty0 = (octree->min[1] - ray.origin[1]) * divy;
double ty1 = (octree->max[1] - ray.origin[1]) * divy;
double tz0 = (octree->min[2] - ray.origin[2]) * divz;
double tz1 = (octree->max[2] - ray.origin[2]) * divz;

if( max(max(tx0,ty0),tz0) < min(min(tx1,ty1),tz1) ){
    proc_subtree(tx0,ty0,tz0,tx1,ty1,tz1,octree->root);
}
}
于 2012-05-09T11:48:24.050 回答
1

自上而下对我来说效果很好;八叉树的上半部分可能是基于指针的,所以大的空子卷不占用内存;下半部分更有效地实现无指针...撞墙的时间复杂度是 log2(N) (这显然是最好的情况)。递归实现非常简单,因此更容易优化代码。所有的数学运算都可以通过整数 SSE 操作有效地实现——计算每个子体积跳跃的新 XYZ 坐标大约需要 x30 个 CPU 周期。顺便说一句,八叉树遍历的公共版本仅适用于教育 - 掌握真正有效的实施可能需要几个月的时间......

斯特凡

于 2012-04-20T06:44:49.127 回答