zoukankan      html  css  js  c++  java
  • Topology and Geometry in OpenCascade-Adapters

    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): 

    类适配器使用多重继承对一个接口与另一个接口进行匹配,如下图所示: 

    wps_clip_image-1576

    Figure 1.1 Class adapter structure 

    对象适配器依赖于对象组合,如下图所示: 

    wps_clip_image-9276

    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)。 

    wps_clip_image-3262

    Figure 1.3 Adaptor3d_Curve class diagram 

    从上面的类图可以看出,GeomAdaptor3d_Curve是Adaptor3d_Curve的子类,该类用来适配Geom_Curve类型,BRepAdaptor_Curve用于适配TopoDS_Edge类型。BRepAdaptor_CompCurve用于适配TopoDS_Wire。对于二维曲线和曲面也有类似功能的类。通过适配器使不同的曲线(几何曲线和拓朴边)在一起工作,如下代码所示,计算几何曲线和拓朴边长度的方式统一了:

    /*
    *    Copyright (c) 2013 eryar All Rights Reserved.
    *
    *        File    : Main.cpp
    *        Author  : eryar@163.com
    *        Date    : 2013-09-27
    *        Version : 1.0v
    *
    *    Description : GeomAdaptor: provides an interface between the services provided by any curve.
    *                  BRepAdaptor: provides classes to access the geometry of the BRep models.                 
    */
    #define WNT
    #include <gp_Circ.hxx>
    #include <Geom_Circle.hxx>
    #include <GeomAdaptor_Curve.hxx>
    #include <TopoDS_Edge.hxx>
    #include <BRepBuilderAPI_MakeEdge.hxx>
    #include <BRepAdaptor_Curve.hxx>
    #include <GCPnts_AbscissaPoint.hxx>
    
    #pragma comment(lib, "TKernel.lib")
    #pragma comment(lib, "TKMath.lib")
    #pragma comment(lib, "TKG3d.lib")
    #pragma comment(lib, "TKBRep.lib")
    #pragma comment(lib, "TKGeomBase.lib")
    #pragma comment(lib, "TKTopAlgo.lib")
    
    int main(void)
    {
        Handle_Geom_Curve aCurve = new Geom_Circle(gp::XOY(), 1.0);
        Standard_Real dCurveLength = GCPnts_AbscissaPoint::Length(GeomAdaptor_Curve(aCurve));
    
        TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(gp_Circ(gp::XOY(), 1.0));
        Standard_Real dEdgeLength = GCPnts_AbscissaPoint::Length(BRepAdaptor_Curve(anEdge));
    
        std::cout << "Circle curve length: " << dCurveLength << std::endl;
        std::cout << "Circle edge length: " << dEdgeLength << std::endl;
    
        return 0;
    }

    程序结果如下所示:

    Circle curve length: 6.28319
    Circle edge length: 6.28319
    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 

  • 相关阅读:
    软件工程第一次实验——软件开发文档与工具的安装与使用
    软件工程第四次作业——ATM管理系统
    软件工程第三次作业——举例分析流程图与活动图的区别与联系
    软件工程第二次作业——小学四则运算题目生成程序
    mac终端进入mysql
    计算机网络
    类和对象(未完成)
    html_css
    Mac下使用C语言生成和使用动态链接库
    模运算
  • 原文地址:https://www.cnblogs.com/opencascade/p/3662581.html
Copyright © 2011-2022 走看看