Skip to content

Commit 2539167

Browse files
committed
More improvements to positioning, minor refactoring
1 parent 6d3e3df commit 2539167

File tree

6 files changed

+112
-85
lines changed

6 files changed

+112
-85
lines changed

source/Plugins/Route.Bve5/MapParser/ApplyRouteData.cs

+13-10
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
406406
string key = Data.Blocks[i].FreeObj[railKey][k].Key;
407407
double span = Data.Blocks[i].FreeObj[railKey][k].Span;
408408
ObjectTransformType type = Data.Blocks[i].FreeObj[railKey][k].Type;
409-
double dx = Data.Blocks[i].FreeObj[railKey][k].X;
410-
double dy = Data.Blocks[i].FreeObj[railKey][k].Y;
411-
double dz = Data.Blocks[i].FreeObj[railKey][k].Z;
409+
double dx = Data.Blocks[i].FreeObj[railKey][k].Position.X;
410+
double dy = Data.Blocks[i].FreeObj[railKey][k].Position.Y;
411+
double dz = Data.Blocks[i].FreeObj[railKey][k].Position.Z;
412412
double tpos = Data.Blocks[i].FreeObj[railKey][k].TrackPosition;
413413
int nextBlock = i + 1;
414414
bool useAverages = false;
@@ -427,6 +427,9 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
427427
* Convert these to a transformation
428428
*
429429
* This gets the best results currently, but it's odd as pitch really ought to be averaged too :/
430+
*
431+
* NOTES:
432+
* The averages calculation still isn't right on s
430433
*/
431434
while (nextBlock < Data.Blocks.Count)
432435
{
@@ -447,7 +450,7 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
447450
Transformation Transformation;
448451
if (j == 0)
449452
{
450-
GetTransformation(Position, Data.Blocks, i, tpos, type, span, Direction, out wpos, out Transformation);
453+
GetTransformation(Position, Data.Blocks, i, Data.Blocks[i].FreeObj[railKey][k], Direction, out wpos, out Transformation);
451454
}
452455
else
453456
{
@@ -498,7 +501,7 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
498501
Transformation Transformation;
499502
if (j == 0)
500503
{
501-
GetTransformation(Position, Data.Blocks, i, tpos, ObjectTransformType.FollowsGradient, InterpolateInterval, Direction, out wpos, out Transformation);
504+
GetTransformation(Position, Data.Blocks, i, Data.Blocks[i].Cracks[k], Direction, out wpos, out Transformation);
502505
}
503506
else
504507
{
@@ -548,18 +551,18 @@ private static void ApplyRouteData(string FileName, bool PreviewOnly, RouteData
548551

549552
for (int k = 0; k < Data.Blocks[i].Signals[j].Count; k++)
550553
{
551-
string key = Data.Blocks[i].Signals[j][k].SignalObjectKey;
554+
string key = Data.Blocks[i].Signals[j][k].Key;
552555
double span = Data.Blocks[i].Signals[j][k].Span;
553556
ObjectTransformType type = Data.Blocks[i].Signals[j][k].Type;
554-
double dx = Data.Blocks[i].Signals[j][k].X;
555-
double dy = Data.Blocks[i].Signals[j][k].Y;
556-
double dz = Data.Blocks[i].Signals[j][k].Z;
557+
double dx = Data.Blocks[i].Signals[j][k].Position.X;
558+
double dy = Data.Blocks[i].Signals[j][k].Position.Y;
559+
double dz = Data.Blocks[i].Signals[j][k].Position.Z;
557560
double tpos = Data.Blocks[i].Signals[j][k].TrackPosition;
558561
Vector3 wpos;
559562
Transformation Transformation;
560563
if (j == 0)
561564
{
562-
GetTransformation(Position, Data.Blocks, i, tpos, type, span, Direction, out wpos, out Transformation);
565+
GetTransformation(Position, Data.Blocks, i, Data.Blocks[i].Signals[j][k], Direction, out wpos, out Transformation);
563566
}
564567
else
565568
{

source/Plugins/Route.Bve5/MapParser/ConfirmComponents.cs

+8-18
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ private static void ConfirmStructure(bool PreviewOnly, MapData ParseData, RouteD
468468
Blocks[BlockIndex].FreeObj.Add(TrackKey, new List<FreeObj>());
469469
}
470470

471-
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));
471+
Vector3 position = new Vector3(Statement.GetArgumentValueAsDouble(ArgumentName.X), Statement.GetArgumentValueAsDouble(ArgumentName.Y), Statement.GetArgumentValueAsDouble(ArgumentName.Z));
472+
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));
472473
}
473474
}
474475
break;
@@ -557,9 +558,7 @@ private static void ConfirmRepeater(bool PreviewOnly, MapData ParseData, RouteDa
557558

558559
Repeater.StartingDistance = Statement.Distance;
559560
Repeater.TrackKey = Convert.ToString(TrackKey);
560-
Repeater.X = Statement.GetArgumentValueAsDouble(ArgumentName.X);
561-
Repeater.Y = Statement.GetArgumentValueAsDouble(ArgumentName.Y);
562-
Repeater.Z = Statement.GetArgumentValueAsDouble(ArgumentName.Z);
561+
Repeater.Position = new Vector3(Statement.GetArgumentValueAsDouble(ArgumentName.X), Statement.GetArgumentValueAsDouble(ArgumentName.Y), Statement.GetArgumentValueAsDouble(ArgumentName.Z));
563562
Repeater.Yaw = RY * 0.0174532925199433;
564563
Repeater.Pitch = -RX * 0.0174532925199433;
565564
Repeater.Roll = RZtoRoll(RY, RZ) * 0.0174532925199433;
@@ -634,7 +633,7 @@ private static void PutRepeater(RouteData RouteData, Repeater Repeater)
634633
Blocks[BlockIndex].FreeObj.Add(TrackKey, new List<FreeObj>());
635634
}
636635

637-
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));
636+
Blocks[BlockIndex].FreeObj[TrackKey].Add(new FreeObj(i, Repeater.ObjectKeys[LoopCount], Repeater.Position, Repeater.Yaw, Repeater.Pitch, Repeater.Roll, Repeater.Type, Repeater.Span));
638637

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

737-
object X = d.X;
738-
object Y = d.Y;
739-
object Z = d.Z;
740736
object RX = d.RX;
741737
object RY = d.RY;
742738
object RZ = d.RZ;
743-
object Tilt = d.Tilt;
744-
object Span = d.Span;
739+
ObjectTransformType Tilt = (ObjectTransformType)d.Tilt;
740+
double Span = (double)d.Span;
745741

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

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

763-
Blocks[BlockIndex].Signals[RailIndex].Add(new Signal
759+
Vector3 Position = new Vector3((double)d.X, (double)d.Y, (double)d.Z);
760+
Blocks[BlockIndex].Signals[RailIndex].Add(new Signal(Statement.Key, Statement.Distance, Tilt, Span, Position)
764761
{
765-
TrackPosition = Statement.Distance,
766-
SignalObjectKey = Statement.Key,
767762
SectionIndex = CurrentSection + Convert.ToInt32(Section),
768-
X = Convert.ToDouble(X),
769-
Y = Convert.ToDouble(Y),
770-
Z = Convert.ToDouble(Z),
771763
Yaw = Convert.ToDouble(RY) * 0.0174532925199433,
772764
Pitch = -Convert.ToDouble(RX) * 0.0174532925199433,
773765
Roll = RZtoRoll(Convert.ToDouble(RY), Convert.ToDouble(RZ)) * 0.0174532925199433,
774-
Type = (ObjectTransformType)Convert.ToInt32(Tilt),
775-
Span = Convert.ToDouble(Span)
776766
});
777767
}
778768
}

source/Plugins/Route.Bve5/MapParser/Functions.cs

+24-10
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private static void CalcTransformation(double CurveRadius, double Pitch, double
245245
}
246246
}
247247

248-
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)
248+
private static void GetTransformation(Vector3 StartingPosition, List<Block> Blocks, int StartingBlock, AbstractStructure Structure, Vector2 Direction, out Vector3 ObjectPosition, out Transformation Transformation)
249249
{
250250
if (Blocks[StartingBlock].Turn != 0.0)
251251
{
@@ -260,7 +260,7 @@ private static void GetTransformation(Vector3 StartingPosition, List<Block> Bloc
260260
double radius = 0;
261261
double pitch = 0;
262262
double cant = 0;
263-
if (Span == 0)
263+
if (Structure.Span == 0)
264264
{
265265
radius = Blocks[StartingBlock].CurrentTrackState.CurveRadius;
266266
pitch = Blocks[StartingBlock].Pitch;
@@ -270,7 +270,8 @@ private static void GetTransformation(Vector3 StartingPosition, List<Block> Bloc
270270
{
271271

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

288+
if (Blocks[currentBlock].CurrentTrackState.CurveRadius != Blocks[StartingBlock].CurrentTrackState.CurveRadius || Blocks[currentBlock].CurrentTrackState.CurveCant != Blocks[StartingBlock].CurrentTrackState.CurveCant || Blocks[currentBlock].Pitch != Blocks[StartingBlock].Pitch)
289+
{
290+
averages = true;
291+
}
287292
currentBlock++;
288293
}
289294

290-
radius /= Span;
291-
pitch /= Span;
292-
cant /= Span;
295+
if (averages)
296+
{
297+
radius /= Structure.Span;
298+
pitch /= Structure.Span;
299+
cant /= Structure.Span;
300+
}
301+
else
302+
{
303+
radius = Blocks[StartingBlock].CurrentTrackState.CurveRadius;
304+
pitch = Blocks[StartingBlock].Pitch;
305+
cant = Blocks[StartingBlock].CurrentTrackState.CurveCant;
306+
}
307+
293308
}
294309

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

305-
CalcTransformation(radius, pitch, Span, ref Direction, out _, out _, out _);
319+
CalcTransformation(radius, pitch, Structure.Span, ref Direction, out _, out _, out _);
306320

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

311-
switch (Type)
325+
switch (Structure.Type)
312326
{
313327
case ObjectTransformType.FollowsGradient:
314328
Transformation = new Transformation(TrackYaw, TrackPitch, 0.0);

source/Plugins/Route.Bve5/MapParser/Structures.cs

+13-47
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424

2525
using System.Collections.Generic;
26+
using OpenBveApi.Interface;
2627
using OpenBveApi.Math;
2728
using OpenBveApi.Objects;
2829
using OpenBveApi.Routes;
@@ -66,41 +67,20 @@ private class Station
6667
internal double InterferenceInDoor;
6768
}
6869

69-
private class FreeObj
70+
private class FreeObj : AbstractStructure
7071
{
71-
/// <summary>The track position of the object</summary>
72-
internal readonly double TrackPosition;
73-
/// <summary>The routefile key of the object</summary>
74-
internal readonly string Key;
75-
/// <summary>The X position of the object (m)</summary>
76-
internal readonly double X;
77-
/// <summary>The Y position of the object (m)</summary>
78-
internal readonly double Y;
79-
/// <summary>The Z position of the object (m)</summary>
80-
internal readonly double Z;
8172
/// <summary>The yaw of the object (radians)</summary>
8273
internal readonly double Yaw;
8374
/// <summary>The pitch of the object (radians)</summary>
8475
internal readonly double Pitch;
8576
/// <summary>The roll of the object (radians)</summary>
8677
internal readonly double Roll;
87-
88-
internal readonly ObjectTransformType Type;
89-
90-
internal readonly double Span;
91-
92-
internal FreeObj(double trackPosition, string key, double x, double y, double z, double yaw, double pitch, double roll, ObjectTransformType type, double span)
78+
79+
internal FreeObj(double trackPosition, string key, Vector3 position, double yaw, double pitch, double roll, ObjectTransformType type, double span) : base(trackPosition, key, type, span, position)
9380
{
94-
TrackPosition = trackPosition;
95-
Key = key;
96-
X = x;
97-
Y = y;
98-
Z = z;
9981
Yaw= yaw;
10082
Pitch = pitch;
10183
Roll = roll;
102-
Type = type;
103-
Span = span;
10484
}
10585
}
10686

@@ -113,12 +93,7 @@ private class Repeater
11393
internal double EndingDistance;
11494
internal double Interval;
11595
internal string[] ObjectKeys;
116-
/// <summary>The X position of the object (m)</summary>
117-
internal double X;
118-
/// <summary>The Y position of the object (m)</summary>
119-
internal double Y;
120-
/// <summary>The Z position of the object (m)</summary>
121-
internal double Z;
96+
internal Vector3 Position;
12297
/// <summary>The yaw of the object (radians)</summary>
12398
internal double Yaw;
12499
/// <summary>The pitch of the object (radians)</summary>
@@ -136,20 +111,13 @@ internal Repeater(string key)
136111
}
137112
}
138113

139-
private class Crack
114+
private class Crack : AbstractStructure
140115
{
141-
/// <summary>The track position of the object</summary>
142-
internal readonly double TrackPosition;
143-
/// <summary>The routefile key of the object</summary>
144-
internal readonly string Key;
145-
146116
internal readonly string PrimaryRail;
147117
internal readonly string SecondaryRail;
148118

149-
internal Crack(string key, double trackPosition, string primaryRail, string secondaryRail)
119+
internal Crack(string key, double trackPosition, string primaryRail, string secondaryRail) : base(trackPosition, key, ObjectTransformType.FollowsGradient, 0, Vector3.Zero)
150120
{
151-
Key = key;
152-
TrackPosition = trackPosition;
153121
PrimaryRail = primaryRail;
154122
SecondaryRail = secondaryRail;
155123
}
@@ -162,19 +130,17 @@ private class Section
162130
internal int DepartureStationIndex = -1;
163131
}
164132

165-
private class Signal
133+
private class Signal : AbstractStructure
166134
{
167-
internal double TrackPosition;
168-
internal string SignalObjectKey;
169-
internal double X;
170-
internal double Y;
171-
internal double Z;
172135
internal double Yaw;
173136
internal double Pitch;
174137
internal double Roll;
175-
internal ObjectTransformType Type;
176-
internal double Span;
177138
internal int SectionIndex;
139+
140+
internal Signal(string key, double trackPosition, ObjectTransformType type, double span, Vector3 position) : base(trackPosition, key, type, span, position)
141+
{
142+
143+
}
178144
}
179145

180146
private class Transponder

source/Plugins/Route.Bve5/Route.Bve5.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="Plugin.cs" />
6969
<Compile Include="Properties\AssemblyInfo.cs" />
7070
<Compile Include="ScenarioParser.cs" />
71+
<Compile Include="Structures\AbstractStructure.cs" />
7172
</ItemGroup>
7273
<ItemGroup>
7374
<ProjectReference Include="..\..\LibRender2\LibRender2.csproj">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//Simplified BSD License (BSD-2-Clause)
2+
//
3+
//Copyright (c) 2020, S520, The OpenBVE Project
4+
//
5+
//Redistribution and use in source and binary forms, with or without
6+
//modification, are permitted provided that the following conditions are met:
7+
//
8+
//1. Redistributions of source code must retain the above copyright notice, this
9+
// list of conditions and the following disclaimer.
10+
//2. Redistributions in binary form must reproduce the above copyright notice,
11+
// this list of conditions and the following disclaimer in the documentation
12+
// and/or other materials provided with the distribution.
13+
//
14+
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18+
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
25+
using OpenBveApi.Math;
26+
27+
namespace Route.Bve5
28+
{
29+
/// <summary>An abstract class for structures to be placed</summary>
30+
internal abstract class AbstractStructure
31+
{
32+
/// <summary>The track position of the object</summary>
33+
internal readonly double TrackPosition;
34+
/// <summary>The routefile key of the object</summary>
35+
internal readonly string Key;
36+
/// <summary>The object transform type to be applied</summary>
37+
internal readonly ObjectTransformType Type;
38+
/// <summary>The span of the object (used to calculate transforms when spanning multiple blocks)</summary>
39+
/// <remarks>Usually the physical length of the object, but this may not necessarily conform</remarks>
40+
internal readonly double Span;
41+
/// <summary>The position of the object</summary>
42+
internal readonly Vector3 Position;
43+
44+
internal AbstractStructure(double trackPosition, string key, ObjectTransformType type, double span, Vector3 position)
45+
{
46+
TrackPosition = trackPosition;
47+
Key = key;
48+
Type = type;
49+
Span = span;
50+
Position = new Vector3(position);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)