author | Radek Brich <radek.brich@devl.cz> |
Thu, 15 May 2008 00:07:25 +0200 | |
branch | pyrit |
changeset 96 | 9eb71e76c7fd |
parent 95 | ca7d4c665531 |
permissions | -rw-r--r-- |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
1 |
/* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
2 |
* serialize.cc: object serialization functions |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
3 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
4 |
* This file is part of Pyrit Ray Tracer. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
5 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
6 |
* Copyright 2008 Radek Brich |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
7 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
9 |
* of this software and associated documentation files (the "Software"), to deal |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
10 |
* in the Software without restriction, including without limitation the rights |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
12 |
* copies of the Software, and to permit persons to whom the Software is |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
13 |
* furnished to do so, subject to the following conditions: |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
14 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
15 |
* The above copyright notice and this permission notice shall be included in |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
16 |
* all copies or substantial portions of the Software. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
17 |
* |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
24 |
* THE SOFTWARE. |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
25 |
*/ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
26 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
27 |
#include "serialize.h" |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
28 |
#include <vector> |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
#include <string> |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
#include <sstream> |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
Indexer vertex_index, shape_index; |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
33 |
vector<Vertex*> vertices; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
34 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
35 |
void resetSerializer() |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
vertex_index.reset(); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
shape_index.reset(); |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
39 |
vertices.clear(); |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
40 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
41 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
42 |
bool Indexer::get(const void *o, int &retidx) |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
{ |
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
44 |
map <const void *, int>::iterator i; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
i = indexmap.find(o); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
if (i == indexmap.end()) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
retidx = index++; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
indexmap[o] = retidx; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
return false; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
else |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
retidx = i->second; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
return true; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
|
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
59 |
Shape *loadShape(istream &st, Material *mat) |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
string s; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
istringstream is; |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
63 |
for (;;) |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
getline(st, s, ','); |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
66 |
trim(s); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
67 |
|
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
68 |
// Vertex |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
69 |
if (s.compare("(v") == 0) |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
70 |
{ |
91 | 71 |
Vector P; |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
72 |
st >> P; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
73 |
getline(st, s, ')'); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
74 |
vertices.push_back(new Vertex(P)); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
75 |
getline(st, s, ','); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
76 |
continue; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
77 |
} |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
78 |
|
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
79 |
// NormalVertex |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
80 |
if (s.compare("(vn") == 0) |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
81 |
{ |
91 | 82 |
Vector P,N; |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
83 |
st >> P; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
84 |
getline(st, s, ','); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
85 |
st >> N; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
86 |
getline(st, s, ')'); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
87 |
vertices.push_back(new NormalVertex(P,N)); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
88 |
getline(st, s, ','); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
89 |
continue; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
90 |
} |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
91 |
|
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
92 |
// Triangle |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
93 |
if (s.compare("(t") == 0) |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
94 |
{ |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
95 |
int a,b,c; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
96 |
st >> a; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
97 |
getline(st, s, ','); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
98 |
st >> b; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
99 |
getline(st, s, ','); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
100 |
st >> c; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
101 |
getline(st, s, ')'); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
102 |
return new Triangle(vertices[a], vertices[b], vertices[c], mat); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
103 |
} |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
104 |
|
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
105 |
// box |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
106 |
if (s.compare("(box") == 0) |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
107 |
{ |
91 | 108 |
Vector L,H; |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
109 |
st >> L; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
110 |
getline(st, s, ','); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
111 |
st >> H; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
112 |
getline(st, s, ')'); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
113 |
return new Box(L, H, mat); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
114 |
} |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
115 |
|
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
116 |
// Sphere |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
117 |
if (s.compare("(sphere") == 0) |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
118 |
{ |
91 | 119 |
Vector center; |
80
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
120 |
Float radius; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
121 |
st >> center; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
122 |
getline(st, s, ','); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
123 |
st >> radius; |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
124 |
getline(st, s, ')'); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
125 |
return new Sphere(center, radius, mat); |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
126 |
} |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
127 |
|
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
128 |
// else... |
907929fa9b59
remove forgotten noise.h includes
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
129 |
return NULL; |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
130 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
131 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
132 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
133 |
ostream & operator<<(ostream &st, const Shape &o) |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
134 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
135 |
return o.dump(st); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
136 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
137 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
138 |
ostream & operator<<(ostream &st, const Vertex &o) |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
139 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
140 |
return o.dump(st); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
141 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
142 |
|
95
ca7d4c665531
build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
143 |
ostream & operator<<(ostream &st, const Container &o) |
78
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
144 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
145 |
return o.dump(st); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
146 |
} |