AABB和OBB包围盒,它们是计算机图形学和碰撞检测中常用的两种边界表示方法。
AABB (Axis-Aligned Bounding Box)
定义:AABB是边平行于坐标轴的矩形(2D)或长方体(3D)。
特点:由于与坐标轴对齐,计算简单,但包围物体不够紧密(尤其物体旋转后)。
OBB (Oriented Bounding Box)
定义:OBB是任意方向的矩形(2D)或长方体(3D),方向由包围盒自身的局部坐标系决定。
特点:可以更紧密地包围物体,但计算相对复杂。
bool AABB_Intersect(AABB a, AABB b) { return (a.max.x >= b.min.x && a.min.x <= b.max.x) && (a.max.y >= b.min.y && a.min.y <= b.max.y) && (a.max.z >= b.min.z && a.min.z <= b.max.z); }
原图如下,还没有添加包围盒
上图中绿框即为AABB包围,红框为OBB包围框。实现上述效果的代码如下:
Acad::ErrorStatus es; std::vector<AcDbObjectId> entIds = YJArxUtil::SelectUtil::SelectEnt(NULL,_T(" 请选择曲线"),_T(" 请选择曲线")); for(size_t i=0;i<entIds.size();++i) { YJArxUtil::YJCurvePtr pCurve(entIds.at(i)); if(!pCurve.IsOpenCorrectly())continue; AcGeCurve3d * pGeCurve = NULL; es = pCurve->getAcGeCurve(pGeCurve); if(Acad::eOk != es) { acutPrintf(_T(" getAcGeCurve失败!%s"),acadErrorStatusText(es));continue; } { AcGeBoundBlock3d obb = pGeCurve->boundBlock(); AcGePoint3d base; AcGeVector3d dir1, dir2, dir3; obb.get(base, dir1, dir2, dir3); std::vector<AcGePoint3d> ptArray; ptArray.push_back(base); ptArray.push_back(base + dir1); ptArray.push_back(base + dir2 + dir1); ptArray.push_back(base + dir2); AcDbPolyline* pPoly = YJArxUtil::PolyUtil::CreatNewPoly(ptArray); pPoly->setClosed(true);pPoly->setColorIndex(1); YJArxUtil::DbUtil::PostToModelSpace(pPoly); } { AcGeBoundBlock3d obb = pGeCurve->orthoBoundBlock(); AcGePoint3d base; AcGeVector3d dir1, dir2, dir3; obb.get(base, dir1, dir2, dir3); std::vector<AcGePoint3d> ptArray; ptArray.push_back(base); ptArray.push_back(base + dir1); ptArray.push_back(base + dir2 + dir1); ptArray.push_back(base + dir2); AcDbPolyline* pPoly = YJArxUtil::PolyUtil::CreatNewPoly(ptArray); pPoly->setClosed(true);pPoly->setColorIndex(3); YJArxUtil::DbUtil::PostToModelSpace(pPoly); } }
下一篇:没有了!