2017-10-02 21:24:00 +00:00
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
2018-03-01 07:51:35 +00:00
|
|
|
#if REAL_T_IS_DOUBLE
|
|
|
|
using real_t = System.Double;
|
|
|
|
#else
|
|
|
|
using real_t = System.Single;
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
namespace Godot
|
|
|
|
{
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
public struct Transform2D : IEquatable<Transform2D>
|
|
|
|
{
|
|
|
|
private static readonly Transform2D identity = new Transform2D
|
|
|
|
(
|
|
|
|
new Vector2(1f, 0f),
|
|
|
|
new Vector2(0f, 1f),
|
|
|
|
new Vector2(0f, 0f)
|
|
|
|
);
|
|
|
|
|
|
|
|
public Vector2 x;
|
|
|
|
public Vector2 y;
|
|
|
|
public Vector2 o;
|
|
|
|
|
|
|
|
public static Transform2D Identity
|
|
|
|
{
|
|
|
|
get { return identity; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 Origin
|
|
|
|
{
|
|
|
|
get { return o; }
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public real_t Rotation
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2017-11-21 22:32:19 +00:00
|
|
|
get { return Mathf.Atan2(y.x, o.y); }
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 Scale
|
|
|
|
{
|
2017-11-21 22:32:19 +00:00
|
|
|
get { return new Vector2(x.Length(), y.Length()); }
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 this[int index]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return x;
|
|
|
|
case 1:
|
|
|
|
return y;
|
|
|
|
case 2:
|
|
|
|
return o;
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
x = value;
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
y = value;
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
o = value;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public real_t this[int index, int axis]
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return x[axis];
|
|
|
|
case 1:
|
|
|
|
return y[axis];
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
switch (index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
x[axis] = value;
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
y[axis] = value;
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Transform2D AffineInverse()
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var inv = this;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t det = this[0, 0] * this[1, 1] - this[1, 0] * this[0, 1];
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
if (det == 0)
|
|
|
|
{
|
|
|
|
return new Transform2D
|
|
|
|
(
|
|
|
|
float.NaN, float.NaN,
|
|
|
|
float.NaN, float.NaN,
|
|
|
|
float.NaN, float.NaN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t idet = 1.0f / det;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t temp = this[0, 0];
|
2017-10-02 21:24:00 +00:00
|
|
|
this[0, 0] = this[1, 1];
|
|
|
|
this[1, 1] = temp;
|
|
|
|
|
|
|
|
this[0] *= new Vector2(idet, -idet);
|
|
|
|
this[1] *= new Vector2(-idet, idet);
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
this[2] = BasisXform(-this[2]);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector2 BasisXform(Vector2 v)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2017-11-21 22:32:19 +00:00
|
|
|
return new Vector2(Tdotx(v), Tdoty(v));
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector2 BasisXformInv(Vector2 v)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2017-11-21 22:32:19 +00:00
|
|
|
return new Vector2(x.Dot(v), y.Dot(v));
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public Transform2D InterpolateWith(Transform2D m, real_t c)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t r1 = Rotation;
|
|
|
|
real_t r2 = m.Rotation;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
Vector2 s1 = Scale;
|
|
|
|
Vector2 s2 = m.Scale;
|
|
|
|
|
|
|
|
// Slerp rotation
|
2018-04-08 03:30:43 +00:00
|
|
|
var v1 = new Vector2(Mathf.Cos(r1), Mathf.Sin(r1));
|
|
|
|
var v2 = new Vector2(Mathf.Cos(r2), Mathf.Sin(r2));
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t dot = v1.Dot(v2);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
// Clamp dot to [-1, 1]
|
2018-04-08 03:39:35 +00:00
|
|
|
dot = dot < -1.0f ? -1.0f : (dot > 1.0f ? 1.0f : dot);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-04-08 03:38:02 +00:00
|
|
|
Vector2 v;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
if (dot > 0.9995f)
|
|
|
|
{
|
|
|
|
// Linearly interpolate to avoid numerical precision issues
|
2017-11-21 22:32:19 +00:00
|
|
|
v = v1.LinearInterpolate(v2, c).Normalized();
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t angle = c * Mathf.Acos(dot);
|
2017-11-21 22:32:19 +00:00
|
|
|
Vector2 v3 = (v2 - v1 * dot).Normalized();
|
|
|
|
v = v1 * Mathf.Cos(angle) + v3 * Mathf.Sin(angle);
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extract parameters
|
|
|
|
Vector2 p1 = Origin;
|
|
|
|
Vector2 p2 = m.Origin;
|
|
|
|
|
|
|
|
// Construct matrix
|
2018-04-08 03:30:43 +00:00
|
|
|
var res = new Transform2D(Mathf.Atan2(v.y, v.x), p1.LinearInterpolate(p2, c));
|
|
|
|
Vector2 scale = s1.LinearInterpolate(s2, c);
|
2017-10-02 21:24:00 +00:00
|
|
|
res.x *= scale;
|
|
|
|
res.y *= scale;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Transform2D Inverse()
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var inv = this;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
// Swap
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t temp = inv.x.y;
|
2017-10-02 21:24:00 +00:00
|
|
|
inv.x.y = inv.y.x;
|
|
|
|
inv.y.x = temp;
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
inv.o = inv.BasisXform(-inv.o);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
return inv;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Transform2D Orthonormalized()
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var on = this;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
Vector2 onX = on.x;
|
|
|
|
Vector2 onY = on.y;
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
onX.Normalize();
|
2018-04-08 03:39:35 +00:00
|
|
|
onY = onY - onX * onX.Dot(onY);
|
2017-11-21 22:32:19 +00:00
|
|
|
onY.Normalize();
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
on.x = onX;
|
|
|
|
on.y = onY;
|
|
|
|
|
|
|
|
return on;
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public Transform2D Rotated(real_t phi)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
return this * new Transform2D(phi, new Vector2());
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Transform2D Scaled(Vector2 scale)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var copy = this;
|
2017-10-02 21:24:00 +00:00
|
|
|
copy.x *= scale;
|
|
|
|
copy.y *= scale;
|
|
|
|
copy.o *= scale;
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
private real_t Tdotx(Vector2 with)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
return this[0, 0] * with[0] + this[1, 0] * with[1];
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
private real_t Tdoty(Vector2 with)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
return this[0, 1] * with[0] + this[1, 1] * with[1];
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Transform2D Translated(Vector2 offset)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:30:43 +00:00
|
|
|
var copy = this;
|
2017-11-21 22:32:19 +00:00
|
|
|
copy.o += copy.BasisXform(offset);
|
2017-10-02 21:24:00 +00:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector2 Xform(Vector2 v)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2017-11-21 22:32:19 +00:00
|
|
|
return new Vector2(Tdotx(v), Tdoty(v)) + o;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
public Vector2 XformInv(Vector2 v)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
|
|
|
Vector2 vInv = v - o;
|
2017-11-21 22:32:19 +00:00
|
|
|
return new Vector2(x.Dot(vInv), y.Dot(vInv));
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
2018-03-01 07:51:35 +00:00
|
|
|
|
|
|
|
// Constructors
|
2017-10-02 21:24:00 +00:00
|
|
|
public Transform2D(Vector2 xAxis, Vector2 yAxis, Vector2 origin)
|
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
x = xAxis;
|
|
|
|
y = yAxis;
|
|
|
|
o = origin;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
2018-03-01 07:51:35 +00:00
|
|
|
|
|
|
|
public Transform2D(real_t xx, real_t xy, real_t yx, real_t yy, real_t ox, real_t oy)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
x = new Vector2(xx, xy);
|
|
|
|
y = new Vector2(yx, yy);
|
|
|
|
o = new Vector2(ox, oy);
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
public Transform2D(real_t rot, Vector2 pos)
|
2017-10-02 21:24:00 +00:00
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
real_t cr = Mathf.Cos(rot);
|
|
|
|
real_t sr = Mathf.Sin(rot);
|
2017-10-02 21:24:00 +00:00
|
|
|
x.x = cr;
|
|
|
|
y.y = cr;
|
|
|
|
x.y = -sr;
|
|
|
|
y.x = sr;
|
|
|
|
o = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Transform2D operator *(Transform2D left, Transform2D right)
|
|
|
|
{
|
2017-11-21 22:32:19 +00:00
|
|
|
left.o = left.Xform(right.o);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-03-01 07:51:35 +00:00
|
|
|
real_t x0, x1, y0, y1;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2017-11-21 22:32:19 +00:00
|
|
|
x0 = left.Tdotx(right.x);
|
|
|
|
x1 = left.Tdoty(right.x);
|
|
|
|
y0 = left.Tdotx(right.y);
|
|
|
|
y1 = left.Tdoty(right.y);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
left.x.x = x0;
|
|
|
|
left.x.y = x1;
|
|
|
|
left.y.x = y0;
|
|
|
|
left.y.y = y1;
|
|
|
|
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator ==(Transform2D left, Transform2D right)
|
|
|
|
{
|
|
|
|
return left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(Transform2D left, Transform2D right)
|
|
|
|
{
|
|
|
|
return !left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (obj is Transform2D)
|
|
|
|
{
|
|
|
|
return Equals((Transform2D)obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(Transform2D other)
|
|
|
|
{
|
|
|
|
return x.Equals(other.x) && y.Equals(other.y) && o.Equals(other.o);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return x.GetHashCode() ^ y.GetHashCode() ^ o.GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1}, {2})", new object[]
|
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
x.ToString(),
|
|
|
|
y.ToString(),
|
|
|
|
o.ToString()
|
2017-10-02 21:24:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ToString(string format)
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1}, {2})", new object[]
|
|
|
|
{
|
2018-04-08 03:28:24 +00:00
|
|
|
x.ToString(format),
|
|
|
|
y.ToString(format),
|
|
|
|
o.ToString(format)
|
2017-10-02 21:24:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|