考虑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体积(如果立方体位于圆锥体内部,或者如果圆锥体位于立方体内部,则它们相交)。
https://stackoverflow.com/questions/22023977
复制相似问题