db1df43f6650dfd2ffb4b7c99afba8eb755e9083
[blender-staging.git] / source / blender / render / intern / raytrace / vbvh.h
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): AndrĂ© Pinto.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #include <assert.h>
30
31 #include <algorithm>
32 #include "rayobject_rtbuild.h"
33 #include "BLI_memarena.h"
34
35
36 /*
37  * VBVHNode represents a BVHNode with support for a variable number of childrens
38  */
39 struct VBVHNode
40 {
41         float   bb[6];
42
43         VBVHNode *child;
44         VBVHNode *sibling;
45 };
46
47
48 /*
49  * Push nodes (used on dfs)
50  */
51 template<class Node>
52 inline static void bvh_node_push_childs(Node *node, Isect *isec, Node **stack, int &stack_pos)
53 {
54         Node *child = node->child;
55
56         if(is_leaf(child))
57         {
58                 stack[stack_pos++] = child;
59         }
60         else
61         {
62                 while(child)
63                 {
64                         //Skips BB tests on primitives
65 /*
66                         if(is_leaf(child->child))
67                                 stack[stack_pos++] = child->child;
68                         else
69 */
70                                 stack[stack_pos++] = child;
71                                 
72                         child = child->sibling;
73                 }
74         }
75 }
76
77
78 template<class Node>
79 int count_childs(Node *parent)
80 {
81         int n = 0;
82         for(Node *i = parent->child; i; i = i->sibling)
83         {
84                 n++;
85                 if(is_leaf(i))
86                         break;
87         }
88                 
89         return n;
90 }
91
92
93 template<class Node>
94 void append_sibling(Node *node, Node *sibling)
95 {
96         while(node->sibling)
97                 node = node->sibling;
98                 
99         node->sibling = sibling;
100 }
101
102
103 /*
104  * Builds a binary VBVH from a rtbuild
105  */
106 template<class Node>
107 struct BuildBinaryVBVH
108 {
109         MemArena *arena;
110
111         BuildBinaryVBVH(MemArena *a)
112         {
113                 arena = a;
114         }
115
116         Node *create_node()
117         {
118                 Node *node = (Node*)BLI_memarena_alloc( arena, sizeof(Node) );
119                 assert( RE_rayobject_isAligned(node) );
120
121                 node->sibling = NULL;
122                 node->child   = NULL;
123
124                 return node;
125         }
126         
127         int rtbuild_split(RTBuilder *builder)
128         {
129                 return ::rtbuild_heuristic_object_split(builder, 2);
130         }
131         
132         Node *transform(RTBuilder *builder)
133         {
134                 
135                 int size = rtbuild_size(builder);
136                 if(size == 1)
137                 {
138                         Node *node = create_node();
139                         INIT_MINMAX(node->bb, node->bb+3);
140                         rtbuild_merge_bb(builder, node->bb, node->bb+3);                
141                         node->child = (Node*) rtbuild_get_primitive( builder, 0 );
142                         return node;
143                 }
144                 else
145                 {
146                         Node *node = create_node();
147
148                         INIT_MINMAX(node->bb, node->bb+3);
149                         rtbuild_merge_bb(builder, node->bb, node->bb+3);
150                         
151                         Node **child = &node->child;
152
153                         int nc = rtbuild_split(builder);
154                         assert(nc == 2);
155                         for(int i=0; i<nc; i++)
156                         {
157                                 RTBuilder tmp;
158                                 rtbuild_get_child(builder, i, &tmp);
159                                 
160                                 *child = transform(&tmp);
161                                 child = &((*child)->sibling);
162                         }
163
164                         *child = 0;
165                         return node;
166                 }
167         }
168 };
169
170 /*
171 template<class Tree,class OldNode>
172 struct Reorganize_VBVH
173 {
174         Tree *tree;
175         
176         Reorganize_VBVH(Tree *t)
177         {
178                 tree = t;
179         }
180         
181         VBVHNode *create_node()
182         {
183                 VBVHNode *node = (VBVHNode*)BLI_memarena_alloc(tree->node_arena, sizeof(VBVHNode));
184                 return node;
185         }
186         
187         void copy_bb(VBVHNode *node, OldNode *old)
188         {
189                 std::copy( old->bb, old->bb+6, node->bb );
190         }
191         
192         VBVHNode *transform(OldNode *old)
193         {
194                 if(is_leaf(old))
195                         return (VBVHNode*)old;
196
197                 VBVHNode *node = create_node();
198                 VBVHNode **child_ptr = &node->child;
199                 node->sibling = 0;
200
201                 copy_bb(node,old);
202
203                 for(OldNode *o_child = old->child; o_child; o_child = o_child->sibling)
204                 {
205                         VBVHNode *n_child = transform(o_child);
206                         *child_ptr = n_child;
207                         if(is_leaf(n_child)) return node;
208                         child_ptr = &n_child->sibling;
209                 }
210                 *child_ptr = 0;
211                 
212                 return node;
213         }       
214 };
215 */