OpenCascade Primitives BRep - Box
eryar@163.com
Abstract. BRep is short for Boundary Representation. Boundary Representation gives a complete description of an object by associating topological and geometric information for solid modeling. In this case, objects are described by their boundaries. There are two types of information in BRep: Topological information and Geometric information. This paper is concerned with the Box BRep in OpenCascade, and also show how to use Tcl script to dump box BRep info.
Key words. OpenCascade, BRep, Boundary Representation, Box, Winged-Edge Structure
1. Introduction
邊界表示法(BRep: Boundary Representation)通過(guò)拓樸(Topology)和幾何(Geometry)給出了一個(gè)物體完整的描述,即用邊界來(lái)表示物體。
在OpenCascade中的拓樸實(shí)體有以下幾種:
l Vertex: a zero-dimensional shape corresponding to a point in geometry;
l Edge: a shape corresponding to a curve, and bound by a vertex at each extremity;
l Wire: a sequence of edges connected by their vertices;
l Face: part of a plane(in 2D geometry) or a surface(in 3D geometry) bounded by a closed wire;
l Shell: a collection of faces connected by some of the edges their wire boundaries;
l Solid: part of 3D space bound by a shell;
l Compound solid: a collection of solids.
Figure 1.1 Topological Entities in OpenCascade
OpenCascade中的拓樸實(shí)體如上圖所示,其中Compound可以包含很多Solid;Solid由Shell包圍而成;Shell由相連的Face組成;Wire由相連的Edge組成;Edge對(duì)應(yīng)一條曲線,且曲線的端點(diǎn)處由Vertex組成;Vertex對(duì)應(yīng)空間中一個(gè)點(diǎn)。OpenCascade的拓樸結(jié)構(gòu)的類層次關(guān)系如下圖所示:
Figure 1.2 Topological Shape Hierarchy of OpenCascade
OpenCascade的BRep表示中幾何曲線曲面是參數(shù)化表示的,即曲線上的點(diǎn)與一個(gè)參數(shù)u有關(guān),曲面上的點(diǎn)與兩個(gè)參數(shù)u,v來(lái)有關(guān)。因?yàn)槭菂?shù)化表示的,所以曲線曲面是有向的(naturally orientated)。方向(Orientation)是重要的,對(duì)面而言方向是面上每個(gè)點(diǎn)處的法向。
Figure 1.3 Orientation of Faces
使用參數(shù)表示的曲線曲面還需要注意曲線曲面的有界性Bounded,奇異性Singularity(曲面上的奇點(diǎn)Singular Point),曲面上的曲線(PCurve: Curve on Surface)等概念。
本文通過(guò)使用Tcl腳本將OpenCascade中的基本形狀長(zhǎng)方體Box的邊界表示BRep數(shù)據(jù)導(dǎo)出到文件,繼而方便分析Box在OpenCascade中的邊界表示。
2. Dump Box BRep Info by Tcl
為了得到長(zhǎng)方體的邊界表示數(shù)據(jù),先要用類BRepPrimAPI_MakeBox來(lái)生成一個(gè)TopoDS_Shape,再使用BRepTools::Dump()可以將這個(gè)TopoDS_Shape的信息以便于理解的方式輸出。如果用C++來(lái)編程,只有這兩句,但是為了編譯鏈接成功,需要包含相關(guān)的頭文件及引用相關(guān)的庫(kù),需要編譯工具來(lái)編譯鏈接,還是比較麻煩的。本文使用Tcl腳本來(lái)輸出,體驗(yàn)一下在OpenCascade中使用Tcl腳本編程的便利。
在Draw Test Harness中輸入以下三行Tcl命令,即可以將Box的BRep信息輸出到屏幕:
Figure 2.1 Dump Box BRep info in Draw Test Harness
考慮到輸出內(nèi)容過(guò)多,在命令窗口查看不便,有些信息被覆蓋,于是將這些信息導(dǎo)出到文件,Tcl代碼如下所示:
#
# Copyright (c) 2014 eryar All Rights Reserved.
#
# File : box.tcl
# Author : eryar@163.com
# Date : 2014-03-17 19:00
# Version : 1.0v
#
# Description : Dump OpenCascade primitive box topology info to file.
#
# Key Words : OpenCascade, BRep, Tcl, Box
#
#
# Load Modeling algorithms.
pload MODELING
# Make the box.
box theBox 1.0 2.0 3.0
set boxInfo [dump theBox]
# Save box BRep info to file.
if [catch {open d:/box.txt w+} theFile] {
puts "Cannot open d:/box.txt for writing: $theFile"
} else {
puts $theFile $boxInfo
flush $theFile
close $theFile
}
puts "Dump Info Finished!"
將上述內(nèi)容保存到文件box.tcl,并在Draw Test Harness中輸入如下命令:
Figure 2.2 Run a Tcl File in Draw Test Harness
執(zhí)行成功后會(huì)在D盤(pán)中生成一個(gè)box.txt的文件,文件部分內(nèi)容如下所示:
Figure 2.3 Box BRep Info Generated by Tcl Script
為了方便讀者測(cè)試及本文中分析的Box數(shù)據(jù)的準(zhǔn)確性,box.tcl和box.txt都可通過(guò)文章后面的鏈接下載。
3. Box BRep in OpenCascade
翼邊結(jié)構(gòu)(Winged-Edge Structure)及其邊的表格方式(Edge Table)可以清晰地表達(dá)出形狀的拓樸關(guān)系,表中內(nèi)容有:
l 邊的名稱,即邊的編號(hào)Edge Name;
l 邊的起止頂點(diǎn)Start vertex and end vertex;
l 相連的兩個(gè)面 Left face and right face;
l 遍歷左面時(shí)訪問(wèn)邊的順序The predecessor and successor edges when traversing its left face;
l 遍歷右面時(shí)訪問(wèn)邊的順序The predecessor and successor edges when traversing its right face;
如下圖所示為Edge Table表示的翼邊結(jié)構(gòu):
Figure 3.1 Edge Table of Winged-Edge Structure
從上圖可知,給出一個(gè)邊a后,從表中可以清晰看出與邊a相連的頂點(diǎn)及面的信息。
如下圖所示為一個(gè)Edge Table的示例:
Figure 3.2 Complete Edge Table for Pyramid
由上可知,給出翼邊結(jié)構(gòu)表示中的一個(gè)邊,可以很方便得到與之相連的頂點(diǎn)和面的信息。
但是OpenCascade中沒(méi)有使用翼邊結(jié)構(gòu)來(lái)表示形狀,可以從圖1.2所示的類結(jié)構(gòu)得出。因?yàn)槊總€(gè)形狀只包含子形狀的數(shù)據(jù),不包含其父形狀的數(shù)據(jù),所以在OpenCascade中不能反向訪問(wèn)其父形狀(In OpenCascade, there is no back pointer from sub-shapes to ancestor shapes.),所以若想得到與給定頂點(diǎn)或邊相連的面或環(huán)等信息,必須使用這個(gè)工具:
TopExp::MapShapesAndAncestors()
如下圖所示使用上述工具得到與指定邊相連的所有面的信息:
Figure 3.3 Get Ancestor Shapes in OpenCascade
根據(jù)前面使用Tcl腳本生成的Box,尺寸分別為1.0,2.0,3.0:
Figure 3.4 The Box generated by Tcl Script
為了正確顯示出Box,每個(gè)面的法向必須與上圖中的箭頭方向一致。下面通過(guò)box.txt中的拓樸信息的編號(hào),從vertex開(kāi)始來(lái)組裝成一個(gè)Solid的形狀。對(duì)應(yīng)頂點(diǎn)的編號(hào)及其在空間中的坐標(biāo)如下圖所示:
Figure 3.5 Vertex of the Box BRep
Figure 3.6 Edges of the Box BRep
上圖中根據(jù)邊中的頂點(diǎn)的方向,標(biāo)示出邊的方向:從標(biāo)號(hào)為+正的頂點(diǎn)到標(biāo)號(hào)為-負(fù)的頂點(diǎn)。
Figure 3.7 Wire #4 of Box BRep
由圖可知,由于Wire #4是由邊E30,E9,E20,E13組成,由于30和9號(hào)邊前有負(fù)號(hào),所以需要反向,方向反向的邊用紅色箭頭標(biāo)示。其它Wire的處理與此類似。通過(guò)上面左右兩幅圖的對(duì)比,可以清楚知道哪些Edge在組成Wire時(shí)反向了。
Figure 3.8 Faces and Shells of Box BRep
由上圖可知,每個(gè)Face由一個(gè)Wire組成。注意到由Face組成Shell時(shí),F(xiàn)ace前面有方向性。如Face 5#前面有負(fù)號(hào),意思是面的法向與Wire的方向相反,其他類似。
最后的Solid由Shell組成。這樣Box的拓樸數(shù)據(jù)就形成了。其中Face, Edge, Vertex中包含了幾何數(shù)據(jù)。Face的參數(shù)表示的幾何數(shù)據(jù)位于surfaces部分,如下圖所示:
Figure 3.9 Parametric Surfaces of the Box
Edge中包含了多種曲線數(shù)據(jù),在Box的Edge中就包含兩種曲線數(shù)據(jù),一種是三維曲線;一種是曲面上的曲線。如下圖所示:
Figure 3.10 Curve info of the Edge in Box
其中Curve3D對(duì)應(yīng)的就是參數(shù)化的三維曲線,這個(gè)很好理解。PCurve為曲面上的曲線。下面以Edge#9為例,來(lái)說(shuō)明邊中的幾何數(shù)據(jù)。三維曲線#12是原點(diǎn)為(0,2,3)方向?yàn)閄方向的直線,在參數(shù)區(qū)間[0,1]的端點(diǎn)處,直線對(duì)應(yīng)的兩個(gè)點(diǎn)為(0,2,3)和(1,2,3),分別對(duì)應(yīng)#31Vertex和#21Vertex。所以Vertex前面的-負(fù)號(hào)表示Edge中曲線終點(diǎn),+正號(hào)表示Edge中曲線的起點(diǎn)。
PCurve為#4號(hào)平面上的#23號(hào)參數(shù)曲線,由下圖可知#4號(hào)曲面對(duì)應(yīng)的Face為#7,對(duì)應(yīng)的Wire為#8:
Figure 3.11 Surface of Face
根據(jù)#23號(hào)二維參數(shù)曲線計(jì)算得出曲面上的U,V分別為(3, 0)和(3, 1),將得到U,V代入#4曲面中得出對(duì)應(yīng)的曲面上的點(diǎn),計(jì)算過(guò)程如下所示:
從中可以看出,PCurve表示的曲線與三維曲線#12相同,不過(guò)其中還保存了曲面的信息。
4. Conclusion
本文通過(guò)使用Tcl在OpenCascade中生成Box的邊界表示信息,通過(guò)對(duì)數(shù)據(jù)的分析,得出Box在OpenCascade中的邊界表示方式。并通過(guò)與翼邊結(jié)構(gòu)的對(duì)比,說(shuō)明OpenCascade中的拓樸結(jié)構(gòu)不是翼邊結(jié)構(gòu)。通過(guò)分析邊界表示中的幾何數(shù)據(jù),來(lái)理解邊界表示中幾何數(shù)據(jù)的應(yīng)用。
若對(duì)文中內(nèi)容有任何意見(jiàn)、建議都可以與我取得聯(lián)系,郵箱:eryar@163.com,歡迎討論、交流、指導(dǎo)。
5. References
1. OpenCascade, Test Harness User’s Guide 2013
2. OpenCascade, BRep Format Description White Paper, 2013
3. John K. Ousterhout, Tcl and Tk Toolkit, 1993