]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
Get rid of some obsolete #includes
[r2c2.git] / source / 3d / catalogue.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/gl/meshbuilder.h>
3 #include "libr2c2/trackappearance.h"
4 #include "catalogue.h"
5 #include "signaltype.h"
6 #include "tracktype.h"
7 #include "vehicletype.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 namespace R2C2 {
13
14 Catalogue3D::Catalogue3D(Catalogue &c):
15         catalogue(c)
16 {
17         const list<DataFile::CollectionSource *> &src = catalogue.get_sources();
18         for(list<DataFile::CollectionSource *>::const_iterator i=src.begin(); i!=src.end(); ++i)
19                 add_source(**i);
20         catalogue.signal_source_added.connect(sigc::mem_fun(static_cast<DataFile::Collection *>(this), &Catalogue3D::add_source));
21 }
22
23 Catalogue3D::~Catalogue3D()
24 {
25         for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
26                 delete i->second;
27 }
28
29 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot)
30 {
31         ObjectMap::iterator i = objects.find(&ot);
32         if(i!=objects.end())
33                 return *i->second;
34
35         ObjectType3D *ot3d = 0;
36         if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
37                 ot3d = new TrackType3D(*this, *tt);
38         else if(const SignalType *st = dynamic_cast<const SignalType *>(&ot))
39                 ot3d = new SignalType3D(*this, *st);
40         else if(const VehicleType *vt = dynamic_cast<const VehicleType *>(&ot))
41                 ot3d = new VehicleType3D(*this, *vt);
42         else
43                 throw key_error(&ot);
44
45         objects[&ot] = ot3d;
46         return *ot3d;
47 }
48
49 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
50 {
51         return *get_item(objects, &ot);
52 }
53
54 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance)
55 {
56         EndpointMap::iterator i = endpoint_meshes.find(&appearance);
57         if(i!=endpoint_meshes.end())
58                 return *i->second;
59
60         const Profile &ballast_profile = appearance.get_ballast_profile();
61         const Vector &ballast_min = ballast_profile.get_min_coords();
62         const Vector &ballast_max = ballast_profile.get_max_coords();
63
64         const Profile &rail_profile = appearance.get_rail_profile();
65         const Vector &rail_min = rail_profile.get_min_coords();
66         const Vector &rail_max = rail_profile.get_max_coords();
67
68         float gauge = appearance.get_gauge();
69
70         float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
71         float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
72
73         GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::VERTEX3));
74         GL::MeshBuilder bld(*mesh);
75         bld.normal(1, 0, 0);
76         bld.begin(GL::QUADS);
77         bld.vertex(0, width/2, 0);
78         bld.vertex(0, width/2, height);
79         bld.vertex(0, -width/2, height);
80         bld.vertex(0, -width/2, 0);
81         bld.end();
82
83         endpoint_meshes[&appearance] = mesh;
84         return *mesh;
85 }
86
87 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance) const
88 {
89         return *get_item(endpoint_meshes, &appearance);
90 }
91
92 } // namespace R2C2