2017-11-05 11:37:59 +01:00
|
|
|
using System;
|
2018-03-01 08:51:35 +01:00
|
|
|
#if REAL_T_IS_DOUBLE
|
|
|
|
using real_t = System.Double;
|
|
|
|
#else
|
|
|
|
using real_t = System.Single;
|
|
|
|
#endif
|
|
|
|
|
2017-11-05 11:37:59 +01:00
|
|
|
namespace Godot
|
|
|
|
{
|
|
|
|
public struct Plane : IEquatable<Plane>
|
|
|
|
{
|
|
|
|
Vector3 normal;
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t x
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return normal.x;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
normal.x = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t y
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return normal.y;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
normal.y = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t z
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return normal.z;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
normal.z = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t d;
|
2017-11-05 11:37:59 +01:00
|
|
|
|
|
|
|
public Vector3 Center
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return normal * d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public real_t DistanceTo(Vector3 point)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return normal.Dot(point) - d;
|
2017-11-05 11:37:59 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 GetAnyPoint()
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
|
|
|
return normal * d;
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
public bool HasPoint(Vector3 point, real_t epsilon = Mathf.Epsilon)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t dist = normal.Dot(point) - d;
|
2017-11-21 23:32:19 +01:00
|
|
|
return Mathf.Abs(dist) <= epsilon;
|
2017-11-05 11:37:59 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Intersect3(Plane b, Plane c)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t denom = normal.Cross(b.normal).Dot(c.normal);
|
2017-11-05 11:37:59 +01:00
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
if (Mathf.Abs(denom) <= Mathf.Epsilon)
|
2017-11-05 11:37:59 +01:00
|
|
|
return new Vector3();
|
|
|
|
|
2018-04-08 05:28:24 +02:00
|
|
|
Vector3 result = (b.normal.Cross(c.normal) * d) +
|
2017-11-21 23:32:19 +01:00
|
|
|
(c.normal.Cross(normal) * b.d) +
|
|
|
|
(normal.Cross(b.normal) * c.d);
|
2017-11-05 11:37:59 +01:00
|
|
|
|
|
|
|
return result / denom;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 IntersectRay(Vector3 from, Vector3 dir)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t den = normal.Dot(dir);
|
2017-11-05 11:37:59 +01:00
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
if (Mathf.Abs(den) <= Mathf.Epsilon)
|
2017-11-05 11:37:59 +01:00
|
|
|
return new Vector3();
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t dist = (normal.Dot(from) - d) / den;
|
2017-11-05 11:37:59 +01:00
|
|
|
|
2018-01-18 21:37:17 +01:00
|
|
|
// This is a ray, before the emitting pos (from) does not exist
|
2017-11-05 11:37:59 +01:00
|
|
|
if (dist > Mathf.Epsilon)
|
|
|
|
return new Vector3();
|
|
|
|
|
|
|
|
return from + dir * -dist;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 IntersectSegment(Vector3 begin, Vector3 end)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
|
|
|
Vector3 segment = begin - end;
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t den = normal.Dot(segment);
|
2017-11-05 11:37:59 +01:00
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
if (Mathf.Abs(den) <= Mathf.Epsilon)
|
2017-11-05 11:37:59 +01:00
|
|
|
return new Vector3();
|
|
|
|
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t dist = (normal.Dot(begin) - d) / den;
|
2017-11-05 11:37:59 +01:00
|
|
|
|
|
|
|
if (dist < -Mathf.Epsilon || dist > (1.0f + Mathf.Epsilon))
|
|
|
|
return new Vector3();
|
|
|
|
|
|
|
|
return begin + segment * -dist;
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public bool IsPointOver(Vector3 point)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return normal.Dot(point) > d;
|
2017-11-05 11:37:59 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Plane Normalized()
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
2018-03-01 08:51:35 +01:00
|
|
|
real_t len = normal.Length();
|
2017-11-05 11:37:59 +01:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return new Plane(0, 0, 0, 0);
|
|
|
|
|
|
|
|
return new Plane(normal / len, d / len);
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:32:19 +01:00
|
|
|
public Vector3 Project(Vector3 point)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
return point - normal * DistanceTo(point);
|
2017-11-05 11:37:59 +01:00
|
|
|
}
|
2018-03-01 08:51:35 +01:00
|
|
|
|
|
|
|
// Constructors
|
|
|
|
public Plane(real_t a, real_t b, real_t c, real_t d)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
|
|
|
normal = new Vector3(a, b, c);
|
|
|
|
this.d = d;
|
|
|
|
}
|
2018-03-01 08:51:35 +01:00
|
|
|
public Plane(Vector3 normal, real_t d)
|
2017-11-05 11:37:59 +01:00
|
|
|
{
|
|
|
|
this.normal = normal;
|
|
|
|
this.d = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Plane(Vector3 v1, Vector3 v2, Vector3 v3)
|
|
|
|
{
|
2017-11-21 23:32:19 +01:00
|
|
|
normal = (v1 - v3).Cross(v1 - v2);
|
|
|
|
normal.Normalize();
|
|
|
|
d = normal.Dot(v1);
|
2017-11-05 11:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Plane operator -(Plane plane)
|
|
|
|
{
|
|
|
|
return new Plane(-plane.normal, -plane.d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator ==(Plane left, Plane right)
|
|
|
|
{
|
|
|
|
return left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool operator !=(Plane left, Plane right)
|
|
|
|
{
|
|
|
|
return !left.Equals(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (obj is Plane)
|
|
|
|
{
|
|
|
|
return Equals((Plane)obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(Plane other)
|
|
|
|
{
|
|
|
|
return normal == other.normal && d == other.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return normal.GetHashCode() ^ d.GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1})", new object[]
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
normal.ToString(),
|
|
|
|
d.ToString()
|
2017-11-05 11:37:59 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ToString(string format)
|
|
|
|
{
|
|
|
|
return String.Format("({0}, {1})", new object[]
|
|
|
|
{
|
2018-04-08 05:28:24 +02:00
|
|
|
normal.ToString(format),
|
|
|
|
d.ToString(format)
|
2017-11-05 11:37:59 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|