]> git.tdb.fi Git - r2c2.git/blob - source/3d/layout.cpp
Add View3D class to bundle Layout3D with a camera and a pipeline
[r2c2.git] / source / 3d / layout.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include "layout.h"
9 #include "track.h"
10 #include "vehicle.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace R2C2 {
16
17 Layout3D::Layout3D(Layout &l):
18         layout(l),
19         catalogue(layout.get_catalogue())
20 {
21         // South, 15° from zenith
22         sun.set_position(0, -0.259, 0.966, 0);
23         lighting.attach(0, sun);
24
25         layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added));
26         layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed));
27         layout.signal_vehicle_added.connect(sigc::mem_fun(this, &Layout3D::vehicle_added));
28         layout.signal_vehicle_removed.connect(sigc::mem_fun(this, &Layout3D::vehicle_removed));
29
30         const set<Track *> &ltracks = layout.get_tracks();
31         for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
32                 track_added(**i);
33 }
34
35 Layout3D::~Layout3D()
36 {
37         while(!tracks.empty())
38                 delete tracks.begin()->second;
39         while(!vehicles.empty())
40                 delete vehicles.begin()->second;
41 }
42
43 void Layout3D::get_bounds(Vector &minp, Vector &maxp) const
44 {
45         minp = maxp = Vector();
46
47         for(TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
48         {
49                 Vector tmin;
50                 Vector tmax;
51                 i->second->get_bounds(0, tmin, tmax);
52                 minp.x = min(minp.x, tmin.x);
53                 minp.y = min(minp.y, tmin.y);
54                 maxp.x = max(maxp.x, tmax.x);
55                 maxp.y = max(maxp.y, tmax.y);
56         }
57 }
58
59 void Layout3D::add_track(Track3D &t)
60 {
61         if(tracks.count(&t.get_track()))
62                 throw KeyError("Duplicate track");
63
64         tracks[&t.get_track()] = &t;
65 }
66
67 void Layout3D::remove_track(Track3D &t)
68 {
69         tracks.erase(&t.get_track());
70 }
71
72 Track3D &Layout3D::get_track(Track &t) const
73 {
74         TrackMap::const_iterator i = tracks.find(&t);
75         if(i==tracks.end())
76                 throw KeyError("Unknown track");
77
78         return *i->second;
79 }
80
81 void Layout3D::add_vehicle(Vehicle3D &v)
82 {
83         if(vehicles.count(&v.get_vehicle()))
84                 throw KeyError("Duplicate vehicle");
85
86         vehicles[&v.get_vehicle()] = &v;
87 }
88
89 void Layout3D::remove_vehicle(Vehicle3D &v)
90 {
91         vehicles.erase(&v.get_vehicle());
92 }
93
94 Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const
95 {
96         VehicleMap::const_iterator i = vehicles.find(&v);
97         if(i==vehicles.end())
98                 throw KeyError("Unknown vehicle");
99
100         return *i->second;
101 }
102
103 void Layout3D::track_added(Track &t)
104 {
105         new Track3D(*this, t);
106 }
107
108 void Layout3D::track_removed(Track &t)
109 {
110         TrackMap::iterator i = tracks.find(&t);
111         if(i!=tracks.end())
112                 delete i->second;
113 }
114
115 void Layout3D::vehicle_added(Vehicle &v)
116 {
117         new Vehicle3D(*this, v);
118 }
119
120 void Layout3D::vehicle_removed(Vehicle &v)
121 {
122         VehicleMap::iterator i = vehicles.find(&v);
123         if(i!=vehicles.end())
124                 delete i->second;
125 }
126
127 } // namespace R2C2