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