author | Radek Brich <radek.brich@devl.cz> |
Wed, 23 Apr 2008 10:38:33 +0200 | |
branch | pyrit |
changeset 78 | 9569e9f35374 |
child 80 | 907929fa9b59 |
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" |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
28 |
#include <string> |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
29 |
#include <sstream> |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
30 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
31 |
Indexer vertex_index, shape_index; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
32 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
33 |
void resetSerializer() |
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 |
vertex_index.reset(); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
36 |
shape_index.reset(); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
37 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
38 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
39 |
bool Indexer::get(void *o, int &retidx) |
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 |
map <void *, int>::iterator i; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
42 |
i = indexmap.find(o); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
43 |
if (i == indexmap.end()) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
44 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
45 |
retidx = index++; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
46 |
indexmap[o] = retidx; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
47 |
return false; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
48 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
49 |
else |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
retidx = i->second; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
return true; |
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 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
Shape *loadShape(istream &st) |
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 |
string s; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
istringstream is; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
getline(st, s, ','); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
trim(s); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
if (s.compare("(box") == 0) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
Vector3 L,H; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
st >> L; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
getline(st, s, ','); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
st >> H; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
getline(st, s, ')'); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
return new Box(L, H, new Material(Colour(1,1,1))); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
70 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
71 |
if (s.compare("(sphere") == 0) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
72 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
73 |
Vector3 center; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
74 |
Float radius; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
75 |
st >> center; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
76 |
getline(st, s, ','); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
st >> radius; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
getline(st, s, ')'); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
return new Sphere(center, radius, new Material(Colour(1,1,1))); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
return NULL; |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
83 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
84 |
ostream & operator<<(ostream &st, Shape &o) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
85 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
86 |
return o.dump(st); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
ostream & operator<<(ostream &st, Vertex &o) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
return o.dump(st); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
92 |
} |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
93 |
|
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
ostream & operator<<(ostream &st, Container &o) |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
95 |
{ |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
96 |
return o.dump(st); |
9569e9f35374
move shapes to extra source file
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
97 |
} |