Topology and Geometry in OpenCascade
Location and Orientaion
eryar@163.com
摘要Abstract:本文簡要介紹了幾何造型中的邊界表示法(BRep),并結合程序說明OpenCascade中的邊界表示的具體實現,即拓樸與幾何的聯系。拓樸結構中的位置(Location)和朝向(Orientation)進行了詳細說明。
關鍵字Key Words:OpenCascade、BRep、Topology、Geometry、Location、Orientation
一、引言 Introduction
OpenCascade中的拓樸(topology)是根據STEP標準ISO-10303-42設計的。也許讀一下這個標準中的有關概念還是很有幫助的。STEP ISO-10303-42的相關資源:
http://www.steptools.com/support/stdev_docs/express/step_irs/index.html
Figure 2.1 Topology data structure in OpenCascade
TopoDS_Shape由值控制,包含三個成員變量:myLocation、myOrient、myTShape。
Figure 2.2 TopoDS_Shape member fields
2.2 拓樸與幾何的聯系 Connection with Geometry
現在我們來考慮一下拓樸結構與幾何的關系。通過繼承TopoDS包中的抽象的拓樸類實現了邊界表示模型。如下圖所示:
Figure 2.3 Topology data structure in OpenCascade
從上面的類圖可以看出只有三種拓樸對象有幾何表示:頂點(vertex)、邊(edge)、面(face),分別為BRep_TVertex、BRep_TEdge、BRep_TFace。
Figure 2.4 TopoDS_TShape class diagram
二、位置 Location
ToposDS_Shape有個TopLoc_Location的成員變量myLocation,該變量定義了子形狀相對于該形狀的偏移量。例如,環(wire)有一個位置變量,該變量沿著向量{0,0,10}移動,意味著環所有的邊都沿著Z軸移動10個單位。下面以一個程序來具體說明:
1 /*
2 * Copyright (c) 2013 eryar All Rights Reserved.
3 *
4 * File : Main.cpp
5 * Author : eryar@163.com
6 * Date : 2013-09-26
7 * Version : V1.0
8 *
9 * Description : Shape location.
10 *
11 */
12
13 #define WNT
14 #include <Geom_Circle.hxx>
15
16 #include <TopoDS_Edge.hxx>
17 #include <TopoDS_Wire.hxx>
18 #include <TopoDS_Iterator.hxx>
19
20 #include <BRepBuilderAPI_MakeEdge.hxx>
21 #include <BRepBuilderAPI_MakeWire.hxx>
22
23 #pragma comment(lib, "TKernel.lib")
24 #pragma comment(lib, "TKMath.lib")
25 #pragma comment(lib, "TKG3d.lib")
26 #pragma comment(lib, "TKBRep.lib")
27 #pragma comment(lib, "TKTopAlgo.lib")
28
29 const std::string dumpShapeType(const TopAbs_ShapeEnum& type)
30 {
31 std::string strType("Shape");
32
33 switch (type)
34 {
35 case TopAbs_COMPOUND:
36 strType = "COMPOUND";
37 break;
38
39 case TopAbs_COMPSOLID:
40 strType = "COMPSOLID";
41 break;
42
43 case TopAbs_SOLID:
44 strType = "SOLID";
45 break;
46
47 case TopAbs_SHELL:
48 strType = "SHELL";
49 break;
50
51 case TopAbs_FACE:
52 strType = "FACE";
53 break;
54
55 case TopAbs_WIRE:
56 strType = "WIRE";
57 break;
58
59 case TopAbs_EDGE:
60 strType = "EDGE";
61 break;
62
63 case TopAbs_VERTEX:
64 strType = "VERTEX";
65 break;
66
67 default:
68 break;
69 }
70
71 return strType;
72 }
73
74 void dumpShapeLocation(const TopoDS_Shape& shape)
75 {
76 std::cout << "Shape Type: " << dumpShapeType(shape.ShapeType()) << std::endl;
77 shape.Location().ShallowDump(std::cout);
78
79 TopoDS_Iterator anItr(shape);
80
81 for (; anItr.More(); anItr.Next())
82 {
83 const TopoDS_Shape& aChild = anItr.Value();
84
85 dumpShapeLocation(aChild);
86 }
87 }
88
89 int main(void)
90 {
91 Handle_Geom_Curve aCircle = new Geom_Circle(gp::XOY(), 5.0);
92
93 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aCircle);
94 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge);
95
96 std::cout << "Before transformation: " << std::endl;
97 dumpShapeLocation(aWire);
98
99 gp_Trsf trsf;
100 trsf.SetTranslation(gp_Vec(0, 0, 10));
101 TopLoc_Location location(trsf);
102
103 aWire.Location(location);
104
105 std::cout << "After transformation: " << std::endl;
106 dumpShapeLocation(aWire);
107
108 return 0;
109 }
程序結果如下所示:
1
Before transformation:
2
Shape Type: WIRE
3
TopLoc_Location : Identity
4
5
Shape Type: EDGE
6
TopLoc_Location : Identity
7
8
Shape Type: VERTEX
9
TopLoc_Location : Identity
10
11
Shape Type: VERTEX
12
TopLoc_Location : Identity
13
14
After transformation:
15
Shape Type: WIRE
16
TopLoc_Location :
17
Exponent : 1
18
TopLoc_Datum3D 034100E8
19
( 1, 0, 0, 0)
20
( 0, 1, 0, 0)
21
( 0, 0, 1, 10)
22
23
24
Shape Type: EDGE
25
TopLoc_Location :
26
Exponent : 1
27
TopLoc_Datum3D 034100E8
28
( 1, 0, 0, 0)
29
( 0, 1, 0, 0)
30
( 0, 0, 1, 10)
31
32
33
Shape Type: VERTEX
34
TopLoc_Location :
35
Exponent : 1
36
TopLoc_Datum3D 034100E8
37
( 1, 0, 0, 0)
38
( 0, 1, 0, 0)
39
( 0, 0, 1, 10)
40
41
42
Shape Type: VERTEX
43
TopLoc_Location :
44
Exponent : 1
45
TopLoc_Datum3D 034100E8
46
( 1, 0, 0, 0)
47
( 0, 1, 0, 0)
48
( 0, 0, 1, 10)
49
50
51
Press any key to continue . . .
三、朝向 Orientation
朝向(orientation)與位置(location)的工作原理相同。當將子對象從實體中分離出來時,父對象的朝向會影響到子對象的朝向。但是有個很重要的例外就是面(Face)上邊(Edge)的朝向不遵守這個規則。在討論面的朝向時,討論過邊的參數空間曲線(pcurve)的material問題。這個例外說的是計算面上邊的朝向時不應該受到面的朝向的影響。即若要使用邊的參數空間曲線(pcurve)就按下面的方式:
1 TopExp_Explorer aFaceExp (myFace.Oriented (TopAbs_FORWARD), TopAbs_EDGE);
2 for (; aFaceExp.More(); aFaceExp.Next())
3 {
4 const TopoDS_Edge& anEdge = TopoDS::Edge (aFaceExp.Current());
5 }
假如你研究得更為深入,這個例外也是可以理解的。讓我們以一個具體例子來理解這個例外,從底層一步步的創建一個面:
1 Handle(Geom_Surface) aSurf = new Geom_Plane (gp::XOY());
2 //anti-clockwise circles if to look from surface normal
3 Handle(Geom_Curve) anExtC = new Geom_Circle (gp::XOY(), 10.);
4 Handle(Geom_Curve) anIntC = new Geom_Circle (gp::XOY(), 5.);
5 TopoDS_Edge anExtE = BRepBuilderAPI_MakeEdge (anExtC);
6 TopoDS_Edge anIntE = BRepBuilderAPI_MakeEdge (anExtC);
7 TopoDS_Wire anExtW = BRepBuilderAPI_MakeWire (anExtE);
8 TopoDS_Wire anIntW = BRepBuilderAPI_MakeWire (anIntE);
9 BRep_Builder aB;
10 TopoDS_Face aFace;
11 aB.MakeFace (aFace, aSurf, Precision::Confusion());
12 aB.Update (aFace, aSurf);
13 aB.Add (aFace, anExtW);
14 //material should lie on the right of the inner wire
15 aB.Add (aFace, anIntW.Reversed());
面默認的朝向是向前的(forward),讓我們來遍歷面的邊(edge)和參數空間曲線(pcurve)。盡管我們沒有顯示地來添加它們,從原來的討論中可知平面上的參數空間曲線可以默認計算(be computed on the fly):
1 void TraversePCurves (const TopoDS_Face& theFace)
2 {
3 TopExp_Explorer anExp (theFace, TopAbs_EDGE);
4 for (; anExp.More(); anExp.Next())
5 {
6 Standard_Real aF = 0.0;
7 Standard_Real aL = 0.0;
8 const TopoDS_Edge& anEdge = TopoDS::Edge (anExp.Current());
9
10 Handle(Geom2d_Curve) aPCurve = BRep_Tool::CurveOnSurface (anEdge, theFace, aF, aL);
11 }
12 }
得到的參數空間曲線(pcurves)如下圖所示,material在紅線的左側,在藍線的右側:
一切都很正確。現在設想一下,如果把面的朝向反向(reverse),然后再遍歷邊和參數空間曲線,會發生什么呢?
1 TopoDS_Face aRFace = TopoDS::Face (aFace.Reversed());
2
3 TraversePCurves (aRFace);
所有的邊將會具有相反的朝向,對應邊的參數空間曲線(pcurve)的material在外環的外側,在內環的內側,這明顯是錯誤的!在前面討論面的朝向時就說過面的朝向僅僅是面的邏輯朝向,而與其底層的曲面(surface)沒有關系。在上面的例子中反轉面aRFace只是一個法向為{0, 0, -1}的面。所以,要獲得邊的正確朝向,必須使用下面的方法來訪問面中的邊:
1 TopExp_Explorer anExp (theFace.Oriented (TopAbs_FORWARD), TopAbs_EDGE);
這樣就確保面上的邊具有正確的朝向,而與曲面(surface,注意在此不是face!)的法向沒有關系。OpenCASCADE的算法對這種特殊的情況都做了處理,你也一定記得這樣做。
四、結論 Conclusion
對拓樸形狀(TopoDS_Shape)的位置(location)和朝向(orientation)進行深入理解,整個拓樸結構就變得清晰了,因為一個拓樸形狀中除了子對象外,剩下就是位置和朝向成員變量了。
理解了拓樸結構后,對OpenCascade的模塊ModelingData就有個較深刻地認識了。
五、參考資料 Reference
1. Roman Lygin, OpenCascade notes, opencascade.blogspot.com
2. 孫家廣等. 計算機圖形學. 清華大學出版社
3. OpenCascade source code.