考虑3D中的两个几何对象:
边长度与轴对齐的立方体,由其中心的位置及其范围(边长度)定义;边不与轴对齐的圆锥体,由其顶点的位置、底面中心的位置以及顶点处的半角定义
下面是在C++中定义这些对象的一小段代码:
// Preprocessor
#include <iostream>
#include <cmath>
#include <array>
// 3D cube from the position of its center and the side extent
class cube
{
public:
cube(const std::array<double, 3>& pos, const double ext)
: _position(pos), _extent(ext)
{;}
double center(const unsigned int idim)
{return _position[idim];}
double min(const unsigned int idim)
{return _position[idim]-_extent/2;}
double max(const unsigned int idim)
{return _position[idim]+_extent/2;}
double extent()
{return _extent;}
double volume()
{return std::pow(_extent, 3);}
protected:
std::array<double, 3> _position;
double _extent;
};
// 3d cone from the position of its vertex, the base center, and the angle
class cone
{
public:
cone(const std::array<double, 3>& vert,
const std::array<double, 3>& bas,
const double ang)
: _vertex(vert), _base(bas), _angle(ang)
{;}
double vertex(const unsigned int idim)
{return _vertex[idim];}
double base(const unsigned int idim)
{return _base[idim];}
double angle()
{return _angle;}
double height()
{return std::sqrt(std::pow(_vertex[0]-_base[0], 2)+std::pow(
_vertex[1]-_base[1], 2)+std::pow(_vertex[2]-_base[2], 2));}
double radius()
{return std::tan(_angle)*height();}
double circle()
{return 4*std::atan(1)*std::pow(radius(), 2);}
double volume()
{return circle()*height()/3;}
protected:
std::array<double, 3> _vertex;
std::array<double, 3> _base;
double _angle;
};
我想写一个函数来检测立方体和圆锥体的交点是否为空:
// Detect whether the intersection between a 3d cube and a 3d cone is not null
bool intersection(const cube& x, const cone& y)
{
// Function that returns false if the intersection of x and y is empty
// and true otherwise
}
这是一个问题的插图(插图是2D的,但我的问题是3D的):
如何有效地做到这一点(我正在寻找一种算法,所以答案可以是C、C++或Python)?
注意:这里的交点定义为:它存在一个位于立方体和圆锥体中的非空3D体积(如果立方体位于圆锥体内部,或者如果圆锥体位于立方体内部,则它们相交)。
发布于 2014-03-07 02:32:06
以获取代码
这个答案将比你的问题稍微普遍一些(例如,我考虑一个长方体而不是一个立方体)。适应你的情况应该是非常简单的。
一些定义
/*
Here is the cone in cone space:
+ ^
/|\ |
/*| \ | H
/ | \ |
/ \ |
+---------+ v
* = alpha (angle from edge to axis)
*/
struct Cone // In cone space (important)
{
double H;
double alpha;
};
/*
A 3d plane
v
^----------+
| |
| |
+----------> u
P
*/
struct Plane
{
double u;
double v;
Vector3D P;
};
// Now, a box.
// It is assumed that the values are coherent (that's only for this answer).
// On each plane, the coordinates are between 0 and 1 to be inside the face.
struct Box
{
Plane faces[6];
};
直线-圆锥体相交
现在,让我们计算线段和圆锥体之间的交点。请注意,我将在锥体空间中进行计算。另请注意,我将Z轴设为垂直轴。将其更改为Y 1留给读者作为练习。假设直线在锥体空间中。线段方向未归一化;相反,线段的长度为方向矢量的长度,并从点P
开始
/*
The segment is points M where PM = P + t * dir, and 0 <= t <= 1
For the cone, we have 0 <= Z <= cone.H
*/
bool intersect(Cone cone, Vector3D dir, Vector3D P)
{
// Beware, indigest formulaes !
double sqTA = tan(cone.alpha) * tan(cone.alpha);
double A = dir.X * dir.X + dir.Y * dir.Y - dir.Z * dir.Z * sqTA;
double B = 2 * P.X * dir.X +2 * P.Y * dir.Y - 2 * (cone.H - P.Z) * dir.Z * sqTA;
double C = P.X * P.X + P.Y * P.Y - (cone.H - P.Z) * (cone.H - P.Z) * sqTA;
// Now, we solve the polynom At² + Bt + C = 0
double delta = B * B - 4 * A * C;
if(delta < 0)
return false; // No intersection between the cone and the line
else if(A != 0)
{
// Check the two solutions (there might be only one, but that does not change a lot of things)
double t1 = (-B + sqrt(delta)) / (2 * A);
double z1 = P.Z + t1 * dir.Z;
bool t1_intersect = (t1 >= 0 && t1 <= 1 && z1 >= 0 && z1 <= cone.H);
double t2 = (-B - sqrt(delta)) / (2 * A);
double z2 = P.Z + t2 * dir.Z;
bool t2_intersect = (t2 >= 0 && t2 <= 1 && z2 >= 0 && z2 <= cone.H);
return t1_intersect || t2_intersect;
}
else if(B != 0)
{
double t = -C / B;
double z = P.Z + t * dir.Z;
return t >= 0 && t <= 1 && z >= 0 && z <= cone.H;
}
else return C == 0;
}
矩形-圆锥体相交
现在,我们可以检查平面的矩形部分是否与圆锥体相交(这将用于检查立方体的一个面是否与圆锥体相交)。仍然在锥体空间中。该计划以一种对我们有帮助的方式通过:两个向量和一个点。向量不归一化,以简化计算。
/*
A point M in the plan 'rect' is defined by:
M = rect.P + a * rect.u + b * rect.v, where (a, b) are in [0;1]²
*/
bool intersect(Cone cone, Plane rect)
{
bool intersection = intersect(cone, rect.u, rect.P)
|| intersect(cone, rect.u, rect.P + rect.v)
|| intersect(cone, rect.v, rect.P)
|| intersect(cone, rect.v, rect.P + rect.u);
if(!intersection)
{
// It is possible that either the part of the plan lie
// entirely in the cone, or the inverse. We need to check.
Vector3D center = P + (u + v) / 2;
// Is the face inside the cone (<=> center is inside the cone) ?
if(center.Z >= 0 && center.Z <= cone.H)
{
double r = (H - center.Z) * tan(cone.alpha);
if(center.X * center.X + center.Y * center.Y <= r)
intersection = true;
}
// Is the cone inside the face (this one is more tricky) ?
// It can be resolved by finding whether the axis of the cone crosses the face.
// First, find the plane coefficient (descartes equation)
Vector3D n = rect.u.crossProduct(rect.v);
double d = -(rect.P.X * n.X + rect.P.Y * n.Y + rect.P.Z * n.Z);
// Now, being in the face (ie, coordinates in (u, v) are between 0 and 1)
// can be verified through scalar product
if(n.Z != 0)
{
Vector3D M(0, 0, -d/n.Z);
Vector3D MP = M - rect.P;
if(MP.scalar(rect.u) >= 0
|| MP.scalar(rect.u) <= 1
|| MP.scalar(rect.v) >= 0
|| MP.scalar(rect.v) <= 1)
intersection = true;
}
}
return intersection;
}
长方体-锥体相交
现在,最后一部分:整个立方体:
bool intersect(Cone cone, Box box)
{
return intersect(cone, box.faces[0])
|| intersect(cone, box.faces[1])
|| intersect(cone, box.faces[2])
|| intersect(cone, box.faces[3])
|| intersect(cone, box.faces[4])
|| intersect(cone, box.faces[5]);
}
数学课
仍然在锥体空间中,锥体方程是:
// 0 is the base, the vertex is at z = H
x² + y² = (H - z)² * tan²(alpha)
0 <= z <= H
现在,3D中线的参数方程为:
x = u + at
y = v + bt
z = w + ct
方向向量为(a,b,c),点(u,v,w)位于直线上。
现在,让我们把这些方程放在一起:
(u + at)² + (v + bt)² = (H - w - ct)² * tan²(alpha)
然后,在开发和重新分解这个方程之后,我们得到以下结果:
At² + Bt + C = 0
其中A、B和C显示在第一个交集函数中。简单地解决这个问题并检查z和t上的边界条件。
发布于 2014-02-27 18:53:18
通过垂直于圆锥轴的点(起始点为立方体中心)的圆锥体的2无限直线(
P
)。圆锥轴是已知的,所以这很容易,第二条线定义为
P+t*(perpendicular vector to cone axis)
此向量可以通过圆锥轴向量和垂直于图像的向量(假设Z轴)的叉积获得。t
是标量值参数...
这两个lines/axises的
如果你不知道这些方程式,你可以派生它们,或者在谷歌上搜索它们。设交点为Q
Q
不在cone内部
(在顶点和底面之间),则点P
不与圆锥体相交。从相交方程中,您将获得参数t1
和t2
t1
设置为P
轴线t2
设置为圆锥轴线如果轴线方向矢量也是圆锥体长度,则交点在圆锥体内部,如果为t2 = <0,1>
P
平面不在三角形内(由这两个axises)生成的切割圆锥体到平面的
这也很容易,你知道圆锥体内部的位置(t2
),所以你知道这个圆锥体在,P
,-axis,-axis,Q
,of,R*t2
,其中,R
是圆锥体的底半径。因此,您可以计算|P-Q|
并检查它是否为<=R*t2
,或者直接使用t1
(如果P
轴方向向量为单位)。
如果距离较大,则R*t2
点P
不与圆锥体相交。
与 P
相交
添加了一些东西
notes
现在最难的部分是,当立方体的顶点不与锥体相交,但立方体本身也与锥体相交时,会出现边缘情况。当||P-Q|-R*t2| = <0,half cube size>
在这种情况下,您应该检查更多的点,而不仅仅是最近的立方体面上的立方体顶点时,可能会发生这种情况。
另一种方法是:
的变换矩阵
其中:
XY
平面与其底面平行<>F2105
所以任何点都在圆锥体内,如果
将
Z = <0,h>
X*X + Y*Y <= (R*Z/h)^2
或X*X + Y*Y <= (R*Z*tan(angle))^2
检查是否有任何顶点在锥体内,你也可以检查所有符合#1 (代数)条件的立方体边线,或者像以前的方法一样沿着立方体表面使用更多的点。
聊天讨论:https://chat.stackoverflow.com/rooms/48756/discussion-between-spektre-and-joojaa
发布于 2014-03-06 05:38:35
信息:我不知道这个想法是否已经是(你所在地区)的专利知识产权,也不知道如何找出,或者其他什么意思。我这样做是为了好玩。:)
但是,这就是问题所在:
优化:通过考虑立方体的内部球体,确定任何给定的三维点是否在立方体内部的函数可能是可优化的。也就是说,任何给定的三维点都在立方体内,如果它距离立方体内部球体的中心足够近,那么它就在该球体内。有趣的事情开始了,当你开始用额外的球体填充空的立方体角落以进行更多的优化(这是完全可选的)。
但是,这种方法的好处是你可以自由地调整粒度,在计算时间和计算精度之间进行微调。
还可以创建在多次迭代中自动减小粒度的求解器。这意味着精度会随着时间的推移而增加(为了获得更好的分辨率)。
https://stackoverflow.com/questions/22023977
复制相似问题