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+kthePeriod)
//! 将等于 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 类的简单例子,演示如何创建区间、合并区间、检查相交、分割区间等操作。

#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 对象 range1range2,分别表示区间 [2.0, 5.0][3.0, 7.0]

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

  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 的一些基本操作,涵盖了创建、合并、相交检查、分割、扩展和移动区间等常见使用场景。

参考

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值