diff options
Diffstat (limited to 'game/network/network.cpp')
-rw-r--r-- | game/network/network.cpp | 88 |
1 files changed, 66 insertions, 22 deletions
diff --git a/game/network/network.cpp b/game/network/network.cpp index a9c9372..ae4865a 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -107,7 +107,7 @@ Network::add(GeoData * geoData, const std::span<const Link::Ptr> links) } void -Network::terrainSplitAt(GenLinkDef & previous, GenLinkDef & next, GlobalPosition3D pos) +Network::connectAt(GenLinkDef & previous, GenLinkDef & next, GlobalPosition3D pos) { std::visit( [pos](auto & typedDefPrevious, auto & typedDefNext) { @@ -125,7 +125,7 @@ Network::terrainSplit(const GeoData * geoData, const GenStraightDef & def) const if (step.previous.is_valid() && geoData->getSurface(step.current) != geoData->getSurface(step.previous)) { const auto surfaceEdgePosition = geoData->positionAt(GeoData::PointFace(step.exitPosition, step.current)); out.emplace_back(out.back()); - terrainSplitAt(*out.rbegin(), *++out.rbegin(), surfaceEdgePosition); + connectAt(*out.rbegin(), *++out.rbegin(), surfaceEdgePosition); } }); return out; @@ -161,26 +161,35 @@ Network::terrainSplit(const GeoData * geoData, const GenCurveDef & def) const GenLinksDef out {def}; std::ranges::for_each(++points.begin(), --points.end(), [&out](const auto pos) { out.emplace_back(out.back()); - terrainSplitAt(*out.rbegin(), *++out.rbegin(), pos); + connectAt(*out.rbegin(), *++out.rbegin(), pos); }); return out; } -GenCurveDef -Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir) +GenLinksDef +Network::genDef(const GlobalPosition3D & start, const GlobalPosition3D & end) +{ + return {GenStraightDef {start, end}}; +} + +GenLinksDef +Network::genDef(const GlobalPosition3D & start, const GlobalPosition3D & end, Angle startDir) { const auto dir = pi + startDir; const auto flatStart = start.xy(), flatEnd = end.xy(); const auto centre = find_arc_centre(flatStart, dir, flatEnd); - if (centre.second) { // right hand arc - return {end, start, centre.first}; + if (centre.second > 0.1F) { + return {GenCurveDef {end, start, centre.first}}; + } + if (centre.second < -0.1F) { + return {GenCurveDef {start, end, centre.first}}; } - return {start, end, centre.first}; + return {GenStraightDef {start, end}}; } -std::pair<GenCurveDef, GenCurveDef> -Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir, float endDir) +GenLinksDef +Network::genDef(const GlobalPosition3D & start, const GlobalPosition3D & end, Angle startDir, Angle endDir) { // Based on formula/code from https://www.ryanjuckett.com/biarc-interpolation/ const auto startVec = -sincos(startDir); @@ -194,7 +203,7 @@ Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & en if (equalTangents && perpT1) { const auto joint = start + (diff * 0.5F); - return {genCurveDef(start, joint, startDir), genCurveDef(end, joint, endDir)}; + return genDef(start, joint, startDir) + genDef(end, joint, endDir); } const auto vDotT = glm::dot(diff.xy(), endsVecTotal); @@ -210,16 +219,41 @@ Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & en const auto joint = (start + end + ((difference(startVec, endVec) * extLen1) || 0.F)) / 2; - return {genCurveDef(start, joint, startDir), genCurveDef(end, joint, endDir)}; + return genDef(start, joint, startDir) + genDef(end, joint, endDir); +} + +namespace { + struct MergeEq { + bool + operator()(GenCurveDef & lhs, const GenCurveDef & rhs) const + { + if (distance(std::get<2>(lhs), std::get<2>(rhs)) < 100.F) { // LHS.centre near RHS.centre + if (std::get<1>(lhs) == std::get<0>(rhs)) { // LHS.end == RHS.start + std::get<1>(lhs) = std::get<1>(rhs); + } + else if (std::get<0>(lhs) == std::get<1>(rhs)) { // LHS.start == RHS.end + std::get<0>(lhs) = std::get<0>(rhs); + } + else { + return false; + } + std::get<2>(lhs) = midpoint(std::get<2>(lhs), std::get<2>(rhs)); + return true; + } + return false; + } + + bool + operator()(const auto &, const auto &) const + { + return false; + } + }; } Link::Collection Network::create(const GeoData * geoData, const CreationDefinition & def) { - // TODO - // Where to make a straight to join because angles align? - // Where to drop part of S curve pair if a single curve works? - const auto linkDefs = [&def]() -> GenLinksDef { if (!def.fromEnd.direction && !def.toEnd.direction) { // No specific directions at either end, straight link @@ -228,15 +262,13 @@ Network::create(const GeoData * geoData, const CreationDefinition & def) if (def.fromEnd.direction) { if (def.toEnd.direction) { // Two specific directions at both ends, S curves - const auto curves = genCurveDef( - def.fromEnd.position, def.toEnd.position, *def.fromEnd.direction, *def.toEnd.direction); - return {curves.first, curves.second}; + return genDef(def.fromEnd.position, def.toEnd.position, *def.fromEnd.direction, *def.toEnd.direction); } // One specific direction, single curve from there - return {genCurveDef(def.fromEnd.position, def.toEnd.position, *def.fromEnd.direction)}; + return genDef(def.fromEnd.position, def.toEnd.position, *def.fromEnd.direction); } // One specific direction, single curve from the other - return {genCurveDef(def.toEnd.position, def.fromEnd.position, *def.toEnd.direction)}; + return genDef(def.toEnd.position, def.fromEnd.position, *def.toEnd.direction); }; const auto splitDefs = [&linkDefs, this, geoData]() { return std::ranges::fold_left(linkDefs(), GenLinksDef {}, [this, geoData](auto && existing, const auto & def) { @@ -247,8 +279,20 @@ Network::create(const GeoData * geoData, const CreationDefinition & def) def); }); }; + // Merge adjacent pairs where possible + auto linkDefsGen = geoData ? splitDefs() : linkDefs(); + if (!linkDefsGen.empty()) { + for (auto lhsIter = linkDefsGen.begin(), rhsIter = lhsIter + 1; rhsIter != linkDefsGen.end();) { + if (std::visit(MergeEq {}, *lhsIter, *rhsIter)) { + rhsIter = linkDefsGen.erase(rhsIter); + } + else { + lhsIter = rhsIter++; + } + } + } Link::Collection links; - std::ranges::transform(geoData ? splitDefs() : linkDefs(), std::back_inserter(links), [this](const auto & def) { + std::ranges::transform(linkDefsGen, std::back_inserter(links), [this](const auto & def) { return std::visit( [this](const auto & typedDef) { return this->create(typedDef); |