Skip to content

Commit 07af931

Browse files
CsantucciChris Jakeman
authored andcommitted
Solved zig-zag display of track layouts in TrackViewer. Taken from perpetualKid.
1 parent d7d241d commit 07af931

File tree

7 files changed

+104
-76
lines changed

7 files changed

+104
-76
lines changed

Source/Contrib/TrackViewer/Drawing/CloseToMouse.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,6 @@ public bool IsCloserThan(CloseToMouse otherItem)
6262
return this.ClosestMouseDistanceSquared < otherItem.ClosestMouseDistanceSquared;
6363
}
6464

65-
/// <summary>
66-
/// get distance between two world locations not taking the height in account
67-
/// </summary>
68-
/// <param name="location1">first location</param>
69-
/// <param name="location2">second location</param>
70-
/// <returns>Distance squared</returns>
71-
/// <remarks>Very similar to WordlLocation.GetDistanceSquared</remarks>
72-
public static float GetGroundDistanceSquared(WorldLocation location1, WorldLocation location2)
73-
{
74-
float dx = location1.Location.X - location2.Location.X;
75-
float dz = location1.Location.Z - location2.Location.Z;
76-
dx += 2048 * (location1.TileX - location2.TileX);
77-
dz += 2048 * (location1.TileZ - location2.TileZ);
78-
return dx * dx + dz * dz;
79-
}
80-
8165
}
8266

8367
/// <summary>
@@ -144,7 +128,7 @@ public CloseToMouseJunctionOrEnd(TrackNode junctionOrEndNode, string description
144128
/// <param name="description">The type of item (needed for later printing in statusbar)</param>
145129
public void CheckMouseDistance(WorldLocation location, WorldLocation mouseLocation, TrackNode junctionOrEndNode, string description)
146130
{
147-
float distanceSquared = CloseToMouse.GetGroundDistanceSquared(location, mouseLocation);
131+
float distanceSquared = WorldLocation.GetDistanceSquared2D(location, mouseLocation);
148132
if (distanceSquared < ClosestDistanceSquared)
149133
{
150134
ClosestDistanceSquared = distanceSquared;
@@ -210,7 +194,7 @@ public override void Reset()
210194
/// <param name="trItem">The track Item that will be stored when it is indeed the closest</param>
211195
public void CheckMouseDistance(WorldLocation location, WorldLocation mouseLocation, DrawableTrackItem trItem)
212196
{
213-
float distanceSquared = CloseToMouse.GetGroundDistanceSquared(location, mouseLocation);
197+
float distanceSquared = WorldLocation.GetDistanceSquared2D(location, mouseLocation);
214198

215199
if (distanceSquared < ClosestDistanceSquared)
216200
{
@@ -317,7 +301,7 @@ public void CheckMouseDistance(WorldLocation location, WorldLocation mouseLocati
317301
TrackNode trackNode, TrVectorSection vectorSection, int tvsi, double pixelsPerMeter)
318302
{
319303
storedMouseLocation = mouseLocation;
320-
float distanceSquared = CloseToMouse.GetGroundDistanceSquared(location, mouseLocation);
304+
float distanceSquared = WorldLocation.GetDistanceSquared2D(location, mouseLocation);
321305
// to make unique distances becasue they also act as Key
322306
double distanceSquaredIndexed = ((double)distanceSquared) * (1 + 1e-16 * trackNode.Index);
323307
if (distanceSquaredIndexed < sortedTrackCandidates.First().Key)
@@ -386,8 +370,8 @@ DistanceLon CalcRealDistanceSquared(TrVectorSection trackVectorSection, TrackSec
386370
X = storedMouseLocation.Location.X - trackVectorSection.X,
387371
Z = storedMouseLocation.Location.Z - trackVectorSection.Z
388372
};
389-
vectorToMouse.X += (storedMouseLocation.TileX - trackVectorSection.TileX) * 2048;
390-
vectorToMouse.Z += (storedMouseLocation.TileZ - trackVectorSection.TileZ) * 2048;
373+
vectorToMouse.X = (float)(vectorToMouse.X + (storedMouseLocation.TileX - trackVectorSection.TileX) * WorldLocation.TileSize);
374+
vectorToMouse.Z = (float)(vectorToMouse.Z + (storedMouseLocation.TileZ - trackVectorSection.TileZ) * WorldLocation.TileSize);
391375

392376
//Now rotate the vector such that a direction along the track is in a direction (x=0, z=1)
393377
vectorToMouse = Vector3.Transform(vectorToMouse, Matrix.CreateRotationY(-trackVectorSection.AY));

Source/Contrib/TrackViewer/Drawing/DrawArea.cs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ public void Update()
188188
public void ZoomReset(DrawTrackDB drawTrackDB)
189189
{
190190
if (drawTrackDB == null) return;
191-
float minX = drawTrackDB.MinTileX * 2048 - 1024f;
192-
float minZ = drawTrackDB.MinTileZ * 2048 - 1024f;
193-
float maxX = drawTrackDB.MaxTileX * 2048 + 1024f;
194-
float maxZ = drawTrackDB.MaxTileZ * 2048 + 1024f;
191+
double minX = drawTrackDB.MinTileX * WorldLocation.TileSize - 1024f;
192+
double minZ = drawTrackDB.MinTileZ * WorldLocation.TileSize - 1024f;
193+
double maxX = drawTrackDB.MaxTileX * WorldLocation.TileSize + 1024f;
194+
double maxZ = drawTrackDB.MaxTileZ * WorldLocation.TileSize + 1024f;
195195
SetDrawArea(minX, maxX, minZ, maxZ);
196196
}
197197

@@ -202,10 +202,10 @@ public void ZoomReset(DrawTrackDB drawTrackDB)
202202
/// <param name="maxX">maximal real world X location</param>
203203
/// <param name="minZ">minimal real world Z location</param>
204204
/// <param name="maxZ">maximal real world Z location</param>
205-
void SetDrawArea(float minX, float maxX, float minZ, float maxZ)
205+
void SetDrawArea(double minX, double maxX, double minZ, double maxZ)
206206
{
207-
float scaleX = AreaW / (maxX - minX);
208-
float scaleY = AreaH / (maxZ - minZ);
207+
double scaleX = AreaW / (maxX - minX);
208+
double scaleY = AreaH / (maxZ - minZ);
209209
//make square tiles
210210
Scale = Math.Min(scaleX, scaleY);
211211
metersPerPixel.ApproximateTo(1.0f / Scale);
@@ -297,7 +297,7 @@ private void ZoomAround(Vector2 fixedAreaLocation, int scaleSteps)
297297
// fixedX = scale_old * (worldX - offsetX_old) = scale_new * (worldX - offsetX_new)
298298
// fixedX/scale_old + offsetX_old = fixedX/scale_new + offsetX_new = worldX
299299
// offsetX_new = offsetX_old + fixedX * (scale_new/scale_old - 1) / scale_new
300-
float scaleFactor = 1.0f / (float)metersPerPixel.AddStep(scaleSteps); // 1.0/xxx because scale is inverse of metersPerPixel
300+
double scaleFactor = 1.0 / metersPerPixel.AddStep(scaleSteps); // 1.0/xxx because scale is inverse of metersPerPixel
301301
Scale = metersPerPixel.InverseScaleValue;
302302
OffsetX += fixedAreaLocation.X * (scaleFactor - 1) / Scale;
303303
OffsetZ += (AreaH - fixedAreaLocation.Y) * (scaleFactor - 1) / Scale;
@@ -311,8 +311,8 @@ public void ZoomToTile()
311311
{
312312
//normal equation: areaX = scale * (worldX - offsetX)
313313
//zoom to tile: screenW = scale_new * 2048
314-
double scaleX = AreaW / 2048.0;
315-
double scaleY = AreaH / 2048.0;
314+
double scaleX = AreaW / WorldLocation.TileSize;
315+
double scaleY = AreaH / WorldLocation.TileSize;
316316
double newScale = Math.Min(scaleX, scaleY);
317317
int stepsNeeded = metersPerPixel.StepsNeededForRatio(Scale / newScale);
318318
ZoomAroundMouse(stepsNeeded);
@@ -323,8 +323,8 @@ public void ZoomToTile()
323323
/// </summary>
324324
public void ZoomToTileCentered()
325325
{
326-
double scaleX = AreaW / 2048.0;
327-
double scaleY = AreaH / 2048.0;
326+
double scaleX = AreaW / WorldLocation.TileSize;
327+
double scaleY = AreaH / WorldLocation.TileSize;
328328
double newScale = Math.Min(scaleX, scaleY);
329329
ZoomCentered(metersPerPixel.StepsNeededForRatio(Scale / newScale));
330330
}
@@ -420,8 +420,8 @@ public void ShiftToLocation(WorldLocation location)
420420
// Basic equation areaX = scale * (worldX - offsetX)
421421
// We want middle of screen to shift to new worldX, so areaW/2 = scale * (worldX - offsetX)
422422
// Similarly
423-
double worldX = location.TileX * 2048 + location.Location.X;
424-
double worldZ = location.TileZ * 2048 + location.Location.Z;
423+
double worldX = location.TileX * WorldLocation.TileSize + location.Location.X;
424+
double worldZ = location.TileZ * WorldLocation.TileSize + location.Location.Z;
425425
OffsetX = worldX - AreaW / (2 * Scale);
426426
OffsetZ = worldZ - AreaH / (2 * Scale);
427427
}
@@ -446,8 +446,8 @@ private float GetWindowSize(float worldSize)
446446
/// <returns>location on the drawing area in a 2d vector (in pixels)</returns>
447447
private Vector2 GetAreaVector(WorldLocation location)
448448
{
449-
double x = location.TileX * 2048 + location.Location.X;
450-
double y = location.TileZ * 2048 + location.Location.Z;
449+
double x = location.TileX * WorldLocation.TileSize + location.Location.X;
450+
double y = location.TileZ * WorldLocation.TileSize + location.Location.Z;
451451
return new Vector2((float)(Scale * (x - OffsetX)),
452452
(float)(AreaH - Scale * (y - OffsetZ)));
453453
}
@@ -486,13 +486,7 @@ private WorldLocation GetWorldLocation(int areaX, int areaY)
486486
{
487487
double x = (OffsetX + (areaX) / Scale);
488488
double z = (OffsetZ + (AreaH - areaY) / Scale);
489-
//WorldLocation location = new WorldLocation(0, 0, cornerIndexX, 0, cornerIndexZ);
490-
//we now do pre-normalization. This normalization is more efficient than Coordinates.normalization
491-
int tileX = (int)x / 2048;
492-
x -= (tileX * 2048);
493-
int tileZ = (int)z / 2048;
494-
z -= tileZ * 2048;
495-
WorldLocation location = new WorldLocation(tileX, tileZ, (float)x, 0, (float)z);
489+
WorldLocation location = new WorldLocation(0, 0, (float)x, 0, (float)z);
496490
location.Normalize();
497491
return location;
498492
}

Source/Contrib/TrackViewer/Drawing/DrawTrackDB.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ public class DrawTrackDB
225225
#endregion
226226

227227
#region private members
228-
/// <summary>Track Section Data</summary>
228+
229+
/// <summary>Track Section Data</summary>
229230
private TrackSectionsFile tsectionDat;
230231
/// <summary>Track database</summary>
231232
private TrackDB trackDB;
@@ -632,18 +633,18 @@ List<WorldLocation> FindLocationList(uint trackNodeIndex, int trackVectorSection
632633
WorldLocation midLocation = FindLocationInSection(tvs, trackSection, trackSectionLength/2);
633634

634635
// (deltaX, deltaZ) is a vector from begin to end.
635-
float deltaX = (endLocation.Location.X - endLocation.Location.X);
636-
float deltaZ = (endLocation.Location.Z - endLocation.Location.Z);
637-
deltaX += 2048 * (endLocation.TileX - endLocation.TileX);
638-
deltaZ += 2048 * (endLocation.TileZ - endLocation.TileZ);
636+
double deltaX = (endLocation.Location.X - endLocation.Location.X);
637+
double deltaZ = (endLocation.Location.Z - endLocation.Location.Z);
638+
deltaX += WorldLocation.TileSize * (endLocation.TileX - endLocation.TileX);
639+
deltaZ += WorldLocation.TileSize * (endLocation.TileZ - endLocation.TileZ);
639640

640641
WorldLocation begin2Location = new WorldLocation(midLocation);
641-
begin2Location.Location.X -= deltaX / 2;
642-
begin2Location.Location.Z -= deltaZ / 2;
642+
begin2Location.Location.X = (float)(begin2Location.Location.X - deltaX / 2);
643+
begin2Location.Location.Z = (float)(begin2Location.Location.Z - deltaZ / 2);
643644

644645
WorldLocation end2Location = new WorldLocation(midLocation);
645-
end2Location.Location.X += deltaX / 2;
646-
end2Location.Location.Z += deltaZ / 2;
646+
end2Location.Location.X = (float)(end2Location.Location.X + deltaX / 2);
647+
end2Location.Location.Z = (float)(end2Location.Location.Z + deltaZ / 2);
647648

648649
boxList.Add(begin2Location);
649650
boxList.Add(end2Location);

Source/Contrib/TrackViewer/Drawing/Labels/DrawLabels.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void Draw(DrawArea drawArea)
8787
{
8888
DrawLabel(label);
8989
}
90-
float distanceSquared = CloseToMouse.GetGroundDistanceSquared(label.WorldLocation, drawArea.MouseLocation);
90+
float distanceSquared = WorldLocation.GetDistanceSquared2D(label.WorldLocation, drawArea.MouseLocation);
9191
if (distanceSquared < closestDistanceSquared )
9292
{
9393
closestDistanceSquared = distanceSquared;

Source/Contrib/TrackViewer/Editing/PathEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ void FindActiveNode(DrawArea drawArea, DrawnPathData drawnPathData)
564564
TrainpathNode closestNode = null;
565565
foreach (TrainpathNode node in drawnPathData.DrawnNodes)
566566
{
567-
float distanceSquared = CloseToMouse.GetGroundDistanceSquared(node.Location, drawArea.MouseLocation);
567+
float distanceSquared = WorldLocation.GetDistanceSquared2D(node.Location, drawArea.MouseLocation);
568568
// by using '<=' instead of '<' we should get the latest one, which overrides earlier ones
569569
// To prevent numerical issues, we add a small number (smaller than two junctions would normally be together
570570
if (distanceSquared <= closestMouseDistanceSquared + 0.1f)

Source/Contrib/TrackViewer/Editing/TrainpathNode.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public override TrainpathNode ShallowCopyNoLinks()
365365
public int FindJunctionOrEndIndex(bool wantJunctionNode)
366366
{
367367
int bestIndex = -1;
368-
float bestDistance2 = 1e10f;
368+
double bestDistance2 = 1e10f;
369369
for (int j = 0; j < TrackDB.TrackNodes.Count(); j++)
370370
{
371371
TrackNode tn = TrackDB.TrackNodes[j];
@@ -374,12 +374,12 @@ public int FindJunctionOrEndIndex(bool wantJunctionNode)
374374
if (!wantJunctionNode && !tn.TrEndNode) continue;
375375
if (tn.UiD.TileX != Location.TileX || tn.UiD.TileZ != Location.TileZ) continue;
376376

377-
float dx = tn.UiD.X - Location.Location.X;
377+
double dx = tn.UiD.X - Location.Location.X;
378378
dx += (tn.UiD.TileX - Location.TileX) * 2048;
379-
float dz = tn.UiD.Z - Location.Location.Z;
379+
double dz = tn.UiD.Z - Location.Location.Z;
380380
dz += (tn.UiD.TileZ - Location.TileZ) * 2048;
381-
float dy = tn.UiD.Y - Location.Location.Y;
382-
float d = dx * dx + dy * dy + dz * dz;
381+
double dy = tn.UiD.Y - Location.Location.Y;
382+
double d = dx * dx + dy * dy + dz * dz;
383383
if (bestDistance2 > d)
384384
{
385385
bestIndex = j;

0 commit comments

Comments
 (0)