Topology and Geometry in OpenCascade-Adapters
eryar@163.com
摘要Abstract:本文簡(jiǎn)要介紹了適配器模式(adapter pattern),并結(jié)合程序?qū)嵗龑?duì)OpenCascade中的拓樸與幾何的適配器的使用進(jìn)行說(shuō)明。
關(guān)鍵字Key Words:OpenCascade、BRep、Topology、Geometry、Adapter
一、適配器模式簡(jiǎn)介 Introduction of Adapter pattern
類對(duì)象結(jié)構(gòu)型模式適配器模式(Adapter):
意圖(Intent):將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另外一個(gè)接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作。
別名(Also Known As):包裝器Wrapper
動(dòng)機(jī)(Motivation):有時(shí),為復(fù)用而設(shè)計(jì)的工具箱類不能夠被復(fù)用的原因僅僅是因?yàn)樗慕涌谂c專業(yè)應(yīng)用領(lǐng)域所需要的接口不匹配。
適用性(Applicability),以下情況使用Adapter模式:
l 你想使用一個(gè)已經(jīng)存在的類,而它的接口不符合你的需求;
l 你想創(chuàng)建一個(gè)可以復(fù)用的類,該類可以與其他不相關(guān)的類或不可預(yù)見(jiàn)的類(即那些接口可能不一定兼容的類)協(xié)同工作;
l (僅適用于對(duì)象Adapter)你想使用一些已經(jīng)存在的子類,但是不可能對(duì)每一個(gè)都進(jìn)行子類化以匹配它們的接口。對(duì)象適配器可以適配它的父類接口;
結(jié)構(gòu)(Structure):
類適配器使用多重繼承對(duì)一個(gè)接口與另一個(gè)接口進(jìn)行匹配,如下圖所示:
Figure 1.1 Class adapter structure
對(duì)象適配器依賴于對(duì)象組合,如下圖所示:
Figure 1.2 Object adapter structure
協(xié)作(Collaborations):Client在Adapter實(shí)例上調(diào)用一些操作,接著適配器調(diào)用Adaptee的操作實(shí)現(xiàn)這個(gè)請(qǐng)求。
關(guān)于適配器模式(Adapter pattern)更多信息,請(qǐng)參考GoF的經(jīng)典之作《Design Patterns-Elements of Reuseable Object-Oriented Software》。
二、適配器模式在OpenCascade中的應(yīng)用
一些OpenCascade的算法可以操作表示曲線的對(duì)象,然而他們提供的API接受Adaptor3d_Curve而不接受Geom_Curve。例如,包Extrema(用來(lái)計(jì)算點(diǎn)、線、面之間的距離)可用來(lái)計(jì)算幾何曲線(Geom_Curve)和拓樸邊(TopoDS_Edge)的求交、投影和其他一些算法。其他的例子有計(jì)算長(zhǎng)度,面積等。這種方法稱為適配器模式(Adapter pattern)。
Figure 1.3 Adaptor3d_Curve class diagram
從上面的類圖可以看出,GeomAdaptor3d_Curve是Adaptor3d_Curve的子類,該類用來(lái)適配Geom_Curve類型,BRepAdaptor_Curve用于適配TopoDS_Edge類型。BRepAdaptor_CompCurve用于適配TopoDS_Wire。對(duì)于二維曲線和曲面也有類似功能的類。通過(guò)適配器使不同的曲線(幾何曲線和拓樸邊)在一起工作,如下代碼所示,計(jì)算幾何曲線和拓樸邊長(zhǎng)度的方式統(tǒng)一了:
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 }
程序結(jié)果如下所示:
1 Circle curve length: 6.28319
2 Circle edge length: 6.28319
3 Press any key to continue . . .
三、結(jié)論 Conclusion
應(yīng)用適配器模式使OpenCascade中接口不兼容的類如幾何曲線(Geom_Curve)與拓樸邊(TopoDS_Edge)可以在一起工作了,如求交計(jì)算、投影計(jì)算、長(zhǎng)度計(jì)算等等。
四、參考資料 Bibliography
1. GoF, Design Patterns-Elements of Reuseable Object-Oriented Software
2. Roman Lygin, OpenCascade notes, opencascade.blogspot.com