I had applied a patch twice.. code was duplicated

This commit is contained in:
2009-07-11 22:29:53 +00:00
parent d6aefa6abd
commit e264087fad
2 changed files with 28 additions and 379 deletions

View File

@@ -1,147 +1,31 @@
/* bvh tree generics */
template<class Tree> static int bvh_intersect(Tree *obj, Isect *isec);
template<class Tree> static void bvh_add(Tree *obj, RayObject *ob)
{
rtbuild_add( obj->builder, ob );
}
template<class Tree> static void bvh_done(Tree *obj);
template<class Tree>
static void bvh_free(Tree *obj)
{
if(obj->builder)
rtbuild_free(obj->builder);
if(obj->node_arena)
BLI_memarena_free(obj->node_arena);
MEM_freeN(obj);
}
template<class Tree>
static void bvh_bb(Tree *obj, float *min, float *max)
{
bvh_node_merge_bb(obj->root, min, max);
}
template<class Tree>
static float bvh_cost(Tree *obj)
{
assert(obj->cost >= 0.0);
return obj->cost;
}
/* bvh tree nodes generics */
template<class Node> static inline int bvh_node_hit_test(Node *node, Isect *isec)
{
return RE_rayobject_bb_intersect(isec, (const float*)node->bb) != FLT_MAX;
}
template<class Node>
static void bvh_node_merge_bb(Node *node, float *min, float *max)
{
if(RayObject_isAligned(node))
{
DO_MIN(node->bb , min);
DO_MAX(node->bb+3, max);
}
else
{
RE_rayobject_merge_bb( (RayObject*)node, min, max);
}
}
/*
* recursivly transverse a BVH looking for a rayhit using a local stack
*/
template<class Node> static inline void bvh_node_push_childs(Node *node, Isect *isec, Node **stack, int &stack_pos);
template<class Node,int MAX_STACK_SIZE>
static int bvh_node_stack_raycast(Node *root, Isect *isec)
{
Node *stack[MAX_STACK_SIZE];
int hit = 0, stack_pos = 0;
stack[stack_pos++] = root;
while(stack_pos)
{
Node *node = stack[--stack_pos];
if(RayObject_isAligned(node))
{
if(bvh_node_hit_test(node,isec))
{
bvh_node_push_childs(node, isec, stack, stack_pos);
assert(stack_pos <= MAX_STACK_SIZE);
}
}
else
{
hit |= RE_rayobject_intersect( (RayObject*)node, isec);
if(hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
return hit;
}
/*
* recursively transverse a BVH looking for a rayhit using system stack
*/
/*
template<class Node>
static int bvh_node_raycast(Node *node, Isect *isec)
{
int hit = 0;
if(bvh_test_node(node, isec))
{
if(isec->idot_axis[node->split_axis] > 0.0f)
{
int i;
for(i=0; i<BVH_NCHILDS; i++)
if(RayObject_isAligned(node->child[i]))
{
if(node->child[i] == 0) break;
hit |= bvh_node_raycast(node->child[i], isec);
if(hit && isec->mode == RE_RAY_SHADOW) return hit;
}
else
{
hit |= RE_rayobject_intersect( (RayObject*)node->child[i], isec);
if(hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
else
{
int i;
for(i=BVH_NCHILDS-1; i>=0; i--)
if(RayObject_isAligned(node->child[i]))
{
if(node->child[i])
{
hit |= dfs_raycast(node->child[i], isec);
if(hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
else
{
hit |= RE_rayobject_intersect( (RayObject*)node->child[i], isec);
if(hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
}
return hit;
}
*/
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2009 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): André Pinto.
*
* ***** END GPL LICENSE BLOCK *****
*/
/* bvh tree generics */
template<class Tree> static int bvh_intersect(Tree *obj, Isect *isec);

View File

@@ -220,241 +220,6 @@ static RayObjectAPI bvh_api =
};
RayObject *RE_rayobject_bvh_create(int size)
{
BVHTree *obj= (BVHTree*)MEM_callocN(sizeof(BVHTree), "BVHTree");
assert( RayObject_isAligned(obj) ); /* RayObject API assumes real data to be 4-byte aligned */
obj->rayobj.api = &bvh_api;
obj->root = NULL;
obj->node_arena = NULL;
obj->builder = rtbuild_create( size );
return RayObject_unalignRayAPI((RayObject*) obj);
}
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2009 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): André Pinto.
*
* ***** END GPL LICENSE BLOCK *****
*/
extern "C"
{
#include <assert.h>
#include "MEM_guardedalloc.h"
#include "BKE_utildefines.h"
#include "BLI_arithb.h"
#include "BLI_memarena.h"
#include "RE_raytrace.h"
#include "rayobject_rtbuild.h"
#include "rayobject.h"
};
#include "bvh.h"
#define BVH_NCHILDS 2
#define RAY_BB_TEST_COST (0.2f)
#define DFS_STACK_SIZE 64
#define DYNAMIC_ALLOC
//#define rtbuild_split rtbuild_mean_split_largest_axis /* objects mean split on the longest axis, childs BB are allowed to overlap */
//#define rtbuild_split rtbuild_median_split_largest_axis /* space median split on the longest axis, childs BB are allowed to overlap */
#define rtbuild_split rtbuild_heuristic_object_split /* split objects using heuristic */
struct BVHNode
{
BVHNode *child[BVH_NCHILDS];
float bb[6];
int split_axis;
};
struct BVHTree
{
RayObject rayobj;
BVHNode *root;
MemArena *node_arena;
float cost;
RTBuilder *builder;
};
/*
* Push nodes (used on dfs)
*/
template<class Node>
inline static void bvh_node_push_childs(Node *node, Isect *isec, Node **stack, int &stack_pos)
{
//push nodes in reverse visit order
if(isec->idot_axis[node->split_axis] < 0.0f)
{
int i;
for(i=0; i<BVH_NCHILDS; i++)
if(node->child[i] == 0)
break;
else
stack[stack_pos++] = node->child[i];
}
else
{
int i;
for(i=BVH_NCHILDS-1; i>=0; i--)
if(node->child[i] != 0)
stack[stack_pos++] = node->child[i];
}
}
/*
* BVH done
*/
static BVHNode *bvh_new_node(BVHTree *tree, int nid)
{
BVHNode *node = (BVHNode*)BLI_memarena_alloc(tree->node_arena, sizeof(BVHNode));
return node;
}
static int child_id(int pid, int nchild)
{
//N child of node A = A * K + (2 - K) + N, (0 <= N < K)
return pid*BVH_NCHILDS+(2-BVH_NCHILDS)+nchild;
}
static BVHNode *bvh_rearrange(BVHTree *tree, RTBuilder *builder, int nid, float *cost)
{
*cost = 0;
if(rtbuild_size(builder) == 0)
return 0;
if(rtbuild_size(builder) == 1)
{
RayObject *child = builder->begin[0];
if(RayObject_isRayFace(child))
{
int i;
BVHNode *parent = bvh_new_node(tree, nid);
parent->split_axis = 0;
INIT_MINMAX(parent->bb, parent->bb+3);
for(i=0; i<1; i++)
{
parent->child[i] = (BVHNode*)builder->begin[i];
bvh_node_merge_bb(parent->child[i], parent->bb, parent->bb+3);
}
for(; i<BVH_NCHILDS; i++)
parent->child[i] = 0;
*cost = RE_rayobject_cost(child)+RAY_BB_TEST_COST;
return parent;
}
else
{
assert(!RayObject_isAligned(child));
//Its a sub-raytrace structure, assume it has it own raycast
//methods and adding a Bounding Box arround is unnecessary
*cost = RE_rayobject_cost(child);
return (BVHNode*)child;
}
}
else
{
int i;
RTBuilder tmp;
BVHNode *parent = bvh_new_node(tree, nid);
int nc = rtbuild_split(builder, BVH_NCHILDS);
INIT_MINMAX(parent->bb, parent->bb+3);
parent->split_axis = builder->split_axis;
for(i=0; i<nc; i++)
{
float cbb[6];
float tcost;
parent->child[i] = bvh_rearrange( tree, rtbuild_get_child(builder, i, &tmp), child_id(nid,i), &tcost );
INIT_MINMAX(cbb, cbb+3);
bvh_node_merge_bb(parent->child[i], cbb, cbb+3);
DO_MIN(cbb, parent->bb);
DO_MAX(cbb+3, parent->bb+3);
*cost += tcost*bb_area(cbb, cbb+3);
}
for(; i<BVH_NCHILDS; i++)
parent->child[i] = 0;
*cost /= bb_area(parent->bb, parent->bb+3);
*cost += nc*RAY_BB_TEST_COST;
return parent;
}
}
template<>
void bvh_done<BVHTree>(BVHTree *obj)
{
int needed_nodes = (rtbuild_size(obj->builder)+1)*2;
if(needed_nodes > BLI_MEMARENA_STD_BUFSIZE)
needed_nodes = BLI_MEMARENA_STD_BUFSIZE;
obj->node_arena = BLI_memarena_new(needed_nodes);
BLI_memarena_use_malloc(obj->node_arena);
obj->root = bvh_rearrange( obj, obj->builder, 1, &obj->cost );
rtbuild_free( obj->builder );
obj->builder = NULL;
}
template<>
int bvh_intersect<BVHTree>(BVHTree *obj, Isect* isec)
{
if(RayObject_isAligned(obj->root))
return bvh_node_stack_raycast<BVHNode,64>(obj->root, isec);
else
return RE_rayobject_intersect( (RayObject*) obj->root, isec );
}
/* the cast to pointer function is needed to workarround gcc bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11407 */
static RayObjectAPI bvh_api =
{
(RE_rayobject_raycast_callback) ((int(*)(BVHTree*,Isect*)) &bvh_intersect<BVHTree>),
(RE_rayobject_add_callback) ((void(*)(BVHTree*,RayObject*)) &bvh_add<BVHTree>),
(RE_rayobject_done_callback) ((void(*)(BVHTree*)) &bvh_done<BVHTree>),
(RE_rayobject_free_callback) ((void(*)(BVHTree*)) &bvh_free<BVHTree>),
(RE_rayobject_merge_bb_callback)((void(*)(BVHTree*,float*,float*)) &bvh_bb<BVHTree>),
(RE_rayobject_cost_callback) ((float(*)(BVHTree*)) &bvh_cost<BVHTree>)
};
RayObject *RE_rayobject_bvh_create(int size)
{
BVHTree *obj= (BVHTree*)MEM_callocN(sizeof(BVHTree), "BVHTree");