src/container.cc
author Radek Brich <radek.brich@devl.cz>
Sun, 27 Apr 2008 14:19:37 +0200
branchpyrit
changeset 85 907a634e5c02
parent 84 6f7fe14782c2
child 91 9d66d323c354
permissions -rw-r--r--
implement triangle packet intersection
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: 28
diff changeset
     1
/*
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     2
 * container.cc: Container class
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     3
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     4
 * This file is part of Pyrit Ray Tracer.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     5
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
     6
 * Copyright 2007  Radek Brich
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
     7
 *
44
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
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: 34
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: 34
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: 34
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: 34
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: 34
diff changeset
    13
 * furnished to do so, subject to the following conditions:
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    14
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
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: 34
diff changeset
    16
 * all copies or substantial portions of the Software.
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
diff changeset
    17
 *
3763b26244f0 MIT license for sources
Radek Brich <radek.brich@devl.cz>
parents: 34
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: 34
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: 34
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: 34
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: 34
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: 34
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: 34
diff changeset
    24
 * THE SOFTWARE.
34
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
    25
 */
28f6e8b9d5d1 quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents: 28
diff changeset
    26
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
#include "common.h"
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
#include "container.h"
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    29
#include "serialize.h"
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
void Container::addShape(Shape* aShape)
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
{
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    33
	const Float e = Eps;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    34
	if (shapes.size() == 0) {
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    35
		/* initialize bounding box */
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    36
		bbox = aShape->get_bbox();
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    37
		const Vector3 E(e, e, e);
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    38
		bbox = BBox(bbox.L - E, bbox.H + E);
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    39
	} else {
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    40
		/* adjust bounding box */
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    41
		BBox shapebb = aShape->get_bbox();
74
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    42
		if (shapebb.L.x - e < bbox.L.x)  bbox.L.x = shapebb.L.x - e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    43
		if (shapebb.L.y - e < bbox.L.y)  bbox.L.y = shapebb.L.y - e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    44
		if (shapebb.L.z - e < bbox.L.z)  bbox.L.z = shapebb.L.z - e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    45
		if (shapebb.H.x + e > bbox.H.x)  bbox.H.x = shapebb.H.x + e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    46
		if (shapebb.H.y + e > bbox.H.y)  bbox.H.y = shapebb.H.y + e;
09aedbf5f95f kd-tree traversal - avoid dynamic memory allocation
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
    47
		if (shapebb.H.z + e > bbox.H.z)  bbox.H.z = shapebb.H.z + e;
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    48
	}
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    49
	shapes.push_back(aShape);
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
};
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
Shape *Container::nearest_intersection(const Shape *origin_shape, const Ray &ray,
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        Float &nearest_distance)
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
{
28
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    55
	Shape *nearest_shape = NULL;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    56
	ShapeList::iterator shape;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    57
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    58
		if (*shape != origin_shape && (*shape)->intersect(ray, nearest_distance))
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    59
			nearest_shape = *shape;
ffe83ca074f3 smooth triangles (aka Phong shading)
Radek Brich <radek.brich@devl.cz>
parents: 24
diff changeset
    60
	return nearest_shape;
24
d0d76e8a5203 new C++ demo: realtime_dragon.cc
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
}
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    62
84
6f7fe14782c2 prepare kd-tree traversal for packet tracing (4 rays at once)
Radek Brich <radek.brich@devl.cz>
parents: 82
diff changeset
    63
void Container::packet_intersection(const Shape **origin_shapes, const RayPacket &rays,
82
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    64
	Float *nearest_distances, Shape **nearest_shapes)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    65
{
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    66
	for (int i = 0; i < 4; i++)
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    67
		nearest_shapes[i] = nearest_intersection(origin_shapes[i], rays[i],
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    68
			nearest_distances[i]);
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    69
}
930a2d3ecaed prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents: 78
diff changeset
    70
78
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    71
ostream & Container::dump(ostream &st)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    72
{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    73
	st << "(container," << shapes.size();
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    74
	ShapeList::iterator shape;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    75
	for (shape = shapes.begin(); shape != shapes.end(); shape++)
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    76
	{
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    77
		int idx;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    78
		if (!shape_index.get(*shape, idx))
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    79
			st << "," << **shape;
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    80
	}
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    81
	return st << ")";
9569e9f35374 move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents: 74
diff changeset
    82
}