]> git.tdb.fi Git - r2c2.git/blob - source/3d/catalogue.cpp
Convert Catalogue to a Collection
[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         src.add_directory(".");
17         src.add_directory("data");
18         add_source(src);
19 }
20
21 Catalogue3D::~Catalogue3D()
22 {
23         for(map<const ObjectType *, ObjectType3D *>::iterator i=objects.begin(); i!=objects.end(); ++i)
24                 delete i->second;
25 }
26
27 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot)
28 {
29         ObjectMap::iterator i = objects.find(&ot);
30         if(i!=objects.end())
31                 return *i->second;
32
33         ObjectType3D *ot3d = 0;
34         if(const TrackType *tt = dynamic_cast<const TrackType *>(&ot))
35                 ot3d = new TrackType3D(*this, *tt);
36         else if(const SignalType *st = dynamic_cast<const SignalType *>(&ot))
37                 ot3d = new SignalType3D(*this, *st);
38         else if(const VehicleType *vt = dynamic_cast<const VehicleType *>(&ot))
39                 ot3d = new VehicleType3D(*this, *vt);
40         else
41                 throw key_error(&ot);
42
43         objects[&ot] = ot3d;
44         return *ot3d;
45 }
46
47 const ObjectType3D &Catalogue3D::get_3d(const ObjectType &ot) const
48 {
49         return *get_item(objects, &ot);
50 }
51
52 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance)
53 {
54         EndpointMap::iterator i = endpoint_meshes.find(&appearance);
55         if(i!=endpoint_meshes.end())
56                 return *i->second;
57
58         const Profile &ballast_profile = appearance.get_ballast_profile();
59         const Vector &ballast_min = ballast_profile.get_min_coords();
60         const Vector &ballast_max = ballast_profile.get_max_coords();
61
62         const Profile &rail_profile = appearance.get_rail_profile();
63         const Vector &rail_min = rail_profile.get_min_coords();
64         const Vector &rail_max = rail_profile.get_max_coords();
65
66         float gauge = appearance.get_gauge();
67
68         float width = max(max(-ballast_min.x, ballast_max.x)*2, gauge+(rail_max.x-rail_min.x)*2)+0.004;
69         float height = ballast_max.y-ballast_min.y+rail_max.y-rail_min.y+0.01;
70
71         GL::Mesh *mesh = new GL::Mesh((GL::NORMAL3, GL::VERTEX3));
72         GL::MeshBuilder bld(*mesh);
73         bld.normal(1, 0, 0);
74         bld.begin(GL::QUADS);
75         bld.vertex(0, width/2, 0);
76         bld.vertex(0, width/2, height);
77         bld.vertex(0, -width/2, height);
78         bld.vertex(0, -width/2, 0);
79         bld.end();
80
81         endpoint_meshes[&appearance] = mesh;
82         return *mesh;
83 }
84
85 const GL::Mesh &Catalogue3D::get_endpoint_mesh(const TrackAppearance &appearance) const
86 {
87         return *get_item(endpoint_meshes, &appearance);
88 }
89
90 } // namespace R2C2