如何确定一个点在立方体的内部还是外部? [英] How to determine a point is inside or outside a cube?

查看:840
本文介绍了如何确定一个点在立方体的内部还是外部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个在3D空间中具有8个顶点的多维数据集.如何确定myPoint在多维数据集之内还是之外?

Given a cube with 8 vertex in 3D space. How could I determine the myPoint is inside or outside the cube?

cube[0] = (x0, y0, z0);
cube[1] = (x1, y1, z1);
cube[2] = (x2, y2, z2);
cube[3] = (x3, y3, z3);
cube[4] = (x4, y4, z4);
cube[5] = (x5, y5, z5);
cube[6] = (x6, y6, z6);
cube[7] = (x7, y7, z7);

myPoint = (x, y, z);

我正在尝试实施数据过滤技术在3D模式下

I am trying to implement this data filter technique in 3D

推荐答案

特殊情况#1(轴对齐的立方体):

maxim1000的答案所示,您可以简单地检查所考虑点的X,Y,Z坐标是否位于多维数据集的X,Y,Z坐标的最小值和最大值.

As maxim1000's answer shows, you can simply check if X, Y, Z coordinates of the point in consideration lie in the minimum and maximum of the X, Y, Z coordinates of the Cube.

X_min <= X <= X_max and Y_min <= Y <= Y_max  and Z_min <= Z <= Z_max

如果满足上述条件,则该点位于立方体内部,否则不位于立方体内.

If the aforementioned condition is satisfied then, the point lies inside the cube, otherwise it does not.

常规案例(定向多维数据集):

有两种方法可以解决此问题.首先,将点引入立方体的局部坐标系并应用上述特殊情况.第二种情况是关于向量投影的概念.第一种情况比第二种情况稍微复杂一点,因为您需要计算旋转矩阵,该矩阵将点从世界坐标系转换为多维数据集的局部坐标系.考虑下图所示的多维数据集.

There are two approaches to solve this. First one brings the point in the local coordinate system of the cube and apply the aforementioned Special Case. Second case works on the concept of the projection of the vector. First case is little more complex than the second as you need to calculate the rotation matrix which transforms the point from the world coordinate system into the Cube's local coordinate system. Consider the cube as show in the following figure.

对于这两种方法,我们都需要从多维数据集表示中得出一些基本信息.让我们将原点固定在立方体左下角的立方体的局部坐标系中.在这种情况下,它是点D.现在,在三个维度上计算单位方向向量,并在这些方向上计算立方体的范围.可以按照以下步骤进行.

For both the approaches we would need to derive some basic information from the cube representation. Let us fix origin in the local coordinate system of the cube at the bottom left corner of the cube; in this case, it is Point D. Now calculate the unit direction vectors in three dimensions and extent of the cube in those directions. It can be done as follows.

X local ,Y local 和Z local 在图中以蓝色,红色,绿色表示.和X length ,Y length 和Z length 是沿轴的范围.

The Xlocal, Ylocal, and Zlocal are illustrated in the figure with Blue, Red, Green colors. And Xlength, Ylength, and Zlength are the extents along the axes.

现在让我们重新解决问题.

Now lets get back to solving the problem.

方法1 :在Cube的局部坐标系中考虑这一点.为此,我们需要估算旋转矩阵.在这种情况下,旋转矩阵是3 x 3矩阵,其中X local ,Y local 和Z local 作为列.

Approach #1: Bring the point in consideration, in the local coordinate system of the Cube. To do that, we need to estimate the rotation matrix. Rotation matrix in this case is 3 x 3 matrix with Xlocal, Ylocal, and Zlocal as columns.

使用旋转矩阵R,可以将点带入局部坐标系,然后应用轴对齐立方体的特殊情况.

Using the rotation Matrix R, you can bring the point in the local coordinate system and then apply the special case of axis aligned cube.

方法2 :

构造从立方体中心到所考虑点的方向向量,并将其投影到每个局部轴上,并检查投影是否沿该轴超出了立方体的范围.如果投影位于沿每个轴的范围内,则点位于内部,否则它位于立方体的外部.

Construct the direction vector from the cube center to the point in consideration and project it onto each local axis and check if the projection spans beyond the extent of the cube along that axis. If the projection lies inside the extent along each axis, then point is inside, otherwise it is outside of the cube.

立方体的中心是I,如图所示.从立方体中心到点P的方向向量为V.向量V在X local ,Y local 和Z local 的计算公式如下.

The center of the cube is I, as show in the figure. The direction vector from the center of the cube to the point P is V. The projection of the vector V on Xlocal, Ylocal, and Zlocal can be calculated as follows.

现在,仅当满足以下所有条件时,点P才在立方体内.

Now the point P is inside the cube only if all of the following conditions are satisfied.

这是方法2中的python快速实现.

Here's the quick implementation in python for the approach #2.

import numpy as np

def inside_test(points , cube3d):
    """
    cube3d  =  numpy array of the shape (8,3) with coordinates in the clockwise order. first the bottom plane is considered then the top one.
    points = array of points with shape (N, 3).

    Returns the indices of the points array which are outside the cube3d
    """
    b1,b2,b3,b4,t1,t2,t3,t4 = cube3d

    dir1 = (t1-b1)
    size1 = np.linalg.norm(dir1)
    dir1 = dir1 / size1

    dir2 = (b2-b1)
    size2 = np.linalg.norm(dir2)
    dir2 = dir2 / size2

    dir3 = (b4-b1)
    size3 = np.linalg.norm(dir3)
    dir3 = dir3 / size3

    cube3d_center = (b1 + t3)/2.0

    dir_vec = points - cube3d_center

    res1 = np.where( (np.absolute(np.dot(dir_vec, dir1)) * 2) > size1 )[0]
    res2 = np.where( (np.absolute(np.dot(dir_vec, dir2)) * 2) > size2 )[0]
    res3 = np.where( (np.absolute(np.dot(dir_vec, dir3)) * 2) > size3 )[0]

    return list( set().union(res1, res2, res3) )

这篇关于如何确定一个点在立方体的内部还是外部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆