src/octree.cc
author Radek Brich <radek.brich@devl.cz>
Fri, 14 Dec 2007 00:05:54 +0100
branchpyrit
changeset 37 5f954c0d34fc
parent 36 b490093b0ac3
child 38 5d043eeb09d9
permissions -rw-r--r--
octree traversal rewritten to avoid recursion reenabled -O3 optimizations (was accidentaly disabled, now it traces even faster! :-)) realtime_bunny: added FPS counter, fixed a bug in ply loader min3 and max3 templates added to common.h
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
/*
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
 * Pyrit Ray Tracer
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
 * file: octree.cc
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
 *
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
 * Radek Brich, 2006-2007
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
 */
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
#include "octree.h"
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
OctreeNode::~OctreeNode()
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
	if (shapes != NULL)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
		delete shapes;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
	else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
		delete[] children;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
void OctreeNode::subdivide(BBox bbox, int maxdepth)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
	if (maxdepth <= 0 || shapes->size() <= 4)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
		return;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
	// make children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
	children = new OctreeNode[8];
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
	// evaluate centres for axes
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
	const Float xsplit = (bbox.L.x + bbox.H.x)*0.5;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
	const Float ysplit = (bbox.L.y + bbox.H.y)*0.5;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
	const Float zsplit = (bbox.L.z + bbox.H.z)*0.5;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
	// set bounding boxes for children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
	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
    33
	for (int i = 0; i < 4; i++)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
		// 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
    36
		// 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
    37
		childbb[i].H.x = xsplit;	// 0,1,2,3
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		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
    39
		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
    40
		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
    41
		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
    42
		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
    43
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
	// distribute shapes to children
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
	ShapeList::iterator sh;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
	unsigned int shapenum = 0;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
	for (sh = shapes->begin(); sh != shapes->end(); sh++)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
	{
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    50
		for (int i = 0; i < 8; i++)
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    51
			if ((*sh)->intersect_bbox(childbb[i]))
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
			{
36
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    53
				getChild(i)->addShape(*sh);
b490093b0ac3 new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents: 35
diff changeset
    54
				shapenum++;
35
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
	}
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
	if (shapes->size() <= 8 && shapenum > 2*shapes->size())
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
		// bad subdivision, revert
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
		delete[] children;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
		return;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
	}
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
	// remove shapes and set this node to non-leaf
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
	delete shapes;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
	shapes = NULL;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
	// recursive subdivision
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
	for (int i = 0; i < 8; i++)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
		children[i].subdivide(childbb[i], maxdepth-1);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
void Octree::build()
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
	dbgmsg(1, "* building octree\n");
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
	root = new OctreeNode();
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
	ShapeList::iterator shape;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
		root->addShape(*shape);
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
	root->subdivide(bbox, max_depth);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
	built = true;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    86
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    87
/*******************************************************
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    88
octree traversal algorithm as described in paper
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    89
"An Efficient Parametric Algorithm for Octree Traversal"
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    90
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
    91
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    92
see revision 37 for original recursive version
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    93
*******************************************************/
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    94
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    95
struct OctreeTravState
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
{
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    97
	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
    98
	OctreeNode *node;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
    99
	int next;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   100
	OctreeTravState() {};
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   101
	OctreeTravState(
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   102
		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
   103
		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
   104
		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
   105
		OctreeNode *const anode, const int anext):
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   106
		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
   107
		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
   108
};
35
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
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
   111
	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
   112
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   113
	if (txm < tym)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   114
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   115
		if (txm < tzm)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   116
			return xnode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   117
		else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
			return znode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
	else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   121
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   122
		if (tym < tzm)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   123
			return ynode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   124
		else
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   125
			return znode;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   126
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   127
}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
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
   130
		Float &nearest_distance)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   131
{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   132
	/* 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
   133
	if (!built)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   134
		return Container::nearest_intersection(origin_shape, ray, nearest_distance);
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   135
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   136
	OctreeTravState st[max_depth+1];
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   137
	register OctreeTravState *st_cur = st;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   138
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   139
#	define node	st_cur->node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   140
#	define tx0	st_cur->tx0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   141
#	define ty0	st_cur->ty0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   142
#	define tz0	st_cur->tz0
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   143
#	define tx1	st_cur->tx1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   144
#	define ty1	st_cur->ty1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   145
#	define tz1	st_cur->tz1
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   146
#	define txm	st_cur->txm
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   147
#	define tym	st_cur->tym
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   148
#	define tzm	st_cur->tzm
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   149
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
	int a = 0;
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   151
	Vector3 ro(ray.o);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   152
	Vector3 rdir(1.0/ray.dir.x, 1.0/ray.dir.y, 1.0/ray.dir.z);
35
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 (rdir.x < 0.0)
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
		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
   157
		rdir.x = -rdir.x;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
		a |= 4;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
	if (rdir.y < 0.0)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
		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
   163
		rdir.y = -rdir.y;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
		a |= 2;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
	}
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
	if (rdir.z < 0.0)
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
	{
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
		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
   169
		rdir.z = -rdir.z;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   170
		a |= 1;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   171
	}
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   172
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   173
	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
   174
	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
   175
	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
   176
	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
   177
	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
   178
	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
   179
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   180
	if (max3(tx0,ty0,tz0) > min3(tx1,ty1,tz1))
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   181
		return NULL;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   182
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   183
	node = root;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   184
	st_cur->next = -1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   185
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   186
	Shape *nearest_shape = NULL;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   187
	while (nearest_shape == NULL)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   188
	{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   189
		if (st_cur->next == -1)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   190
		{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   191
			st_cur->next = 8;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   192
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   193
			// if ray does intersect this node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   194
			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
   195
			{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   196
				if (node->isLeaf())
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   197
				{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   198
					ShapeList::iterator shape;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   199
					register Float mindist = max3(tx0,ty0,tz0);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   200
					/* correct & slow */
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   201
					//Float dist = min(min3(tx1,ty1,tz1),nearest_distance);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   202
					/* faster */
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   203
					register Float dist = nearest_distance;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   204
					for (shape = node->shapes->begin(); shape != node->shapes->end(); shape++)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   205
						if (*shape != origin_shape && (*shape)->intersect(ray, dist) && dist >= mindist)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   206
						{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   207
							nearest_shape = *shape;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   208
							nearest_distance = dist;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   209
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   210
				}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   211
				else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   212
				{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   213
					txm = 0.5 * (tx0+tx1);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   214
					tym = 0.5 * (ty0+ty1);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   215
					tzm = 0.5 * (tz0+tz1);
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   216
37
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   217
					// first node
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   218
					st_cur->next = 0;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   219
					if (tx0 > ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   220
					{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   221
						if (tx0 > tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   222
						{ // YZ
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   223
							if (tym < tx0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   224
								st_cur->next |= 2;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   225
							if (tzm < tx0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   226
								st_cur->next |= 1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   227
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   228
						else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   229
						{ // XY
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   230
							if (txm < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   231
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   232
							if (tym < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   233
								st_cur->next |= 2;
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
					}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   236
					else
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
						if (ty0 > tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   239
						{ // XZ
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   240
							if (txm < ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   241
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   242
							if (tzm < ty0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   243
								st_cur->next |= 1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   244
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   245
						else
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   246
						{ // XY
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   247
							if (txm < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   248
								st_cur->next |= 4;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   249
							if (tym < tz0)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   250
								st_cur->next |= 2;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   251
						}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   252
					}
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
			}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   255
		}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   256
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   257
		while (st_cur->next == 8)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   258
		{
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   259
			// pop state from stack
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   260
			if (st_cur == st)
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   261
				return NULL; // nothing to pop, finish
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   262
			--st_cur;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   263
		}
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
		// push current state
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   266
		*(st_cur+1) = *st_cur;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   267
		++st_cur;
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
		switch (st_cur->next)
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
			case 0:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   272
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   273
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   274
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   275
				node = node->getChild(a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   276
				(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
   277
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   278
			case 1:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   279
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   280
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   281
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   282
				node = node->getChild(1^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   283
				(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
   284
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   285
			case 2:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   286
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   287
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   288
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   289
				node = node->getChild(2^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   290
				(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
   291
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   292
			case 3:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   293
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   294
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   295
				tx1 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   296
				node = node->getChild(3^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   297
				(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
   298
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   299
			case 4:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   300
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   301
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   302
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   303
				node = node->getChild(4^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   304
				(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
   305
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   306
			case 5:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   307
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   308
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   309
				ty1 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   310
				node = node->getChild(5^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   311
				(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
   312
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   313
			case 6:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   314
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   315
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   316
				tz1 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   317
				node = node->getChild(6^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   318
				(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
   319
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   320
			case 7:
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   321
				tx0 = txm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   322
				ty0 = tym;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   323
				tz0 = tzm;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   324
				node = node->getChild(7^a);
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   325
				(st_cur-1)->next = 8;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   326
				break;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   327
		}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   328
		st_cur->next = -1;
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   329
	}
5f954c0d34fc octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents: 36
diff changeset
   330
	return nearest_shape;
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   331
}