在OpenSceneGraph中绘制OpenCascade的曲线

Render OpenCascade Geometry Curves in OpenSceneGraph

eryar@163.com

摘要Abstract:本文简要说明OpenCascade中几何曲线的数据,并将这些几何曲线在OpenSceneGraph中绘制出来。

关键字KeyWords:OpenCascade、Geometry Curve、OpenSceneGraph、B-Spline、NURBS

一、引言 Introduction

结合《BRep Format Description White Paper》对OpenCascade中的几何数据结构有详细的介绍。OpenCascade中BRep格式中的曲线总共分为九种,不过有二维三维之分:

1.直线 Line

2.圆 Circle

3.椭圆 Ellipse

4.抛物线 Parabola

5.双曲线 Hyperbola

6.Bezier曲线 Bezier Curve

7.B-Spline曲线 B-Spline Curve

8.裁剪曲线 Trimmed Curve

9.偏移曲线 Offset Curve

曲线的几何数据都有一个抽象基类Geom_Curve,类图如下所示:

Figure 1.1 Geometry curve class diagram

抽象基类Geom_Curve有几个纯虚函数FirstParameter()、LastParameter()、Value(),根据这几个虚函数,就可以计算曲线上对应参数U的值。类图如下图所示:

Figure 1.2 Geom_Curve Inherited class diagram

每种曲线都对那些纯虚函数进行实现,使计算曲线上点的方式统一。

二、程序示例 Code Example

根据抽象基类Geom_Curve的几个纯虚函数:

1.FirstParameter();

2.LastParameter();

3.Value(u);

利用多态可将曲线上点都以统一的方式计算出来,并使用GL_LINE_STRIP绘制出来。示例程序如下所示:

  1. /*
  2. * Copyright (c) 2013 eryar All Rights Reserved.
  3. *
  4. * File : Main.cpp
  5. * Author : eryar@163.com
  6. * Date : 2013-08-09 18:09
  7. * Version : 1.0v
  8. *
  9. * Description : Draw OpenCascade Geometry Curves in OpenSceneGraph.
  10. *
  11. */
  12.  
  13. // OpenSceneGraph library.
  14. #include <osgDB/ReadFile>
  15. #include <osgViewer/Viewer>
  16. #include <osgViewer/ViewerEventHandlers>
  17. #include <osgGA/StateSetManipulator>
  18.  
  19. #pragma comment(lib, "osgd.lib")
  20. #pragma comment(lib, "osgDbd.lib")
  21. #pragma comment(lib, "osgGAd.lib")
  22. #pragma comment(lib, "osgViewerd.lib")
  23.  
  24. // OpenCascade library.
  25. #include <TColgp_Array1OfPnt.hxx>
  26. #include <TColStd_Array1OfReal.hxx>
  27. #include <TColStd_Array1OfInteger.hxx>
  28.  
  29. #include <Geom_Circle.hxx>
  30. #include <Geom_Ellipse.hxx>
  31. #include <Geom_Hyperbola.hxx>
  32. #include <Geom_Parabola.hxx>
  33. #include <Geom_BezierCurve.hxx>
  34. #include <Geom_BSplineCurve.hxx>
  35.  
  36. #pragma comment(lib, "TKernel.lib")
  37. #pragma comment(lib, "TKMath.lib")
  38. #pragma comment(lib, "TKG3d.lib")
  39.  
  40. // Curve Segment Delta.
  41. const double CURVE_SEGMENT_DELTA = 0.01;
  42.  
  43. /*
  44. * @brief Build geometry curve of OpenCascade.
  45. */
  46. osg::Node* buildCurve(const Geom_Curve& curve)
  47. {
  48. osg::ref_ptr<osg::Geode> geode = new osg::Geode();
  49. osg::ref_ptr<osg::Geometry> linesGeom = new osg::Geometry();
  50. osg::ref_ptr<osg::Vec3Array> pointsVec = new osg::Vec3Array();
  51.  
  52. gp_Pnt point;
  53. double dFirst = curve.FirstParameter();
  54. double dLast = curve.LastParameter();
  55.  
  56. Precision::IsNegativeInfinite(dFirst) ? dFirst = -1.0 : dFirst;
  57. Precision::IsInfinite(dLast) ? dLast = 1.0 : dLast;
  58.  
  59. for (double u = dFirst; u <= dLast; u += CURVE_SEGMENT_DELTA)
  60. {
  61. point = curve.Value(u);
  62.  
  63. pointsVec->push_back(osg::Vec3(point.X(), point.Y(), point.Z()));
  64. }
  65.  
  66. // Set the colors.
  67. osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
  68. colors->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 0.0f));
  69. linesGeom->setColorArray(colors.get());
  70. linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
  71.  
  72. // Set the normal in the same way of color.
  73. osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
  74. normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f));
  75. linesGeom->setNormalArray(normals.get());
  76. linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
  77.  
  78. // Set vertex array.
  79. linesGeom->setVertexArray(pointsVec);
  80. linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, , pointsVec->size()));
  81.  
  82. geode->addDrawable(linesGeom.get());
  83.  
  84. return geode.release();
  85. }
  86.  
  87. /**
  88. * @breif Build geometry curve of OpenCascade.
  89. */
  90. osg::Node* buildScene()
  91. {
  92. osg::ref_ptr<osg::Group> root = new osg::Group();
  93.  
  94. // 1. Build circle curve.
  95. Geom_Circle circle(gp::YOZ(), 1.0);
  96.  
  97. root->addChild(buildCurve(circle));
  98.  
  99. // 2. Build ellipse curve.
  100. Geom_Ellipse ellipse(gp::ZOX(), 1.0, 0.3);
  101.  
  102. root->addChild(buildCurve(ellipse));
  103.  
  104. // 3. Build Hyperbola curve.
  105. Geom_Hyperbola hyperbola(gp::XOY(), 1.0, 0.6);
  106.  
  107. root->addChild(buildCurve(hyperbola));
  108.  
  109. // 4. Build parabola curve.
  110. Geom_Parabola parabola(gp::ZOX(), 1.0);
  111.  
  112. root->addChild(buildCurve(parabola));
  113.  
  114. // 5. Build Bezier curve.
  115. TColgp_Array1OfPnt poles(, );
  116. poles.SetValue(, gp_Pnt(-, -, ));
  117. poles.SetValue(, gp_Pnt(, , ));
  118. poles.SetValue(, gp_Pnt(, , ));
  119. poles.SetValue(, gp_Pnt(, , ));
  120. Geom_BezierCurve bezierCurve(poles);
  121.  
  122. root->addChild(buildCurve(bezierCurve));
  123.  
  124. // 6. Build BSpline curve.
  125. TColgp_Array1OfPnt ctrlPnts(, );
  126. TColStd_Array1OfReal knots(, );
  127. TColStd_Array1OfInteger mults(, );
  128.  
  129. ctrlPnts.SetValue(, gp_Pnt(, , ));
  130. ctrlPnts.SetValue(, gp_Pnt(, -, ));
  131. ctrlPnts.SetValue(, gp_Pnt(, , ));
  132.  
  133. knots.SetValue(, 0.0);
  134. knots.SetValue(, 0.25);
  135. knots.SetValue(, 0.5);
  136. knots.SetValue(, 0.75);
  137. knots.SetValue(, 1.0);
  138.  
  139. mults.Init();
  140.  
  141. Geom_BSplineCurve bsplineCurve(ctrlPnts, knots, mults, );
  142.  
  143. root->addChild(buildCurve(bsplineCurve));
  144.  
  145. return root.release();
  146. }
  147.  
  148. int main(int argc, char* argv[])
  149. {
  150. osgViewer::Viewer myViewer;
  151.  
  152. myViewer.setSceneData(buildScene());
  153.  
  154. myViewer.addEventHandler(new osgGA::StateSetManipulator(myViewer.getCamera()->getOrCreateStateSet()));
  155. myViewer.addEventHandler(new osgViewer::StatsHandler);
  156. myViewer.addEventHandler(new osgViewer::WindowSizeHandler);
  157.  
  158. return myViewer.run();
  159. }

因抛物线和双曲线的FirstParameter()和LastParameter()为负无穷和正无穷,所以对其进行处理,只输出了部分曲线。

程序效果如下图所示:

Figure 2.1 OpenCascade Geometry Curves in OpenSceneGraph

三、结论 Conclusion

OpenCascade的几何数据使用还是很方便的,只要将相应的曲线构造出来之后,计算曲线上的点使用函数Value()即可,还可计算相应参数处的微分值等。

通过理解《BRep Format Description White Paper》,可将BRep文件中数据导入OpenCascade中与上面实现的程序进行对比,结果正确。如下图所示:

Figure 3.1 B-Spline in OpenSceneGraph

Figure 3.2 B-Spline in OpenCascade Draw

Render OpenCascade Geometry Curves in OpenSceneGraph的更多相关文章

  1. Render OpenCascade Geometry Surfaces in OpenSceneGraph

    在OpenSceneGraph中绘制OpenCascade的曲面 Render OpenCascade Geometry Surfaces in OpenSceneGraph eryar@163.co ...

  2. OpenCascade Shape Representation in OpenSceneGraph

    OpenCascade Shape Representation in OpenSceneGraph eryar@163.com 摘要Abstract:本文通过程序实例,将OpenCascade中的拓 ...

  3. Opencascade、OpenGL和OpenSceneGraph的区别与联系

    OpenGL只是三维显示 Openscenegraph基于场景图的概念,它提供一个在OpenGL之上的面向对象的框架,从而能把开发者从实现和优化底层图形的调用中解脱出来 Opencascade更适合算 ...

  4. OpenCASCADE Rational Bezier Curves

    OpenCASCADE Rational Bezier Curves eryar@163.com Abstract. Although polynomials offer many advantage ...

  5. OpenCASCADE Linear Extrusion Surface

    OpenCASCADE Linear Extrusion Surface eryar@163.com Abstract. OpenCASCADE linear extrusion surface is ...

  6. Polynomial Library in OpenCascade

    Polynomial Library in OpenCascade eryar@163.com 摘要Abstract:分析幂基曲线即多项式曲线在OpenCascade中的计算方法,以及利用OpenSc ...

  7. Representation Data in OpenCascade BRep

    Representation Data in OpenCascade BRep eryar@163.com 摘要Abstract:现在的显示器大多数是光栅显示器,即可以看做一个像素的矩阵.在光栅显示器 ...

  8. Parametric Curves and Surfaces

    Parametric Curves and Surfaces eryar@163.com Abstract. This paper is concerned with parametric curve ...

  9. OpenCASCADE Data Exchange - 3D PDF

    OpenCASCADE Data Exchange - 3D PDF eryar@163.com Abstract. Today most 3D engineering model data are ...

随机推荐

  1. 将图片在指定窗口中显示-OpenCV应用学习笔记一

    1.OpenCV模块划分 OpenCV其实就是一堆用C和C++语言来实现计算机视觉算法的源代码文件:例如C接口函数cvCany()实现了Canny边缘提取算法,我们可以直接将这些源代码添加到自己的软件 ...

  2. python安装MySQLdb模块

    以Ubuntu下安装为例: 下载地址:https://pypi.python.org/pypi/MySQL-python/ 解压后直接进入解压目录运行安装命令. python setup.py ins ...

  3. sphinx的配置

    ## Sphinx configuration file sample## WARNING! While this sample file mentions all available options ...

  4. mysql5.7 代价模型浅析

    代价模型 mysql 5.7.10代价计算相对之前的版本有5.7 代价模型浅析较大的改进.例如 代价模型参数可以动态配置,可以适应不同的硬件 区分考虑数据在内存和在磁盘中的代价 代价精度提升为浮点型 ...

  5. innodb 锁分裂继承与迁移

    innodb行锁简介 行锁类型 LOCK_S:共享锁 LOCK_X: 排他锁 GAP类型 LOCK_GAP:只锁间隙 LOCK_REC_NO_GAP:只锁记录 LOCK_ORDINARY: 锁记录和记 ...

  6. 如何禁用Marlin温度保护

    最近在玩3D打印,搞了套MEGA 2560 + RAMPS 1.4 + A4988,刷Marlin(https://github.com/MarlinFirmware/Marlin)固件,接上电机调试 ...

  7. 生产环境下实践DDD中的规约模式

    最近的开发工作涉及到两个模块“任务”和“日周报”.关系是日周报消费任务,因为用户在写日周报的时候,需要按一定的规则筛选当前用户的任务,作为日周报的一部分提交.整个项目采用类似于Orchard那种平台加 ...

  8. [.NET领域驱动设计实战系列]专题四:前期准备之工作单元模式(Unit Of Work)

    一.前言 在前一专题中介绍了规约模式的实现,然后在仓储实现中,经常会涉及工作单元模式的实现.然而,在我的网上书店案例中也将引入工作单元模式,所以本专题将详细介绍下该模式,为后面案例的实现做一个铺垫. ...

  9. Linux Ubuntu上手动安装.NET Core SDK

    今天重装了一台Linux服务器的Ubuntu 14.04系统,需要重新安装.NET Core 1.0. 按照官网上的文档用apt-get命令进行安装: sudo sh -c 'echo "d ...

  10. 【转】 Nginx深入详解之多进程网络模型

    [转自]http://blog.chinaunix.net/uid-22312037-id-3974068.html 一.进程模型        Nginx之所以为广大码农喜爱,除了其高性能外,还有其 ...