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

@ -198,14 +198,21 @@ namespace MapControl
public ViewTransform ViewTransform { get; } = new ViewTransform();
/// <summary>
/// Gets the map scale as the horizontal and vertical scaling factors from geographic
/// coordinates to view coordinates at the specified location, as pixels per meter.
/// Gets the map scale as horizontal and vertical scaling factors from meters to
/// view coordinates at the specified location.
/// </summary>
public Point GetScale(Location location)
{
var relativeScale = MapProjection.GetRelativeScale(location);
return ViewTransform.GetMapScale(MapProjection.GetRelativeScale(location));
}
return new Point(ViewTransform.Scale * relativeScale.X, ViewTransform.Scale * relativeScale.Y);
/// <summary>
/// Gets a transform Matrix from meters to view coordinates for scaling and rotating
/// objects that are anchored at a Location.
/// </summary>
public Matrix GetMapTransform(Location location)
{
return ViewTransform.GetMapTransform(MapProjection.GetRelativeScale(location));
}
/// <summary>
@ -554,7 +561,7 @@ namespace MapControl
}
}
SetViewScale(ViewTransform.Scale);
ViewScale = ViewTransform.Scale;
// Check if view center has moved across 180° longitude.
//

View file

@ -54,21 +54,10 @@ namespace MapControl
public Matrix ViewToMapMatrix { get; private set; }
/// <summary>
/// Transforms a Point from projected map coordinates to view coordinates.
/// 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 Point MapToView(Point point)
{
return MapToViewMatrix.Transform(point);
}
/// <summary>
/// Transforms a Point from view coordinates to projected map coordinates.
/// </summary>
public Point ViewToMap(Point point)
{
return ViewToMapMatrix.Transform(point);
}
public void SetTransform(Point mapCenter, Point viewCenter, double scale, double rotation)
{
Scale = scale;
@ -85,6 +74,46 @@ namespace MapControl
ViewToMapMatrix = transform;
}
/// <summary>
/// Transforms a Point from projected map coordinates to view coordinates.
/// </summary>
public Point MapToView(Point point)
{
return MapToViewMatrix.Transform(point);
}
/// <summary>
/// Transforms a Point from view coordinates to projected map coordinates.
/// </summary>
public Point ViewToMap(Point point)
{
return ViewToMapMatrix.Transform(point);
}
/// <summary>
/// Gets scaling factors from meters to view coordinates for a relative map scale.
/// </summary>
public Point GetMapScale(Point relativeScale)
{
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);
var transform = new Matrix(scale.X, 0d, 0d, scale.Y, 0d, 0d);
transform.Rotate(Rotation);
return transform;
}
/// <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.
@ -106,6 +135,9 @@ namespace MapControl
return transform;
}
/// <summary>
/// Gets the index bounds of a tile matrix.
/// </summary>
public Rect GetTileMatrixBounds(double tileMatrixScale, Point tileMatrixTopLeft, Size viewSize)
{
// View origin in map coordinates.
@ -128,5 +160,17 @@ namespace MapControl
return new MatrixTransform { Matrix = transform }
.TransformBounds(new Rect(0d, 0d, viewSize.Width, viewSize.Height));
}
internal static Matrix CreateTransformMatrix(
double translation1X, double translation1Y,
double rotation,
double translation2X, double translation2Y)
{
var transform = new Matrix(1d, 0d, 0d, 1d, translation1X, translation1Y);
transform.Rotate(rotation);
transform.Translate(translation2X, translation2Y);
return transform;
}
}
}

View file

@ -9,18 +9,13 @@ using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
#if AVALONIA
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using DependencyProperty = Avalonia.AvaloniaProperty;
using FrameworkElement = Avalonia.Controls.Control;
using ImageSource = Avalonia.Media.IImage;
#elif WINUI
using Windows.Foundation;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#elif UWP
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#else
@ -284,16 +279,12 @@ namespace MapControl
}
var viewRect = GetViewRect(rect.Value);
#if AVALONIA
var transform
= Matrix.CreateTranslation(-viewSize.Width / 2d, -viewSize.Height / 2d)
* Matrix.CreateRotation(-viewRect.Rotation * Math.PI / 180d)
* Matrix.CreateTranslation(viewRect.Rect.Width / 2d, viewRect.Rect.Height / 2d);
#else
var transform = new Matrix(1d, 0d, 0d, 1d, -viewSize.Width / 2d, -viewSize.Height / 2d);
transform.Rotate(-viewRect.Rotation);
transform.Translate(viewRect.Rect.Width / 2d, viewRect.Rect.Height / 2d);
#endif
var transform = ViewTransform.CreateTransformMatrix(
-viewSize.Width / 2d, -viewSize.Height / 2d,
-viewRect.Rotation,
viewRect.Rect.Width / 2d, viewRect.Rect.Height / 2d);
var imagePos = transform.Transform(position);
var queryParameters = new Dictionary<string, string>