+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2010 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <algorithm>
+#include <cmath>
+#include <msp/gl/meshbuilder.h>
+#include "catalogue.h"
+#include "tracktype.h"
+
+using namespace std;
+using namespace Msp;
+
+namespace {
+
+bool compare_z(const Marklin::Point &p1, const Marklin::Point &p2)
+{
+ return p1.z<p2.z;
+}
+
+template<typename Iter>
+Iter graham_scan(Iter begin, Iter end)
+{
+ // http://en.wikipedia.org/wiki/Graham_scan
+
+ // Find point with lowest X coordinate
+ Marklin::Point lowest = *begin;
+ for(Iter i=begin; i!=end; ++i)
+ if(i->x<lowest.x || (i->x==lowest.x && i->y>lowest.y))
+ lowest = *i;
+
+ // Compute tangents and sort points
+ for(Iter i=begin; i!=end; ++i)
+ i->z = (i->x==lowest.x ? 1e5/(i->y-lowest.y-1) : (i->y-lowest.y)/(i->x-lowest.x));
+ sort(begin, end, compare_z);
+
+ for(Iter k=begin, i=k++, j=k++;; )
+ {
+ // Compute winding by cross product
+ float turn = (j->x-i->x)*(k->y-j->y) - (k->x-j->x)*(j->y-i->y);
+
+ if(turn<1e-5)
+ {
+ // Right turn - throw the middle point away
+ // Special case for collinear vertices in the beginning
+ if(i==begin)
+ j = k++;
+ else
+ j = i--;
+ }
+ else
+ {
+ // Left turn - store the middle point and advance
+ if(++i!=j)
+ *i = *j;
+ j = k++;
+ }
+
+ // Cycle back to beginning and terminate after checking the last point
+ if(k==end)
+ k = begin;
+ else if(j==begin)
+ return ++i;
+ }
+}
+
+}
+
+namespace Marklin {
+
+TrackType3D::TrackType3D(const Catalogue3D &cat3d, const TrackType &tt):
+ ballast_mesh((GL::NORMAL3, GL::COLOR4_UBYTE, GL::VERTEX3)),
+ rail_mesh((GL::NORMAL3, GL::COLOR4_UBYTE, GL::VERTEX3))
+{
+ const Catalogue &cat = cat3d.get_catalogue();
+ const vector<TrackPart> &parts = tt.get_parts();
+
+ const Profile &ballast_profile = cat.get_ballast_profile();
+ const Point &ballast_min = ballast_profile.get_min_coords();
+ const Point &ballast_max = ballast_profile.get_max_coords();
+
+ const Profile &rail_profile = cat.get_rail_profile();
+ const Point &rail_min = rail_profile.get_min_coords();
+ const Point &rail_max = rail_profile.get_max_coords();
+
+ float gauge = cat.get_gauge();
+
+ {
+ unsigned index = 0;
+ GL::MeshBuilder bld(ballast_mesh);
+ bld.color(0.25f, 0.25f, 0.25f);
+ for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+ build_part(*i, ballast_profile, Point(0, -ballast_min.y), bld, index);
+ }
+
+ {
+ unsigned index = 0;
+ GL::MeshBuilder bld(rail_mesh);
+ bld.color(0.85f, 0.85f, 0.85f);
+ float y = ballast_max.y-ballast_min.y-rail_min.y;
+ for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+ build_part(*i, rail_profile, Point(-gauge/2-rail_max.x, y), bld, index);
+ for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+ build_part(*i, rail_profile, Point(gauge/2-rail_min.x, y), bld, index);
+ }
+
+ min_z = max_z = border.front().z;
+ for(vector<Point>::iterator i=border.begin(); i!=border.end(); ++i)
+ {
+ min_z = min(min_z, i->z);
+ max_z = max(max_z, i->z);
+ }
+ border.erase(graham_scan(border.begin(), border.end()), border.end());
+}
+
+void TrackType3D::get_bounds(float angle, Point &minp, Point &maxp) const
+{
+ float c = cos(-angle);
+ float s = sin(-angle);
+
+ minp = maxp = Point();
+ minp.z = min_z;
+ maxp.z = max_z;
+
+ for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
+ {
+ float x = c*i->x-s*i->y;
+ float y = s*i->x+c*i->y;
+
+ minp.x = min(minp.x, x);
+ minp.y = min(minp.y, y);
+ maxp.x = max(maxp.x, x);
+ maxp.y = max(maxp.y, y);
+ }
+}
+
+void TrackType3D::render() const
+{
+ ballast_mesh.draw();
+ rail_mesh.draw();
+}
+
+void TrackType3D::build_part(const TrackPart &part, const Profile &profile, const Point &offset, GL::MeshBuilder &bld, unsigned &base_index)
+{
+ unsigned nsegs = (part.radius ? static_cast<unsigned>(part.length*16)+1 : 1);
+ float plen = part.length;
+ if(part.radius)
+ plen *= abs(part.radius);
+
+ unsigned n_points = profile.get_n_points();
+ for(unsigned i=0; i<=nsegs; ++i)
+ {
+ float a = part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
+ float c = cos(a);
+ float s = sin(a);
+ Point basep = part.get_point(i*plen/nsegs);
+
+ Point p;
+ for(unsigned j=0; j<n_points; ++j)
+ {
+ // TODO: smoothing - only duplicate vertex if the angle is large enough
+
+ p = profile.get_point(j);
+ p.z = basep.z+p.y+offset.y;
+ p.y = basep.y-c*(p.x+offset.x);
+ p.x = basep.x+s*(p.x+offset.x);
+ if(j>0)
+ bld.vertex(p.x, p.y, p.z);
+
+ if(j+1<n_points)
+ {
+ Point n = profile.get_edge_normal(j);
+ bld.normal(s*n.x, -c*n.x, n.y);
+ bld.vertex(p.x, p.y, p.z);
+ }
+
+ border.push_back(p);
+ }
+ }
+
+ for(unsigned i=0; i+1<n_points; ++i)
+ {
+ bld.begin(GL::TRIANGLE_STRIP);
+ for(unsigned j=0; j<=nsegs; ++j)
+ {
+ unsigned k = (j*(n_points-1)+i)*2;
+ bld.element(base_index+k+1);
+ bld.element(base_index+k);
+ }
+ bld.end();
+ }
+
+ base_index += (nsegs+1)*(n_points-1)*2;
+}
+
+} // namespace Marklin