summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config/types.h1
-rw-r--r--game/network/link.cpp29
-rw-r--r--game/network/link.h21
-rw-r--r--game/network/network.cpp34
-rw-r--r--game/network/network.h42
-rw-r--r--game/network/network.impl.h16
-rw-r--r--game/network/rail.cpp59
-rw-r--r--game/network/rail.h10
-rw-r--r--lib/maths.h16
-rw-r--r--test/test-maths.cpp18
-rw-r--r--test/test-network.cpp23
-rw-r--r--test/testStructures.h4
-rw-r--r--ui/builders/freeExtend.h2
-rw-r--r--ui/builders/straight.cpp2
-rw-r--r--ui/builders/straight.h4
-rw-r--r--ui/editNetwork.h2
16 files changed, 141 insertions, 142 deletions
diff --git a/config/types.h b/config/types.h
index aff0390..081530d 100644
--- a/config/types.h
+++ b/config/types.h
@@ -20,7 +20,6 @@ template<glm::length_t D> using Normal = Direction<D>;
template<glm::length_t D> using Rotation = glm::vec<D, Angle>;
template<glm::length_t Channels> using Colour = glm::vec<Channels, float>;
-using Position2D = Position<2>; // deprecate
using Position3D = Position<3>; // deprecate
using BaryPosition = glm::vec<2, float>;
using RelativePosition2D = RelativePosition<2>;
diff --git a/game/network/link.cpp b/game/network/link.cpp
index e8eaea2..932fc67 100644
--- a/game/network/link.cpp
+++ b/game/network/link.cpp
@@ -1,5 +1,4 @@
#include "link.h"
-#include <compare>
#include <glm/gtx/transform.hpp>
#include <location.h>
#include <maths.h>
@@ -8,10 +7,10 @@
Link::Link(End a, End b, float l) : ends {{std::move(a), std::move(b)}}, length {l} { }
-LinkCurve::LinkCurve(Position3D c, float r, Arc a) : centreBase {c}, radius {r}, arc {std::move(a)} { }
+LinkCurve::LinkCurve(GlobalPosition3D c, RelativeDistance r, Arc a) : centreBase {c}, radius {r}, arc {std::move(a)} { }
bool
-operator<(const Position3D & a, const Position3D & b)
+operator<(const GlobalPosition3D & a, const GlobalPosition3D & b)
{
// NOLINTNEXTLINE(hicpp-use-nullptr,modernize-use-nullptr)
return std::tie(a.x, a.y, a.z) < std::tie(b.x, b.y, b.z);
@@ -24,12 +23,13 @@ operator<(const Node & a, const Node & b)
}
Location
-LinkStraight::positionAt(float dist, unsigned char start) const
+LinkStraight::positionAt(RelativeDistance dist, unsigned char start) const
{
const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())};
- const auto diff {es.second->pos - es.first->pos};
+ const RelativePosition3D diff {es.second->pos - es.first->pos};
const auto dir {glm::normalize(diff)};
- return Location {es.first->pos + vehiclePositionOffset() + dir * dist, {vector_pitch(dir), vector_yaw(dir), 0}};
+ return Location {es.first->pos + GlobalPosition3D(vehiclePositionOffset() + dir * dist),
+ {vector_pitch(dir), vector_yaw(dir), 0}};
}
bool
@@ -49,9 +49,11 @@ LinkCurve::positionAt(float dist, unsigned char start) const
const auto ang {as.first + ((as.second - as.first) * frac)};
const auto relPos {(sincosf(ang) || 0.F) * radius};
const auto relClimb {vehiclePositionOffset()
- + Position3D {0, 0, es.first->pos.z - centreBase.z + ((es.second->pos.z - es.first->pos.z) * frac)}};
- const auto pitch {vector_pitch({0, 0, (es.second->pos.z - es.first->pos.z) / length})};
- return Location {relPos + relClimb + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}};
+ + RelativePosition3D {0, 0,
+ static_cast<RelativeDistance>(es.first->pos.z - centreBase.z)
+ + (static_cast<RelativeDistance>(es.second->pos.z - es.first->pos.z) * frac)}};
+ const auto pitch {vector_pitch({0, 0, static_cast<RelativeDistance>(es.second->pos.z - es.first->pos.z) / length})};
+ return Location {GlobalPosition3D(relPos + relClimb) + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}};
}
bool
@@ -61,15 +63,14 @@ LinkCurve::intersectRay(const Ray<GlobalPosition3D> & ray) const
const auto & e1p {ends[1].node->pos};
const auto slength = round_frac(length / 2.F, 5.F);
const auto segs = std::round(15.F * slength / std::pow(radius, 0.7F));
- const auto step {Position3D {arc_length(arc), e1p.z - e0p.z, slength} / segs};
- const auto trans {glm::translate(centreBase)};
+ const auto step {glm::vec<2, RelativeDistance> {arc_length(arc), e1p.z - e0p.z} / segs};
auto segCount = static_cast<std::size_t>(std::lround(segs)) + 1;
std::vector<GlobalPosition3D> points;
points.reserve(segCount);
- for (Position3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) {
- const auto t {trans * glm::rotate(half_pi - swing.x, up) * glm::translate(Position3D {radius, 0.F, swing.y})};
- points.emplace_back(t * glm::vec4 {0, 0, 0, 1});
+ for (std::remove_const_t<decltype(step)> swing = {arc.first, centreBase.z - e0p.z}; segCount;
+ swing += step, --segCount) {
+ points.emplace_back(centreBase + GlobalPosition3D((sincosf(swing.x) * radius) || swing.y));
}
return ray.passesCloseToEdges(points, 1.F);
}
diff --git a/game/network/link.h b/game/network/link.h
index 95c141e..725e023 100644
--- a/game/network/link.h
+++ b/game/network/link.h
@@ -4,7 +4,6 @@
#include <glm/glm.hpp>
#include <location.h>
#include <maths.h>
-#include <memory>
#include <special_members.h>
#include <stdTypeDefs.h>
#include <utility>
@@ -17,12 +16,12 @@ template<typename> class Ray;
// it has location
class Node : public StdTypeDefs<Node> {
public:
- explicit Node(Position3D p) noexcept : pos(p) {};
+ explicit Node(GlobalPosition3D p) noexcept : pos(p) {};
virtual ~Node() noexcept = default;
NO_COPY(Node);
NO_MOVE(Node);
- Position3D pos;
+ GlobalPosition3D pos;
};
// Generic network link
@@ -44,21 +43,21 @@ public:
NO_COPY(Link);
NO_MOVE(Link);
- [[nodiscard]] virtual Location positionAt(float dist, unsigned char start) const = 0;
+ [[nodiscard]] virtual Location positionAt(RelativeDistance dist, unsigned char start) const = 0;
[[nodiscard]] virtual bool intersectRay(const Ray<GlobalPosition3D> &) const = 0;
std::array<End, 2> ends;
float length;
protected:
- [[nodiscard]] virtual Position3D
+ [[nodiscard]] virtual RelativePosition3D
vehiclePositionOffset() const
{
return {};
}
};
-bool operator<(const Position3D & a, const Position3D & b);
+bool operator<(const GlobalPosition3D & a, const GlobalPosition3D & b);
bool operator<(const Node & a, const Node & b);
class LinkStraight : public virtual Link {
@@ -68,7 +67,7 @@ public:
NO_COPY(LinkStraight);
NO_MOVE(LinkStraight);
- [[nodiscard]] Location positionAt(float dist, unsigned char start) const override;
+ [[nodiscard]] Location positionAt(RelativeDistance dist, unsigned char start) const override;
[[nodiscard]] bool intersectRay(const Ray<GlobalPosition3D> &) const override;
};
@@ -77,15 +76,15 @@ LinkStraight::~LinkStraight() = default;
class LinkCurve : public virtual Link {
public:
inline ~LinkCurve() override = 0;
- LinkCurve(Position3D, float, Arc);
+ LinkCurve(GlobalPosition3D, RelativeDistance, Arc);
NO_COPY(LinkCurve);
NO_MOVE(LinkCurve);
- [[nodiscard]] Location positionAt(float dist, unsigned char start) const override;
+ [[nodiscard]] Location positionAt(RelativeDistance dist, unsigned char start) const override;
[[nodiscard]] bool intersectRay(const Ray<GlobalPosition3D> &) const override;
- Position3D centreBase;
- float radius;
+ GlobalPosition3D centreBase;
+ RelativeDistance radius;
Arc arc;
};
diff --git a/game/network/network.cpp b/game/network/network.cpp
index d52e804..8aa9adb 100644
--- a/game/network/network.cpp
+++ b/game/network/network.cpp
@@ -6,7 +6,6 @@
#include <game/network/link.h>
#include <gfx/models/texture.h>
#include <glm/gtx/intersect.hpp>
-#include <initializer_list>
#include <ray.h>
#include <stdexcept>
#include <utility>
@@ -14,13 +13,13 @@
Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(tn)} { }
Node::Ptr
-Network::nodeAt(Position3D pos)
+Network::nodeAt(GlobalPosition3D pos)
{
return newNodeAt(pos).first;
}
Network::NodeInsertion
-Network::newNodeAt(Position3D pos)
+Network::newNodeAt(GlobalPosition3D pos)
{
if (auto [n, i] = candidateNodeAt(pos); i == NodeIs::NotInNetwork) {
return {*nodes.insert(std::move(n)).first, i};
@@ -31,7 +30,7 @@ Network::newNodeAt(Position3D pos)
}
Node::Ptr
-Network::findNodeAt(Position3D pos) const
+Network::findNodeAt(GlobalPosition3D pos) const
{
if (const auto n = nodes.find(pos); n != nodes.end()) {
return *n;
@@ -40,7 +39,7 @@ Network::findNodeAt(Position3D pos) const
}
Network::NodeInsertion
-Network::candidateNodeAt(Position3D pos) const
+Network::candidateNodeAt(GlobalPosition3D pos) const
{
if (const auto n = nodes.find(pos); n != nodes.end()) {
return {*n, NodeIs::InNetwork};
@@ -80,7 +79,7 @@ Network::joinLinks(const Link::Ptr & l, const Link::Ptr & ol)
}
Link::Nexts
-Network::routeFromTo(const Link::End & start, Position3D dest) const
+Network::routeFromTo(const Link::End & start, GlobalPosition3D dest) const
{
auto destNode {findNodeAt(dest)};
if (!destNode) {
@@ -96,7 +95,7 @@ Network::routeFromTo(const Link::End & end, const Node::Ptr & dest) const
}
GenCurveDef
-Network::genCurveDef(const Position3D & start, const Position3D & end, float startDir)
+Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir)
{
const auto diff {end - start};
const auto vy {vector_yaw(diff)};
@@ -112,28 +111,29 @@ Network::genCurveDef(const Position3D & start, const Position3D & end, float sta
}
std::pair<GenCurveDef, GenCurveDef>
-Network::genCurveDef(const Position3D & start, const Position3D & end, float startDir, float endDir)
+Network::genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir, float endDir)
{
startDir += pi;
endDir += pi;
- const Position2D flatStart {start.xy()}, flatEnd {end.xy()};
+ const auto flatStart {start.xy()}, flatEnd {end.xy()};
auto midheight = [&](auto mid) {
- const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid);
- return start.z + ((end.z - start.z) * (sm / (sm + em)));
+ const auto sm = glm::length(RelativePosition2D(flatStart - mid)),
+ em = glm::length(RelativePosition2D(flatEnd - mid));
+ return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (sm / (sm + em)));
};
if (const auto radii = find_arcs_radius(flatStart, startDir, flatEnd, endDir); radii.first < radii.second) {
const auto radius {radii.first};
- const auto c1 = flatStart + sincosf(startDir + half_pi) * radius;
- const auto c2 = flatEnd + sincosf(endDir + half_pi) * radius;
- const auto mid = (c1 + c2) / 2.F;
+ const auto c1 = flatStart + GlobalPosition2D(sincosf(startDir + half_pi) * radius);
+ const auto c2 = flatEnd + GlobalPosition2D(sincosf(endDir + half_pi) * radius);
+ const auto mid = (c1 + c2) / 2;
const auto midh = mid || midheight(mid);
return {{start, midh, c1}, {end, midh, c2}};
}
else {
const auto radius {radii.second};
- const auto c1 = flatStart + sincosf(startDir - half_pi) * radius;
- const auto c2 = flatEnd + sincosf(endDir - half_pi) * radius;
- const auto mid = (c1 + c2) / 2.F;
+ const auto c1 = flatStart + GlobalPosition2D(sincosf(startDir - half_pi) * radius);
+ const auto c2 = flatEnd + GlobalPosition2D(sincosf(endDir - half_pi) * radius);
+ const auto mid = (c1 + c2) / 2;
const auto midh = mid || midheight(mid);
return {{midh, start, c1}, {midh, end, c2}};
}
diff --git a/game/network/network.h b/game/network/network.h
index 9ce6e81..fa311ac 100644
--- a/game/network/network.h
+++ b/game/network/network.h
@@ -26,32 +26,32 @@ public:
virtual ~Network() = default;
DEFAULT_MOVE_NO_COPY(Network);
- [[nodiscard]] Node::Ptr findNodeAt(Position3D) const;
- [[nodiscard]] Node::Ptr nodeAt(Position3D);
+ [[nodiscard]] Node::Ptr findNodeAt(GlobalPosition3D) const;
+ [[nodiscard]] Node::Ptr nodeAt(GlobalPosition3D);
enum class NodeIs { InNetwork, NotInNetwork };
using NodeInsertion = std::pair<Node::Ptr, NodeIs>;
- [[nodiscard]] NodeInsertion newNodeAt(Position3D);
- [[nodiscard]] NodeInsertion candidateNodeAt(Position3D) const;
+ [[nodiscard]] NodeInsertion newNodeAt(GlobalPosition3D);
+ [[nodiscard]] NodeInsertion candidateNodeAt(GlobalPosition3D) const;
[[nodiscard]] virtual Link::Ptr intersectRayLinks(const Ray<GlobalPosition3D> &) const = 0;
[[nodiscard]] virtual Node::Ptr intersectRayNodes(const Ray<GlobalPosition3D> &) const;
- [[nodiscard]] Link::Nexts routeFromTo(const Link::End &, Position3D) const;
+ [[nodiscard]] Link::Nexts routeFromTo(const Link::End &, GlobalPosition3D) const;
[[nodiscard]] Link::Nexts routeFromTo(const Link::End &, const Node::Ptr &) const;
- virtual Link::CCollection candidateStraight(Position3D, Position3D) = 0;
- virtual Link::CCollection candidateJoins(Position3D, Position3D) = 0;
- virtual Link::CCollection candidateExtend(Position3D, Position3D) = 0;
- virtual Link::CCollection addStraight(Position3D, Position3D) = 0;
- virtual Link::CCollection addJoins(Position3D, Position3D) = 0;
- virtual Link::CCollection addExtend(Position3D, Position3D) = 0;
+ 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(GlobalPosition3D, GlobalPosition3D) = 0;
+ virtual Link::CCollection addJoins(GlobalPosition3D, GlobalPosition3D) = 0;
+ virtual Link::CCollection addExtend(GlobalPosition3D, GlobalPosition3D) = 0;
[[nodiscard]] virtual float findNodeDirection(Node::AnyCPtr) const = 0;
protected:
static void joinLinks(const Link::Ptr & l, const Link::Ptr & ol);
- static GenCurveDef genCurveDef(const Position3D & start, const Position3D & end, float startDir);
+ static GenCurveDef genCurveDef(const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir);
static std::pair<GenCurveDef, GenCurveDef> genCurveDef(
- const Position3D & start, const Position3D & end, float startDir, float endDir);
+ const GlobalPosition3D & start, const GlobalPosition3D & end, float startDir, float endDir);
using Nodes = std::set<Node::Ptr, PtrMemberSorter<Node::Ptr, &Node::pos>>;
Nodes nodes;
@@ -71,7 +71,7 @@ protected:
public:
template<typename L, typename... Params>
std::shared_ptr<L>
- candidateLink(Position3D a, Position3D b, Params &&... params)
+ candidateLink(GlobalPosition3D a, GlobalPosition3D b, Params &&... params)
requires std::is_base_of_v<T, L>
{
const auto node1 = candidateNodeAt(a).first, node2 = candidateNodeAt(b).first;
@@ -80,7 +80,7 @@ public:
template<typename L, typename... Params>
std::shared_ptr<L>
- addLink(Position3D a, Position3D b, Params &&... params)
+ addLink(GlobalPosition3D a, GlobalPosition3D b, Params &&... params)
requires std::is_base_of_v<T, L>
{
const auto node1 = nodeAt(a), node2 = nodeAt(b);
@@ -89,12 +89,12 @@ public:
return l;
}
- Link::CCollection candidateStraight(Position3D n1, Position3D n2) override;
- Link::CCollection candidateJoins(Position3D, Position3D) override;
- Link::CCollection candidateExtend(Position3D, Position3D) override;
- Link::CCollection addStraight(Position3D n1, Position3D n2) override;
- Link::CCollection addJoins(Position3D, Position3D) override;
- Link::CCollection addExtend(Position3D, Position3D) override;
+ Link::CCollection candidateStraight(GlobalPosition3D n1, GlobalPosition3D n2) override;
+ Link::CCollection candidateJoins(GlobalPosition3D, GlobalPosition3D) override;
+ Link::CCollection candidateExtend(GlobalPosition3D, GlobalPosition3D) override;
+ Link::CCollection addStraight(GlobalPosition3D n1, GlobalPosition3D n2) override;
+ Link::CCollection addJoins(GlobalPosition3D, GlobalPosition3D) override;
+ Link::CCollection addExtend(GlobalPosition3D, GlobalPosition3D) override;
[[nodiscard]] float findNodeDirection(Node::AnyCPtr) const override;
diff --git a/game/network/network.impl.h b/game/network/network.impl.h
index 4acbb6d..f9595ed 100644
--- a/game/network/network.impl.h
+++ b/game/network/network.impl.h
@@ -54,16 +54,16 @@ NetworkOf<T>::findNodeDirection(Node::AnyCPtr n) const
template<typename T>
Link::CCollection
-NetworkOf<T>::candidateStraight(Position3D n1, Position3D n2)
+NetworkOf<T>::candidateStraight(GlobalPosition3D n1, GlobalPosition3D n2)
{
return {candidateLink<typename T::StraightLink>(n1, n2)};
}
template<typename T>
Link::CCollection
-NetworkOf<T>::candidateJoins(Position3D start, Position3D end)
+NetworkOf<T>::candidateJoins(GlobalPosition3D start, GlobalPosition3D end)
{
- if (glm::distance(start, end) < 2.F) {
+ if (glm::length(RelativePosition3D(start - end)) < 2000.F) {
return {};
}
const auto defs = genCurveDef(
@@ -75,7 +75,7 @@ NetworkOf<T>::candidateJoins(Position3D start, Position3D end)
template<typename T>
Link::CCollection
-NetworkOf<T>::candidateExtend(Position3D start, Position3D end)
+NetworkOf<T>::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)};
@@ -83,16 +83,16 @@ NetworkOf<T>::candidateExtend(Position3D start, Position3D end)
template<typename T>
Link::CCollection
-NetworkOf<T>::addStraight(Position3D n1, Position3D n2)
+NetworkOf<T>::addStraight(GlobalPosition3D n1, GlobalPosition3D n2)
{
return {addLink<typename T::StraightLink>(n1, n2)};
}
template<typename T>
Link::CCollection
-NetworkOf<T>::addJoins(Position3D start, Position3D end)
+NetworkOf<T>::addJoins(GlobalPosition3D start, GlobalPosition3D end)
{
- if (glm::distance(start, end) < 2.F) {
+ if (glm::length(RelativePosition3D(start - end)) < 2000.F) {
return {};
}
const auto defs = genCurveDef(start, end, findNodeDirection(nodeAt(start)), findNodeDirection(nodeAt(end)));
@@ -103,7 +103,7 @@ NetworkOf<T>::addJoins(Position3D start, Position3D end)
template<typename T>
Link::CCollection
-NetworkOf<T>::addExtend(Position3D start, Position3D end)
+NetworkOf<T>::addExtend(GlobalPosition3D start, GlobalPosition3D end)
{
const auto [cstart, cend, centre] = genCurveDef(start, end, findNodeDirection(nodeAt(start)));
return {addLink<typename T::CurveLink>(cstart, cend, centre)};
diff --git a/game/network/rail.cpp b/game/network/rail.cpp
index 303f1c8..79aaf97 100644
--- a/game/network/rail.cpp
+++ b/game/network/rail.cpp
@@ -9,9 +9,7 @@
#include <gfx/models/vertex.h>
#include <glad/gl.h>
#include <glm/gtx/transform.hpp>
-#include <initializer_list>
#include <maths.h>
-#include <stdexcept>
#include <utility>
#include <vector>
@@ -28,7 +26,7 @@ RailLinks::tick(TickDuration)
}
std::shared_ptr<RailLink>
-RailLinks::addLinksBetween(Position3D start, Position3D end)
+RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end)
{
auto node1ins = newNodeAt(start), node2ins = newNodeAt(end);
if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::NotInNetwork) {
@@ -41,31 +39,32 @@ RailLinks::addLinksBetween(Position3D start, Position3D end)
std::swap(start, end);
}
// Find start link/end - opposite entry dir to existing link; so pi +...
- const float dir = pi + findNodeDirection(node1ins.first);
+ const Angle dir = pi + findNodeDirection(node1ins.first);
if (dir == vector_yaw(end - start)) {
return addLink<RailLinkStraight>(start, end);
}
- const Position2D flatStart {start.xy()}, flatEnd {end.xy()};
+ const auto flatStart {start.xy()}, flatEnd {end.xy()};
if (node2ins.second == NodeIs::InNetwork) {
auto midheight = [&](auto mid) {
- const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid);
- return start.z + ((end.z - start.z) * (sm / (sm + em)));
+ const auto sm = glm::length(RelativePosition2D(flatStart - mid)),
+ em = glm::length(RelativePosition2D(flatEnd - mid));
+ return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (sm / (sm + em)));
};
const float dir2 = pi + findNodeDirection(node2ins.first);
if (const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); radii.first < radii.second) {
const auto radius {radii.first};
- const auto c1 = flatStart + sincosf(dir + half_pi) * radius;
- const auto c2 = flatEnd + sincosf(dir2 + half_pi) * radius;
- const auto mid = (c1 + c2) / 2.F;
+ const auto c1 = flatStart + GlobalPosition2D(sincosf(dir + half_pi) * radius);
+ const auto c2 = flatEnd + GlobalPosition2D(sincosf(dir2 + half_pi) * radius);
+ const auto mid = (c1 + c2) / 2;
const auto midh = mid || midheight(mid);
addLink<RailLinkCurve>(start, midh, c1);
return addLink<RailLinkCurve>(end, midh, c2);
}
else {
const auto radius {radii.second};
- const auto c1 = flatStart + sincosf(dir - half_pi) * radius;
- const auto c2 = flatEnd + sincosf(dir2 - half_pi) * radius;
- const auto mid = (c1 + c2) / 2.F;
+ const auto c1 = flatStart + GlobalPosition2D(sincosf(dir - half_pi) * radius);
+ const auto c2 = flatEnd + GlobalPosition2D(sincosf(dir2 - half_pi) * radius);
+ const auto mid = (c1 + c2) / 2;
const auto midh = mid || midheight(mid);
addLink<RailLinkCurve>(midh, start, c1);
return addLink<RailLinkCurve>(midh, end, c2);
@@ -100,7 +99,7 @@ RailLink::render(const SceneShader &) const
mesh->Draw();
}
-constexpr const std::array<std::pair<Position3D, float>, RAIL_CROSSSECTION_VERTICES> railCrossSection {{
+constexpr const std::array<std::pair<RelativePosition3D, float>, RAIL_CROSSSECTION_VERTICES> railCrossSection {{
// ___________
// _/ \_
// left to right
@@ -122,59 +121,57 @@ RailLinkStraight::RailLinkStraight(const Node::Ptr & a, const Node::Ptr & b) : R
{
}
-RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const Position3D & diff) :
+RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const RelativePosition3D & diff) :
Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff))
{
if (glGenVertexArrays) {
std::vector<Vertex> vertices;
vertices.reserve(2 * railCrossSection.size());
const auto len = round_sleepers(length / 2000.F);
- const auto e {flat_orientation(diff)};
+ const glm::mat3 trans {flat_orientation(diff)};
for (auto ei : {1U, 0U}) {
- const auto trans {glm::translate(ends[ei].node->pos) * e};
for (const auto & rcs : railCrossSection) {
- const Position3D m {(trans * (rcs.first || 1.F))};
- vertices.emplace_back(m, Position2D {rcs.second, len * static_cast<float>(ei)}, up);
+ const auto m {ends[ei].node->pos + GlobalPosition3D(trans * rcs.first)};
+ vertices.emplace_back(m, TextureRelCoord {rcs.second, len * static_cast<float>(ei)}, up);
}
}
mesh = defaultMesh(vertices);
}
}
-RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position2D c) :
- RailLinkCurve(a, b, c || a->pos.z, {c || 0.F, a->pos, b->pos})
+RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, GlobalPosition2D c) :
+ RailLinkCurve(a, b, c || a->pos.z, {c || 0, a->pos, b->pos})
{
}
-RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position3D c, const Arc arc) :
+RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, GlobalPosition3D c, const Arc arc) :
Link({a, normalize(arc.first + half_pi)}, {b, normalize(arc.second - half_pi)},
- (glm::length(a->pos - c)) * arc_length(arc)),
- LinkCurve {c, glm::length(ends[0].node->pos - c), arc}
+ glm::length(RelativePosition3D(a->pos - c)) * arc_length(arc)),
+ LinkCurve {c, glm::length(RelativePosition3D(ends[0].node->pos - c)), arc}
{
if (glGenVertexArrays) {
const auto & e0p {ends[0].node->pos};
const auto & e1p {ends[1].node->pos};
const auto slength = round_sleepers(length / 2.F);
const auto segs = std::round(slength / std::pow(radius, 0.7F));
- const auto step {Position3D {arc_length(arc), e1p.z - e0p.z, slength / 1000.F} / segs};
- const auto trans {glm::translate(centreBase)};
+ const auto step {RelativePosition3D {arc_length(arc), e1p.z - e0p.z, slength / 1000.F} / segs};
auto segCount = static_cast<std::size_t>(std::lround(segs)) + 1;
std::vector<Vertex> vertices;
vertices.reserve(segCount * railCrossSection.size());
- for (Position3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) {
+ for (RelativePosition3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) {
const auto t {
- trans * glm::rotate(half_pi - swing.x, up) * glm::translate(Position3D {radius, 0.F, swing.y})};
+ glm::rotate(half_pi - swing.x, up) * glm::translate(RelativePosition3D {radius, 0.F, swing.y})};
for (const auto & rcs : railCrossSection) {
- const Position3D m {(t * (rcs.first || 1.F))};
- vertices.emplace_back(m, Position2D {rcs.second, swing.z}, up);
+ const auto m {centreBase + GlobalPosition3D(t * (rcs.first || 1.F))};
+ vertices.emplace_back(m, TextureRelCoord {rcs.second, swing.z}, up);
}
}
mesh = defaultMesh(vertices);
}
}
-Position3D
+RelativePosition3D
RailLink::vehiclePositionOffset() const
{
return RAIL_HEIGHT;
diff --git a/game/network/rail.h b/game/network/rail.h
index 4a1932f..986b0aa 100644
--- a/game/network/rail.h
+++ b/game/network/rail.h
@@ -32,7 +32,7 @@ public:
NO_MOVE(RailLink);
protected:
- [[nodiscard]] Position3D vehiclePositionOffset() const override;
+ [[nodiscard]] RelativePosition3D vehiclePositionOffset() const override;
[[nodiscard]] static Mesh::Ptr defaultMesh(const std::span<Vertex> vertices);
Mesh::Ptr mesh;
@@ -45,22 +45,22 @@ public:
RailLinkStraight(const Node::Ptr &, const Node::Ptr &);
private:
- RailLinkStraight(Node::Ptr, Node::Ptr, const Position3D & diff);
+ RailLinkStraight(Node::Ptr, Node::Ptr, const RelativePosition3D & diff);
};
class RailLinkCurve : public RailLink, public LinkCurve {
public:
- RailLinkCurve(const Node::Ptr &, const Node::Ptr &, Position2D);
+ RailLinkCurve(const Node::Ptr &, const Node::Ptr &, GlobalPosition2D);
private:
- RailLinkCurve(const Node::Ptr &, const Node::Ptr &, Position3D, const Arc);
+ RailLinkCurve(const Node::Ptr &, const Node::Ptr &, GlobalPosition3D, const Arc);
};
class RailLinks : public NetworkOf<RailLink>, public WorldObject {
public:
RailLinks();
- std::shared_ptr<RailLink> addLinksBetween(Position3D start, Position3D end);
+ std::shared_ptr<RailLink> addLinksBetween(GlobalPosition3D start, GlobalPosition3D end);
private:
void tick(TickDuration elapsed) override;
diff --git a/lib/maths.h b/lib/maths.h
index b5af9ca..a867c39 100644
--- a/lib/maths.h
+++ b/lib/maths.h
@@ -157,9 +157,9 @@ find_arc_centre(glm::vec<2, T, Q> start, Rotation2D startDir, glm::vec<2, T, Q>
{
const auto det = endDir.x * startDir.y - endDir.y * startDir.x;
if (det != 0) { // near parallel line will yield noisy results
- const auto d = end - start;
+ const glm::vec<2, RelativeDistance, Q> d = end - start;
const auto u = (d.y * endDir.x - d.x * endDir.y) / det;
- return {start + startDir * u, u < 0};
+ return {start + glm::vec<2, T, Q>(startDir * u), u < 0};
}
throw std::runtime_error("no intersection");
}
@@ -186,16 +186,16 @@ find_arcs_radius(glm::vec<2, T, Q> start, Rotation2D ad, glm::vec<2, T, Q> end,
// r=(-2 m X+2 X o+2 m Z-2 o Z-2 n Y+2 Y p+2 n W-2 p W-sqrt((2 m X-2 X o-2 m Z+2 o Z+2 n Y-2 Y p-2 n W+2 p W)^(2)-4
// (X^(2)-2 X Z+Z^(2)+Y^(2)-2 Y W+W^(2)-4) (m^(2)-2 m o+o^(2)+n^(2)-2 n p+p^(2))))/(2 (X^(2)-2 X Z+Z^(2)+Y^(2)-2 Y
// W+W^(2)-4))
+ // Locally simplified to work relative, removing one half of the problem and operating on relative positions.
// These exist cos limitations of online formula rearrangement, and I'm OK with that.
- const auto &m {start.x}, &n {start.y}, &o {end.x}, &p {end.y};
+ const RelativePosition2D diff {end - start};
+ const auto &o {diff.x}, &p {diff.y};
const auto &X {ad.x}, &Y {ad.y}, &Z {bd.x}, &W {bd.y};
- return (2 * m * X - 2 * X * o - 2 * m * Z + 2 * o * Z + 2 * n * Y - 2 * Y * p - 2 * n * W + 2 * p * W
- - sqrt(sq(-2 * m * X + 2 * X * o + 2 * m * Z - 2 * o * Z - 2 * n * Y + 2 * Y * p + 2 * n * W
- - 2 * p * W)
- - (4 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4)
- * (sq(m) - 2 * m * o + sq(o) + sq(n) - 2 * n * p + sq(p)))))
+ return (-2 * X * o + 2 * o * Z - 2 * Y * p + 2 * p * W
+ - sqrt(sq(2 * X * o - 2 * o * Z + 2 * Y * p - 2 * p * W)
+ - (4 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4) * (sq(o) + sq(p)))))
/ (2 * (sq(X) - 2 * X * Z + sq(Z) + sq(Y) - 2 * Y * W + sq(W) - 4));
}
diff --git a/test/test-maths.cpp b/test/test-maths.cpp
index 8680409..ccfb113 100644
--- a/test/test-maths.cpp
+++ b/test/test-maths.cpp
@@ -177,7 +177,7 @@ BOOST_AUTO_TEST_CASE(test_find_arcs_radius)
struct TestLinkStraight : public LinkStraight {
explicit TestLinkStraight(glm::vec3 v) :
- Link {{std::make_shared<Node>(Position3D {}), vector_yaw(v)}, {std::make_shared<Node>(v), vector_yaw(-v)},
+ Link {{std::make_shared<Node>(GlobalPosition3D {}), vector_yaw(v)}, {std::make_shared<Node>(v), vector_yaw(-v)},
glm::length(v)}
{
}
@@ -216,7 +216,7 @@ struct TestLinkCurve : public LinkCurve {
}
};
-using CurvesData = std::tuple<glm::vec3 /*e1*/, glm::vec3 /*ctr*/, float /*angFor*/, float /* angBack*/>;
+using CurvesData = std::tuple<GlobalPosition3D /*e1*/, GlobalPosition3D /*ctr*/, Angle /*angFor*/, Angle /* angBack*/>;
BOOST_DATA_TEST_CASE(curve1,
boost::unit_test::data::make<CurvesData>({
@@ -232,13 +232,13 @@ BOOST_DATA_TEST_CASE(curve1,
BOOST_CHECK_EQUAL(l.radius, 1.F);
{
const auto p = l.positionAt(0, 0);
- BOOST_CHECK_CLOSE_VEC(RelativePosition3D {p.pos}, RelativePosition3D {});
+ BOOST_CHECK_CLOSE_VECI(p.pos, GlobalPosition3D {});
BOOST_CHECK_CLOSE_VEC(p.rot, glm::vec3(0, angFor, 0));
}
{
const auto p = l.positionAt(0, 1);
- BOOST_CHECK_CLOSE_VECI(RelativePosition3D {p.pos}, e1);
- BOOST_CHECK_CLOSE_VECI(p.rot, glm::vec3(0, angBack, 0));
+ BOOST_CHECK_CLOSE_VECI(p.pos, e1);
+ BOOST_CHECK_CLOSE_VEC(p.rot, glm::vec3(0, angBack, 0));
}
}
@@ -248,13 +248,13 @@ BOOST_DATA_TEST_CASE(curve1,
{
const auto p = l.positionAt(0, 0);
const auto angForReversed = normalize(vector_yaw(-e1) * 2 - angFor);
- BOOST_CHECK_CLOSE_VECI(RelativePosition3D {p.pos}, e1);
- BOOST_CHECK_CLOSE_VECI(p.rot, glm::vec3(0, angForReversed, 0));
+ BOOST_CHECK_CLOSE_VECI(p.pos, e1);
+ BOOST_CHECK_CLOSE_VEC(p.rot, glm::vec3(0, angForReversed, 0));
}
{
const auto p = l.positionAt(0, 1);
const auto angBackReversed = normalize(vector_yaw(e1) * 2 - angBack);
- BOOST_CHECK_CLOSE_VEC(RelativePosition3D {p.pos}, Position3D {});
+ BOOST_CHECK_CLOSE_VECI(p.pos, GlobalPosition3D {});
BOOST_CHECK_CLOSE_VEC(p.rot, glm::vec3(0, angBackReversed, 0));
}
}
@@ -330,6 +330,6 @@ BOOST_DATA_TEST_CASE(rayLineDistance,
for (float along = 0.2F; along <= 0.8F; along += 0.1F) {
const auto target = n1 + (along * nstep);
const auto direction = glm::normalize(target - c);
- BOOST_CHECK_LE(Ray(c, direction).distanceToLine(n1, n2), 0.01F);
+ BOOST_CHECK_LE(Ray<RelativePosition3D>(c, direction).distanceToLine(n1, n2), 0.01F);
}
}
diff --git a/test/test-network.cpp b/test/test-network.cpp
index ab0818a..0274b00 100644
--- a/test/test-network.cpp
+++ b/test/test-network.cpp
@@ -26,7 +26,10 @@ BOOST_GLOBAL_FIXTURE(TestMainWindow);
struct TestLink : public LinkStraight {
TestLink(const Node::Ptr & a, const Node::Ptr & b) : TestLink {a, b, (a->pos - b->pos)} { }
- TestLink(Node::Ptr a, Node::Ptr b, glm::vec2 l) : Link {{std::move(a), 0}, {std::move(b), pi}, glm::length(l)} { }
+ TestLink(Node::Ptr a, Node::Ptr b, RelativePosition2D l) :
+ Link {{std::move(a), 0}, {std::move(b), pi}, glm::length(l)}
+ {
+ }
TestLink(Node::Ptr a, Node::Ptr b, float l) : Link {{std::move(a), 0}, {std::move(b), pi}, l} { }
@@ -34,8 +37,8 @@ struct TestLink : public LinkStraight {
using CurveLink = TestLink;
};
-constexpr glm::vec3 p000 {0, 0, 0}, p100 {1, 0, 0}, p200 {2, 0, 0}, p300 {3, 0, 0};
-constexpr glm::vec3 p110 {1, 1, 0};
+constexpr GlobalPosition3D p000 {0, 0, 0}, p100 {10000, 0, 0}, p200 {20000, 0, 0}, p300 {30000, 0, 0};
+constexpr GlobalPosition3D p110 {10000, 10000, 0};
struct TestNetwork : public NetworkOf<TestLink> {
TestNetwork() : NetworkOf<TestLink> {RESDIR "rails.jpg"}
@@ -55,13 +58,13 @@ struct TestNetwork : public NetworkOf<TestLink> {
}
};
-const auto VALID_NODES = boost::unit_test::data::make<glm::vec3>({
+const auto VALID_NODES = boost::unit_test::data::make<GlobalPosition3D>({
p000,
p100,
p200,
p300,
});
-const auto INVALID_NODES = boost::unit_test::data::make<glm::vec3>({
+const auto INVALID_NODES = boost::unit_test::data::make<GlobalPosition3D>({
{1000, 0, 0},
{0, 1000, 0},
{0, 0, 1000},
@@ -201,7 +204,7 @@ BOOST_FIXTURE_TEST_CASE(test_rail_network, RailLinks)
// --------
auto l0 = addLinksBetween(p000, p100);
BOOST_CHECK(dynamic_cast<RailLinkStraight *>(l0.get()));
- BOOST_CHECK_EQUAL(l0->length, 1.F);
+ BOOST_CHECK_EQUAL(l0->length, 10000);
BOOST_CHECK_CLOSE(l0->ends[0].dir, half_pi, 0.1F);
BOOST_CHECK_CLOSE(l0->ends[1].dir, -half_pi, 0.1F);
BOOST_CHECK(l0->ends[0].nexts.empty());
@@ -209,7 +212,7 @@ BOOST_FIXTURE_TEST_CASE(test_rail_network, RailLinks)
auto l1 = addLinksBetween(p200, p100);
BOOST_CHECK(dynamic_cast<RailLinkStraight *>(l1.get()));
- BOOST_CHECK_EQUAL(l1->length, 1.F);
+ BOOST_CHECK_EQUAL(l1->length, 10000);
BOOST_CHECK_CLOSE(l1->ends[0].dir, half_pi, 0.1F);
BOOST_CHECK_CLOSE(l1->ends[1].dir, -half_pi, 0.1F);
BOOST_CHECK(l0->ends[0].nexts.empty());
@@ -221,7 +224,7 @@ BOOST_FIXTURE_TEST_CASE(test_rail_network, RailLinks)
auto l2 = addLinksBetween(p200, p300);
BOOST_CHECK(dynamic_cast<RailLinkStraight *>(l2.get()));
- BOOST_CHECK_EQUAL(l2->length, 1.F);
+ BOOST_CHECK_EQUAL(l2->length, 10000);
BOOST_CHECK_CLOSE(l2->ends[0].dir, half_pi, 0.1F);
BOOST_CHECK_CLOSE(l2->ends[1].dir, -half_pi, 0.1F);
BOOST_CHECK(l0->ends[0].nexts.empty());
@@ -233,7 +236,7 @@ BOOST_FIXTURE_TEST_CASE(test_rail_network, RailLinks)
auto l3 = addLinksBetween(p000, p110);
BOOST_CHECK(dynamic_cast<RailLinkCurve *>(l3.get()));
- BOOST_CHECK_CLOSE(l3->length, pi + half_pi, 0.1F);
+ BOOST_CHECK_CLOSE(l3->length, (pi + half_pi) * 10000.F, 0.1F);
BOOST_CHECK_CLOSE(l3->ends[0].dir, -half_pi, 0.1F);
BOOST_CHECK_CLOSE(l3->ends[1].dir, 0, 0.1F);
BOOST_CHECK_EQUAL(l0->ends[0].nexts.at(0).first.lock(), l3);
@@ -243,7 +246,7 @@ BOOST_FIXTURE_TEST_CASE(test_rail_network, RailLinks)
BOOST_CHECK(l3->ends[1].nexts.empty());
auto l4 = addLinksBetween(p110, p300);
- BOOST_CHECK_CLOSE(l4->length, 3.04F, 0.1F);
+ BOOST_CHECK_CLOSE(l4->length, 30400.F, 0.1F);
BOOST_CHECK_BETWEEN(l4->ends[0].dir, .23F, .24F);
BOOST_CHECK_CLOSE(l4->ends[1].dir, half_pi, 0.1F);
}
diff --git a/test/testStructures.h b/test/testStructures.h
index aaaf940..064c00c 100644
--- a/test/testStructures.h
+++ b/test/testStructures.h
@@ -32,9 +32,9 @@ struct TestObject : public Persistence::Persistable {
float flt {};
std::string str {};
bool bl {};
- Position3D pos {};
+ RelativePosition3D pos {};
std::vector<float> flts;
- std::vector<Position3D> poss;
+ std::vector<RelativePosition3D> poss;
std::vector<std::vector<std::vector<std::string>>> nest;
std::unique_ptr<TestObject> ptr;
std::unique_ptr<AbsObject> aptr;
diff --git a/ui/builders/freeExtend.h b/ui/builders/freeExtend.h
index 127fdc6..0d5f327 100644
--- a/ui/builders/freeExtend.h
+++ b/ui/builders/freeExtend.h
@@ -11,5 +11,5 @@ class BuilderFreeExtend : public EditNetwork::Builder {
void move(Network * network, const GeoData * geoData, const SDL_MouseMotionEvent & e,
const Ray<GlobalPosition3D> & ray) override;
- std::optional<Position3D> p1;
+ std::optional<GlobalPosition3D> p1;
};
diff --git a/ui/builders/straight.cpp b/ui/builders/straight.cpp
index 866705a..0c4a3e2 100644
--- a/ui/builders/straight.cpp
+++ b/ui/builders/straight.cpp
@@ -49,7 +49,7 @@ BuilderStraight::click(
}
void
-BuilderStraight::create(Network * network, Position3D p1, Position3D p2) const
+BuilderStraight::create(Network * network, GlobalPosition3D p1, GlobalPosition3D p2) const
{
network->addStraight(p1, p2);
}
diff --git a/ui/builders/straight.h b/ui/builders/straight.h
index 63f9a40..28eb66e 100644
--- a/ui/builders/straight.h
+++ b/ui/builders/straight.h
@@ -11,7 +11,7 @@ class BuilderStraight : public EditNetwork::Builder {
void move(Network * network, const GeoData * geoData, const SDL_MouseMotionEvent & e,
const Ray<GlobalPosition3D> & ray) override;
- void create(Network * network, Position3D p1, Position3D p2) const;
+ void create(Network * network, GlobalPosition3D p1, GlobalPosition3D p2) const;
- std::optional<Position3D> p1;
+ std::optional<GlobalPosition3D> p1;
};
diff --git a/ui/editNetwork.h b/ui/editNetwork.h
index c8a2f13..23dcf43 100644
--- a/ui/editNetwork.h
+++ b/ui/editNetwork.h
@@ -21,7 +21,7 @@ public:
void render(const SceneShader &) const override;
void render(const UIShader & shader, const UIComponent::Position & pos) const override;
- using NetworkClickPos = std::variant<Position3D, Node::Ptr>;
+ using NetworkClickPos = std::variant<GlobalPosition3D, Node::Ptr>;
class Builder {
public: