//-----------------------------------------------------------------------
//
// Copyright (c) 2016 Mapbox. All rights reserved.
//
//-----------------------------------------------------------------------
namespace Mapbox.Unity.Utilities
{
using Mapbox.Utils;
using UnityEngine;
public static class VectorExtensions
{
///
/// Vector2 convenience method to convert Vector2 to Vector3.
///
/// Vector3 with a y value of zero.
/// Vector2.
public static Vector3 ToVector3xz(this Vector2 v)
{
return new Vector3(v.x, 0, v.y);
}
///
/// Vector2d convenience method to convert Vector2d to Vector3.
///
/// Vector3 with a y value of zero.
/// Vector2d.
public static Vector3 ToVector3xz(this Vector2d v)
{
return new Vector3((float)v.x, 0, (float)v.y);
}
///
/// Vector3 convenience method to convert Vector3 to Vector2.
///
/// The Vector2.
/// Vector3.
public static Vector2 ToVector2xz(this Vector3 v)
{
return new Vector2(v.x, v.z);
}
///
/// Vector3 convenience method to convert Vector3 to Vector2d.
///
/// The Vector2d.
/// Vector3.
public static Vector2d ToVector2d(this Vector3 v)
{
return new Vector2d(v.x, v.z);
}
public static Vector3 Perpendicular(this Vector3 v)
{
return new Vector3(-v.z, v.y, v.x);
}
///
/// Transform extension method to move a Unity transform to a specific latitude/longitude.
///
/// Transform.
/// Latitude.
/// Longitude.
/// Reference point.
/// Scale.
///
/// Place a Unity transform at 10, 10, with a map center of (0, 0) and scale 1:
///
/// transform.MoveToGeocoordinate(10, 10, new Vector2d(0, 0), 1f);
/// Debug.Log(transform.position);
/// // (1113195.0, 0.0, 1118890.0)
///
///
public static void MoveToGeocoordinate(this Transform t, double lat, double lng, Vector2d refPoint, float scale = 1)
{
t.position = Conversions.GeoToWorldPosition(lat, lng, refPoint, scale).ToVector3xz();
}
///
/// Transform extension method to move a Unity transform to a specific Vector2d.
///
/// Transform.
/// Latitude Longitude.
/// Reference point.
/// Scale.
///
/// Place a Unity transform at 10, 10, with a map center of (0, 0) and scale 1:
///
/// transform.MoveToGeocoordinate(new Vector2d(10, 10), new Vector2d(0, 0), 1f);
/// Debug.Log(transform.position);
/// // (1113195.0, 0.0, 1118890.0)
///
///
public static void MoveToGeocoordinate(this Transform t, Vector2d latLon, Vector2d refPoint, float scale = 1)
{
t.MoveToGeocoordinate(latLon.x, latLon.y, refPoint, scale);
}
///
/// Vector2 extension method to convert from a latitude/longitude to a Unity Vector3.
///
/// The Vector3 Unity position.
/// Latitude Longitude.
/// Reference point.
/// Scale.
public static Vector3 AsUnityPosition(this Vector2 latLon, Vector2d refPoint, float scale = 1)
{
return Conversions.GeoToWorldPosition(latLon.x, latLon.y, refPoint, scale).ToVector3xz();
}
///
/// Transform extension method to return the transform's position as a Vector2d latitude/longitude.
///
/// Vector2d that represents latitude/longitude.
/// T.
/// Reference point.
/// Scale.
///
/// Get the latitude/longitude of a transform at (1113195, 0, 1118890), map center (0, 0) and scale 1.
///
/// var latLng = transform.GetGeoPosition(new Vector2d(0, 0), 1);
/// Debug.Log(latLng);
/// // (10.00000, 10.00000)
///
///
public static Vector2d GetGeoPosition(this Transform t, Vector2d refPoint, float scale = 1)
{
var pos = refPoint + (t.position / scale).ToVector2d();
return Conversions.MetersToLatLon(pos);
}
public static Vector2d GetGeoPosition(this Vector3 position, Vector2d refPoint, float scale = 1)
{
var pos = refPoint + (position / scale).ToVector2d();
return Conversions.MetersToLatLon(pos);
}
public static Vector2d GetGeoPosition(this Vector2 position, Vector2d refPoint, float scale = 1)
{
return position.ToVector3xz().GetGeoPosition(refPoint, scale);
}
}
}