OpenCASCADE BRepTools

eryar@163.com

Abstract. OpenCASCADE BRepTools provides utilities for BRep data structure. OuterWire method to find the outer wire of a face. Dump method to dump a BRep object. It also can be used as the data exchange for OpenCASCADE native shapes.

Key Words. OpenCASCADE, BRepTools, BRep, Topology

1. Introduction

OpenCASCADE 提供了一个类BRepTools,其中有许多static函数,主要用来对BRep表示的拓朴形状的数据进行读写,也提供了查找一个面中外环(Outer Wire)的函数。因为OpenCASCADE中的边界表示法BRep的数据结构如下图1.1所示:

Figure 1.1 BRep Data Structure of OpenCASCADE

因为OpenCASCADE中拓朴结构采用了包含关系,当需要将TopoDS_Shape数据保存到文件时,如何保持TopoDS_Shape中的关系,以便于从文件读取这些数据时,可以重构出TopoDS_Shape中的各种关系?

参 考opennurbs中的BRep表示时数据的存储方式,可知直接在BRep中保存拓朴及几何数据的索引,这样对数据的存储及读取时重构拓朴结构还是很方 便的。而在OpenCASCADE中拓朴数据是以Handle来保存的,且为组合关系,即一个父结构中有一个列表 (TopoDS_ListOfShape)给包含了子结构数据。对于没有索引的OpenCASCADE的拓朴结构,如何进行读写操作呢?

本文结合类BRepTools中的函数,对OpenCASCADE中TopoDS_Shape数据的保存和读取功能的代码进行分析,从而对ModelingData中的BRep数据做进一步的理解。

2.Topology Shape Serialization

OpenCASCADE的类BRepTools中提供了如下函数,可以TopoDS_Shape中的数据进行导入导出:

v BRepTools::Dump();

v BRepTools::Read();

v BRepTools::Write();

这 几个函数比较常用,因为可以方便地将TopoDS_Shape导出,或导入到OpenCASCADE的Draw Test Harness中,来对程序一些算法进行验证。对于使用了组合关系的TopoDS_Shape如何确保数据的保存及读取后,能够维持这些关系?带着这个问 题去看BRep文件读写的功能,应该更为清晰。

还是看看代码,如下所示为输出TopoDS_Shape的函数,在程序Debug时比较常用:

//=======================================================================
//function : Dump
//purpose :
//=======================================================================
void BRepTools::Dump(const TopoDS_Shape& Sh, Standard_OStream& S)
{
BRepTools_ShapeSet SS;
SS.Add(Sh);
SS.Dump(Sh,S);
SS.Dump(S);
}

其中使用了类BRepTools_ShapeSet,这里的Set的意思我理解为集合的意思,其Add函数如下:

//=======================================================================
//function : Add
//purpose :
//=======================================================================
Standard_Integer TopTools_ShapeSet::Add(const TopoDS_Shape& S)
{
if (S.IsNull()) return ;
myLocations.Add(S.Location());
TopoDS_Shape S2 = S;
S2.Location(TopLoc_Location());
Standard_Integer index = myShapes.FindIndex(S2);
if (index == ) {
AddGeometry(S2); for (TopoDS_Iterator its(S2,Standard_False,Standard_False);
its.More(); its.Next())
Add(its.Value());
index = myShapes.Add(S2);
}
return index;
}

这是一个递归函数,通过AddGeometry函数,将TopoDS_Shape中的几何信息都保存到相应的集合Set中,Set中使用了Map,即给每个几何信息一个唯一的编号与之对应。

//=======================================================================
//function : AddGeometry
//purpose :
//======================================================================= void BRepTools_ShapeSet::AddGeometry(const TopoDS_Shape& S)
{
// Add the geometry if (S.ShapeType() == TopAbs_VERTEX) { Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points()); while (itrp.More()) {
const Handle(BRep_PointRepresentation)& PR = itrp.Value(); if (PR->IsPointOnCurve()) {
myCurves.Add(PR->Curve());
} else if (PR->IsPointOnCurveOnSurface()) {
myCurves2d.Add(PR->PCurve());
mySurfaces.Add(PR->Surface());
} else if (PR->IsPointOnSurface()) {
mySurfaces.Add(PR->Surface());
} ChangeLocations().Add(PR->Location());
itrp.Next();
} }
else if (S.ShapeType() == TopAbs_EDGE) { // Add the curve geometry
Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape());
BRep_ListIteratorOfListOfCurveRepresentation itrc(TE->Curves()); while (itrc.More()) {
const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
if (CR->IsCurve3D()) {
if (!CR->Curve3D().IsNull()) {
myCurves.Add(CR->Curve3D());
ChangeLocations().Add(CR->Location());
}
}
else if (CR->IsCurveOnSurface()) {
mySurfaces.Add(CR->Surface());
myCurves2d.Add(CR->PCurve());
ChangeLocations().Add(CR->Location());
if (CR->IsCurveOnClosedSurface())
myCurves2d.Add(CR->PCurve2());
}
else if (CR->IsRegularity()) {
mySurfaces.Add(CR->Surface());
ChangeLocations().Add(CR->Location());
mySurfaces.Add(CR->Surface2());
ChangeLocations().Add(CR->Location2());
}
else if (myWithTriangles) { // for XML Persistence
if (CR->IsPolygon3D()) {
if (!CR->Polygon3D().IsNull()) {
myPolygons3D.Add(CR->Polygon3D());
ChangeLocations().Add(CR->Location());
}
}
else if (CR->IsPolygonOnTriangulation()) {
myTriangulations.Add(CR->Triangulation());
myNodes.Add(CR->PolygonOnTriangulation());
ChangeLocations().Add(CR->Location());
if (CR->IsPolygonOnClosedTriangulation())
myNodes.Add(CR->PolygonOnTriangulation2());
}
else if (CR->IsPolygonOnSurface()) {
mySurfaces.Add(CR->Surface());
myPolygons2D.Add(CR->Polygon());
ChangeLocations().Add(CR->Location());
if (CR->IsPolygonOnClosedSurface())
myPolygons2D.Add(CR->Polygon2());
}
}
itrc.Next();
}
} else if (S.ShapeType() == TopAbs_FACE) { // Add the surface geometry
Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
if (!TF->Surface().IsNull()) mySurfaces.Add(TF->Surface()); if (myWithTriangles) { // for XML Persistence
Handle(Poly_Triangulation) Tr = TF->Triangulation();
if (!Tr.IsNull()) myTriangulations.Add(Tr);
} ChangeLocations().Add(TF->Location());
}
}

由上述代码可知,Edge中的几何信息较多,Face中的几何信息最少,只是几何曲面或其用于显示的网格数据。在将拓朴数据输出时,拓朴面、边及顶点中包含的几何信息都是前面几何数据的编号,即相当于索引号的形式输出,代码如下所示:

//=======================================================================
//function : WriteGeometry
//purpose :
//======================================================================= void BRepTools_ShapeSet::WriteGeometry(const TopoDS_Shape& S,
Standard_OStream& OS)const
{
// Write the geometry if (S.ShapeType() == TopAbs_VERTEX) { // Write the point geometry
TopoDS_Vertex V = TopoDS::Vertex(S);
OS << BRep_Tool::Tolerance(V) << "\n";
gp_Pnt p = BRep_Tool::Pnt(V);
OS<<p.X()<<" "<<p.Y()<<" "<<p.Z()<<"\n"; Handle(BRep_TVertex) TV = Handle(BRep_TVertex)::DownCast(S.TShape());
BRep_ListIteratorOfListOfPointRepresentation itrp(TV->Points()); while (itrp.More()) {
const Handle(BRep_PointRepresentation)& PR = itrp.Value(); OS << PR->Parameter();
if (PR->IsPointOnCurve()) {
OS << " 1 " << myCurves.Index(PR->Curve());
} else if (PR->IsPointOnCurveOnSurface()) {
OS << " 2 " << myCurves2d.Index(PR->PCurve());
OS << " " << mySurfaces.Index(PR->Surface());
} else if (PR->IsPointOnSurface()) {
OS << " 3 " << PR->Parameter2() << " ";
OS << mySurfaces.Index(PR->Surface());
} OS << " " << Locations().Index(PR->Location());
OS << "\n"; itrp.Next();
} OS << "0 0\n"; // end representations } else if (S.ShapeType() == TopAbs_EDGE) { // Write the curve geometry Handle(BRep_TEdge) TE = Handle(BRep_TEdge)::DownCast(S.TShape()); OS << " " << TE->Tolerance() << " ";
OS << ((TE->SameParameter()) ? : ) << " ";
OS << ((TE->SameRange()) ? : ) << " ";
OS << ((TE->Degenerated()) ? : ) << "\n"; Standard_Real first, last;
BRep_ListIteratorOfListOfCurveRepresentation itrc = TE->Curves();
while (itrc.More()) {
const Handle(BRep_CurveRepresentation)& CR = itrc.Value();
if (CR->IsCurve3D()) {
if (!CR->Curve3D().IsNull()) {
Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
GC->Range(first, last);
OS << "1 "; // -1- Curve 3D
OS << " "<<myCurves.Index(CR->Curve3D());
OS << " "<<Locations().Index(CR->Location());
OS << " "<<first<<" "<<last;
OS << "\n";
}
}
else if (CR->IsCurveOnSurface()) {
Handle(BRep_GCurve) GC = Handle(BRep_GCurve)::DownCast(itrc.Value());
GC->Range(first, last);
if (!CR->IsCurveOnClosedSurface())
OS << "2 "; // -2- Curve on surf
else
OS << "3 "; // -3- Curve on closed surf
OS <<" "<<myCurves2d.Index(CR->PCurve());
if (CR->IsCurveOnClosedSurface()) {
OS <<" " << myCurves2d.Index(CR->PCurve2());
PrintRegularity(CR->Continuity(),OS);
}
OS << " " << mySurfaces.Index(CR->Surface());
OS << " " << Locations().Index(CR->Location());
OS << " "<<first<<" "<<last;
OS << "\n"; // Write UV Points // for XML Persistence higher performance
if (FormatNb() == )
{
gp_Pnt2d Pf,Pl;
if (CR->IsCurveOnClosedSurface()) {
Handle(BRep_CurveOnClosedSurface) COCS =
Handle(BRep_CurveOnClosedSurface)::DownCast(CR);
COCS->UVPoints2(Pf,Pl);
}
else {
Handle(BRep_CurveOnSurface) COS =
Handle(BRep_CurveOnSurface)::DownCast(CR);
COS->UVPoints(Pf,Pl);
}
OS << Pf.X() << " " << Pf.Y() << " " << Pl.X() << " " << Pl.Y() << "\n";
}
}
else if (CR->IsRegularity()) {
OS << "4 "; // -4- Regularity
PrintRegularity(CR->Continuity(),OS);
OS << " "<<mySurfaces.Index(CR->Surface());
OS << " "<<Locations().Index(CR->Location());
OS << " "<<mySurfaces.Index(CR->Surface2());
OS << " "<<Locations().Index(CR->Location2());
OS << "\n";
} else if (myWithTriangles) { // for XML Persistence
if (CR->IsPolygon3D()) {
Handle(BRep_Polygon3D) GC = Handle(BRep_Polygon3D)::DownCast(itrc.Value());
if (!GC->Polygon3D().IsNull()) {
OS << "5 "; // -5- Polygon3D
OS << " "<<myPolygons3D.FindIndex(CR->Polygon3D());
OS << " "<<Locations().Index(CR->Location());
OS << "\n";
}
}
else if (CR->IsPolygonOnTriangulation()) {
Handle(BRep_PolygonOnTriangulation) PT =
Handle(BRep_PolygonOnTriangulation)::DownCast(itrc.Value());
if (!CR->IsPolygonOnClosedTriangulation())
OS << "6 "; // -6- Polygon on triangulation
else
OS << "7 "; // -7- Polygon on closed triangulation
OS << " " << myNodes.FindIndex(PT->PolygonOnTriangulation());
if (CR->IsPolygonOnClosedTriangulation()) {
OS << " " << myNodes.FindIndex(PT->PolygonOnTriangulation2());
}
OS << " " << myTriangulations.FindIndex(PT->Triangulation());
OS << " "<<Locations().Index(CR->Location());
OS << "\n";
}
} itrc.Next();
}
OS << "0\n"; // end of the list of representations
} else if (S.ShapeType() == TopAbs_FACE) { Handle(BRep_TFace) TF = Handle(BRep_TFace)::DownCast(S.TShape());
const TopoDS_Face& F = TopoDS::Face(S); if (!(TF->Surface()).IsNull()) {
OS << ((BRep_Tool::NaturalRestriction(F)) ? : );
OS << " ";
// Write the surface geometry
OS << " " <<TF->Tolerance();
OS << " " <<mySurfaces.Index(TF->Surface());
OS << " " <<Locations().Index(TF->Location());
OS << "\n";
}
else //For correct reading of null face
{
OS << ;
OS << " ";
OS << " " <<TF->Tolerance();
OS << " " << ;
OS << " " << ;
OS << "\n";
}
if (myWithTriangles) { // for XML Persistence
if (!(TF->Triangulation()).IsNull()) {
OS << ;
OS << " ";
// Write the triangulation
OS << " " <<myTriangulations.FindIndex(TF->Triangulation());
}
}
} }

通过先将几何数据收集到相应的集合(映射)中,再在拓朴结构对应的地方以索引号的方式输出,这样就便于从文件读取数据时,以类似的方式来重构BRep边界表示的拓朴Shape的结构。即读取文件重构拓朴结构数据是输出的逆过程。

在实现从文件读取BRep表示的数据时,先将几何信息读取到对应的集合中,再读取拓朴结构数据时,若拓朴结构中包含几何信息,则以索引的方式,找到对应的几何数据即可。详细实现可参考源程序。

3. For Debugging

由 于BRepTools为Toolkit TKBRep中的类,所以依赖的动态库较少,所以在编程时,若要验证一些算法的正确性时,经常需要将TopoDS_Shape的数据导出,甚至可以直接先 在Draw Test Harness中使用相关命令来将导出的数据导入来查看结果。

4. Conclusion

通 过BRepTools中对TopoDS_Shape数据的输出及导入的代码分析可知,对于只有组合关系的数据,若想维持这种关系,就需要引入集合映射的类 来产生索引,进而在读取数据时,可以根据索引来重构拓朴关系。由于opennurbs中的BRep在内存中本来就是索引的方式,所以在数据存取时,实现要 简单很多。

5. References

1. OpenCASCADE Team. BRep Format. 2014.12

2. Shing Liu. Topology and Geometry in OpenCascade-Topology.

http://www.cppblog.com/eryar/archive/2013/09/21/203338.html

3. Shing Liu. Topology and Geometry in OpenCascade-Vertex

http://www.cppblog.com/eryar/archive/2013/08/20/202678.html

4. Shing Liu. Topology and Geometry in OpenCascade-Edge

http://www.cppblog.com/eryar/archive/2013/08/24/202739.html

5. Shing Liu. Topology and Geometry in OpenCascade-Face

http://www.cppblog.com/eryar/archive/2013/09/12/203199.html

OpenCASCADE BRepTools的更多相关文章

  1. Make Helix Curve in OpenCASCADE

    Make Helix Curve in OpenCASCADE eryar@163.com Abstract. OpenCASCADE does not provide helix curve dir ...

  2. OpenCASCADE Hidden Line Removal

    OpenCASCADE Hidden Line Removal eryar@163.com Abstract. To provide the precision required in industr ...

  3. OpenCASCADE Make Primitives-Sphere

    OpenCASCADE Make Primitives-Sphere eryar@163.com Abstract. The sphere is the simplest topology shape ...

  4. OpenCASCADE Make Primitives-Box

    OpenCASCADE Make Primitives-Box eryar@163.com Abstract. By making a simple box to demonstrate the BR ...

  5. OpenNURBS to OpenCASCADE

    OpenNURBS to OpenCASCADE eryar@163.com Abstract. The OpenNURBS initiative provides CAD/CAM/CAE and c ...

  6. OpenCascade Primitives BRep - Box

    OpenCascade Primitives BRep - Box eryar@163.com Abstract. BRep is short for Boundary Representation. ...

  7. Topology Shapes of OpenCascade BRep

    Topology Shapes of OpenCascade BRep eryar@163.com 摘要Abstract:通过对OpenCascade中的BRep数据的读写,理解边界表示法的概念及实现 ...

  8. OpenCASCADE 基础

    OpenCASCADE 基础 转载▼ 一直在用OCC作项目,但这方面的中文资料很少,看来OCC在中国还不是十分普及: 后来,项目中使用OCC和DirectX结合使用,取得了很好的效果: 随着OCC6. ...

  9. FreeType in OpenCASCADE

    FreeType in OpenCASCADE eryar@163.com Abstract. FreeType is required for text display in the 3D view ...

随机推荐

  1. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  2. 弱省互测#2 t2

    题意 给两个树,大小分别为n和m,现在两棵树各选一些点(包括1),使得这棵树以1号点为根同构(同构就是每个点的孩子数目相同),求最大的同构树.(n, m<=500) 分析 我们从两棵树中各取出一 ...

  3. Android 学习笔记之一 “Unable to establish loopback connection”

    今天碰到一个错误:Unable to establish loopback connection,在网上找各种方法都解决不了,后来看一个帖子说是要关闭系统防火墙,尝试了下还是不行.最后是进任务管理器杀 ...

  4. C语言中函数声明实现的位置

    在学习C语言的时候我遇到了这么个事情,因为之前先学习的C#,在C#编译器中,函数的声明位置不会影响编译的结果,但是在C语言中却发生了错误 先看一段代码: #include <stdio.h> ...

  5. eclipse常见问题

    使用eclipse进入断点,当弹出"Confir Perspective Switch"视图时,选择"Yes".之后每次进入断点都会自动切换到debug视图. ...

  6. Javascript的自执行函数

    自执行函数其实也就是"立即执行的函数",它有四个特点:提高性能.利于压缩.避免冲突.依赖加载: 1.减少作用域查找 JS代码: // Anonymous function that ...

  7. redis 的理解

    1.Redis使用 C语言开发的.Redis 约定此版本号,为偶数的版本是稳定版(如:2.4版 2.6版),奇数版是非稳定版(如:2.5版 2.7版) 2.Redis 数据库中的所有的数据都存储在内存 ...

  8. 关于Android中res目录strings.xml文件中的转义字符之笔录

    res目录strings.xml文件中的转义字符:         ------------------>     代表着一个汉字的位置:                        ---- ...

  9. VS2010 中,windows服务不能添加 System.Web 引用

    今天在写windows服务的时候,右键添加引用->.NET中没有找到System.Web,在->Recent中找到System.Web,但添加完后却显示黄色叹号,最后百度一下解决了这个问题 ...

  10. sql编程(四)触发器

    触发器:一种特殊的存储过程,和制定的表相关,而对于一个表进行的数据操作时自动的触发执行类似winform 事件创建语法:create trigger 触发器的名称on 表名 for insert | ...