diff --git a/Intersection/IntrLine3dPlane.cs b/Intersection/IntrLine3dPlane.cs index 72ec498d6dabc0f6e37fdd28be1a38a6dbbbf188..72262185adbbafb4ba4a993b98ea81fdec7732ce 100644 --- a/Intersection/IntrLine3dPlane.cs +++ b/Intersection/IntrLine3dPlane.cs @@ -8,6 +8,8 @@ */ +using Unity.Mathematics; + namespace Oroxylum { /// @@ -16,6 +18,6 @@ namespace Oroxylum public struct IntrLine3dPlane { - + } } diff --git a/Math/Line2d.cs b/Math/Line2d.cs index 5dcd0fbaaf4036031ca449dde53a3b499be4647a..3781f885ed6ba8468e5ae8e680b5f0326509dd9a 100644 --- a/Math/Line2d.cs +++ b/Math/Line2d.cs @@ -34,6 +34,12 @@ namespace Oroxylum public double Project(float2 p) { return (p - Origin).Dot(Direction); } + public float2 ClosestPoint(float2 p) + { + float t = (p - Origin).Dot(Direction); + return Origin + t * Direction; + } + public double DistanceSquared(float2 p) { float t = (p - Origin).Dot(Direction); diff --git a/Math/Line3d.cs b/Math/Line3d.cs new file mode 100644 index 0000000000000000000000000000000000000000..dae02c2f16cea8a3389c66b02789cea8fb5f4189 --- /dev/null +++ b/Math/Line3d.cs @@ -0,0 +1,49 @@ +/* + *Copyright(C) 2020 by Cyf All rights reserved. + *Unity版本:2021.3.11f1c2 + *作者:程一峰 + *创建日期: 2022-11-10 + *模块说明:木蝴蝶数学库:数据格式 + *版本: 1.0 +*/ + +using Unity.Mathematics; + +namespace Oroxylum +{ + /// + /// 3d 直线 + /// + public struct Line3d + { + public float3 Origin; + public float3 Direction; + + public Line3d(float3 origin, float3 direction) + { + this.Origin = origin; + this.Direction = direction; + } + + public static Line3d FromPoints(float3 p0, float3 p1) + { + return new Line3d(p0, math.normalize(p1 - p0)); + } + + public float Project(float3 p) { return (p - Origin).Dot(Direction); } + + public float3 ClosestPoint(float3 p) + { + float t = (p - Origin).Dot(Direction); + return Origin + t * Direction; + } + + public float DistanceSquared(float3 p) + { + float t = (p - Origin).Dot(Direction); + float3 proj = Origin + t * Direction; + return (proj - p).LengthSquared(); + } + + } +} \ No newline at end of file diff --git a/Math/Plane.cs.meta b/Math/Line3d.cs.meta similarity index 83% rename from Math/Plane.cs.meta rename to Math/Line3d.cs.meta index 16d29febc9f941dc917d6bc1a5dd859d4e679314..cb8962d08d6efecbfb14af7f30c1095c8a0c43d9 100644 --- a/Math/Plane.cs.meta +++ b/Math/Line3d.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 4ea5947ca82e2104da89c84439ced2f7 +guid: 43c47208483b92d40b735849b69bfff2 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Math/Plane.cs b/Math/Plane.cs deleted file mode 100644 index b33f4187a20c17f8a175353c448d6aee372853c8..0000000000000000000000000000000000000000 --- a/Math/Plane.cs +++ /dev/null @@ -1,21 +0,0 @@ -/* - *Copyright(C) 2020 by Cyf All rights reserved. - *Unity版本:2021.3.11f1c2 - *作者:程一峰 - *创建日期: 2022-11-10 - *模块说明:木蝴蝶数学库:数据格式 - *版本: 1.0 -*/ - - -namespace Oroxylum -{ - /// - /// 平面 - /// - public struct Plane - { - - - } -} \ No newline at end of file diff --git a/OMath.cs b/OMath.cs index 6ceda00e2962a3bcff35e996741106d825343536..3c9ba300491aec070550d51f55cdeceeec342f57 100644 --- a/OMath.cs +++ b/OMath.cs @@ -154,6 +154,8 @@ namespace Oroxylum public static float LengthSquared(this float2 a) { return math.lengthsq(a); } + public static float LengthSquared(this float3 a) { return math.lengthsq(a); } + public static float DotPerp(this float2 a, float2 b) { return (a.x * b.y) - (a.y * b.x); } ///