]> git.tdb.fi Git - r2c2.git/blob - source/3d/layout.cpp
db419d3639065c2e76a574702ebfe062085b6eae
[r2c2.git] / source / 3d / layout.cpp
1 #include "layout.h"
2 #include "signal.h"
3 #include "track.h"
4 #include "vehicle.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 namespace R2C2 {
10
11 Layout3D::Layout3D(Layout &l):
12         layout(l),
13         catalogue(layout.get_catalogue())
14 {
15         // South, 15° from zenith
16         sun.set_position(0, -0.259, 0.966, 0);
17         lighting.attach(0, sun);
18
19         layout.signal_object_added.connect(sigc::mem_fun(this, &Layout3D::object_added));
20         layout.signal_object_removed.connect(sigc::mem_fun(this, &Layout3D::object_removed));
21
22         const set<Object *> &lobjs = layout.get_all<Object>();
23         for(set<Object *>::iterator i=lobjs.begin(); i!=lobjs.end(); ++i)
24                 object_added(**i);
25 }
26
27 Layout3D::~Layout3D()
28 {
29         while(!objects.empty())
30                 delete objects.begin()->second;
31 }
32
33 void Layout3D::get_bounds(Vector &minp, Vector &maxp) const
34 {
35         Geometry::BoundingBox<float, 3> bbox;
36
37         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
38                 bbox = bbox|i->second->get_object().get_type().get_shape()->get_axis_aligned_bounding_box();
39
40         minp = bbox.get_minimum_point();
41         maxp = bbox.get_maximum_point();
42 }
43
44 void Layout3D::add(Object3D &o)
45 {
46         insert_unique(objects, &o.get_object(), &o);
47 }
48
49 Object3D &Layout3D::get(Object &o) const
50 {
51         return *get_item(objects, &o);
52 }
53
54 void Layout3D::remove(Object3D &o)
55 {
56         objects.erase(&o.get_object());
57 }
58
59 void Layout3D::object_added(Object &o)
60 {
61         if(Track *t = dynamic_cast<Track *>(&o))
62                 new Track3D(*this, *t);
63         else if(Signal *s = dynamic_cast<Signal *>(&o))
64                 new Signal3D(*this, *s);
65         else if(Vehicle *v = dynamic_cast<Vehicle *>(&o))
66                 new Vehicle3D(*this, *v);
67 }
68
69 void Layout3D::object_removed(Object &o)
70 {
71         ObjectMap::iterator i = objects.find(&o);
72         if(i!=objects.end())
73                 delete i->second;
74 }
75
76 } // namespace R2C2