diff options
Diffstat (limited to 'game/network')
-rw-r--r-- | game/network/network.cpp | 88 | ||||
-rw-r--r-- | game/network/network.h | 45 | ||||
-rw-r--r-- | game/network/network.impl.h | 102 | ||||
-rw-r--r-- | game/network/rail.cpp | 55 | ||||
-rw-r--r-- | game/network/rail.h | 1 |
5 files changed, 71 insertions, 220 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); diff --git a/game/network/network.h b/game/network/network.h index adaae5f..d3370b3 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -8,7 +8,6 @@ #include "sorting.h" #include "special_members.h" #include <glm/glm.hpp> -#include <memory> #include <set> #include <string> #include <utility> @@ -55,13 +54,6 @@ public: [[nodiscard]] Link::Nexts routeFromTo(const Link::End &, GlobalPosition3D) const; [[nodiscard]] static Link::Nexts routeFromTo(const Link::End &, const Node::Ptr &); - virtual Link::CCollection candidateStraight(GlobalPosition3D, GlobalPosition3D) = 0; - virtual Link::CCollection candidateJoins(GlobalPosition3D, GlobalPosition3D) = 0; - virtual Link::CCollection candidateExtend(GlobalPosition3D, GlobalPosition3D) = 0; - virtual Link::CCollection addStraight(const GeoData *, GlobalPosition3D, GlobalPosition3D) = 0; - virtual Link::CCollection addJoins(const GeoData *, GlobalPosition3D, GlobalPosition3D) = 0; - virtual Link::CCollection addExtend(const GeoData *, GlobalPosition3D, GlobalPosition3D) = 0; - [[nodiscard]] virtual float findNodeDirection(Node::AnyCPtr) const = 0; [[nodiscard]] Link::Collection create(const GeoData *, const CreationDefinition &); @@ -74,13 +66,14 @@ public: protected: static void joinLinks(const Link::Ptr & link, const Link::Ptr & oldLink); - static GenCurveDef genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir); - static std::pair<GenCurveDef, GenCurveDef> genCurveDef( - const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir, float endDir); + static GenLinksDef genDef(const GlobalPosition3D & start, const GlobalPosition3D & end); + static GenLinksDef genDef(const GlobalPosition3D & start, const GlobalPosition3D & end, Angle startDir); + static GenLinksDef genDef( + const GlobalPosition3D & start, const GlobalPosition3D & end, Angle startDir, Angle endDir); [[nodiscard]] GenLinksDef terrainSplit(const GeoData *, const GenStraightDef &) const; [[nodiscard]] GenLinksDef terrainSplit(const GeoData *, const GenCurveDef &) const; - static void terrainSplitAt(GenLinkDef & previous, GenLinkDef & next, GlobalPosition3D pos); + static void connectAt(GenLinkDef & previous, GenLinkDef & next, GlobalPosition3D pos); [[nodiscard]] virtual Link::Ptr create(const GenStraightDef &) = 0; [[nodiscard]] virtual Link::Ptr create(const GenCurveDef &) = 0; @@ -109,33 +102,6 @@ protected: [[nodiscard]] Link::Ptr intersectRayLinks(const Ray<GlobalPosition3D> &) const override; public: - template<typename L, typename... Params> - std::shared_ptr<L> - candidateLink(GlobalPosition3D positionA, GlobalPosition3D positionB, Params &&... params) - requires std::is_base_of_v<T, L> - { - const auto node1 = candidateNodeAt(positionA).first, node2 = candidateNodeAt(positionB).first; - return std::make_shared<L>(*this, node1, node2, std::forward<Params>(params)...); - } - - template<typename L, typename... Params> - std::shared_ptr<L> - addLink(GlobalPosition3D positionA, GlobalPosition3D positionB, Params &&... params) - requires std::is_base_of_v<T, L> - { - const auto node1 = nodeAt(positionA), node2 = nodeAt(positionB); - auto newLink = links.template create<L>(*this, node1, node2, std::forward<Params>(params)...); - joinLinks(newLink); - return std::move(newLink); - } - - Link::CCollection candidateStraight(GlobalPosition3D, GlobalPosition3D) override; - Link::CCollection candidateJoins(GlobalPosition3D, GlobalPosition3D) override; - Link::CCollection candidateExtend(GlobalPosition3D, GlobalPosition3D) override; - Link::CCollection addStraight(const GeoData *, GlobalPosition3D, GlobalPosition3D) override; - Link::CCollection addJoins(const GeoData *, GlobalPosition3D, GlobalPosition3D) override; - Link::CCollection addExtend(const GeoData *, GlobalPosition3D, GlobalPosition3D) override; - [[nodiscard]] float findNodeDirection(Node::AnyCPtr) const override; using Network::create; [[nodiscard]] Link::Ptr create(const GenStraightDef &) override; @@ -144,6 +110,5 @@ public: void add(GeoData *, const Link::Ptr &) override; protected: - Link::CCollection addCurve(const GeoData *, const GenCurveDef &); [[nodiscard]] bool anyLinks() const; }; diff --git a/game/network/network.impl.h b/game/network/network.impl.h index 04c5d7c..dda4974 100644 --- a/game/network/network.impl.h +++ b/game/network/network.impl.h @@ -1,4 +1,3 @@ -#include "collections.h" #include "network.h" #include <game/geoData.h> #include <gfx/gl/sceneShader.h> @@ -44,107 +43,6 @@ NetworkOf<T, Links...>::findNodeDirection(Node::AnyCPtr n) const } template<typename T, typename... Links> -Link::CCollection -NetworkOf<T, Links...>::candidateStraight(GlobalPosition3D positionA, GlobalPosition3D positionB) -{ - return {candidateLink<typename T::StraightLink>(positionA, positionB)}; -} - -template<typename T, typename... Links> -Link::CCollection -NetworkOf<T, Links...>::candidateJoins(GlobalPosition3D start, GlobalPosition3D end) -{ - static constexpr auto MIN_DISTANCE = 2000.F; - if (::distance(start, end) < MIN_DISTANCE) { - return {}; - } - const auto defs = genCurveDef( - start, end, findNodeDirection(candidateNodeAt(start).first), findNodeDirection(candidateNodeAt(end).first)); - const auto & [c1s, c1e, c1c] = defs.first; - const auto & [c2s, c2e, c2c] = defs.second; - return {candidateLink<typename T::CurveLink>(c1s, c1e, c1c), candidateLink<typename T::CurveLink>(c2s, c2e, c2c)}; -} - -template<typename T, typename... Links> -Link::CCollection -NetworkOf<T, Links...>::candidateExtend(GlobalPosition3D start, GlobalPosition3D end) -{ - const auto [cstart, cend, centre] = genCurveDef(start, end, findNodeDirection(candidateNodeAt(start).first)); - return {candidateLink<typename T::CurveLink>(cstart, cend, centre)}; -} - -template<typename T, typename... Links> -Link::CCollection -NetworkOf<T, Links...>::addStraight(const GeoData * geoData, GlobalPosition3D positionA, GlobalPosition3D positionB) -{ - Link::CCollection out; - geoData->walk(positionA.xy(), positionB, [geoData, &out, this, &positionA](const GeoData::WalkStep & step) { - 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(addLink<typename T::StraightLink>(positionA, surfaceEdgePosition)); - positionA = surfaceEdgePosition; - } - }); - out.emplace_back(addLink<typename T::StraightLink>(positionA, positionB)); - return out; -} - -template<typename T, typename... Links> -Link::CCollection -NetworkOf<T, Links...>::addCurve(const GeoData * geoData, const GenCurveDef & curve) -{ - static constexpr auto MIN_DISTANCE = 2000.F; - auto [cstart, cend, centre] = curve; - Link::CCollection out; - std::set<GeoData::WalkStepCurve, SortedBy<&GeoData::WalkStepCurve::angle>> breaks; - const auto radiusMid = ::distance(cstart.xy(), centre); - for (const auto radiusOffset : {-getBaseWidth() / 2.F, 0.F, getBaseWidth() / 2.F}) { - const auto radius = radiusOffset + radiusMid; - const auto start = centre + (difference(cstart.xy(), centre) * radius) / radiusMid; - const auto end = centre + (difference(cend.xy(), centre) * radius) / radiusMid; - geoData->walk(start, end, centre, [geoData, &breaks](const GeoData::WalkStepCurve & step) { - if (step.previous.is_valid() && geoData->getSurface(step.current) != geoData->getSurface(step.previous)) { - breaks.insert(step); - } - }); - } - std::vector<GlobalPosition3D> points; - points.reserve(breaks.size() + 2); - points.push_back(cstart); - std::ranges::transform( - breaks, std::back_inserter(points), [geoData, centre, radiusMid](const GeoData::WalkStepCurve & step) { - return (centre + (sincos(step.angle) * radiusMid)) - || geoData->positionAt(GeoData::PointFace(step.exitPosition, step.current)).z; - }); - points.push_back(cend); - mergeClose(points, ::distance<3, GlobalDistance>, ::midpoint<3, GlobalDistance>, MIN_DISTANCE); - std::ranges::transform(points | std::views::pairwise, std::back_inserter(out), [this, centre](const auto pair) { - const auto [a, b] = pair; - return this->addLink<typename T::CurveLink>(a, b, centre); - }); - return out; -} - -template<typename T, typename... Links> -Link::CCollection -NetworkOf<T, Links...>::addJoins(const GeoData * geoData, GlobalPosition3D start, GlobalPosition3D end) -{ - static constexpr auto MIN_DISTANCE = 2000.F; - if (::distance(start, end) < MIN_DISTANCE) { - return {}; - } - const auto defs = genCurveDef(start, end, findNodeDirection(nodeAt(start)), findNodeDirection(nodeAt(end))); - return addCurve(geoData, defs.first) + addCurve(geoData, defs.second); -} - -template<typename T, typename... Links> -Link::CCollection -NetworkOf<T, Links...>::addExtend(const GeoData * geoData, GlobalPosition3D start, GlobalPosition3D end) -{ - return addCurve(geoData, genCurveDef(start, end, findNodeDirection(nodeAt(start)))); -} - -template<typename T, typename... Links> Link::Ptr NetworkOf<T, Links...>::create(const GenStraightDef & def) { diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 37eb7df..342a2ad 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -18,61 +18,6 @@ RailLinks::tick(TickDuration) { } -std::shared_ptr<RailLink> -RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) -{ - auto node1ins = newNodeAt(start), node2ins = newNodeAt(end); - if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::NotInNetwork) { - // Both nodes are new, direct link, easy - return addLink<RailLinkStraight>(start, end); - } - if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::InNetwork) { - // node1 is new, node2 exists, but we build from existing outwards - std::swap(node1ins, node2ins); - std::swap(start, end); - } - // Find start link/end - opposite entry dir to existing link; so pi +... - const Angle dir = pi + findNodeDirection(node1ins.first); - if (dir == vector_yaw(difference(end, start))) { - return addLink<RailLinkStraight>(start, end); - } - const auto flatStart {start.xy()}, flatEnd {end.xy()}; - if (node2ins.second == NodeIs::InNetwork) { - auto midheight = [&](auto mid) { - const auto startToMid = ::distance<2>(flatStart, mid); - const auto endToMid = ::distance<2>(flatEnd, mid); - return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (startToMid / (startToMid + endToMid))); - }; - const float dir2 = pi + findNodeDirection(node2ins.first); - const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); - if (radii.first < radii.second) { - const auto radius = radii.first; - const auto centre1 = flatStart + (sincos(dir + half_pi) * radius); - const auto centre2 = flatEnd + (sincos(dir2 + half_pi) * radius); - const auto mid = (centre1 + centre2) / 2; - const auto midh = mid || midheight(mid); - addLink<RailLinkCurve>(start, midh, centre1); - return addLink<RailLinkCurve>(end, midh, centre2); - } - const auto radius = radii.second; - const auto centre1 = flatStart + (sincos(dir - half_pi) * radius); - const auto centre2 = flatEnd + (sincos(dir2 - half_pi) * radius); - const auto mid = (centre1 + centre2) / 2; - const auto midh = mid || midheight(mid); - addLink<RailLinkCurve>(midh, start, centre1); - return addLink<RailLinkCurve>(midh, end, centre2); - } - const auto diff = difference(end, start); - const auto yaw = vector_yaw(diff); - const auto n2ed = (yaw * 2) - dir - pi; - const auto centre = find_arc_centre(flatStart, dir, flatEnd, n2ed); - - if (centre.second) { // right hand arc - std::swap(start, end); - } - return addLink<RailLinkCurve>(start, end, centre.first); -} - namespace { constexpr const std::array<RelativePosition3D, RAIL_CROSSSECTION_VERTICES> RAIL_CROSS_SECTION {{ {-1330.F, 0.F, 0}, diff --git a/game/network/rail.h b/game/network/rail.h index 4aef9e3..15b9ae4 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -74,7 +74,6 @@ class RailLinks : public NetworkOf<RailLink, RailLinkStraight, RailLinkCurve>, p public: RailLinks(); - std::shared_ptr<RailLink> addLinksBetween(GlobalPosition3D start, GlobalPosition3D end); void render(const SceneShader &, const Frustum &) const override; [[nodiscard]] const Surface * getBaseSurface() const override; |