简体   繁体   English

获取3D中两个矩形之间的相交线

[英]Get Intersection line between two rectangles in 3D

I am trying to find a way to get the line (two points in 3D space) of the intersection between two rectangles. 我试图找到一种方法来获取两个矩形之间的交点的线(3D空间中的两个点)。

I ran into this question: Intersection between two rectangles in 3D 我遇到了这个问题: 3D中两个矩形之间的交点

But this is not my issue. 但这不是我的问题。 In that question, the rectangle is treated as only the bounds (the perimeter), while I am looking for the rectangle as a whole (think about a picture frame vs the picture itself). 在这个问题中,当我正在寻找整个矩形时(考虑一下相框与图片本身),矩形仅被视为边界(周长)。

I've figured out that, in every case, there will either be an intersection line (two points), or no intersection at all. 我已经发现,在每种情况下,都会有一个相交线(两个点),或者根本没有相交线。 If the intersection was just on the borders, therefore just a point, it can be treated as no intersection in my case. 如果相交只是在边界上,那么只是一个点,在我的情况下,可以将其视为没有相交。

My scenario is that one of these rectangle represents a "static" surface, which cannot move or change. 我的情况是这些矩形之一代表“静态”曲面,该曲面无法移动或更改。 The other one represents a "dynamic" surface, which I have to adapt to avoid crossing 另一个代表“动态”表面,我必须对其进行调整以避免交叉

Example: 例:

在此处输入图片说明

Once I obtain p1 and p2, which are points in the 3D space, my goal is to modify the Dynamic rectangle into a 3d polygon, which will no longer cross the static rectangle, like this: 一旦获得了3D空间中的点p1和p2,我的目标就是将动态矩形修改为3d多边形,该多边形将不再与静态矩形交叉,如下所示:

在此处输入图片说明

So you can see why "edge intersections" are irrelevant to my situation. 因此,您可以了解为什么“边缘交叉点”与我的情况无关。 I am turning "real" intersections into edge intersections, so any edge intersection doesn't require me to do anything with it. 我正在将“真实的”相交变成边缘相交,因此任何边缘相交都不需要我做任何事情。

I am only looking for a formula, starting with two sets of 4 points (the rectangles), that would give me the two points of the line of their intersection, or would tell me that there is no (relevant) intersection. 我只是在寻找一个公式,从两组四个点(矩形)开始,这将给我它们交点线的两个点,或者告诉我没有(相关的)交点。

Every formula I've found on this site or others doesn't fit my needs, or doesn't let me input arbitrary rectangles (for example, I can't fix my problem with a formula that uses planes or that treats a rectangle as simply 4 lines) 我在本网站或其他网站上找到的每个公式都不符合我的需要,或者不允许我输入任意矩形(例如,我无法解决使用平面或将矩形视为只需4行)

I am, of course, trying to code it (in C#), therefore any code answer is a great help, but I am confident that even a math-only answer would suffice for me to produce the code from it, therefore I will accept an answer that is only composed of pseudo-code or straight up mathematical formulas, provided they are either simple enough or explained well enough for me to understand what is happening. 当然,我正在尝试使用C#对其进行编码,因此任何代码答案都将为您提供很大的帮助,但是我相信,即使是仅数学答案也足以满足我从中生成代码的需要,因此我会接受仅由伪代码或简单的数学公式组成的答案,只要它们足够简单或足以让我理解正在发生的事情即可。

If you are OK with just algorithm rather than full code here is a sketch: 如果您只使用算法而不是完整代码,可以使用以下示意图:

  1. Build 2 planes from the rectangles (any 3 points will do as in this answer ) 从矩形构建2个平面(任何3个点都将按照此答案进行
  2. Find the intersection line I of those 2 planes as in this answer or find out that the planes are parallel so there is no intersection 此答案中找到这两个平面的交线I或找出平面是平行的,因此没有交点
  3. Find the intersections of the I line with the lines containing all sides of the rectangles as in this answer 此答案中找到I线与包含矩形所有边的线的交点
  4. Check whether some points found in the previous step lie inside the corresponding sides of the rectangles (line segments). 检查在上一步中找到的一些点是否在矩形(线段)的相应边内。 This step potentially can be merged with the previous one, but I put it separately for simplicity. 此步骤可能可以与上一步合并,但是为了简单起见,我将其分开放置。 Now you potentially have 0, 1 or 2 segments that represent the intersections of the I line with your two rectangles (note that here point is treated as an edge case of a segment where both ends are the same). 现在,您可能有0、1或2个线段,它们代表I线与两个矩形的交点(请注意,这里的点被视为两端相同的线段的边沿情况)。 If you don't have 2 segments, there is no intersection of the rectangles. 如果没有2个线段,则矩形不相交。
  5. Assuming at the previous step you found 2 segments (one in each rectangle) on the line I , you just need to find their intersection and it will be your answer (again, empty means no intersection). 假设在上一步中,在I线上找到了两个线段(每个矩形中一个),您只需要找到它们的交点即可,这就是您的答案(同样,空白表示没有交点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM