src/octree.cc
author Radek Brich <radek.brich@devl.cz>
Tue, 06 May 2008 09:39:58 +0200
branchpyrit
changeset 93 96d65f841791
parent 92 9af5c039b678
child 95 ca7d4c665531
permissions -rw-r--r--
more build script tuning make all float constants single precision solve many warnings from msvc and gcc with various -W... flags add common.cc file for dbgmsg() function witch apparently cannot be inlined fix python module building with msvc, add manifest file handling remove forgotten RenderrowData class add stanford models download script for windows (.bat)
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
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
OctreeNode::~OctreeNode()
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
{
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    31
	if (isLeaf())
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    32
	{
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    33
		leaf = leaf^1; // zero leaf bit
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
		delete shapes;
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    35
	}
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
	else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
		delete[] children;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
    40
void OctreeNode::subdivide(const BBox &bbox, int maxdepth)
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
{
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    42
	ShapeList *l_shapes = getShapes();
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    44
	// 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
    45
	makeChildren();
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
	// 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
    48
	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
    49
	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
    50
	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
    51
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	// set bounding boxes for children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
	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
    54
	for (int i = 0; i < 4; i++)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
		// 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
    57
		// 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
    58
		childbb[i].H.x = xsplit;	// 0,1,2,3
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
		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
    60
		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
    61
		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
    62
		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
    63
		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
    64
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
	// distribute shapes to children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
	ShapeList::iterator sh;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
	unsigned int shapenum = 0;
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    69
	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
    70
	{
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    71
		for (int i = 0; i < 8; i++)
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    72
			if ((*sh)->intersect_bbox(childbb[i]))
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
			{
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    74
				getChild(i)->addShape(*sh);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    75
				shapenum++;
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
			}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    79
	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
    80
	|| shapenum >= 6*l_shapes->size())
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
		// bad subdivision, revert
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
		delete[] children;
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    84
		setShapes(l_shapes);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
		return;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
	// 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
    89
	delete l_shapes;
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
	// recursive subdivision
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
	for (int i = 0; i < 8; i++)
43
0b8b968b42d1 memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    93
		if (maxdepth > 1 && getChild(i)->getShapes()->size() > 4)
42
fbdeb3e04543 cleaned Texture interface
Radek Brich <radek.brich@devl.cz>
parents: 38
diff changeset
    94
			children[i].subdivide(childbb[i], maxdepth-1);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
void Octree::build()
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
	dbgmsg(1, "* building octree\n");
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
	root = new OctreeNode();
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
	ShapeList::iterator shape;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
		root->addShape(*shape);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
	root->subdivide(bbox, max_depth);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
	built = true;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   108
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   109
/*******************************************************
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   110
octree traversal algorithm as described in paper
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   111
"An Efficient Parametric Algorithm for Octree Traversal"
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   112
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
   113
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   114
see revision 37 for original recursive version
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
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   117
struct OctreeTravState
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
{
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   119
	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
   120
	OctreeNode *node;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   121
	int next;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   122
	OctreeTravState() {};
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   123
	OctreeTravState(
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   124
		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
   125
		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
   126
		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
   127
		OctreeNode *const anode, const int anext):
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   128
		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
   129
		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
   130
};
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   132
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
   133
	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
   134
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
	if (txm < tym)
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 < tzm)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   138
			return xnode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   139
		else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
			return znode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   141
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   142
	else
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
		if (tym < tzm)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
			return ynode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
		else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   147
			return znode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
}
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
Shape * Octree::nearest_intersection(const Shape *origin_shape, const Ray &ray,
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
		Float &nearest_distance)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
	/* 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
   155
	if (!built)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
		return Container::nearest_intersection(origin_shape, ray, nearest_distance);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   158
#ifdef MSVC
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   159
	// MSVC wants constant expression here... hope it won't overflow :)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   160
	OctreeTravState st[20];
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   161
#else
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   162
	OctreeTravState st[max_depth+1];
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   163
#endif
93
96d65f841791 more build script tuning
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
   164
	OctreeTravState *st_cur = st;
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   165
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   166
#	define node	st_cur->node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   167
#	define tx0	st_cur->tx0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   168
#	define ty0	st_cur->ty0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   169
#	define tz0	st_cur->tz0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   170
#	define tx1	st_cur->tx1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   171
#	define ty1	st_cur->ty1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   172
#	define tz1	st_cur->tz1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   173
#	define txm	st_cur->txm
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   174
#	define tym	st_cur->tym
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   175
#	define tzm	st_cur->tzm
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   176
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   177
	int a = 0;
91
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   178
	Vector ro(ray.o);
9d66d323c354 packetize Phong shader
Radek Brich <radek.brich@devl.cz>
parents: 49
diff changeset
   179
	Vector rdir(ray.dir);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   180
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   181
	if (rdir.x < 0.0)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   182
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   183
		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
   184
		rdir.x = -rdir.x;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   185
		a |= 4;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   186
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   187
	if (rdir.y < 0.0)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   188
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   189
		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
   190
		rdir.y = -rdir.y;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
		a |= 2;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
	if (rdir.z < 0.0)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   195
		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
   196
		rdir.z = -rdir.z;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   197
		a |= 1;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
	}
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   199
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   200
	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
   201
	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
   202
	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
   203
	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
   204
	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
   205
	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
   206
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   207
	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
   208
	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
   209
	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
   210
	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
   211
	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
   212
	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
   213
49
558fde7da82a workaround for divide by zero bug in octree
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   214
	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
   215
		return NULL;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   216
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   217
	node = root;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   218
	st_cur->next = -1;
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
	Shape *nearest_shape = NULL;
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   221
	for (;;)
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   222
	{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   223
		if (st_cur->next == -1)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   224
		{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   225
			st_cur->next = 8;
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 ray does intersect this node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   228
			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
   229
			{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   230
				if (node->isLeaf())
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   231
				{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   232
					ShapeList::iterator shape;
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   233
					//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
   234
					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
   235
					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
   236
						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
   237
						{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   238
							nearest_shape = *shape;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   239
							nearest_distance = dist;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   240
						}
38
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   241
					if (nearest_shape != NULL)
5d043eeb09d9 realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents: 37
diff changeset
   242
						return nearest_shape;
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   243
				}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   244
				else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   245
				{
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents: 91
diff changeset
   246
					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
   247
					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
   248
					tzm = 0.5f * (tz0+tz1);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   249
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   250
					// first node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   251
					st_cur->next = 0;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   252
					if (tx0 > ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   253
					{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   254
						if (tx0 > tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   255
						{ // YZ
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   256
							if (tym < tx0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   257
								st_cur->next |= 2;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   258
							if (tzm < tx0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   259
								st_cur->next |= 1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   260
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   261
						else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   262
						{ // XY
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   263
							if (txm < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   264
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   265
							if (tym < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   266
								st_cur->next |= 2;
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
					}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   269
					else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   270
					{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   271
						if (ty0 > tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   272
						{ // XZ
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   273
							if (txm < ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   274
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   275
							if (tzm < ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   276
								st_cur->next |= 1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   277
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   278
						else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   279
						{ // XY
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   280
							if (txm < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   281
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   282
							if (tym < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   283
								st_cur->next |= 2;
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
			}
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
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   290
		while (st_cur->next == 8)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   291
		{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   292
			// pop state from stack
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   293
			if (st_cur == st)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   294
				return NULL; // nothing to pop, finish
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   295
			--st_cur;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   296
		}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   297
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   298
		// push current state
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   299
		*(st_cur+1) = *st_cur;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   300
		++st_cur;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   301
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   302
		switch (st_cur->next)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   303
		{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   304
			case 0:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   305
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   306
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   307
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   308
				node = node->getChild(a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   309
				(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
   310
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   311
			case 1:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   312
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   313
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   314
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   315
				node = node->getChild(1^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   316
				(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
   317
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   318
			case 2:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   319
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   320
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   321
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   322
				node = node->getChild(2^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   323
				(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
   324
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   325
			case 3:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   326
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   327
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   328
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   329
				node = node->getChild(3^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   330
				(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
   331
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   332
			case 4:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   333
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   334
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   335
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   336
				node = node->getChild(4^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   337
				(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
   338
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   339
			case 5:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   340
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   341
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   342
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   343
				node = node->getChild(5^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   344
				(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
   345
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   346
			case 6:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   347
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   348
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   349
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   350
				node = node->getChild(6^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   351
				(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
   352
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   353
			case 7:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   354
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   355
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   356
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   357
				node = node->getChild(7^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   358
				(st_cur-1)->next = 8;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   359
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   360
		}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   361
		st_cur->next = -1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   362
	}
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   363
}