Topology and Geometry in OpenCascade-Adapters
eryar@163.com
摘要Abstract:本文簡要介紹了適配器模式(adapter pattern),并結合程序實例對OpenCascade中的拓樸與幾何的適配器的使用進行說明。
關鍵字Key Words:OpenCascade、BRep、Topology、Geometry、Adapter
一、適配器模式簡介 Introduction of Adapter pattern
類對象結構型模式適配器模式(Adapter):
意圖(Intent):將一個類的接口轉換成客戶希望的另外一個接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作。
別名(Also Known As):包裝器Wrapper
動機(Motivation):有時,為復用而設計的工具箱類不能夠被復用的原因僅僅是因為它的接口與專業應用領域所需要的接口不匹配。
適用性(Applicability),以下情況使用Adapter模式:
l 你想使用一個已經存在的類,而它的接口不符合你的需求;
l 你想創建一個可以復用的類,該類可以與其他不相關的類或不可預見的類(即那些接口可能不一定兼容的類)協同工作;
l (僅適用于對象Adapter)你想使用一些已經存在的子類,但是不可能對每一個都進行子類化以匹配它們的接口。對象適配器可以適配它的父類接口;
結構(Structure):
類適配器使用多重繼承對一個接口與另一個接口進行匹配,如下圖所示:
Figure 1.1 Class adapter structure
對象適配器依賴于對象組合,如下圖所示:
Figure 1.2 Object adapter structure
協作(Collaborations):Client在Adapter實例上調用一些操作,接著適配器調用Adaptee的操作實現這個請求。
關于適配器模式(Adapter pattern)更多信息,請參考GoF的經典之作《Design Patterns-Elements of Reuseable Object-Oriented Software》。
二、適配器模式在OpenCascade中的應用
一些OpenCascade的算法可以操作表示曲線的對象,然而他們提供的API接受Adaptor3d_Curve而不接受Geom_Curve。例如,包Extrema(用來計算點、線、面之間的距離)可用來計算幾何曲線(Geom_Curve)和拓樸邊(TopoDS_Edge)的求交、投影和其他一些算法。其他的例子有計算長度,面積等。這種方法稱為適配器模式(Adapter pattern)。
Figure 1.3 Adaptor3d_Curve class diagram
從上面的類圖可以看出,GeomAdaptor3d_Curve是Adaptor3d_Curve的子類,該類用來適配Geom_Curve類型,BRepAdaptor_Curve用于適配TopoDS_Edge類型。BRepAdaptor_CompCurve用于適配TopoDS_Wire。對于二維曲線和曲面也有類似功能的類。通過適配器使不同的曲線(幾何曲線和拓樸邊)在一起工作,如下代碼所示,計算幾何曲線和拓樸邊長度的方式統一了:
1 /*
2 * Copyright (c) 2013 eryar All Rights Reserved.
3 *
4 * File : Main.cpp
5 * Author : eryar@163.com
6 * Date : 2013-09-27
7 * Version : 1.0v
8 *
9 * Description : GeomAdaptor: provides an interface between the services provided by any curve.
10 * BRepAdaptor: provides classes to access the geometry of the BRep models.
11 *
12 */
13
14 #define WNT
15 #include <gp_Circ.hxx>
16 #include <Geom_Circle.hxx>
17 #include <GeomAdaptor_Curve.hxx>
18
19 #include <TopoDS_Edge.hxx>
20 #include <BRepBuilderAPI_MakeEdge.hxx>
21 #include <BRepAdaptor_Curve.hxx>
22
23 #include <GCPnts_AbscissaPoint.hxx>
24
25 #pragma comment(lib, "TKernel.lib")
26 #pragma comment(lib, "TKMath.lib")
27 #pragma comment(lib, "TKG3d.lib")
28 #pragma comment(lib, "TKBRep.lib")
29 #pragma comment(lib, "TKGeomBase.lib")
30 #pragma comment(lib, "TKTopAlgo.lib")
31
32 int main(void)
33 {
34 Handle_Geom_Curve aCurve = new Geom_Circle(gp::XOY(), 1.0);
35 Standard_Real dCurveLength = GCPnts_AbscissaPoint::Length(GeomAdaptor_Curve(aCurve));
36
37 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(gp_Circ(gp::XOY(), 1.0));
38 Standard_Real dEdgeLength = GCPnts_AbscissaPoint::Length(BRepAdaptor_Curve(anEdge));
39
40 std::cout << "Circle curve length: " << dCurveLength << std::endl;
41 std::cout << "Circle edge length: " << dEdgeLength << std::endl;
42
43 return 0;
44 }
程序結果如下所示:
1 Circle curve length: 6.28319
2 Circle edge length: 6.28319
3 Press any key to continue . . .
三、結論 Conclusion
應用適配器模式使OpenCascade中接口不兼容的類如幾何曲線(Geom_Curve)與拓樸邊(TopoDS_Edge)可以在一起工作了,如求交計算、投影計算、長度計算等等。
四、參考資料 Bibliography
1. GoF, Design Patterns-Elements of Reuseable Object-Oriented Software
2. Roman Lygin, OpenCascade notes, opencascade.blogspot.com