## 检测一个立方体和一个圆锥体是否相交？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (82)

• 一个与轴线对齐并由其中心位置和范围（边缘长度）定义的立方体，
• 一个不与轴线对齐并由其顶点位置定义的圆锥体，其基底中心的位置以及顶点处的半角

``````// 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));}
{return std::tan(_angle)*height();}
double circle()
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
}
``````

# 代码

## 一些定义

``````/*
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];
};
``````

## 线 - 锥交点

``````/*
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
``````

``````x = u + at
y = v + bt
z = w + ct
``````

``````(u + at)² + (v + bt)² = (H - w - ct)² * tan²(alpha)
``````

``````At² + Bt + C = 0
``````

1. 想象2无限线
• 圆锥轴
• 线穿过`P`垂直于锥轴的点（起始者的立方体中心）。

圆锥轴是你所知道的，所以很容易，第二行被定义为 `P+t*(perpendicular vector to cone axis) ` 这个矢量可以通过锥轴矢量和垂直于你的图像的矢量（假设Z轴）的叉积来获得。这`t`是标量值参数...

2. 计算这两条线/轴的交点 如果你不知道这些公式派生它们或谷歌他们。让交点为`Q`
3. 如果交点`Q`不在锥内 （在顶点和底部之间），那么点`P`不是相交锥体。根据交点方程，您将获得参数`t1``t2`
• `t1``P`轴线
• `t2`锥轴线

如果你的轴线方向矢量也是圆锥体长度，那么交点在圆锥体内 `t2 = <0,1>`

4. 如果`P`不在三角形内（由这两个轴产生的切割锥到平面） 这也很容易，你知道的位置`Q`内锥（`t2`），所以你知道，锥体在`P`轴从`Q`到距离`R*t2`这里`R`是圆锥体底座半径。所以你可以计算`|P-Q|`并检查它是否`<=R*t2`直接使用`t1`（如果`P`轴方向矢量是单位）。 如果距离较大，则`R*t2``P`不与锥体相交。
5. 如果＃3和＃4是正数则`P`与锥体相交

• 希望你不要介意这里是你的形象，为清晰起见添加了几件事情

• 其顶点作为原点
• 其轴作为`+Z`
• `XY`平面平行于它的基

• `Z = <0,h>`
• `X*X + Y*Y <= (R*Z/h)^2` 要么 `X*X + Y*Y <= (R*Z*tan(angle))^2`