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