]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
Add basic support for signals
[r2c2.git] / source / 3d / catalogue.cpp
1 #include <msp/fs/stat.h>
2 #include <msp/gl/meshbuilder.h>
3 #include <msp/gl/object.h>
4 #include <msp/gl/program.h>
5 #include <msp/gl/technique.h>
6 #include "catalogue.h"
7 #include "signaltype.h"
8 #include "tracktype.h"
9 #include "vehicletype.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 Catalogue3D::Catalogue3D(Catalogue &c):
17         catalogue(c),
18         endpoint_mesh((GL::NORMAL3, GL::VERTEX3))
19 {
20         add_type<GL::Material>().creator(&Catalogue3D::create<GL::Material>);
21         add_type<GL::Mesh>().creator(&Catalogue3D::create<GL::Mesh>);
22         add_type<GL::Object>().creator(&Catalogue3D::create<GL::Object>);
23         add_type<GL::Program>().creator(&Catalogue3D::create<GL::Program>);
24         add_type<GL::Technique>().creator(&Catalogue3D::create<GL::Technique>);
25
26         catalogue.signal_track_added.connect(sigc::mem_fun(this, &Catalogue3D::track_added));
27         catalogue.signal_vehicle_added.connect(sigc::mem_fun(this, &Catalogue3D::vehicle_added));
28         catalogue.signal_signal_added.connect(sigc::mem_fun(this, &Catalogue3D::signal_added));
29
30         const Catalogue::TrackMap &trks = catalogue.get_tracks();
31         for(Catalogue::TrackMap::const_iterator i=trks.begin(); i!=trks.end(); ++i)
32                 track_added(*i->second);
33         const Catalogue::SignalMap &sigs = catalogue.get_signals();
34         for(Catalogue::SignalMap::const_iterator i=sigs.begin(); i!=sigs.end(); ++i)
35                 signal_added(*i->second);
36
37         build_endpoint_mesh();
38 }
39
40 Catalogue3D::~Catalogue3D()
41 {
42         for(map<const TrackType *, TrackType3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
43                 delete i->second;
44         for(map<const VehicleType *, VehicleType3D *>::iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
45                 delete i->second;
46         for(map<const SignalType *, SignalType3D *>::iterator i=signals.begin(); i!=signals.end(); ++i)
47                 delete i->second;
48 }
49
50 const TrackType3D &Catalogue3D::get_track(const TrackType &tt) const
51 {
52         return *get_item(tracks, &tt);
53 }
54
55 const VehicleType3D &Catalogue3D::get_vehicle(const VehicleType &vt) const
56 {
57         return *get_item(vehicles, &vt);
58 }
59
60 const SignalType3D &Catalogue3D::get_signal(const SignalType &vt) const
61 {
62         return *get_item(signals, &vt);
63 }
64
65 void Catalogue3D::track_added(const TrackType &track)
66 {
67         tracks[&track] = new TrackType3D(*this, track);
68 }
69
70 void Catalogue3D::vehicle_added(const VehicleType &veh)
71 {
72         vehicles[&veh] = new VehicleType3D(*this, veh);
73 }
74
75 void Catalogue3D::signal_added(const SignalType &sig)
76 {
77         signals[&sig] = new SignalType3D(*this, sig);
78 }
79
80 void Catalogue3D::build_endpoint_mesh()
81 {
82         const Profile &ballast_profile = catalogue.get_ballast_profile();
83         const Vector &ballast_min = ballast_profile.get_min_coords();
84         const Vector &ballast_max = ballast_profile.get_max_coords();
85
86         const Profile &rail_profile = catalogue.get_rail_profile();
87         const Vector &rail_min = rail_profile.get_min_coords();
88         const Vector &rail_max = rail_profile.get_max_coords();
89
90         float gauge = catalogue.get_gauge();
91
92         float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
93         float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
94
95         GL::MeshBuilder bld(endpoint_mesh);
96         bld.normal(1, 0, 0);
97         bld.begin(GL::QUADS);
98         bld.vertex(0, width/2, 0);
99         bld.vertex(0, width/2, height);
100         bld.vertex(0, -width/2, height);
101         bld.vertex(0, -width/2, 0);
102         bld.end();
103 }
104
105 FS::Path Catalogue3D::locate_file(const string &name)
106 {
107         if(FS::exists(name))
108                 return name;
109
110         FS::Path path = FS::Path("data")/name;
111         if(FS::exists(path))
112                 return path;
113
114         throw runtime_error("Can't locate "+name);
115 }
116
117 template<typename T>
118 T *Catalogue3D::create(const string &name)
119 {
120         RefPtr<T> obj = new T;
121         DataFile::load(*obj, locate_file(name).str());
122         return obj.release();
123 }
124
125 template<typename T>
126 T *Catalogue3D::create2(const string &name)
127 {
128         RefPtr<T> obj = new T;
129         DataFile::load(*obj, locate_file(name).str(), *this);
130         return obj.release();
131 }
132
133 } // namespace R2C2