Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rayobject_bvh.cpp « raytrace « intern « render « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bf54f889ebf2a864e33c81128bbd165b34740bed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
 * $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 *****
 */
#include <assert.h>

#include "RE_raytrace.h"
#include "rayobject_rtbuild.h"
#include "rayobject.h"
#include "MEM_guardedalloc.h"
#include "BKE_utildefines.h"
#include "BLI_arithb.h"
#include "BLI_memarena.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 = rtbuild_get_primitive( builder, 0 );

		if(RE_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*)rtbuild_get_primitive( builder, 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(!RE_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;
	}

	assert(false);
}

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(RE_rayobject_isAligned(obj->root))
		return bvh_node_stack_raycast<BVHNode,64,true>(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");
	assert( RE_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 RE_rayobject_unalignRayAPI((RayObject*) obj);
}