author | Radek Brich <radek.brich@devl.cz> |
Sat, 10 May 2008 14:29:37 +0200 | |
branch | pyrit |
changeset 95 | ca7d4c665531 |
parent 92 | 9af5c039b678 |
permissions | -rw-r--r-- |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
28
diff
changeset
|
1 |
/* |
44 | 2 |
* container.cc: Container class |
3 |
* |
|
4 |
* This file is part of Pyrit Ray Tracer. |
|
5 |
* |
|
91 | 6 |
* Copyright 2007, 2008 Radek Brich |
34
28f6e8b9d5d1
quaternion moved to extra header file
Radek Brich <radek.brich@devl.cz>
parents:
28
diff
changeset
|
7 |
* |
44 | 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9 |
* of this software and associated documentation files (the "Software"), to deal |
|
10 |
* in the Software without restriction, including without limitation the rights |
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12 |
* copies of the Software, and to permit persons to whom the Software is |
|
13 |
* furnished to do so, subject to the following conditions: |
|
14 |
* |
|
15 |
* The above copyright notice and this permission notice shall be included in |
|
16 |
* all copies or substantial portions of the Software. |
|
17 |
* |
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
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 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
92
diff
changeset
|
31 |
void Container::addShape(const Shape* aShape) |
24
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(); |
91 | 37 |
const Vector E(e, e, e); |
74
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 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
92
diff
changeset
|
52 |
const Shape *Container::nearest_intersection(const Shape *origin_shape, const Ray &ray, |
24
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 |
{ |
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
92
diff
changeset
|
55 |
const Shape *nearest_shape = NULL; |
28
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 |
|
92
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
63 |
#ifndef NO_SIMD |
9af5c039b678
add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
64 |
void Container::packet_intersection(const Shape* const* origin_shapes, const RayPacket &rays, |
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
92
diff
changeset
|
65 |
Float *nearest_distances, const Shape **nearest_shapes) |
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
66 |
{ |
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
67 |
for (int i = 0; i < 4; i++) |
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
68 |
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
|
69 |
nearest_distances[i]); |
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
70 |
} |
91 | 71 |
#endif |
82
930a2d3ecaed
prepare structures for packet tracing
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
72 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
92
diff
changeset
|
73 |
ostream & Container::dump(ostream &st) const |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
74 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
75 |
st << "(container," << shapes.size(); |
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
92
diff
changeset
|
76 |
ShapeList::const_iterator shape; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
77 |
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
|
78 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
79 |
int idx; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
80 |
if (!shape_index.get(*shape, idx)) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
81 |
st << "," << **shape; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
82 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
83 |
return st << ")"; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
74
diff
changeset
|
84 |
} |