src/kdtree.cc
author Radek Brich <radek.brich@devl.cz>
Tue, 29 Apr 2008 23:31:08 +0200
branchpyrit
changeset 90 f6a72eb99631
parent 87 1081e3dd3f3e
child 91 9d66d323c354
permissions -rw-r--r--
rename Python module from 'raytracer' to 'pyrit' improve Python binding: - new objects: Container, Octree, KdTree, Shape, Pixmap, Sampler, DefaultSampler - all shapes are now subclasses of Shape - clean, remove redundant Getattr's - Raytracer render method now just wraps C++ method without doing any additional work adjust all demos for changes in Python binding, remove PIL dependency add demo_PIL.py as a example of pyrit + PIL usage render_nff.py now either loads file given as a argument or reads input from stdin otherwise fix bug in pixmap float to char conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
     1
/*
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     2
 * kdtree.cc: KdTree class
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     3
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     4
 * This file is part of Pyrit Ray Tracer.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     5
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
     6
 * Copyright 2006, 2007  Radek Brich
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
     7
 *
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
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: 42
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: 42
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: 42
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: 42
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: 42
diff changeset
    13
 * furnished to do so, subject to the following conditions:
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    14
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
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: 42
diff changeset
    16
 * all copies or substantial portions of the Software.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
diff changeset
    17
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 42
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: 42
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: 42
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: 42
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: 42
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: 42
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: 42
diff changeset
    24
 * THE SOFTWARE.
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    25
 */
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 30
diff changeset
    26
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    27
#include <algorithm>
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    28
#include <stack>
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
    29
#include <string>
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
    30
#include <sstream>
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    31
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    32
#include "kdtree.h"
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
    33
#include "serialize.h"
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    35
class ShapeBound
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    36
{
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    37
public:
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    38
	Shape *shape;
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    39
	Float pos;
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    40
	bool end;
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    41
	ShapeBound(Shape *ashape, const Float apos, const bool aend):
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    42
		shape(ashape), pos(apos), end(aend) {};
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    43
	friend bool operator<(const ShapeBound& a, const ShapeBound& b)
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    44
	{
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    45
		if (a.pos == b.pos)
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    46
			return a.shape < b.shape;
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    47
		else
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    48
			return a.pos < b.pos;
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    49
	};
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    50
};
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    51
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    52
// stack element for kd-tree traversal
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    53
struct StackElem
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    54
{
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    55
	KdNode* node; /* pointer to far child */
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
    56
	Float t; /* the entry/exit signed distance */
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    57
	Vector3 pb; /* the coordinates of entry/exit point */
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    58
	int prev;
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    59
};
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    60
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    61
// ----------------------------------------
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
    62
16
20bceb605f48 add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    63
KdNode::~KdNode()
20bceb605f48 add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    64
{
20bceb605f48 add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    65
	if (isLeaf())
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    66
		delete getShapes();
16
20bceb605f48 add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    67
	else
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    68
		delete[] getLeftChild();
16
20bceb605f48 add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    69
}
20bceb605f48 add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
    70
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    71
// kd-tree recursive build algorithm, inspired by PBRT (www.pbrt.org)
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    72
void KdTree::recursive_build(KdNode *node, BBox bounds, int maxdepth)
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
{
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    74
	ShapeList *shapes = node->getShapes();
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    75
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    76
	if (maxdepth <= 0 || shapes->size() <= 2)
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    77
	{
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    78
		node->setLeaf();
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    79
		return;
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    80
	}
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
    81
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    82
	// choose split axis
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    83
	/*axis = 0;
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    84
	if (bounds.h() > bounds.w() && bounds.h() > bounds.d())
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    85
		axis = 1;
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    86
	if (bounds.d() > bounds.w() && bounds.d() > bounds.h())
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    87
		axis = 2;
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    88
*/
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    89
	// create sorted list of shape bounds (= find all posible splits)
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    90
	vector<ShapeBound> edges[3];
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    91
	ShapeList::iterator shape;
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    92
	for (shape = shapes->begin(); shape != shapes->end(); shape++)
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
    93
	{
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    94
		BBox shapebounds = (*shape)->get_bbox();
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    95
		for (int ax = 0; ax < 3; ax++)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    96
		{
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    97
			edges[ax].push_back(ShapeBound(*shape, shapebounds.L[ax], 0));
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    98
			edges[ax].push_back(ShapeBound(*shape, shapebounds.H[ax], 1));
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
    99
		}
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   100
	}
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   101
	for (int ax = 0; ax < 3; ax++)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   102
		sort(edges[ax].begin(), edges[ax].end());
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   103
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   104
	// choose best split pos
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   105
	const Float K = 1.4; // constant, K = cost of traversal / cost of ray-triangle intersection
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   106
	Float SAV = (bounds.w()*bounds.h() +  // surface area of node
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   107
		bounds.w()*bounds.d() + bounds.h()*bounds.d());
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   108
	Float cost = SAV * (K + shapes->size()); // initial cost = non-split cost
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   109
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   110
	vector<ShapeBound>::iterator edge, splitedge = edges[2].end();
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   111
	int axis = 0;
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   112
	for (int ax = 0; ax < 3; ax++)
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   113
	{
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   114
		int lnum = 0, rnum = shapes->size();
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   115
		BBox lbb = bounds;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   116
		BBox rbb = bounds;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   117
		for (edge = edges[ax].begin(); edge != edges[ax].end(); edge++)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   118
		{
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   119
			if (edge->end)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   120
				rnum--;
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   121
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   122
			// calculate SAH cost of this split
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   123
			lbb.H.cell[ax] = edge->pos;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   124
			rbb.L.cell[ax] = edge->pos;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   125
			Float SAL = (lbb.w()*lbb.h() + lbb.w()*lbb.d() + lbb.h()*lbb.d());
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   126
			Float SAR = (rbb.w()*rbb.h() + rbb.w()*rbb.d() + rbb.h()*rbb.d());
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   127
			Float splitcost = K*SAV + SAL*(K + lnum) + SAR*(K + rnum);
9
3239f749e394 kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   128
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   129
			if (splitcost < cost)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   130
			{
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   131
				axis = ax;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   132
				splitedge = edge;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   133
				cost = splitcost;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   134
			}
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   135
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   136
			if (!edge->end)
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   137
				lnum++;
9
3239f749e394 kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   138
		}
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   139
	}
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   140
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   141
	if (splitedge == edges[2].end())
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
   142
	{
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   143
		node->setLeaf();
9
3239f749e394 kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   144
		return;
15
a0a3e334744f C++ demos: prepare infrastructure, add spheres_shadow.cc
Radek Brich <radek.brich@devl.cz>
parents: 14
diff changeset
   145
	}
9
3239f749e394 kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   146
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   147
	node->setSplit(splitedge->pos);
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   148
29
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
   149
#if 0
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   150
// export kd-tree as .obj for visualization
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   151
// this would be hard to reconstruct later
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   152
	static ofstream F("kdtree.obj");
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   153
	Vector3 v;
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   154
	static int f=0;
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   155
	v.cell[axis] = node->getSplit();
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   156
	v.cell[(axis+1)%3] = bounds.L.cell[(axis+1)%3];
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   157
	v.cell[(axis+2)%3] = bounds.L.cell[(axis+2)%3];
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   158
	F << "v " << v.x << " " << v.y << " " << v.z << endl;
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   159
	v.cell[(axis+1)%3] = bounds.L.cell[(axis+1)%3];
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   160
	v.cell[(axis+2)%3] = bounds.H.cell[(axis+2)%3];
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   161
	F << "v " << v.x << " " << v.y << " " << v.z << endl;
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   162
	v.cell[(axis+1)%3] = bounds.H.cell[(axis+1)%3];
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   163
	v.cell[(axis+2)%3] = bounds.H.cell[(axis+2)%3];
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   164
	F << "v " << v.x << " " << v.y << " " << v.z << endl;
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   165
	v.cell[(axis+1)%3] = bounds.H.cell[(axis+1)%3];
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   166
	v.cell[(axis+2)%3] = bounds.L.cell[(axis+2)%3];
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   167
	F << "v " << v.x << " " << v.y << " " << v.z << endl;
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   168
	F << "f " << f+1 << " " << f+2 << " " << f+3 << " " << f+4 << endl;
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   169
	f += 4;
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   170
#endif
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   171
9
3239f749e394 kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   172
	// split this node
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   173
	delete shapes;
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   174
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   175
	BBox lbb = bounds;
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   176
	BBox rbb = bounds;
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   177
	lbb.H.cell[axis] = node->getSplit();
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   178
	rbb.L.cell[axis] = node->getSplit();
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   179
	node->setChildren(new KdNode[2]);
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   180
	node->setAxis(axis);
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   181
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   182
	for (edge = edges[axis].begin(); edge != splitedge; edge++)
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   183
		if (!edge->end && edge->shape->intersect_bbox(lbb))
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   184
			node->getLeftChild()->addShape(edge->shape);
71
4fedf7290929 simplify kd-tree building, it's also much faster now
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
   185
	for (edge = splitedge; edge < edges[axis].end(); edge++)
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 71
diff changeset
   186
		if (edge->end && edge->shape->intersect_bbox(rbb))
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   187
			node->getRightChild()->addShape(edge->shape);
9
3239f749e394 kd-tree: build algorithm - completed, untested
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
   188
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   189
	recursive_build(node->getLeftChild(), lbb, maxdepth-1);
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   190
	recursive_build(node->getRightChild(), rbb, maxdepth-1);
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   191
}
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
11
4d192e13ee84 move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   193
void KdTree::build()
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
{
30
33f95441790e pyrit_verbosity: new variable for controlling amount of output, see common.h
Radek Brich <radek.brich@devl.cz>
parents: 29
diff changeset
   195
	dbgmsg(1, "* building kd-tree\n");
7
bf17f9f84c91 kd-tree: build algorithm - searching for all posible splits
Radek Brich <radek.brich@devl.cz>
parents: 0
diff changeset
   196
	root = new KdNode();
17
5176ba000a67 fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   197
	ShapeList::iterator shape;
5176ba000a67 fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   198
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
5176ba000a67 fix last leak as reported by valgrind
Radek Brich <radek.brich@devl.cz>
parents: 16
diff changeset
   199
		root->addShape(*shape);
76
3b60fd0bea64 kd-tree: move recursive build subroutine from KdNode to KdTree
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
   200
	recursive_build(root, bbox, max_depth);
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   201
	built = true;
0
3547b885df7e initial commit, raytracer source as written year ago and unchanged since 2007-03-25
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
}
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   203
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   204
/* algorithm by Vlastimil Havran, Heuristic Ray Shooting Algorithms, appendix C */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   205
Shape *KdTree::nearest_intersection(const Shape *origin_shape, const Ray &ray,
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   206
	Float &nearest_distance)
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   207
{
22
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   208
	Float a, b; /* entry/exit signed distance */
76b7bd51d64a new make infrastructure
Radek Brich <radek.brich@devl.cz>
parents: 21
diff changeset
   209
	Float t;    /* signed distance to the splitting plane */
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   210
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   211
	/* if we have no tree, fall back to naive test */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   212
	if (!built)
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   213
		return Container::nearest_intersection(origin_shape, ray, nearest_distance);
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   214
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   215
	if (!bbox.intersect(ray, a, b))
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   216
		return NULL;
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   217
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   218
	/* pointers to the far child node and current node */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   219
	KdNode *farchild, *node;
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   220
	node = root;
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   221
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   222
	StackElem stack[max_depth];
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   223
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   224
	int entry = 0, exit = 1;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   225
	stack[entry].t = a;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   226
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   227
	/* distinguish between internal and external origin of a ray*/
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   228
	if (a >= 0.0)
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   229
		stack[entry].pb = ray.o + ray.dir * a; /* external */
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   230
	else
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   231
		stack[entry].pb = ray.o;               /* internal */
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   232
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   233
	/* setup initial exit point in the stack */
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   234
	stack[exit].t = b;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   235
	stack[exit].pb = ray.o + ray.dir * b;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   236
	stack[exit].node = NULL;
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   237
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   238
	/* loop, traverse through the whole kd-tree, until an object is intersected or ray leaves the scene */
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   239
	Float splitVal;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   240
	int axis;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   241
	static const int mod3[] = {0,1,2,0,1};
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   242
	const Vector3 invdir = 1 / ray.dir;
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   243
	while (node)
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   244
	{
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   245
		/* loop until a leaf is found */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   246
		while (!node->isLeaf())
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   247
		{
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   248
			/* retrieve position of splitting plane */
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   249
			splitVal = node->getSplit();
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   250
			axis = node->getAxis();
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   251
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   252
			if (stack[entry].pb[axis] <= splitVal)
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   253
			{
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   254
				if (stack[exit].pb[axis] <= splitVal)
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   255
				{ /* case N1, N2, N3, P5, Z2, and Z3 */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   256
					node = node->getLeftChild();
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   257
					continue;
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   258
				}
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   259
				if (stack[entry].pb[axis] == splitVal)
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   260
				{ /* case Z1 */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   261
					node = node->getRightChild();
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   262
					continue;
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   263
				}
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   264
				/* case N4 */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   265
				farchild = node->getRightChild();
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   266
				node = node->getLeftChild();
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   267
			}
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   268
			else
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   269
			{ /* (stack[entry].pb[axis] > splitVal) */
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   270
				if (stack[exit].pb[axis] > splitVal)
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   271
				{
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   272
					/* case P1, P2, P3, and N5 */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   273
					node = node->getRightChild();
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   274
					continue;
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   275
				}
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   276
				/* case P4 */
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   277
				farchild = node->getLeftChild();
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   278
				node = node->getRightChild();
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   279
			}
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   280
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   281
			/* case P4 or N4 . . . traverse both children */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   282
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   283
			/* signed distance to the splitting plane */
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   284
			t = (splitVal - ray.o[axis]) * invdir[axis];
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   285
16
20bceb605f48 add Raytracer::setThreads()
Radek Brich <radek.brich@devl.cz>
parents: 15
diff changeset
   286
			/* setup the new exit point and push it onto stack */
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   287
			register int tmp = exit;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   288
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   289
			exit++;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   290
			if (exit == entry)
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   291
				exit++;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   292
			assert(exit < max_depth);
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   293
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   294
			stack[exit].prev = tmp;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   295
			stack[exit].t = t;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   296
			stack[exit].node = farchild;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   297
			stack[exit].pb.cell[axis] = splitVal;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   298
			stack[exit].pb.cell[mod3[axis+1]] = ray.o.cell[mod3[axis+1]]
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   299
				+ t * ray.dir.cell[mod3[axis+1]];
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   300
			stack[exit].pb.cell[mod3[axis+2]] = ray.o.cell[mod3[axis+2]]
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   301
				+ t * ray.dir.cell[mod3[axis+2]];
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   302
		}
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   303
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   304
		/* current node is the leaf . . . empty or full */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   305
		Shape *nearest_shape = NULL;
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   306
		Float dist = stack[exit].t;
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   307
		ShapeList::iterator shape;
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   308
		for (shape = node->getShapes()->begin(); shape != node->getShapes()->end(); shape++)
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   309
			if (*shape != origin_shape && (*shape)->intersect(ray, dist)
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   310
			&& dist >= stack[entry].t - Eps)
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   311
			{
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   312
				nearest_shape = *shape;
35
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   313
				nearest_distance = dist;
fb170fccb19f new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
   314
			}
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   315
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   316
		if (nearest_shape)
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   317
			return nearest_shape;
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   318
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   319
		entry = exit;
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   320
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   321
		/* retrieve the pointer to the next node,
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   322
		it is possible that ray traversal terminates */
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   323
		node = stack[entry].node;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   324
		exit = stack[entry].prev;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   325
	}
14
fc18ac4833f2 replace Plane with axis-aligned Box (because infinite Plane is not usable with kd-tree)
Radek Brich <radek.brich@devl.cz>
parents: 13
diff changeset
   326
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   327
	/* ray leaves the scene */
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   328
	return NULL;
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   329
}
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   330
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   331
// stack element for kd-tree traversal (packet version)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   332
struct StackElem4
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   333
{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   334
	KdNode* node; /* pointer to far child */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   335
	__m128 t; /* the entry/exit signed distance */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   336
	VectorPacket pb; /* the coordinates of entry/exit point */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   337
	int prev;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   338
};
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   339
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   340
void KdTree::packet_intersection(const Shape **origin_shapes, const RayPacket &rays,
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   341
		Float *nearest_distances, Shape **nearest_shapes)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   342
{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   343
	__m128 a, b; /* entry/exit signed distance */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   344
	__m128 t;    /* signed distance to the splitting plane */
86
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   345
	__m128 mask = mZero;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   346
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   347
	/* if we have no tree, fall back to naive test */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   348
	if (!built)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   349
		Container::packet_intersection(origin_shapes, rays, nearest_distances, nearest_shapes);
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   350
86
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   351
	// nearest_shapes[0..4] = NULL
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   352
	memset(nearest_shapes, 0, 4*sizeof(Shape*));
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   353
86
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   354
	mask = bbox.intersect_packet(rays, a, b);
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   355
	if (!_mm_movemask_ps(mask))
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   356
		return;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   357
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   358
	/* pointers to the far child node and current node */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   359
	KdNode *farchild, *node;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   360
	node = root;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   361
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   362
	StackElem4 stack[max_depth];
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   363
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   364
	int entry = 0, exit = 1;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   365
	stack[entry].t = a;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   366
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   367
	/* distinguish between internal and external origin of a ray*/
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   368
	t = _mm_cmplt_ps(a, mZero);
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   369
	stack[entry].pb = rays.o + rays.dir * a;
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   370
	stack[entry].pb.mx = _mm_or_ps(_mm_andnot_ps(t, stack[entry].pb.mx),
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   371
		_mm_and_ps(t, rays.o.mx));
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   372
	stack[entry].pb.my = _mm_or_ps(_mm_andnot_ps(t, stack[entry].pb.my),
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   373
		_mm_and_ps(t, rays.o.my));
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   374
	stack[entry].pb.mz = _mm_or_ps(_mm_andnot_ps(t, stack[entry].pb.mz),
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   375
		_mm_and_ps(t, rays.o.mz));
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   376
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   377
	/* setup initial exit point in the stack */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   378
	stack[exit].t = b;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   379
	stack[exit].pb = rays.o + rays.dir * b;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   380
	stack[exit].node = NULL;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   381
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   382
	/* loop, traverse through the whole kd-tree,
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   383
	until an object is intersected or ray leaves the scene */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   384
	__m128 splitVal;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   385
	int axis;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   386
	static const int mod3[] = {0,1,2,0,1};
86
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   387
	const VectorPacket invdirs = mOne / rays.dir;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   388
	while (node)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   389
	{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   390
		/* loop until a leaf is found */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   391
		while (!node->isLeaf())
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   392
		{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   393
			/* retrieve position of splitting plane */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   394
			splitVal = _mm_set_ps1(node->getSplit());
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   395
			axis = node->getAxis();
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   396
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   397
			// mask out invalid rays with near > far
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   398
			const __m128 curmask = _mm_and_ps(mask, _mm_cmple_ps(stack[entry].t, stack[exit].t));
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   399
			const __m128 entry_lt = _mm_cmplt_ps(stack[entry].pb.ma[axis], splitVal);
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   400
			const __m128 entry_gt = _mm_cmpgt_ps(stack[entry].pb.ma[axis], splitVal);
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   401
			const __m128 exit_lt = _mm_cmplt_ps(stack[exit].pb.ma[axis], splitVal);
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   402
			const __m128 exit_gt = _mm_cmpgt_ps(stack[exit].pb.ma[axis], splitVal);
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   403
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   404
			// if all of
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   405
			// stack[entry].pb[axis] <= splitVal,
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   406
			// stack[exit].pb[axis] <= splitVal
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   407
			if (!_mm_movemask_ps(
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   408
				_mm_and_ps(_mm_or_ps(entry_gt, exit_gt), curmask)))
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   409
			{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   410
				node = node->getLeftChild();
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   411
				continue;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   412
			}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   413
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   414
			// if all of
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   415
			// stack[entry].pb[axis] >= splitVal,
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   416
			// stack[exit].pb[axis] >= splitVal
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   417
			if (!_mm_movemask_ps(
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   418
				_mm_and_ps(_mm_or_ps(entry_lt, exit_lt), curmask)))
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   419
			{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   420
				node = node->getRightChild();
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   421
				continue;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   422
			}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   423
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   424
			// any of
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   425
			// stack[entry].pb[axis] < splitVal,
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   426
			// stack[exit].pb[axis] > splitVal
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   427
			bool cond1 = _mm_movemask_ps(
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   428
				_mm_and_ps(_mm_and_ps(entry_lt, exit_gt), curmask));
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   429
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   430
			// any of
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   431
			// stack[entry].pb[axis] > splitVal,
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   432
			// stack[exit].pb[axis] < splitVal
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   433
			bool cond2 = _mm_movemask_ps(
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   434
				_mm_and_ps(_mm_and_ps(entry_gt, exit_lt), curmask));
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   435
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   436
			if ((!cond1 && !cond2) || (cond1 && cond2))
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   437
			{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   438
				// fall back to single rays
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   439
				// FIXME: split rays and continue
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   440
				for (int i = 0; i < 4; i++)
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
   441
					if (!nearest_shapes[i])
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   442
						nearest_shapes[i] = nearest_intersection(origin_shapes[i],
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   443
							rays[i], nearest_distances[i]);
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   444
				return;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   445
			}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   446
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   447
			if (cond1)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   448
			{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   449
				farchild = node->getRightChild();
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   450
				node = node->getLeftChild();
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   451
			}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   452
			else
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   453
			{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   454
				farchild = node->getLeftChild();
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   455
				node = node->getRightChild();
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   456
			}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   457
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   458
			/* traverse both children */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   459
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   460
			/* signed distance to the splitting plane */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   461
			t = _mm_mul_ps(_mm_sub_ps(splitVal, rays.o.ma[axis]), invdirs.ma[axis]);
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   462
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   463
			/* setup the new exit point and push it onto stack */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   464
			register int tmp = exit;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   465
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   466
			exit++;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   467
			if (exit == entry)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   468
				exit++;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   469
			assert(exit < max_depth);
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   470
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   471
			stack[exit].prev = tmp;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   472
			stack[exit].t = t;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   473
			stack[exit].node = farchild;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   474
			stack[exit].pb.ma[axis] = splitVal;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   475
			stack[exit].pb.ma[mod3[axis+1]] =
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   476
				_mm_add_ps(rays.o.ma[mod3[axis+1]], _mm_mul_ps(t, rays.dir.ma[mod3[axis+1]]));
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   477
			stack[exit].pb.ma[mod3[axis+2]] =
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   478
				_mm_add_ps(rays.o.ma[mod3[axis+2]], _mm_mul_ps(t, rays.dir.ma[mod3[axis+2]]));
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   479
		}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   480
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   481
		/* current node is the leaf . . . empty or full */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   482
		__m128 dists = stack[exit].t;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   483
		ShapeList::iterator shape;
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
   484
		__m128 results;
86
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   485
		__m128 newmask = mask;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   486
		for (shape = node->getShapes()->begin(); shape != node->getShapes()->end(); shape++)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   487
		{
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
   488
			results = (*shape)->intersect_packet(rays, dists);
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
   489
			int valid = _mm_movemask_ps(
87
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   490
				_mm_and_ps(mask, _mm_and_ps(results,
1081e3dd3f3e Sphere, Box - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 86
diff changeset
   491
				_mm_cmpge_ps(dists, _mm_sub_ps(stack[entry].t, mEps)))));
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   492
			for (int i = 0; i < 4; i++)
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   493
			{
85
907a634e5c02 implement triangle packet intersection
Radek Brich <radek.brich@devl.cz>
parents: 84
diff changeset
   494
				if (*shape != origin_shapes[i] && ((valid>>i)&1))
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   495
				{
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   496
					nearest_shapes[i] = *shape;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   497
					nearest_distances[i] = ((float*)&dists)[i];
86
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   498
					((int*)&newmask)[i] = 0;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   499
				}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   500
			}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   501
		}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   502
86
ce6abe0aeeae BBox - RayPacket intersection
Radek Brich <radek.brich@devl.cz>
parents: 85
diff changeset
   503
		mask = newmask;
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   504
		if (!_mm_movemask_ps(mask))
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   505
			return;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   506
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   507
		entry = exit;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   508
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   509
		/* retrieve the pointer to the next node,
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   510
		it is possible that ray traversal terminates */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   511
		node = stack[entry].node;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   512
		exit = stack[entry].prev;
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   513
	}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   514
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   515
	/* ray leaves the scene */
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   516
}
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 80
diff changeset
   517
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   518
ostream & operator<<(ostream &st, KdNode &node)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   519
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   520
	if (node.isLeaf())
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   521
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   522
		st << "(l," << node.getShapes()->size();
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   523
		ShapeList::iterator shape;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   524
		for (shape = node.getShapes()->begin(); shape != node.getShapes()->end(); shape++)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   525
			st << "," << shape_index[*shape];
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   526
		st << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   527
	}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   528
	else
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   529
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   530
		st << "(s," << (char)('x'+node.getAxis()) << "," << node.getSplit() << ",";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   531
		st << *node.getLeftChild() << ",";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   532
		st << *node.getRightChild() << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   533
	}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   534
	return st;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   535
}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   536
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   537
ostream & KdTree::dump(ostream &st)
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   538
{
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   539
	if (!built)
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   540
		return Container::dump(st);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   541
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   542
	st << "(kdtree," << shapes.size();
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   543
	ShapeList::iterator shape;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   544
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   545
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   546
		int idx;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   547
		if (!shape_index.get(*shape, idx))
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   548
			st << "," << endl << **shape;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   549
	}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   550
	return st << "," << endl << *getRootNode() << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   551
}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   552
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   553
void KdTree::recursive_load(istream &st, KdNode *node)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   554
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   555
	string s;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   556
	istringstream is;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   557
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   558
	getline(st, s, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   559
	trim(s);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   560
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   561
	if (s.compare("(s") == 0)
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   562
	{
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   563
		// split
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   564
		int axis;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   565
		Float split;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   566
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   567
		delete node->getShapes();
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   568
		node->setChildren(new KdNode[2]);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   569
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   570
		getline(st, s, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   571
		axis = s.c_str()[0]-'x';
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   572
		node->setAxis(axis);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   573
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   574
		st >> split;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   575
		getline(st, s, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   576
		node->setSplit(split);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   577
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   578
		recursive_load(st, node->getLeftChild());
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   579
		getline(st, s, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   580
		recursive_load(st, node->getRightChild());
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   581
		getline(st, s, ')');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   582
	}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   583
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   584
	if (s.compare("(l") == 0)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   585
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   586
		// leaf
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   587
		int count, idx;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   588
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   589
		node->setLeaf();
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   590
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   591
		st >> count;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   592
		for (int i = 0; i < count; i++)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   593
		{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   594
			getline(st, s, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   595
			st >> idx;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   596
			node->addShape(shapes[idx]);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   597
		}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   598
		getline(st, s, ')');
10
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   599
	}
f9fad94cd0cc kd-tree: build algorithm tested and fixed
Radek Brich <radek.brich@devl.cz>
parents: 9
diff changeset
   600
}
11
4d192e13ee84 move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   601
80
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   602
istream & KdTree::load(istream &st, Material *mat)
11
4d192e13ee84 move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   603
{
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   604
	string s;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   605
	istringstream is;
12
f4fcabf05785 kd-tree: traversal algorithm (KdTree::nearest_intersection)
Radek Brich <radek.brich@devl.cz>
parents: 11
diff changeset
   606
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   607
	getline(st, s, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   608
	if (s.compare("(kdtree") != 0)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   609
		return st;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   610
80
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   611
	dbgmsg(1, "* loading kd-tree\n");
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   612
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   613
	shapes.clear();
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   614
	if (root) delete root;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   615
	root = new KdNode();
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   616
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   617
	getline(st, s, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   618
	int shape_count;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   619
	is.str(s);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   620
	is >> shape_count;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   621
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   622
	Shape *shape;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   623
	for (int i = 0; i < shape_count; i++)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   624
	{
80
907929fa9b59 remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
   625
		shape = loadShape(st, mat);
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   626
		Container::addShape(shape);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   627
		getline(st, s, ',');
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   628
	}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   629
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   630
	recursive_load(st, root);
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   631
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   632
	built = true;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 76
diff changeset
   633
	return st;
11
4d192e13ee84 move nearest_intersection() to Container, add dummy KdTree.load(), plus small fixes
Radek Brich <radek.brich@devl.cz>
parents: 10
diff changeset
   634
}