Moved Matrix creation to ViewTransform

This commit is contained in:
ClemensFischer 2024-05-22 09:42:21 +02:00
parent a1b8ed1ce8
commit 9cb4a9be7e
7 changed files with 138 additions and 103 deletions

View file

@ -7,7 +7,6 @@ using Avalonia.Animation;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Styling;
using System;
using System.Threading;
using System.Threading.Tasks;
@ -36,7 +35,7 @@ namespace MapControl
public static readonly StyledProperty<double> MaxZoomLevelProperty =
DependencyPropertyHelper.Register<MapBase, double>(nameof(MaxZoomLevel), 20d, false,
(map, oldValue, newValue) => map.MaxZoomLevelPropertyChanged(newValue),
(map, value) => map.CoerceMinZoomLevelProperty(value));
(map, value) => map.CoerceMaxZoomLevelProperty(value));
public static readonly StyledProperty<double> ZoomLevelProperty =
DependencyPropertyHelper.Register<MapBase, double>(nameof(ZoomLevel), 1d, true,
@ -106,24 +105,8 @@ namespace MapControl
/// </summary>
public double ViewScale
{
get => ViewTransform.Scale;
}
private void SetViewScale(double viewScale)
{
RaisePropertyChanged(ViewScaleProperty, double.NaN, viewScale);
}
/// <summary>
/// Gets a transform Matrix for scaling and rotating objects that are anchored
/// at a Location from map coordinates (i.e. meters) to view coordinates.
/// </summary>
public Matrix GetMapTransform(Location location)
{
var scale = GetScale(location);
return Matrix.CreateScale(scale.X, scale.Y)
* Matrix.CreateRotation(ViewTransform.Rotation * Math.PI / 180d);
get => (double)GetValue(ViewScaleProperty);
private set => RaisePropertyChanged(ViewScaleProperty, double.NaN, value);
}
private void CenterPropertyChanged(Location center)

View file

@ -43,6 +43,25 @@ namespace MapControl
/// </summary>
public Matrix ViewToMapMatrix { get; private set; }
/// <summary>
/// Initializes a ViewTransform from a map center point in projected coordinates,
/// a view conter point, a scaling factor from projected coordinates to view coordinates
/// and a rotation angle in degrees.
/// </summary>
public void SetTransform(Point mapCenter, Point viewCenter, double scale, double rotation)
{
Scale = scale;
Rotation = ((rotation % 360d) + 360d) % 360d;
MapToViewMatrix
= Matrix.CreateTranslation(-mapCenter.X, -mapCenter.Y)
* Matrix.CreateScale(scale, -scale)
* Matrix.CreateRotation(Rotation * Math.PI / 180d)
* Matrix.CreateTranslation(viewCenter.X, viewCenter.Y);
ViewToMapMatrix = MapToViewMatrix.Invert();
}
/// <summary>
/// Transforms a Point from projected map coordinates to view coordinates.
/// </summary>
@ -59,20 +78,29 @@ namespace MapControl
return ViewToMapMatrix.Transform(point);
}
public void SetTransform(Point mapCenter, Point viewCenter, double scale, double rotation)
/// <summary>
/// Transform relative to absolute map scale. Returns horizontal and vertical
/// scaling factors from meters to view coordinates.
/// </summary>
public Point GetMapScale(Point relativeScale)
{
Scale = scale;
Rotation = ((rotation % 360d) + 360d) % 360d;
MapToViewMatrix
= Matrix.CreateTranslation(-mapCenter.X, -mapCenter.Y)
* Matrix.CreateScale(scale, -scale)
* Matrix.CreateRotation(Rotation * Math.PI / 180d)
* Matrix.CreateTranslation(viewCenter.X, viewCenter.Y);
ViewToMapMatrix = MapToViewMatrix.Invert();
return new Point(Scale * relativeScale.X, Scale * relativeScale.Y);
}
/// <summary>
/// Gets a transform Matrix from meters to view coordinates for a relative map scale.
/// </summary>
public Matrix GetMapTransform(Point relativeScale)
{
var scale = GetMapScale(relativeScale);
return Matrix.CreateScale(scale.X, scale.Y)
* Matrix.CreateRotation(Rotation * Math.PI / 180d);
}
/// <summary>
/// Gets the transform Matrix for the RenderTranform of a MapTileLayer.
/// </summary>
public Matrix GetTileLayerTransform(double tileMatrixScale, Point tileMatrixTopLeft, Point tileMatrixOrigin)
{
// Tile matrix origin in map coordinates.
@ -92,6 +120,9 @@ namespace MapControl
* Matrix.CreateTranslation(viewOrigin.X, viewOrigin.Y);
}
/// <summary>
/// Gets the index bounds of a tile matrix.
/// </summary>
public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, Size viewSize)
{
// View origin in map coordinates.
@ -114,5 +145,15 @@ namespace MapControl
//
return new Rect(0d, 0d, viewSize.Width, viewSize.Height).TransformToAABB(transform);
}
internal static Matrix CreateTransformMatrix(
double translation1X, double translation1Y,
double rotation,
double translation2X, double translation2Y)
{
return Matrix.CreateTranslation(translation1X, translation1Y)
* Matrix.CreateRotation(rotation * Math.PI / 180d)
* Matrix.CreateTranslation(translation2X, translation2Y);
}
}
}