提交 24773abb 编写于 作者: 魔术师Dix's avatar 魔术师Dix

平面与直线的交点计算;

上级 b2551d25
......@@ -7,7 +7,6 @@
*版本: 1.0
*/
using Unity.Mathematics;
namespace Oroxylum
......@@ -15,9 +14,44 @@ namespace Oroxylum
/// <summary>
/// 计算结果:3d直线与线段
/// </summary>
public struct IntrLine3dPlane
public struct IntrLine3dPlane
{
public E_IntersectionResult Result;
public E_IntersectionType Type;
public float3 CastPoint;
public IntrLine3dPlane(Line3d line, Plane3d plane)
{
Result = E_IntersectionResult.NotComputed;
float d1 = math.dot(plane.PlanePoint - line.Origin, plane.Normal);
float d2 = math.dot(line.Direction, plane.Normal);
if (d2 == 0)
{
float distance = plane.GetDistanceToPoint(line.Origin);
if (distance <= OMath.ZeroTolerance)
{
//直线就在平面上;
Type = E_IntersectionType.Line;
Result = E_IntersectionResult.NotComputed;
CastPoint = line.Origin;
}
else
{
//此时直线与平面平行;
Type = E_IntersectionType.Empty;
Result = E_IntersectionResult.NoIntersection;
CastPoint = float.MaxValue;
}
return;
}
Type = E_IntersectionType.Point;
Result = E_IntersectionResult.Intersects;
CastPoint = (d1 / d2 * line.Direction) + line.Origin;
}
}
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@
*/
using Unity.Mathematics;
using UnityEngine;
namespace Oroxylum
{
......@@ -17,12 +18,21 @@ namespace Oroxylum
public struct Line3d
{
public float3 Origin;
/// <summary>
/// 直线方向,已经 normalize
/// </summary>
public float3 Direction;
public Line3d(float3 origin, float3 direction)
{
this.Origin = origin;
this.Direction = direction;
this.Direction = math.normalize(direction);
}
public Line3d(Ray ray)
{
this.Origin = ray.origin;
this.Direction = ray.direction;
}
public static Line3d FromPoints(float3 p0, float3 p1)
......
/*
*Copyright(C) 2020 by Cyf All rights reserved.
*Unity版本:2021.3.11f1c2
*作者:程一峰
*创建日期: 2022-11-10
*模块说明:木蝴蝶数学库:数据格式
*版本: 1.0
*/
using Unity.Mathematics;
using UnityEngine;
namespace Oroxylum
{
/// <summary>
/// 平面,与 UnityEngine.Plane 等价;
/// 不用Unity的Plane是因为其是基于 Vector 构建的;
/// 而 Plane3d 是基于 float3 构建
/// </summary>
public struct Plane3d
{
/// <summary>
/// 平面法线方向,已经 normalize
/// </summary>
public float3 Normal;
public float Distance;
/// <summary>
/// 平面上的一点;
/// </summary>
public float3 PlanePoint => Normal * -Distance;
/// <summary>
/// 构造平面
/// </summary>
/// <param name="normal">法线</param>
/// <param name="distance">平面到原点的距离</param>
public Plane3d(float3 normal, float distance)
{
Normal = math.normalize(normal);
Distance = distance;
}
/// <summary>
/// 构造平面
/// </summary>
/// <param name="normal">法线</param>
/// <param name="point">平面上一点</param>
public Plane3d(float3 normal, float3 point)
{
Normal = math.normalize(normal);
Distance = -math.dot(Normal, point);
}
// N = Cross(P1-P0,P2-P0)/Length(Cross(P1-P0,P2-P0)), c = Dot(N,P0) where
// P0, P1, P2 are points on the plane.
/// <summary>
/// 根据平面上的3个点进行构建;
/// </summary>
public Plane3d(float3 a, float3 b, float3 c)
{
Normal = math.normalize(math.cross(b - a, c - a));
Distance = -math.dot(Normal, a);
}
public Plane3d(Plane p)
{
Normal = p.normal;
Distance = p.distance;
}
// Compute d = Dot(N,P)-c where N is the plane normal and c is the plane
// constant. This is a signed distance. The sign of the return value is
// positive if the point is on the positive side of the plane, negative if
// the point is on the negative side, and zero if the point is on the
// plane.
public float GetDistanceToPoint(float3 p)
{
return Normal.Dot(p) - Distance;
}
// The "positive side" of the plane is the half space to which the plane
// normal points. The "negative side" is the other half space. The
// function returns +1 when P is on the positive side, -1 when P is on the
// the negative side, or 0 when P is on the plane.
public int WhichSide(float3 p)
{
double distance = GetDistanceToPoint(p);
if (distance < 0)
return -1;
else if (distance > 0)
return +1;
else
return 0;
}
public bool SameSide(float3 inPt0, float3 inPt1)
{
float distanceToPoint = GetDistanceToPoint(inPt0);
float distanceToPoint2 = GetDistanceToPoint(inPt1);
return (distanceToPoint > 0f && distanceToPoint2 > 0f) || (distanceToPoint <= 0f && distanceToPoint2 <= 0f);
}
}
}
fileFormatVersion: 2
guid: b1abfdd23e097334abac5f2a46dd672e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -152,6 +152,15 @@ namespace Oroxylum
public static float3 Cross(this float2 a, float2 b) { return (a.x * b.y) - (a.y * b.x); }
public static float3 UnitCross(this float3 v1, float3 v2)
{
float3 n = new float3(
(v1.y * v2.z) - (v1.z * v2.y),
(v1.z * v2.x) - (v1.x * v2.z),
(v1.x * v2.y) - (v1.y * v2.x));
return math.normalize(n);
}
public static float LengthSquared(this float2 a) { return math.lengthsq(a); }
public static float LengthSquared(this float3 a) { return math.lengthsq(a); }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册