Skip to content

Commit

Permalink
More improvements to positioning, minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leezer3 committed Jul 19, 2024
1 parent 6d3e3df commit 2539167
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 85 deletions.
23 changes: 13 additions & 10 deletions source/Plugins/Route.Bve5/MapParser/ApplyRouteData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,9 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
string key = Data.Blocks[i].FreeObj[railKey][k].Key;
double span = Data.Blocks[i].FreeObj[railKey][k].Span;
ObjectTransformType type = Data.Blocks[i].FreeObj[railKey][k].Type;
double dx = Data.Blocks[i].FreeObj[railKey][k].X;
double dy = Data.Blocks[i].FreeObj[railKey][k].Y;
double dz = Data.Blocks[i].FreeObj[railKey][k].Z;
double dx = Data.Blocks[i].FreeObj[railKey][k].Position.X;
double dy = Data.Blocks[i].FreeObj[railKey][k].Position.Y;
double dz = Data.Blocks[i].FreeObj[railKey][k].Position.Z;
double tpos = Data.Blocks[i].FreeObj[railKey][k].TrackPosition;
int nextBlock = i + 1;
bool useAverages = false;
Expand All @@ -427,6 +427,9 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
* Convert these to a transformation
*
* This gets the best results currently, but it's odd as pitch really ought to be averaged too :/
*
* NOTES:
* The averages calculation still isn't right on s
*/
while (nextBlock < Data.Blocks.Count)
{
Expand All @@ -447,7 +450,7 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
Transformation Transformation;
if (j == 0)
{
GetTransformation(Position, Data.Blocks, i, tpos, type, span, Direction, out wpos, out Transformation);
GetTransformation(Position, Data.Blocks, i, Data.Blocks[i].FreeObj[railKey][k], Direction, out wpos, out Transformation);
}
else
{
Expand Down Expand Up @@ -498,7 +501,7 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
Transformation Transformation;
if (j == 0)
{
GetTransformation(Position, Data.Blocks, i, tpos, ObjectTransformType.FollowsGradient, InterpolateInterval, Direction, out wpos, out Transformation);
GetTransformation(Position, Data.Blocks, i, Data.Blocks[i].Cracks[k], Direction, out wpos, out Transformation);
}
else
{
Expand Down Expand Up @@ -548,18 +551,18 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData

for (int k = 0; k < Data.Blocks[i].Signals[j].Count; k++)
{
string key = Data.Blocks[i].Signals[j][k].SignalObjectKey;
string key = Data.Blocks[i].Signals[j][k].Key;
double span = Data.Blocks[i].Signals[j][k].Span;
ObjectTransformType type = Data.Blocks[i].Signals[j][k].Type;
double dx = Data.Blocks[i].Signals[j][k].X;
double dy = Data.Blocks[i].Signals[j][k].Y;
double dz = Data.Blocks[i].Signals[j][k].Z;
double dx = Data.Blocks[i].Signals[j][k].Position.X;
double dy = Data.Blocks[i].Signals[j][k].Position.Y;
double dz = Data.Blocks[i].Signals[j][k].Position.Z;
double tpos = Data.Blocks[i].Signals[j][k].TrackPosition;
Vector3 wpos;
Transformation Transformation;
if (j == 0)
{
GetTransformation(Position, Data.Blocks, i, tpos, type, span, Direction, out wpos, out Transformation);
GetTransformation(Position, Data.Blocks, i, Data.Blocks[i].Signals[j][k], Direction, out wpos, out Transformation);
}
else
{
Expand Down
26 changes: 8 additions & 18 deletions source/Plugins/Route.Bve5/MapParser/ConfirmComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,8 @@ private static void ConfirmStructure(bool PreviewOnly, MapData ParseData, RouteD
Blocks[BlockIndex].FreeObj.Add(TrackKey, new List<FreeObj>());
}

Blocks[BlockIndex].FreeObj[TrackKey].Add(new FreeObj(Statement.Distance, Statement.Key, Statement.GetArgumentValueAsDouble(ArgumentName.X), Statement.GetArgumentValueAsDouble(ArgumentName.Y), Statement.GetArgumentValueAsDouble(ArgumentName.Z), RY * 0.0174532925199433, -RX * 0.0174532925199433, RZtoRoll(RY, RZ) * 0.0174532925199433, (ObjectTransformType)Tilt, Span));
Vector3 position = new Vector3(Statement.GetArgumentValueAsDouble(ArgumentName.X), Statement.GetArgumentValueAsDouble(ArgumentName.Y), Statement.GetArgumentValueAsDouble(ArgumentName.Z));
Blocks[BlockIndex].FreeObj[TrackKey].Add(new FreeObj(Statement.Distance, Statement.Key, position, RY * 0.0174532925199433, -RX * 0.0174532925199433, RZtoRoll(RY, RZ) * 0.0174532925199433, (ObjectTransformType)Tilt, Span));
}
}
break;
Expand Down Expand Up @@ -557,9 +558,7 @@ private static void ConfirmRepeater(bool PreviewOnly, MapData ParseData, RouteDa

Repeater.StartingDistance = Statement.Distance;
Repeater.TrackKey = Convert.ToString(TrackKey);
Repeater.X = Statement.GetArgumentValueAsDouble(ArgumentName.X);
Repeater.Y = Statement.GetArgumentValueAsDouble(ArgumentName.Y);
Repeater.Z = Statement.GetArgumentValueAsDouble(ArgumentName.Z);
Repeater.Position = new Vector3(Statement.GetArgumentValueAsDouble(ArgumentName.X), Statement.GetArgumentValueAsDouble(ArgumentName.Y), Statement.GetArgumentValueAsDouble(ArgumentName.Z));
Repeater.Yaw = RY * 0.0174532925199433;
Repeater.Pitch = -RX * 0.0174532925199433;
Repeater.Roll = RZtoRoll(RY, RZ) * 0.0174532925199433;
Expand Down Expand Up @@ -634,7 +633,7 @@ private static void PutRepeater(RouteData RouteData, Repeater Repeater)
Blocks[BlockIndex].FreeObj.Add(TrackKey, new List<FreeObj>());
}

Blocks[BlockIndex].FreeObj[TrackKey].Add(new FreeObj(i, Repeater.ObjectKeys[LoopCount], Repeater.X, Repeater.Y, Repeater.Z, Repeater.Yaw, Repeater.Pitch, Repeater.Roll, Repeater.Type, Repeater.Span));
Blocks[BlockIndex].FreeObj[TrackKey].Add(new FreeObj(i, Repeater.ObjectKeys[LoopCount], Repeater.Position, Repeater.Yaw, Repeater.Pitch, Repeater.Roll, Repeater.Type, Repeater.Span));

if (LoopCount >= Repeater.ObjectKeys.Length - 1)
{
Expand Down Expand Up @@ -734,14 +733,11 @@ private static void ConfirmSignal(bool PreviewOnly, MapData ParseData, RouteData
TrackKey = "0";
}

object X = d.X;
object Y = d.Y;
object Z = d.Z;
object RX = d.RX;
object RY = d.RY;
object RZ = d.RZ;
object Tilt = d.Tilt;
object Span = d.Span;
ObjectTransformType Tilt = (ObjectTransformType)d.Tilt;
double Span = (double)d.Span;

int RailIndex = RouteData.TrackKeyList.IndexOf(Convert.ToString(TrackKey));

Expand All @@ -760,19 +756,13 @@ private static void ConfirmSignal(bool PreviewOnly, MapData ParseData, RouteData
CurrentSection += Blocks[i].Sections.Count(s => s.TrackPosition <= Statement.Distance);
}

Blocks[BlockIndex].Signals[RailIndex].Add(new Signal
Vector3 Position = new Vector3((double)d.X, (double)d.Y, (double)d.Z);
Blocks[BlockIndex].Signals[RailIndex].Add(new Signal(Statement.Key, Statement.Distance, Tilt, Span, Position)
{
TrackPosition = Statement.Distance,
SignalObjectKey = Statement.Key,
SectionIndex = CurrentSection + Convert.ToInt32(Section),
X = Convert.ToDouble(X),
Y = Convert.ToDouble(Y),
Z = Convert.ToDouble(Z),
Yaw = Convert.ToDouble(RY) * 0.0174532925199433,
Pitch = -Convert.ToDouble(RX) * 0.0174532925199433,
Roll = RZtoRoll(Convert.ToDouble(RY), Convert.ToDouble(RZ)) * 0.0174532925199433,
Type = (ObjectTransformType)Convert.ToInt32(Tilt),
Span = Convert.ToDouble(Span)
});
}
}
Expand Down
34 changes: 24 additions & 10 deletions source/Plugins/Route.Bve5/MapParser/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private static void CalcTransformation(double CurveRadius, double Pitch, double
}
}

private static void GetTransformation(Vector3 StartingPosition, List<Block> Blocks, int StartingBlock, double TrackDistance, ObjectTransformType Type, double Span, Vector2 Direction, out Vector3 ObjectPosition, out Transformation Transformation)
private static void GetTransformation(Vector3 StartingPosition, List<Block> Blocks, int StartingBlock, AbstractStructure Structure, Vector2 Direction, out Vector3 ObjectPosition, out Transformation Transformation)
{
if (Blocks[StartingBlock].Turn != 0.0)
{
Expand All @@ -260,7 +260,7 @@ private static void GetTransformation(Vector3 StartingPosition, List<Block> Bloc
double radius = 0;
double pitch = 0;
double cant = 0;
if (Span == 0)
if (Structure.Span == 0)
{
radius = Blocks[StartingBlock].CurrentTrackState.CurveRadius;
pitch = Blocks[StartingBlock].Pitch;
Expand All @@ -270,7 +270,8 @@ private static void GetTransformation(Vector3 StartingPosition, List<Block> Bloc
{

int currentBlock = StartingBlock + 1;
double remainingDistance = Span;
double remainingDistance = Structure.Span;
bool averages = false;
while (currentBlock < Blocks.Count - 1)
{
double blockLength = currentBlock != 0 ? Blocks[currentBlock].StartingDistance - Blocks[currentBlock - 1].StartingDistance : 0;
Expand All @@ -284,16 +285,29 @@ private static void GetTransformation(Vector3 StartingPosition, List<Block> Bloc
break;
}

if (Blocks[currentBlock].CurrentTrackState.CurveRadius != Blocks[StartingBlock].CurrentTrackState.CurveRadius || Blocks[currentBlock].CurrentTrackState.CurveCant != Blocks[StartingBlock].CurrentTrackState.CurveCant || Blocks[currentBlock].Pitch != Blocks[StartingBlock].Pitch)
{
averages = true;
}
currentBlock++;
}

radius /= Span;
pitch /= Span;
cant /= Span;
if (averages)
{
radius /= Structure.Span;
pitch /= Structure.Span;
cant /= Structure.Span;
}
else
{
radius = Blocks[StartingBlock].CurrentTrackState.CurveRadius;
pitch = Blocks[StartingBlock].Pitch;
cant = Blocks[StartingBlock].CurrentTrackState.CurveCant;
}

}


CalcTransformation(radius, pitch, TrackDistance - Blocks[StartingBlock].StartingDistance, ref Direction, out double a, out double c, out double h);
CalcTransformation(radius, pitch, Structure.TrackPosition - Blocks[StartingBlock].StartingDistance, ref Direction, out double a, out double c, out double h);
ObjectPosition.X += Direction.X * c;
ObjectPosition.Y += h;
ObjectPosition.Z += Direction.Y * c;
Expand All @@ -302,13 +316,13 @@ private static void GetTransformation(Vector3 StartingPosition, List<Block> Bloc
Direction.Rotate(Math.Cos(-a), Math.Sin(-a));
}

CalcTransformation(radius, pitch, Span, ref Direction, out _, out _, out _);
CalcTransformation(radius, pitch, Structure.Span, ref Direction, out _, out _, out _);

double TrackYaw = Math.Atan2(Direction.X, Direction.Y);
double TrackPitch = Math.Atan(pitch);
double TrackRoll = Math.Atan(cant);

switch (Type)
switch (Structure.Type)
{
case ObjectTransformType.FollowsGradient:
Transformation = new Transformation(TrackYaw, TrackPitch, 0.0);
Expand Down
60 changes: 13 additions & 47 deletions source/Plugins/Route.Bve5/MapParser/Structures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System.Collections.Generic;
using OpenBveApi.Interface;
using OpenBveApi.Math;
using OpenBveApi.Objects;
using OpenBveApi.Routes;
Expand Down Expand Up @@ -66,41 +67,20 @@ private class Station
internal double InterferenceInDoor;
}

private class FreeObj
private class FreeObj : AbstractStructure
{
/// <summary>The track position of the object</summary>
internal readonly double TrackPosition;
/// <summary>The routefile key of the object</summary>
internal readonly string Key;
/// <summary>The X position of the object (m)</summary>
internal readonly double X;
/// <summary>The Y position of the object (m)</summary>
internal readonly double Y;
/// <summary>The Z position of the object (m)</summary>
internal readonly double Z;
/// <summary>The yaw of the object (radians)</summary>
internal readonly double Yaw;
/// <summary>The pitch of the object (radians)</summary>
internal readonly double Pitch;
/// <summary>The roll of the object (radians)</summary>
internal readonly double Roll;

internal readonly ObjectTransformType Type;

internal readonly double Span;

internal FreeObj(double trackPosition, string key, double x, double y, double z, double yaw, double pitch, double roll, ObjectTransformType type, double span)

internal FreeObj(double trackPosition, string key, Vector3 position, double yaw, double pitch, double roll, ObjectTransformType type, double span) : base(trackPosition, key, type, span, position)
{
TrackPosition = trackPosition;
Key = key;
X = x;
Y = y;
Z = z;
Yaw= yaw;
Pitch = pitch;
Roll = roll;
Type = type;
Span = span;
}
}

Expand All @@ -113,12 +93,7 @@ private class Repeater
internal double EndingDistance;
internal double Interval;
internal string[] ObjectKeys;
/// <summary>The X position of the object (m)</summary>
internal double X;
/// <summary>The Y position of the object (m)</summary>
internal double Y;
/// <summary>The Z position of the object (m)</summary>
internal double Z;
internal Vector3 Position;
/// <summary>The yaw of the object (radians)</summary>
internal double Yaw;
/// <summary>The pitch of the object (radians)</summary>
Expand All @@ -136,20 +111,13 @@ internal Repeater(string key)
}
}

private class Crack
private class Crack : AbstractStructure
{
/// <summary>The track position of the object</summary>
internal readonly double TrackPosition;
/// <summary>The routefile key of the object</summary>
internal readonly string Key;

internal readonly string PrimaryRail;
internal readonly string SecondaryRail;

internal Crack(string key, double trackPosition, string primaryRail, string secondaryRail)
internal Crack(string key, double trackPosition, string primaryRail, string secondaryRail) : base(trackPosition, key, ObjectTransformType.FollowsGradient, 0, Vector3.Zero)
{
Key = key;
TrackPosition = trackPosition;
PrimaryRail = primaryRail;
SecondaryRail = secondaryRail;
}
Expand All @@ -162,19 +130,17 @@ private class Section
internal int DepartureStationIndex = -1;
}

private class Signal
private class Signal : AbstractStructure
{
internal double TrackPosition;
internal string SignalObjectKey;
internal double X;
internal double Y;
internal double Z;
internal double Yaw;
internal double Pitch;
internal double Roll;
internal ObjectTransformType Type;
internal double Span;
internal int SectionIndex;

internal Signal(string key, double trackPosition, ObjectTransformType type, double span, Vector3 position) : base(trackPosition, key, type, span, position)
{

}
}

private class Transponder
Expand Down
1 change: 1 addition & 0 deletions source/Plugins/Route.Bve5/Route.Bve5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="Plugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScenarioParser.cs" />
<Compile Include="Structures\AbstractStructure.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\LibRender2\LibRender2.csproj">
Expand Down
53 changes: 53 additions & 0 deletions source/Plugins/Route.Bve5/Structures/AbstractStructure.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Simplified BSD License (BSD-2-Clause)
//
//Copyright (c) 2020, S520, The OpenBVE Project
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions are met:
//
//1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using OpenBveApi.Math;

namespace Route.Bve5
{
/// <summary>An abstract class for structures to be placed</summary>
internal abstract class AbstractStructure
{
/// <summary>The track position of the object</summary>
internal readonly double TrackPosition;
/// <summary>The routefile key of the object</summary>
internal readonly string Key;
/// <summary>The object transform type to be applied</summary>
internal readonly ObjectTransformType Type;
/// <summary>The span of the object (used to calculate transforms when spanning multiple blocks)</summary>
/// <remarks>Usually the physical length of the object, but this may not necessarily conform</remarks>
internal readonly double Span;
/// <summary>The position of the object</summary>
internal readonly Vector3 Position;

internal AbstractStructure(double trackPosition, string key, ObjectTransformType type, double span, Vector3 position)
{
TrackPosition = trackPosition;
Key = key;
Type = type;
Span = span;
Position = new Vector3(position);
}
}
}

0 comments on commit 2539167

Please sign in to comment.