From e9dd46f7950b2fce4672758f40e0d6065ba59c25 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 28 Mar 2010 10:55:41 +0100 Subject: [PATCH] Cache meshes for S-bends --- src/SBend.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/SBend.cpp b/src/SBend.cpp index 218834a..6b1695c 100644 --- a/src/SBend.cpp +++ b/src/SBend.cpp @@ -23,6 +23,8 @@ #include "OpenGLHelper.hpp" #include +#include + #include // Spline curves which start and finish in the same direction @@ -60,16 +62,19 @@ private: BezierCurve curve; IMeshPtr railMesh; + + typedef tuple Parameters; + typedef map MeshCache; + static MeshCache meshCache; }; +SBend::MeshCache SBend::meshCache; + SBend::SBend(track::Direction dir, int straight, int off) : straight(straight), offset(off), height(0.0f), axis(dir) { - debug() << "SBend axis=" << axis - << " straight=" << straight << " off=" << off; - assert(straight > 0); const float pinch = static_cast(straight) / 3.0f; @@ -85,10 +90,15 @@ SBend::SBend(track::Direction dir, int straight, int off) Vector p4 = makeVector(straightF, 0.0f, offsetF); curve = makeBezierCurve(p1, p2, p3, p4); - railMesh = makeBezierRailMesh(curve); - debug() << "f(0) = " << curve(0.0f) - << " f(1) = " << curve(1.0f); + Parameters parms = make_tuple(straight, offset * reflect); + MeshCache::iterator it = meshCache.find(parms); + if (it == meshCache.end()) { + railMesh = makeBezierRailMesh(curve); + meshCache[parms] = railMesh; + } + else + railMesh = (*it).second; } void SBend::setOrigin(int x, int y, float h) -- 2.39.2