opencascade Bnd_B3f源码学习 包围盒

opencascade Bnd_B3f
在这里插入图片描述

方法

1

//! 空构造函数。
Bnd_B3f();

2

//! 构造函数。
Bnd_B3f(const gp_XYZ& theCenter, const gp_XYZ& theHSize);

3

//! 如果盒子是空的(未初始化),则返回 True。
Standard_Boolean IsVoid() const;

4

//! 重置盒子数据。
void Clear();

5

//! 通过一个点更新盒子。
Standard_EXPORT void Add (const gp_XYZ& thePnt);

6

//! 通过一个点更新盒子。
void Add (const gp_Pnt& thePnt);

7

//! 通过另一个盒子更新当前盒子。
void Add (const Bnd_B3f& theBox);

8

//! 查询下角点:(中心 - 半对角线)。你必须确保盒子不是空的(参见 IsVoid()),否则方法会返回无效结果。
gp_XYZ CornerMin() const;

9

//! 查询上角点:(中心 + 半对角线)。你必须确保盒子不是空的(参见 IsVoid()),否则方法会返回无效结果。
gp_XYZ CornerMax() const;

10

//! 查询平方对角线。如果盒子是空的(参见 IsVoid()),则返回一个非常大的实数值。
Standard_Real SquareExtent() const;

11

//! 通过绝对值 of theDiff 扩展盒子。
void Enlarge (const Standard_Real theDiff);

12

//! 通过另一个盒子的内部限制当前盒子。
//! 如果限制发生,则返回 True,否则返回 False,表示盒子不相交。
Standard_EXPORT Standard_Boolean Limit (const Bnd_B3f& theOtherBox);

13

//! 使用给定的变换变换边界盒。
//! 如果变换包含旋转,结果盒子会更大。
Standard_NODISCARD Standard_EXPORT Bnd_B3f Transformed (const gp_Trsf& theTrsf) const;

14

//! 检查给定点是否在盒子内。
//! 如果点在盒子外,则返回 True。
Standard_Boolean IsOut (const gp_XYZ& thePnt) const;

15

//! 检查一个球体是否与当前盒子相交。
//! 如果盒子完全在球体内,则返回 True,表示没有相交(否则该方法会报告相交)。
Standard_EXPORT Standard_Boolean IsOut (const gp_XYZ& theCenter, const Standard_Real theRadius, const Standard_Boolean isSphereHollow = Standard_False) const;

16

//! 检查给定盒子是否与当前盒子相交。
//! 如果盒子不相交,则返回 True。
Standard_Boolean IsOut (const Bnd_B3f& theOtherBox) const;

17

//! 检查通过给定变换定向的给定盒子是否与当前盒子相交。
//! 如果盒子不相交,则返回 True。
Standard_EXPORT Standard_Boolean IsOut (const Bnd_B3f& theOtherBox, const gp_Trsf& theTrsf) const;

18

//! 检查给定直线是否与当前盒子相交。
//! 如果没有相交,则返回 True。
//! isRay==True 表示检查与正半直线的相交
//! theOverthickness 是当前盒子尺寸的附加值(可能是负值)。如果是正值,它可以被视为直线 ‘theLine’ 的厚度或沿 ‘theLine’ 的圆柱的半径
IsOut

使用示例

以下是 Bnd_B3f 类的使用示例:

#include <gp_XYZ.hxx>
#include <Bnd_B3f.hxx>
#include <gp_Pnt.hxx>
#include <gp_Trsf.hxx>
#include <Standard_Real.hxx>
#include <Standard_Boolean.hxx>

int main() {
    // 创建一个空的 Bnd_B3f 对象
    Bnd_B3f boundingBox;

    // 定义盒子的中心和半对角线
    gp_XYZ center(0.0, 0.0, 0.0);
    gp_XYZ hSize(1.0, 1.0, 1.0);

    // 使用中心和半对角线创建一个 Bnd_B3f 对象
    Bnd_B3f boundingBoxWithParams(center, hSize);

    // 检查盒子是否为空(未初始化)
    if (boundingBoxWithParams.IsVoid()) {
        std::cout << "Box is void." << std::endl;
    } else {
        std::cout << "Box is initialized." << std::endl;
    }

    // 通过点更新盒子
    gp_XYZ point(2.0, 2.0, 2.0);
    boundingBoxWithParams.Add(point);

    // 通过点更新盒子(另一种方式)
    gp_Pnt pnt(3.0, 3.0, 3.0);
    boundingBoxWithParams.Add(pnt);

    // 通过另一个盒子更新当前盒子
    Bnd_B3f anotherBox(gp_XYZ(1.0, 1.0, 1.0), gp_XYZ(0.5, 0.5, 0.5));
    boundingBoxWithParams.Add(anotherBox);

    // 查询盒子的下角点
    gp_XYZ minCorner = boundingBoxWithParams.CornerMin();
    std::cout << "Min Corner: (" << minCorner.X() << ", " << minCorner.Y() << ", " << minCorner.Z() << ")" << std::endl;

    // 查询盒子的上角点
    gp_XYZ maxCorner = boundingBoxWithParams.CornerMax();
    std::cout << "Max Corner: (" << maxCorner.X() << ", " << maxCorner.Y() << ", " << maxCorner.Z() << ")" << std::endl;

    // 查询盒子的平方对角线
    Standard_Real squareExtent = boundingBoxWithParams.SquareExtent();
    std::cout << "Square Extent: " << squareExtent << std::endl;

    // 扩展盒子的大小
    boundingBoxWithParams.Enlarge(2.0);

    // 限制盒子为另一个盒子的内部
    Standard_Boolean isLimited = boundingBoxWithParams.Limit(anotherBox);
    if (isLimited) {
        std::cout << "Box is limited to the other box." << std::endl;
    } else {
        std::cout << "Boxes do not intersect." << std::endl;
    }

    // 变换盒子
    gp_Trsf transform;
    transform.SetRotation(gp_Ax1(gp_Pnt(0,0,0), gp_Dir(0,0,1)), M_PI/4);  // 旋转 45 度
    Bnd_B3f transformedBox = boundingBoxWithParams.Transformed(transform);

    // 检查点是否在盒子内
    gp_XYZ testPoint(1.0, 1.0, 1.0);
    if (boundingBoxWithParams.IsOut(testPoint)) {
        std::cout << "Point is outside the box." << std::endl;
    } else {
        std::cout << "Point is inside the box." << std::endl;
    }

    // 检查一个球体是否与盒子相交
    Standard_Boolean isOutSphere = boundingBoxWithParams.IsOut(gp_XYZ(0.0, 0.0, 0.0), 2.0);
    if (isOutSphere) {
        std::cout << "Sphere does not intersect with the box." << std::endl;
    } else {
        std::cout << "Sphere intersects with the box." << std::endl;
    }

    // 检查两个盒子是否相交
    Standard_Boolean isOutBox = boundingBoxWithParams.IsOut(anotherBox);
    if (isOutBox) {
        std::cout << "Boxes do not intersect." << std::endl;
    } else {
        std::cout << "Boxes intersect." << std::endl;
    }

    // 检查直线是否与盒子相交
    gp_Ax1 line(gp_Pnt(0,0,0), gp_Dir(1,0,0));
    Standard_Boolean isOutLine = boundingBoxWithParams.IsOut(line, Standard_False, 0.1);
    if (isOutLine) {
        std::cout << "Line does not intersect with the box." << std::endl;
    } else {
        std::cout << "Line intersects with the box." << std::endl;
    }

    // 检查平面是否与盒子相交
    gp_Ax3 plane(gp_Pnt(0,0,0), gp_Dir(0,0,1), gp_Dir(1,0,0));
    Standard_Boolean isOutPlane = boundingBoxWithParams.IsOut(plane);
    if (isOutPlane) {
        std::cout << "Plane does not intersect with the box." << std::endl;
    } else {
        std::cout << "Plane intersects with the box." << std::endl;
    }

    // 检查盒子是否完全在另一个盒子内
    Standard_Boolean isIn = boundingBoxWithParams.IsIn(anotherBox);
    if (isIn) {
        std::cout << "Box is fully inside the other box." << std::endl;
    } else {
        std::cout << "Box is not fully inside the other box." << std::endl;
    }

    // 检查变换后的盒子是否完全在另一个盒子内
    Standard_Boolean isInTransformed = boundingBoxWithParams.IsIn(anotherBox, transform);
    if (isInTransformed) {
        std::cout << "Box is fully inside the transformed other box." << std::endl;
    } else {
        std::cout << "Box is not fully inside the transformed other box." << std::endl;
    }

    // 设置盒子的中心坐标
    boundingBoxWithParams.SetCenter(gp_XYZ(5.0, 5.0, 5.0));

    // 设置半对角线坐标
    boundingBoxWithParams.SetHSize(gp_XYZ(2.0, 2.0, 2.0));

    return 0;
}

代码解释

  1. 创建和初始化 Bnd_B3f 对象

    • 使用空构造函数创建一个空的 Bnd_B3f 对象。
    • 使用指定的中心和半对角线创建一个 Bnd_B3f 对象。
  2. 检查和更新盒子

    • 使用 IsVoid() 检查盒子是否为空。
    • 使用 Add() 方法通过点或另一个盒子更新当前盒子。
    • 使用 Clear() 方法重置盒子数据。
  3. 查询和扩展盒子

    • 使用 CornerMin()CornerMax() 查询盒子的下角点和上角点。
    • 使用 SquareExtent() 查询盒子的平方对角线。
    • 使用 Enlarge() 方法扩展盒子的大小。
  4. 限制和变换盒子

    • 使用 Limit() 方法限制当前盒子到另一个盒子的内部。
    • 使用 Transformed() 方法将盒子变换到新的位置和方向。
  5. 检测交集

    • 检查点、球体、另一个盒子、直线、平面是否与当前盒子相交。
    • 使用 IsOut() 方法进行这些检查。
  6. 检查包含关系

    • 使用 IsIn() 方法检查当前盒子是否完全在另一个盒子内。
    • 使用 IsIn() 方法检查当前盒子是否完全在变换后的另一个盒子内。
  7. 设置盒子属性

    • 使用 SetCenter() 设置盒子的中心坐标。
    • 使用 SetHSize() 设置盒子的半对角线坐标。

参考

  • 21
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值