opencascade Bnd_Range源码学习区间表示

opencascade Bnd_Range

前言

这个类描述了由两个实数值限定的 1D 空间中的区间。

一个区间可以是无效的,这表示区间中不包含任何点。

方法

1

//! 默认构造函数。创建一个无效区间。

Bnd_Range() : myFirst(0.0), myLast(-1.0) {}

2

//! 构造函数。不会创建无效区间。

Bnd_Range(const Standard_Real theMin, const Standard_Real theMax) :

myFirst(theMin), myLast(theMax)

{

if(myLast < myFirst)

throw Standard_ConstructionError("Last < First");

}

3

//! 将 替换为 和 theOther 的交集。

Standard_EXPORT void Common(const Bnd_Range& theOther);

4

//! 将 *this 和 theOther 合并为一个区间。

//! 用结果替换 *this。

//! 如果操作无法完成(例如,输入参数为空或分离),则返回 false。

//! @sa 使用方法 ::Add() 以无条件合并两个区间。

Standard_EXPORT Standard_Boolean Union(const Bnd_Range& theOther);

5

//! 通过 theVal 值将 分割为多个子区间

//!(例如,区间 3, 15 将通过 theVal == 5 被分割为两个区间:3, 5 和 5, 15)。

//! 新的区间将被推入 theList(在调用此方法之前,theList 必须正确初始化)。

//! 如果 thePeriod != 0.0,则至少有一个新的区间边界(如果 <this> 交叉 theVal+k thePeriod)

//! 将等于 theVal+thePeriod*k,其中 k 是一个整数(k = 0, +/-1, +/-2, ...)。

//! (假设上面的 thePeriod 为 4 ==> 我们将获得四个区间:3, 5、5, 9、9, 13 和 13, 15)。

Standard_EXPORT void Split(const Standard_Real theVal,

NCollection_List<Bnd_Range>& theList,

const Standard_Real thePeriod = 0.0) const;

6

//! 检查 是否与如下形式的值相交:

//! theVal+kthePeriod,其中 k 是一个整数(k = 0, +/-1, +/-2, ...)。
//! 返回:
//! 0 - 如果 不与 theVal+k
thePeriod 相交。

//! 1 - 如果 与 theVal+kthePeriod 相交。
//! 2 - 如果 myFirst 或/和 myLast 等于 theVal+k
thePeriod。

//!

//! 注意!!!

//! 如果 (myFirst == myLast),则此函数仅返回 0 或 2。

Standard_EXPORT Standard_Integer

IsIntersected(const Standard_Real theVal,

const Standard_Real thePeriod = 0.0)

7

//! 扩展 以包含 theParameter。

void Add(const Standard_Real theParameter)

8

//! 扩展此区间以包含两个区间。

//! @sa 使用方法 ::Union() 检查两个区间是否重叠。

void Add (const Bnd_Range& theRange)

9

//! 获取 的最小边界。

//! 如果 是无效的,方法返回 false。

Standard_Boolean GetMin(Standard_Real& thePar)

10

//! 获取 的最大边界。

//! 如果 是无效的,方法返回 false。

Standard_Boolean GetMax(Standard_Real& thePar)

11

//! 获取 的最小和最大边界。

//! 如果 是无效的,方法返回 false。

Standard_Boolean GetBounds(Standard_Real& theFirstPar,

Standard_Real& theLastPar)

12

//! 获取满足方程的 theParameter 值

//! (theParameter-MIN)/(MAX-MIN) == theLambda。

//! * theLambda == 0 --> 返回最小边界;

//! * theLambda == 0.5 --> 返回中点;

//! * theLambda == 1 --> 返回最大边界;

//! * theLambda < 0 --> 返回小于最小值的值;

//! * theLambda > 1 --> 返回大于最大值的值。

//! 如果 是无效的,方法返回 false。

Standard_Boolean GetIntermediatePoint(const Standard_Real theLambda,

Standard_Real& theParameter)

13

//! 返回区间值 (MAX-MIN)。对于无效区间,返回负值。

Standard_Real Delta() const

14

//! 判断 是否已初始化。

Standard_Boolean IsVoid() const

15

//! 使用默认参数初始化 。将 设置为无效。

void SetVoid()

16

//! 扩展区间以包含给定值(向两边扩展)。

void Enlarge(const Standard_Real theDelta)

17

//! 返回偏移 theVal 后的 <*this> 的副本。

Bnd_Range Shifted(const Standard_Real theVal)

18

//! 偏移 <*this> by theVal。

void Shift(const Standard_Real theVal)

19

//! 通过给定的下限修剪区间的最小值。

//! 如果给定的下限大于区间最大值,则标记区间为无效。

void TrimFrom (const Standard_Real theValLower)

20

//! 通过给定的上限修剪区间的最大值。

//! 如果给定的上限小于区间最大值,则标记区间为无效。

void TrimTo (const Standard_Real theValUpper)

21

//! 如果值超出此区间,则返回 True。

Standard_Boolean IsOut (Standard_Real theValue)

22

//! 如果给定的区间超出此区间,则返回 True。

Standard_Boolean IsOut (const Bnd_Range& theRange)

23

//! 如果 theOther 等于 <*this> 则返回 TRUE。

Standard_Boolean operator==(const Bnd_Range& theOther)

24

//! 将内容输出到流中。

Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1)

使用例子

Bnd_Range 类表示一维空间中的一个区间,用于定义范围或区间,并提供了多种操作方法。下面是一个使用 Bnd_Range 类的简单例子,演示如何创建区间、合并区间、检查相交、分割区间等操作。

cpp 复制代码
#include <iostream>
#include <Bnd_Range.hxx>
#include <NCollection_List.hxx>

int main()
{
    // 创建一个区间 [2.0, 5.0]
    Bnd_Range range1(2.0, 5.0);
    std::cout << "Range1: [" << range1.GetMin() << ", " << range1.GetMax() << "]" << std::endl;

    // 创建另一个区间 [3.0, 7.0]
    Bnd_Range range2(3.0, 7.0);
    std::cout << "Range2: [" << range2.GetMin() << ", " << range2.GetMax() << "]" << std::endl;

    // 合并两个区间
    if (range1.Union(range2)) {
        std::cout << "Union of Range1 and Range2: [" << range1.GetMin() << ", " << range1.GetMax() << "]" << std::endl;
    } else {
        std::cout << "Union operation failed." << std::endl;
    }

    // 检查区间是否相交
    Standard_Integer isIntersected = range1.IsIntersected(4.0);
    std::cout << "Range1 intersects with 4.0: " << (isIntersected ? "Yes" : "No") << std::endl;

    // 分割区间
    NCollection_List<Bnd_Range> splitRanges;
    range1.Split(4.0, splitRanges);
    std::cout << "Split Range1 at 4.0:" << std::endl;
    for (auto it = splitRanges.cbegin(); it != splitRanges.cend(); ++it) {
        std::cout << "  [" << it->GetMin() << ", " << it->GetMax() << "]" << std::endl;
    }

    // 扩展区间
    range1.Enlarge(1.0);
    std::cout << "Enlarged Range1: [" << range1.GetMin() << ", " << range1.GetMax() << "]" << std::endl;

    // 移动区间
    range1.Shift(2.0);
    std::cout << "Shifted Range1 by 2.0: [" << range1.GetMin() << ", " << range1.GetMax() << "]" << std::endl;

    return 0;
}

示例说明:

  1. 创建区间 :我们创建了两个 Bnd_Range 对象 range1 和 range2,分别表示区间 [2.0, 5.0] 和 [3.0, 7.0]。

  2. 合并区间 :使用 Union 方法合并 range1 和 range2。如果两个区间相交,它们将合并为一个新的区间。

  3. 检查相交 :使用 IsIntersected 方法检查 range1 是否与 4.0 相交。

  4. 分割区间 :使用 Split 方法在 4.0 处将 range1 分割为多个子区间,并将结果存储在 splitRanges 列表中。

  5. 扩展区间 :使用 Enlarge 方法将 range1 向两边扩展 1.0。

  6. 移动区间 :使用 Shift 方法将 range1 向右移动 2.0。

输出示例:

复制代码
Range1: [2.0, 5.0]
Range2: [3.0, 7.0]
Union of Range1 and Range2: [2.0, 7.0]
Range1 intersects with 4.0: Yes
Split Range1 at 4.0:
  [2.0, 4.0]
  [4.0, 7.0]
Enlarged Range1: [1.0, 8.0]
Shifted Range1 by 2.0: [3.0, 10.0]

这个例子展示了 Bnd_Range 的一些基本操作,涵盖了创建、合并、相交检查、分割、扩展和移动区间等常见使用场景。

参考

相关推荐
具身AGI2 小时前
物理AI 人类学习路线:三大判断怎么兑现
大数据·人工智能·学习
我命由我123455 小时前
Excel - Excel 固定列,滚动时该列始终可见
运维·学习·职场和发展·excel·职场发展·运维开发·学习方法
minglie16 小时前
劳斯–赫尔维茨定理的证明
学习
Nᴏsᴛᴀʟɢɪᴀ念8 小时前
Redis学习笔记|第一章:初识Redis与分布式架构
redis·笔记·学习
zhangrelay8 小时前
Arduino-MEGA-ESP单片机理论课笔记-12数字-13模拟-14智能工具-2026
笔记·单片机·嵌入式硬件·学习·机器人
ss2739 小时前
我用若依微服务写了套在线教育平台:8 个服务、313 个前端文件;若依微服务实战:课程、订单、学习进度全链路的在线教育后台- 学知在线教育云
前端·学习·微服务
技术小齐11 小时前
网络运维学习笔记 029 OSPF路由汇总与认证配置
运维·网络·笔记·学习·智能路由器
kyrie_sakura11 小时前
大模型学习33 -- 大模型部署1 -- vLLM 运行架构
学习·架构·vllm
m4Rk_11 小时前
【论文阅读】Agent 记忆机制(151):DUNE——让源事实更新沿依赖图传播到派生记忆
论文阅读·人工智能·学习·开源·github
qq_2813174712 小时前
Kubernetes面试题学习笔记
笔记·学习·kubernetes