From b2551d2510cac17e069b8d0957509cb5d8d11886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E3=80=91=E7=A8=8B=E4=B8=80?= =?UTF-8?q?=E5=B3=B0?= <649669121@qq.com> Date: Thu, 10 Nov 2022 13:52:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=B0=E6=8D=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84=20line3d=EF=BC=8C=E5=B9=B6=E7=A7=BB=E9=99=A4=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84plane?= =?UTF-8?q?=EF=BC=88=E4=BD=BF=E7=94=A8Unity=E8=87=AA=E5=B8=A6=E7=9A=84?= =?UTF-8?q?=E5=8D=B3=E5=8F=AF=EF=BC=89=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Intersection/IntrLine3dPlane.cs | 4 ++- Math/Line2d.cs | 6 ++++ Math/Line3d.cs | 49 ++++++++++++++++++++++++++ Math/{Plane.cs.meta => Line3d.cs.meta} | 2 +- Math/Plane.cs | 21 ----------- OMath.cs | 2 ++ 6 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 Math/Line3d.cs rename Math/{Plane.cs.meta => Line3d.cs.meta} (83%) delete mode 100644 Math/Plane.cs diff --git a/Intersection/IntrLine3dPlane.cs b/Intersection/IntrLine3dPlane.cs index 72ec498..7226218 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 5dcd0fb..3781f88 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 0000000..dae02c2 --- /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 16d29fe..cb8962d 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 b33f418..0000000 --- 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 6ceda00..3c9ba30 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); } /// -- GitLab