在我的光线追踪器中,我正在构建一个边界体积层次结构。我用了几天试图解决这个错误,但我似乎做了一些根本错误的事情。
第一种方法是使用typedef std :: vector Objects数组调用的构造。
void BVH::build(Objects* objs)
{
// construct the bounding volume hierarchy
int size = objs->size();
// Calculate the bounding box for this node
BBox bb = (*objs)[0]->boundingBox();
for ( int p = 1; p < size; p++ )
bb.expandToInclude( (*objs)[p]->boundingBox());
Vector3 pivot = (bb.max + bb.min) * 0.5f;
tree.bbox = bb;
int split = qsplit(objs, size, pivot.x, 0);
tree.left = subdivision(objs, split, 1);
tree.right = subdivision(&objs[split], size - split, 1);
}
下面使用此方法在我的二叉树中构造叶节点。
Node* BVH::makeLeaf(Objects* objs, int num)
{
Node* node = new Node;
if ( num == 1 ) { node->objs = &objs[0]; }
else if ( num == 2 ) { node->objs = ((&objs)[0],(&objs)[1]); }
node->isLeaf = true;
return node;
}
这是一个递归方法,使用Objects数组来拆分和构造节点和叶节点。
Node* BVH::subdivision(Objects* objs, int size, int axis)
{
if ( size == 1 ) { return makeLeaf(objs, 1); }
if ( size == 2 ) { return makeLeaf(objs, 2); }
Node* node = new Node;
node->isLeaf = false;
BBox bb = (*objs)[0]->boundingBox();
for ( int p = 1; p < size; p++ )
bb.expandToInclude((*objs)[p]->boundingBox());
node->bbox = bb;
Vector3 pivot = (bb.max + bb.min) * 0.5f;
int split = qsplit(objs, size, pivot[axis], axis);
node->left = subdivision(objs, split, (axis + 1) % 3);
node->right = subdivision(&objs[split], size - split, (axis + 1) % 3);
return node;
}
运行此代码会在细分内部给出分段错误。
这是来自gdb的空指针的帖子:
debug: Loading "teapot.obj"...
debug: Loaded "teapot.obj" with 576 triangles
Program received signal SIGSEGV, Segmentation fault.
0x000000000040bb9e in BVH::subdivision (this=0x61f0c0, objs=0x61f100, size=5, axis=1) at BVH.cpp:44
44 BBox bb = (*objs)[0]->boundingBox();
(gdb) bt
0 0x000000000040bb9e in BVH::subdivision (this=0x61f0c0, objs=0x61f100, size=5, axis=1) at BVH.cpp:44
1 0x000000000040bdef in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=9, axis=0) at BVH.cpp:56
2 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=18, axis=2) at BVH.cpp:53
3 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=36, axis=1) at BVH.cpp:53
4 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=72, axis=0) at BVH.cpp:53
5 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=144, axis=2) at BVH.cpp:53
6 0x000000000040bd62 in BVH::subdivision (this=0x61f0c0, objs=0x61f0a0, size=288, axis=1) at BVH.cpp:53
7 0x000000000040b9ca in BVH::build (this=0x61f0c0, objs=0x61f0a0) at BVH.cpp:21
8 0x00000000004121ac in Scene::preCalc (this=0x61f0a0) at Scene.cpp:42
9 0x0000000000402fde in makeTeapotScene () at assignment1.cpp:113
10 0x000000000040f973 in main (argc=1, argv=0x7fffffffe798) at main.cpp:65
(gdb) p (*objs)[0]
$1 = (Object *&) @0x0: <error reading variable>
答案 0 :(得分:2)
(&objs)[0]
与
相同*(&objs)
与
相同objs
以同样的方式,
(&objs)[1]
与
相同objs + 1
最有可能不有效对象的地址。
该行
node->objs = ((&objs)[0],(&objs)[1]);
因为它使用逗号运算符,会抛弃(&objs)[0]
的值并将node->objs
设置为objs + 1
,这是无效的。
目前还不清楚你希望完成任务的目的是什么,所以我不知道你应该用它替换它。
答案 1 :(得分:0)
在这个有趣的typedef上使用了很多小时之后,我得到了朋友的帮助,我们写了一个转换器方法来转
vector<Object*>* into vector<Object*>&
使每种方法都更加清洁和美观。
void BVH::build(Objects* objs_orig)
{
m_objects = objs_orig;
std::vector<Object*>* objs = objs_orig;
return build(*objs);
}
void BVH::build(const std::vector<Object*>& objs)
{
tree = makeTree(objs, 1);
}