3D 中两个矩形之间的交集

2023-11-22

为了获得 3D 中两个矩形之间的交线,我将它们转换为平面,然后使用法线的叉积获得交线,然后尝试获得与矩形的每个线段的线交点。

问题是这条线平行于三段,并且只与 NAN,NAN,NAN 中的一条相交,这是完全错误的。你能告诉我我的代码有什么问题吗?

我使用此链接中的 vector3http://www.koders.com/csharp/fidCA8558A72AF7D3E654FDAFA402A168B8BC23C22A.aspx

并创建了我的飞机类如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace referenceLineAlgorithm
{
struct Line
{

    public Vector3 direction;
    public Vector3 point;

}

struct lineSegment
{

    public Vector3 firstPoint;
    public Vector3 secondPoint;

}

class plane_test
{
    public enum Line3DResult
    {
        Line3DResult_Parallel = 0,
        Line3DResult_SkewNoCross = 1,
        Line3DResult_SkewCross = 2
    };

    #region Fields

    public Vector3 Normal;
    public float D;
    public Vector3[] cornersArray;
    public Vector3 FirstPoint;
    public Vector3 SecondPoint;
    public Vector3 temp;
    public Vector3 normalBeforeNormalization;


    #endregion

    #region constructors

    public plane_test(Vector3 point0, Vector3 point1, Vector3 point2, Vector3 point3)
    {
        Vector3 edge1 = point1 - point0;
        Vector3 edge2 = point2 - point0;
        Normal = edge1.Cross(edge2);
        normalBeforeNormalization = Normal;

        Normal.Normalize();
        D = -Normal.Dot(point0);

        ///// Set the Rectangle corners 
        cornersArray = new Vector3[] { point0, point1, point2, point3 };

    }

    #endregion

    #region Methods
    /// <summary>
    /// This is a pseudodistance. 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.
    /// The absolute value of the return value is the true distance only
    /// when the plane normal is a unit length vector.
    /// </summary>
    /// <param name="point"></param>
    /// <returns></returns>
    public float GetDistance(Vector3 point)
    {
        return Normal.Dot(point) + D;
    }

    public void Intersection(plane_test SecondOne)
    {
        ///////////////////////////// Get the parallel to the line of interrsection (Direction )
        Vector3 LineDirection = Normal.Cross(SecondOne.Normal);

        float d1 = this.GetDistance(LineDirection);
        float d2 = SecondOne.GetDistance(LineDirection);

        temp = (LineDirection - (this.Normal * d1) - (SecondOne.Normal * d2));

        temp.x = Math.Abs((float)Math.Round((decimal)FirstPoint.x, 2));
        temp.y = Math.Abs((float)Math.Round((decimal)FirstPoint.y, 2));

        Line line;
        line.direction = LineDirection;
        line.point = temp;

        ////////// Line segments 

        lineSegment AB, BC, CD, DA;

        AB.firstPoint = cornersArray[0]; AB.secondPoint = cornersArray[1];
        BC.firstPoint = cornersArray[1]; BC.secondPoint = cornersArray[2];
        CD.firstPoint = cornersArray[2]; CD.secondPoint = cornersArray[3];
        DA.firstPoint = cornersArray[3]; DA.secondPoint = cornersArray[0];

        Vector3 r1 = new Vector3(-1, -1, -1);
        Vector3 r2 = new Vector3(-1, -1, -1);
        Vector3 r3 = new Vector3(-1, -1, -1);
        Vector3 r4 = new Vector3(-1, -1, -1);

        /*
        0,0 |----------------| w,0
            |                |
            |                |
        0,h |________________|  w,h


         */

        IntersectionPointBetweenLines(AB, line, ref r1);
        IntersectionPointBetweenLines(BC, line, ref r2);
        IntersectionPointBetweenLines(CD, line, ref r3);
        IntersectionPointBetweenLines(DA, line, ref r4);

        List<Vector3> points = new List<Vector3>();
        points.Add(r1);
        points.Add(r2);
        points.Add(r3);
        points.Add(r4);
        points.RemoveAll(

           t => ((t.x == -1) && (t.y == -1) && (t.z == -1))


           );

        if (points.Count == 2)
        {
            FirstPoint = points[0];
            SecondPoint = points[1];


        }




    }

    public Line3DResult IntersectionPointBetweenLines(lineSegment first, Line aSecondLine, ref Vector3 result)
    {
        Vector3 p1 = first.firstPoint;
        Vector3 n1 = first.secondPoint - first.firstPoint;


        Vector3 p2 = aSecondLine.point;
        Vector3 n2 = aSecondLine.direction;

        bool parallel = AreLinesParallel(first, aSecondLine);
        if (parallel)
        {

            return Line3DResult.Line3DResult_Parallel;
        }
        else
        {
            float d = 0, dt = 0, dk = 0;
            float t = 0, k = 0;

            if (Math.Abs(n1.x * n2.y - n2.x * n1.y) > float.Epsilon)
            {
                d = n1.x * (-n2.y) - (-n2.x) * n1.y;
                dt = (p2.x - p1.x) * (-n2.y) - (p2.y - p1.y) * (-n2.x);
                dk = n1.x * (p2.x - p1.x) - n1.y * (p2.y - p1.y);
            }
            else if (Math.Abs(n1.z * n2.y - n2.z * n1.y) > float.Epsilon)
            {
                d = n1.z * (-n2.y) - (-n2.z) * n1.y;
                dt = (p2.z - p1.z) * (-n2.y) - (p2.y - p1.y) * (-n2.z);
                dk = n1.z * (p2.z - p1.z) - n1.y * (p2.y - p1.y);
            }
            else if (Math.Abs(n1.x * n2.z - n2.x * n1.z) > float.Epsilon)
            {
                d = n1.x * (-n2.z) - (-n2.x) * n1.z;
                dt = (p2.x - p1.x) * (-n2.z) - (p2.z - p1.z) * (-n2.x);
                dk = n1.x * (p2.x - p1.x) - n1.z * (p2.z - p1.z);
            }

            t = dt / d;
            k = dk / d;

            result = n1 * t + p1;

            // Check if the point on the segmaent or not 
           // if (! isPointOnSegment(first, result))
            //{
               // result = new Vector3(-1,-1,-1);


           // }

            return Line3DResult.Line3DResult_SkewCross;

        }



    }
    private bool AreLinesParallel(lineSegment first, Line aSecondLine)
    {
        Vector3 vector = (first.secondPoint - first.firstPoint);
        vector.Normalize();

        float kl = 0, km = 0, kn = 0;
        if (vector.x != aSecondLine.direction.x)
        {
            if (vector.x != 0 && aSecondLine.direction.x != 0)
            {
                kl = vector.x / aSecondLine.direction.x;
            }
        }
        if (vector.y != aSecondLine.direction.y)
        {
            if (vector.y != 0 && aSecondLine.direction.y != 0)
            {
                km = vector.y / aSecondLine.direction.y;
            }
        }
        if (vector.z != aSecondLine.direction.z)
        {
            if (vector.z != 0 && aSecondLine.direction.z != 0)
            {
                kn = vector.z / aSecondLine.direction.z;
            }
        }

        // both if all are null or all are equal, the lines are parallel
        return (kl == km && km == kn);




    }

    private bool isPointOnSegment(lineSegment segment, Vector3 point)
    {
        //(x - x1) / (x2 - x1) = (y - y1) / (y2 - y1) = (z - z1) / (z2 - z1)
        float component1 = (point.x - segment.firstPoint.x) / (segment.secondPoint.x  - segment.firstPoint.x);
        float component2 = (point.y - segment.firstPoint.y) / (segment.secondPoint.y - segment.firstPoint.y);
        float component3 = (point.z - segment.firstPoint.z) / (segment.secondPoint.z - segment.firstPoint.z); 

        if ((component1 == component2) && (component2 == component3))
        {
            return true;


        }
        else
        {
            return false;

        }

    }

    #endregion
}
}

static void Main(string[] args)
    {

        //// create the first plane points 
        Vector3 point11 =new Vector3(-255.5f, -160.0f,-1.5f) ;    //0,0
        Vector3 point21 = new Vector3(256.5f, -160.0f, -1.5f);   //0,w
        Vector3 point31 = new Vector3(256.5f, -160.0f, -513.5f); //h,0
        Vector3 point41 = new Vector3(-255.5f, -160.0f, -513.5f); //w,h 

        plane_test plane1 = new plane_test(point11, point21, point41, point31);

        //// create the Second plane points 

        Vector3 point12 = new Vector3(-201.6289f, -349.6289f, -21.5f);
        Vector3 point22 =new Vector3(310.3711f,-349.6289f,-21.5f);
        Vector3 point32 = new Vector3(310.3711f, 162.3711f, -21.5f);
        Vector3 point42 =new Vector3(-201.6289f,162.3711f,-21.5f);
        plane_test plane2 = new plane_test(point12, point22, point42, point32);


        plane2.Intersection(plane1);



    }

这是测试值 此致


您需要首先指定一件事:

  • 3D 矩形是指 3D 平面上的平面矩形。 (不是一个 长方体)。

假设您的矩形不是共面非平行的,因此有一条唯一的线 D1 表示每个矩形所描述的平面的交点。

鉴于此假设,2 个矩形 R1 和 R2 相交有 4 种可能的情况:

enter image description here

(注意:有时 D1 既不与 R1 也不与 R2 和 R1 相交,R2 可以旋转一点,因此 D1 并不总是与平行边相交,而是与连续边相交)

当两个矩形之间存在交集时,D1 始终与 R1 和 R2 相交在同一个交集上(参见第一张和第二张图片)

你的模型不好,因为你的线不能平行于同一个矩形的 3 段......

正如您在这个问题中所问的:3D 线相交算法一旦你有了 D1 (获取两个矩形相交定义的线段的端点)只需确定与矩形各线段的交点即可。(需要检查每个矩形的4条线段)

然后检查是否有共同的交集……如果找到,那么你的矩形就相交了。

抱歉,直接检查代码非常困难,但我想通过这些信息您应该能够找到错误。

希望能帮助到你。


EDIT:

通过一个点和 2 个向量定义一个矩形:

R2 {A ,u ,v}
R1 {B, u',v'}

定义由 R1 和 R2 描述的平面:P1 和 P2

P1(或 P2)的一个正交向量是 n1(或 n2)。让n1 = u ^ v and n2 = u' ^ v' with :

enter image description here

then

P1: n1.(x-xA,y-yA,z-zA)=0
P2: n2.(x-xB,y-yB,z-zB)=0

那么,如果您只是寻找 D1,则 D1 的方程为:

D1: P1^2 + P2 ^2 =0 (x,y,z verify P1 =0  an P2 =0 )

D1 : n1.(x-xA,y-yA,z-zA)^2 + n2.(x-xB,y-yB,z-zB)^2 =0

(因此,只需使用矩形的表达式,您就可以得到具有封闭公式的 D1 方程。)

现在让我们看看交叉点:

R1 中的 4 个点是:

{ A , A+u , A+v , A+u+v }

如中所述3D 线相交算法 do :

D1 inter [A,A+u] = I1
D1 inter [A,A+v] = I2
D1 inter [A+u,A+u+v] = I3
D1 inter [A+v,A+u+v] = I4

(I1,I2,I3,I4 可以为空)

same for D2 you get I1' I2' I3' I4'

如果 Ij'=Ik' != null 那么它是一个交点

如果你一步一步正确地做到了这一点,你应该得到正确的解决方案;除非我没有完全理解这个问题......

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

3D 中两个矩形之间的交集 的相关文章

  • 在 Python 中从 Excel 复制 YEARFRAC() 函数

    因此 我使用 python 来自动执行一些必须在 Excel 中执行的重复任务 我需要做的计算之一需要使用yearfrac 这在Python中被复制了吗 I found this https lists oasis open org arc
  • 更快的算法来计算有多少数字可以被范围内的特定整数整除

    int a b c d 0 cin gt gt a gt gt b gt gt c for int i a i lt b i if i c 0 d cout lt
  • 没有 Unicode 字节顺序标记。无法切换到 Unicode

    我正在使用 XSD 编写 XML 验证器 下面是我所做的 但是当验证器到达该线时while list Read 它给了我错误 没有 Unicode 字节顺序标记 无法切换到 Unicode 有人可以帮我解决吗 public class Va
  • 使用不带参数的 Split() 时,默认分隔符是什么?

    所以我看了看String Split 今天 C 中的方法 我意识到你也可以向它传递零参数 这是我从未考虑过的 使用时默认的分隔符是什么Split 没有任何参数 如果没有值 则为空白 来源自here https msdn microsoft
  • 添加边后更新最大流量

    考虑我们有一个网络流量 并使用 Edmond Karp 算法 我们已经拥有网络上的最大流量 现在 如果我们向网络添加任意边 具有一定容量 更新最大流量的最佳方法是什么 我正在考虑更新关于新边缘的残差网络 并再次寻找增强路径 直到找到新的最大
  • rand() 播种与 time() 问题

    我很难弄清楚如何使用 rand 并使用 Xcode 用 time 为其播种 我想生成 0 到 1 之间的随机十进制数 该代码为我提供了元素 1 和 2 看似随机的数字 但元素 0 始终在 0 077 左右 有什么想法吗 我的代码是 incl
  • F10键没被抓住

    I have a Windows Form and there overriden ProcessCmdKey However this works with all of the F Keys except for F10 I am tr
  • 将成员函数作为参数传递/c++

    我想用 C 实现一个类b可以通过封装该迭代器类型的成员集进行某种迭代 喜欢 b object for each x do function f so 函数 f会得到每个人的x成员并做任何事情 比方说 void function f x me
  • 线程安全的 C++ 堆栈

    我是 C 新手 正在编写一个多线程应用程序 不同的编写者将对象推入堆栈 读者将它们从堆栈中拉出 或至少将指针推入对象 C 中是否有任何内置结构可以在不添加锁定代码等的情况下处理此问题 如果没有 那么 Boost 库呢 EDIT 你好 感谢您
  • QThread - 使用槽 quit() 退出线程

    我想在线程完成运行时通知对象 但是 我无法让线程正确退出 我有以下代码 处理器 cpp thread new QThread tw new ThreadWorker connect tw SIGNAL updateStatus QStrin
  • c# 如何生成锦标赛括号 HTML 表

    所以我已经被这个问题困扰了三个星期 但我一生都无法弄清楚 我想做的是使用表格获得这种输出 演示 http www esl world net masters season6 hanover sc2 playoffs rankings htt
  • 如何在 C# 中使用 XmlDsigC14NTransform 类

    我正在尝试使用规范化 xml 节点System Security Cryptography Xml XMLDsigC14nTransformC net Framework 2 0 的类 该实例需要三种不同的输入类型 NodeList Str
  • C# 中处理 SQL 死锁的模式?

    我正在用 C 编写一个访问 SQL Server 2005 数据库的应用程序 该应用程序是数据库密集型的 即使我尝试优化所有访问 设置适当的索引等 我预计迟早会遇到死锁 我知道为什么会发生数据库死锁 但我怀疑我能否在某个时候发布不发生死锁的
  • 为什么WCF中不允许方法重载?

    假设这是一个ServiceContract ServiceContract public interface MyService OperationContract int Sum int x int y OperationContract
  • 你能解释一下这个C++删除问题吗?

    我有以下代码 std string F WideString ws GetMyWideString std string ret StringUtils ConvertWideStringToUTF8 ws ret return ret W
  • 是否有任何不使用公共虚拟方法的正当理由? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 是否有任何不使用公共虚拟方法的正当理由 我在某处读到我们应该避免使用公共虚拟方法 但我想向专家确认这是否是有效的声明 对于良好且稳定的 API
  • 为什么 C# 接口名称前面加上“I”

    这种命名约定背后的基本原理是什么 我没有看到任何好处 额外的前缀只会污染 API 我的想法与康拉德一致response https stackoverflow com a 222502 9898与此相关的question https sta
  • 如何强制执行特定的 UserControl 设计

    我正在编写一个基本用户控件 它将由一堆其他用户控件继承 我需要对所有这些后代控件强制执行某种设计 例如 顶部必须有几个按钮以及一个或两个标签 后代用户控件区域的其余部分可以自由放置任何内容 最初 我认为我可以将一个面板放到 Base Use
  • c# 替代方案中 cfusion_encrypt 中填充的密钥是什么?

    我找到了从这里复制 C 中的 cfusion encrypt 函数的答案 ColdFusion cfusion encrypt 和 cfusion decrypt C 替代方案 https stackoverflow com questio
  • 将文本从文本文件添加到 PDF 文件[重复]

    这个问题在这里已经有答案了 这是我的代码 using FileStream msReport new FileStream pdfPath FileMode Create step 1 using Document pdfDoc new D

随机推荐

  • switch 的替代语法

    嘿 PHP 中的 switch 语句有一种替代语法 但此代码不起作用 div div Newspage div div Forum div div 解析错误 语法错误 意外的T INLINE HTML 期待 path to file 行中的
  • mysqli出现问题怎么办?类似 mysqli_fetch_array() 的错误:参数 #1 必须是 mysqli_result 类型等

    在我的本地 开发环境中 MySQLi 查询执行正常 但是 当我将其上传到我的网络主机环境时 出现以下错误 致命错误 在 中的非对象上调用成员函数bind param 这是代码 global mysqli stmt mysqli gt pre
  • Javascript 文件无法在 github 页面上运行 [重复]

    这个问题在这里已经有答案了 我能够在本地计算机上加载我的 javascript 文件 但在 github 页面上 它不起作用 我已将该文件包含在 index html 中 该网站是https abhikulshrestha22 github
  • Java 堆转储:如何通过 1. io.netty.buffer.ByteBuf Util 2. byte[] 数组查找占用内存的对象/类

    我发现我的一个 Spring Boot 项目的内存 RAM 消耗 日益增加 当我将 jar 文件上传到 AWS 服务器时 它占用了 582 MB 的 RAM 最大分配的 RAM 为 1500 MB 但是每天 RAM 都会增加 50MB 到
  • 用作接口替代品的内部类示例

    我被告知的内容激发了我对这个话题的好奇 Java gui 类可以实现数百个侦听器和回调 并且许多书籍教您在 gui 类中实现所有这些接口 或者 这些方面可以在内部类中实现 因此侦听器调用的方法不会混淆 我想知道如何在 ActionScrip
  • 在Python中,在具有特定扩展名的目录中构建文件列表的最快方法

    在 GNU Linux 系统上的 Python 中 递归扫描目录中所有内容的最快方法是什么 MOV or AVI文件 并将它们存储在列表中 您可以使用 os walk 进行递归行走 使用 glob glob 或 fnmatch filter
  • 设置自定义字体

    我正在尝试在程序中将自定义字体 bilboregular ttf 设置为 2 个 jLabels 但字体未成功加载 主要方法调用如下 this should work if the build is in a jar file otherw
  • 如何在 MySQL 中将字符串添加到列值之前?

    我需要一个 SQL 更新语句来更新所有行的特定字段 并在现有值的前面添加一个字符串 test 例如 如果现有值为 try 则应变为 testtry 您可以使用CONCAT函数来做到这一点 UPDATE tbl SET col CONCAT
  • 从现有存储库的子目录创建新的 git 存储库

    我想从现有存储库的子文件夹中创建一个单独的存储库 将子目录分离 移动 到单独的 Git 存储库中恰恰表明了这一点 但是 我无法用它得到一个干净的仓库 我最终遇到了新存储库的两个问题 历史似乎是重复的 我无法保留分支历史记录 这是我所做的 g
  • 用 range() 以相反的顺序打印列表?

    如何生成以下列表range 在Python中 9 8 7 6 5 4 3 2 1 0 Use reversed 函数 高效 因为range实施 reversed reversed range 10 这更有意义 Update list cas
  • 关于 emacs 中的自动完成和 yasnippet

    我在 Emacs 中使用自动完成和 yasnippet 但我对它们的设置感到困惑 我将以下代码放入我的 emacs add to list load path emacs d plugins yasnippet require yasnip
  • 我应该使用 Windows 服务还是 ASP.NET 后台线程?

    我正在 ASP NET 3 5 中编写一个 Web 应用程序 它负责一些基本的数据输入场景 应用程序还有一个组件需要不断轮询一些数据并根据业务逻辑执行操作 实现 轮询 组件的最佳方法是什么 它需要每隔几分钟左右运行一次并检查数据 我过去见过
  • Android模拟器-dns-server使用

    我目前正在努力让 Android 模拟器使用在本地主机 127 0 0 1 上运行的自定义 DNS 服务器 到目前为止 我已经尝试了各种方法试图说服模拟器使用我的 DNS 服务器 但到目前为止还没有成功 最后 它总是归结为以下错误消息 WA
  • “not, and, or, not_eq..”是 C++ 标准的一部分吗? (为什么在代码中可以使用或避免它们?)

    所以它看起来像所有这些 http www cplusplus com reference clibrary ciso646 是c 中的关键字 我的问题是 这是 C 标准的一部分吗 我可以依赖它得到主要编译器的支持吗 我知道 gcc 确实支持
  • 如何在 Bash 中比较点分隔版本格式的两个字符串?

    有没有办法在 bash 上比较这些字符串 例如 2 4 5 and 2 8 and 2 4 5 1 这是一个纯 Bash 版本 不需要任何外部实用程序 bin bash vercomp if 1 2 then return 0 fi loc
  • 在 R 中将 CSV 读取为 data.frame

    我是 R 新手 正在尝试读取 csv 文档显示了一个函数read csv 但是 当我读取文件并检查变量的类型时 它会显示一个列表 文档将其显示为data frame 有人可以解释为什么会这样吗 到目前为止我的代码 mytable lt re
  • 移动设备放大 Google Apps 脚本

    我在 GAS 中制作了一个 400 像素宽的网络应用程序 我希望在加载时缩放以填充移动设备的屏幕 而不是只填充一半屏幕 尽管仍然使用固定的像素宽度 我尝试使用下面的视口代码但没有成功 Google 最近几个月解决了这个问题 并使用 addM
  • OpenSSL 静态库太大,有什么替代方法或方法可以减小其大小?

    我已经使用了 OpenSSL 1 0 的预构建静态库 但它使我的二进制文件太大 在发布模式下将其大小增加了约 800Kb 我不需要 OpenSSL 的大部分功能 例如 BIO 我使用自己的套接字 因此在代码中我只使用几个 SSL XXXXX
  • 如何防止ggplot2中的轴相交

    我正在使用 ggplot2 制作一些对数转换数据的线图 这些数据都有很大的值 在 10 6 和 10 8 之间 由于轴不是从零开始 我不想让它们在 原点 相交 以下是轴当前的样子 我更喜欢从基础图形中获得的东西 但我另外使用geom rib
  • 3D 中两个矩形之间的交集

    为了获得 3D 中两个矩形之间的交线 我将它们转换为平面 然后使用法线的叉积获得交线 然后尝试获得与矩形的每个线段的线交点 问题是这条线平行于三段 并且只与 NAN NAN NAN 中的一条相交 这是完全错误的 你能告诉我我的代码有什么问题