src/serialize.cc
branchpyrit
changeset 78 9569e9f35374
child 80 907929fa9b59
equal deleted inserted replaced
77:dbe8438d5dca 78:9569e9f35374
       
     1 /*
       
     2  * serialize.cc: object serialization functions
       
     3  *
       
     4  * This file is part of Pyrit Ray Tracer.
       
     5  *
       
     6  * Copyright 2008  Radek Brich
       
     7  *
       
     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.
       
    25  */
       
    26 
       
    27 #include "serialize.h"
       
    28 #include <string>
       
    29 #include <sstream>
       
    30 
       
    31 Indexer vertex_index, shape_index;
       
    32 
       
    33 void resetSerializer()
       
    34 {
       
    35 	vertex_index.reset();
       
    36 	shape_index.reset();
       
    37 }
       
    38 
       
    39 bool Indexer::get(void *o, int &retidx)
       
    40 {
       
    41 	map <void *, int>::iterator i;
       
    42 	i = indexmap.find(o);
       
    43 	if (i == indexmap.end())
       
    44 	{
       
    45 		retidx = index++;
       
    46 		indexmap[o] = retidx;
       
    47 		return false;
       
    48 	}
       
    49 	else
       
    50 	{
       
    51 		retidx = i->second;
       
    52 		return true;
       
    53 	}
       
    54 }
       
    55 
       
    56 Shape *loadShape(istream &st)
       
    57 {
       
    58 	string s;
       
    59 	istringstream is;
       
    60 	getline(st, s, ',');
       
    61 	trim(s);
       
    62 	if (s.compare("(box") == 0)
       
    63 	{
       
    64 		Vector3 L,H;
       
    65 		st >> L;
       
    66 		getline(st, s, ',');
       
    67 		st >> H;
       
    68 		getline(st, s, ')');
       
    69 		return new Box(L, H, new Material(Colour(1,1,1)));
       
    70 	}
       
    71 	if (s.compare("(sphere") == 0)
       
    72 	{
       
    73 		Vector3 center;
       
    74 		Float radius;
       
    75 		st >> center;
       
    76 		getline(st, s, ',');
       
    77 		st >> radius;
       
    78 		getline(st, s, ')');
       
    79 		return new Sphere(center, radius, new Material(Colour(1,1,1)));
       
    80 	}
       
    81 	return NULL;
       
    82 }
       
    83 
       
    84 ostream & operator<<(ostream &st, Shape &o)
       
    85 {
       
    86 	return o.dump(st);
       
    87 }
       
    88 
       
    89 ostream & operator<<(ostream &st, Vertex &o)
       
    90 {
       
    91 	return o.dump(st);
       
    92 }
       
    93 
       
    94 ostream & operator<<(ostream &st, Container &o)
       
    95 {
       
    96 	return o.dump(st);
       
    97 }