src/octree.cc
author Radek Brich <radek.brich@devl.cz>
Tue, 26 Jul 2016 18:19:37 +0200
branchpyrit
changeset 104 2274a07510c1
parent 103 3b3257a410fe
permissions -rw-r--r--
Cleanup, dropped Windows support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
/*
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     2
 * octree.cc: Octree class
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     3
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     4
 * This file is part of Pyrit Ray Tracer.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     5
 *
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
     6
 * Copyright 2007, 2008  Radek Brich
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
 *
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
     9
 * of this software and associated documentation files (the "Software"), to deal
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    10
 * in the Software without restriction, including without limitation the rights
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    12
 * copies of the Software, and to permit persons to whom the Software is
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    13
 * furnished to do so, subject to the following conditions:
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    14
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    15
 * The above copyright notice and this permission notice shall be included in
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    16
 * all copies or substantial portions of the Software.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    17
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 43
diff changeset
    24
 * THE SOFTWARE.
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
 */
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
#include "octree.h"
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
103
3b3257a410fe Updated to compile:
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    29
const int Octree::MAX_DEPTH = 10;
3b3257a410fe Updated to compile:
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    30
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
OctreeNode::~OctreeNode()
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
{
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    33
	if (isLeaf())
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    34
	{
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    35
		leaf = leaf^1; // zero leaf bit
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
		delete shapes;
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    37
	}
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
	else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
		delete[] children;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    42
void OctreeNode::subdivide(const BBox &bbox, int maxdepth)
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
{
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    44
	ShapeList *l_shapes = getShapes();
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    45
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    46
	// prepare children (this also sets this node as non-leaf)
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    47
	makeChildren();
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
	// evaluate centres for axes
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    50
	const Float xsplit = (bbox.L.x + bbox.H.x)*0.5f;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    51
	const Float ysplit = (bbox.L.y + bbox.H.y)*0.5f;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    52
	const Float zsplit = (bbox.L.z + bbox.H.z)*0.5f;
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
	// set bounding boxes for children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
	BBox childbb[8] = {bbox, bbox, bbox, bbox, bbox, bbox, bbox, bbox};
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
	for (int i = 0; i < 4; i++)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
		// this is little obfuscated, so on right are listed affected children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
		// the idea is to cut every axis once per child, making 8 combinations
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
		childbb[i].H.x = xsplit;	// 0,1,2,3
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
		childbb[i+4].L.x = xsplit;  // 4,5,6,7
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
		childbb[i+(i>>1<<1)].H.y = ysplit;	// 0,1,4,5
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
		childbb[i+(i>>1<<1)+2].L.y = ysplit;// 2,3,6,7
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
		childbb[i<<1].H.z = zsplit;		// 0,2,4,6
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
		childbb[(i<<1)+1].L.z = zsplit; // 1,3,5,7
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
	// distribute shapes to children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
	ShapeList::iterator sh;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
	unsigned int shapenum = 0;
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    71
	for (sh = l_shapes->begin(); sh != l_shapes->end(); sh++)
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
	{
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    73
		for (int i = 0; i < 8; i++)
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    74
			if ((*sh)->intersect_bbox(childbb[i]))
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
			{
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    76
				getChild(i)->addShape(*sh);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    77
				shapenum++;
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
			}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    81
	if ((l_shapes->size() <= 8 && shapenum > 2*l_shapes->size())
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    82
	|| shapenum >= 6*l_shapes->size())
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
		// bad subdivision, revert
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
		delete[] children;
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    86
		setShapes(l_shapes);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
		return;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
	// remove shapes and set this node to non-leaf
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    91
	delete l_shapes;
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
	// recursive subdivision
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
	for (int i = 0; i < 8; i++)
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    95
		if (maxdepth > 1 && getChild(i)->getShapes()->size() > 4)
42
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    96
			children[i].subdivide(childbb[i], maxdepth-1);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
void Octree::build()
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
	dbgmsg(1, "* building octree\n");
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
	root = new OctreeNode();
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
	ShapeList::iterator shape;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
		root->addShape(*shape);
103
3b3257a410fe Updated to compile:
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   106
	root->subdivide(bbox, MAX_DEPTH);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
	built = true;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   109
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   110
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   111
/*******************************************************
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   112
octree traversal algorithm as described in paper
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   113
"An Efficient Parametric Algorithm for Octree Traversal"
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   114
by J. Revelles, C. Urena and M. Lastra.
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   115
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   116
see revision 37 for original recursive version
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   117
*******************************************************/
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   118
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   119
struct OctreeTravState
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
{
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   121
	Float tx0,ty0,tz0,tx1,ty1,tz1,txm,tym,tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   122
	OctreeNode *node;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   123
	int next;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   124
	OctreeTravState() {};
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   125
	OctreeTravState(
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   126
		const Float atx0, const Float aty0, const Float atz0,
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   127
		const Float atx1, const Float aty1, const Float atz1,
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   128
		const Float atxm, const Float atym, const Float atzm,
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   129
		OctreeNode *const anode, const int anext):
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   130
		tx0(atx0), ty0(aty0), tz0(atz0), tx1(atx1), ty1(aty1), tz1(atz1),
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   131
		txm(atxm), tym(atym), tzm(atzm), node(anode), next(anext) {};
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   132
};
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   133
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   134
inline const int &next_node(const Float &txm, const int &xnode,
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   135
	const Float &tym, const int &ynode, const Float &tzm, const int &znode)
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   136
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   137
	if (txm < tym)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
		if (txm < tzm)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
			return xnode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
		else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
			return znode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   143
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   144
	else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
		if (tym < tzm)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   147
			return ynode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
		else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
			return znode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   153
const Shape * Octree::nearest_intersection(const Shape *origin_shape, const Ray &ray,
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
		Float &nearest_distance)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
	/* if we have no tree, fall back to naive test */
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
	if (!built)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
		return Container::nearest_intersection(origin_shape, ray, nearest_distance);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
103
3b3257a410fe Updated to compile:
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
   160
	OctreeTravState st[MAX_DEPTH+1];
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   161
	OctreeTravState *st_cur = st;
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   162
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   163
#	define node	st_cur->node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   164
#	define tx0	st_cur->tx0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   165
#	define ty0	st_cur->ty0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   166
#	define tz0	st_cur->tz0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   167
#	define tx1	st_cur->tx1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   168
#	define ty1	st_cur->ty1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   169
#	define tz1	st_cur->tz1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   170
#	define txm	st_cur->txm
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   171
#	define tym	st_cur->tym
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   172
#	define tzm	st_cur->tzm
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   173
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   174
	int a = 0;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   175
	Vector ro(ray.o);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   176
	Vector rdir(ray.dir);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   178
	if (rdir.x < 0.0)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   179
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
		ro.x = (bbox.L.x+bbox.H.x) - ro.x;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
		rdir.x = -rdir.x;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
		a |= 4;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   183
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   184
	if (rdir.y < 0.0)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   185
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   186
		ro.y = (bbox.L.y+bbox.H.y) - ro.y;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   187
		rdir.y = -rdir.y;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   188
		a |= 2;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   189
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   190
	if (rdir.z < 0.0)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
		ro.z = (bbox.L.z+bbox.H.z) - ro.z;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
		rdir.z = -rdir.z;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
		a |= 1;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   195
	}
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   196
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   197
	if (rdir.x == 0.0f) rdir.x = Eps;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   198
	if (rdir.y == 0.0f) rdir.y = Eps;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   199
	if (rdir.z == 0.0f) rdir.z = Eps;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   200
	rdir.x = 1.0f/rdir.x;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   201
	rdir.y = 1.0f/rdir.y;
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   202
	rdir.z = 1.0f/rdir.z;
49
558fde7da82a workaround for divide by zero bug in octree
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   203
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   204
	tx0 = (bbox.L.x - ro.x) * rdir.x;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   205
	tx1 = (bbox.H.x - ro.x) * rdir.x;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   206
	ty0 = (bbox.L.y - ro.y) * rdir.y;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   207
	ty1 = (bbox.H.y - ro.y) * rdir.y;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   208
	tz0 = (bbox.L.z - ro.z) * rdir.z;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   209
	tz1 = (bbox.H.z - ro.z) * rdir.z;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   210
49
558fde7da82a workaround for divide by zero bug in octree
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   211
	if (max3(tx0,ty0,tz0) >= min3(tx1,ty1,tz1))
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   212
		return NULL;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   213
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   214
	node = root;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   215
	st_cur->next = -1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   216
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 93
diff changeset
   217
	const Shape *nearest_shape = NULL;
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   218
	for (;;)
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   219
	{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   220
		if (st_cur->next == -1)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   221
		{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   222
			st_cur->next = 8;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   223
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   224
			// if ray does intersect this node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   225
			if (!(tx1 < 0.0 || ty1 < 0.0 || tz1 < 0.0))
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   226
			{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   227
				if (node->isLeaf())
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   228
				{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   229
					ShapeList::iterator shape;
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   230
					//register Float mindist = max3(tx0,ty0,tz0);
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   231
					register Float dist = min(nearest_distance, min3(tx1,ty1,tz1));
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
   232
					for (shape = node->getShapes()->begin(); shape != node->getShapes()->end(); shape++)
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   233
						if (*shape != origin_shape && (*shape)->intersect(ray, dist))
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   234
						{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   235
							nearest_shape = *shape;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   236
							nearest_distance = dist;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   237
						}
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   238
					if (nearest_shape != NULL)
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   239
						return nearest_shape;
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   240
				}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   241
				else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   242
				{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   243
					txm = 0.5f * (tx0+tx1);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   244
					tym = 0.5f * (ty0+ty1);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   245
					tzm = 0.5f * (tz0+tz1);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   246
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   247
					// first node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   248
					st_cur->next = 0;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   249
					if (tx0 > ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   250
					{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   251
						if (tx0 > tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   252
						{ // YZ
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   253
							if (tym < tx0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   254
								st_cur->next |= 2;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   255
							if (tzm < tx0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   256
								st_cur->next |= 1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   257
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   258
						else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   259
						{ // XY
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   260
							if (txm < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   261
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   262
							if (tym < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   263
								st_cur->next |= 2;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   264
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   265
					}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   266
					else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   267
					{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   268
						if (ty0 > tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   269
						{ // XZ
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   270
							if (txm < ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   271
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   272
							if (tzm < ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   273
								st_cur->next |= 1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   274
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   275
						else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   276
						{ // XY
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   277
							if (txm < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   278
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   279
							if (tym < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   280
								st_cur->next |= 2;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   281
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   282
					}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   283
				}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   284
			}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   285
		}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   286
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   287
		while (st_cur->next == 8)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   288
		{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   289
			// pop state from stack
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   290
			if (st_cur == st)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   291
				return NULL; // nothing to pop, finish
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   292
			--st_cur;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   293
		}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   294
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   295
		// push current state
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   296
		*(st_cur+1) = *st_cur;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   297
		++st_cur;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   298
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   299
		switch (st_cur->next)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   300
		{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   301
			case 0:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   302
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   303
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   304
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   305
				node = node->getChild(a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   306
				(st_cur-1)->next = next_node(txm, 4, tym, 2, tzm, 1);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   307
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   308
			case 1:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   309
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   310
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   311
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   312
				node = node->getChild(1^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   313
				(st_cur-1)->next = next_node(txm, 5, tym, 3, tz1, 8);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   314
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   315
			case 2:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   316
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   317
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   318
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   319
				node = node->getChild(2^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   320
				(st_cur-1)->next = next_node(txm, 6, ty1, 8, tzm, 3);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   321
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   322
			case 3:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   323
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   324
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   325
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   326
				node = node->getChild(3^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   327
				(st_cur-1)->next = next_node(txm, 7, ty1, 8, tz1, 8);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   328
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   329
			case 4:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   330
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   331
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   332
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   333
				node = node->getChild(4^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   334
				(st_cur-1)->next = next_node(tx1, 8, tym, 6, tzm, 5);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   335
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   336
			case 5:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   337
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   338
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   339
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   340
				node = node->getChild(5^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   341
				(st_cur-1)->next = next_node(tx1, 8, tym, 7, tz1, 8);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   342
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   343
			case 6:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   344
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   345
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   346
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   347
				node = node->getChild(6^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   348
				(st_cur-1)->next = next_node(tx1, 8, ty1, 8, tzm, 7);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   349
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   350
			case 7:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   351
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   352
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   353
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   354
				node = node->getChild(7^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   355
				(st_cur-1)->next = 8;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   356
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   357
		}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   358
		st_cur->next = -1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   359
	}
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   360
}