在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绘制出来。示例程序如下所示:

/*
* Copyright (c) 2013 eryar All Rights Reserved.
*
* File : Main.cpp
* Author : eryar@163.com
* Date : 2013-08-09 18:09
* Version : 1.0v
*
* Description : Draw OpenCascade Geometry Curves in OpenSceneGraph.
*
*/ // OpenSceneGraph library.
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator> #pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDbd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib") // OpenCascade library.
#include <TColgp_Array1OfPnt.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_Array1OfInteger.hxx> #include <Geom_Circle.hxx>
#include <Geom_Ellipse.hxx>
#include <Geom_Hyperbola.hxx>
#include <Geom_Parabola.hxx>
#include <Geom_BezierCurve.hxx>
#include <Geom_BSplineCurve.hxx> #pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKG3d.lib") // Curve Segment Delta.
const double CURVE_SEGMENT_DELTA = 0.01; /*
* @brief Build geometry curve of OpenCascade.
*/
osg::Node* buildCurve(const Geom_Curve& curve)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
osg::ref_ptr<osg::Geometry> linesGeom = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> pointsVec = new osg::Vec3Array(); gp_Pnt point;
double dFirst = curve.FirstParameter();
double dLast = curve.LastParameter(); Precision::IsNegativeInfinite(dFirst) ? dFirst = -1.0 : dFirst;
Precision::IsInfinite(dLast) ? dLast = 1.0 : dLast; for (double u = dFirst; u <= dLast; u += CURVE_SEGMENT_DELTA)
{
point = curve.Value(u); pointsVec->push_back(osg::Vec3(point.X(), point.Y(), point.Z()));
} // Set the colors.
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f, 1.0f, 0.0f, 0.0f));
linesGeom->setColorArray(colors.get());
linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL); // Set the normal in the same way of color.
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f));
linesGeom->setNormalArray(normals.get());
linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL); // Set vertex array.
linesGeom->setVertexArray(pointsVec);
linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, , pointsVec->size())); geode->addDrawable(linesGeom.get()); return geode.release();
} /**
* @breif Build geometry curve of OpenCascade.
*/
osg::Node* buildScene()
{
osg::ref_ptr<osg::Group> root = new osg::Group(); // 1. Build circle curve.
Geom_Circle circle(gp::YOZ(), 1.0); root->addChild(buildCurve(circle)); // 2. Build ellipse curve.
Geom_Ellipse ellipse(gp::ZOX(), 1.0, 0.3); root->addChild(buildCurve(ellipse)); // 3. Build Hyperbola curve.
Geom_Hyperbola hyperbola(gp::XOY(), 1.0, 0.6); root->addChild(buildCurve(hyperbola)); // 4. Build parabola curve.
Geom_Parabola parabola(gp::ZOX(), 1.0); root->addChild(buildCurve(parabola)); // 5. Build Bezier curve.
TColgp_Array1OfPnt poles(, );
poles.SetValue(, gp_Pnt(-, -, ));
poles.SetValue(, gp_Pnt(, , ));
poles.SetValue(, gp_Pnt(, , ));
poles.SetValue(, gp_Pnt(, , ));
Geom_BezierCurve bezierCurve(poles); root->addChild(buildCurve(bezierCurve)); // 6. Build BSpline curve.
TColgp_Array1OfPnt ctrlPnts(, );
TColStd_Array1OfReal knots(, );
TColStd_Array1OfInteger mults(, ); ctrlPnts.SetValue(, gp_Pnt(, , ));
ctrlPnts.SetValue(, gp_Pnt(, -, ));
ctrlPnts.SetValue(, gp_Pnt(, , )); knots.SetValue(, 0.0);
knots.SetValue(, 0.25);
knots.SetValue(, 0.5);
knots.SetValue(, 0.75);
knots.SetValue(, 1.0); mults.Init(); Geom_BSplineCurve bsplineCurve(ctrlPnts, knots, mults, ); root->addChild(buildCurve(bsplineCurve)); return root.release();
} int main(int argc, char* argv[])
{
osgViewer::Viewer myViewer; myViewer.setSceneData(buildScene()); myViewer.addEventHandler(new osgGA::StateSetManipulator(myViewer.getCamera()->getOrCreateStateSet()));
myViewer.addEventHandler(new osgViewer::StatsHandler);
myViewer.addEventHandler(new osgViewer::WindowSizeHandler); return myViewer.run();
}

因抛物线和双曲线的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. android中布局文件中 layout_weight 的属性详解

    在不同的情况下,layout_weight属性作用是不同的.主要有两种属性: 1.当布局中的控件的尺寸(宽和高)都有指定时,它所表示的该控件在父容器中的比重,及它在父容器中所占的比例,数值越大,比重越 ...

  2. 转发 win7+iis7.5+asp.net下 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 解决方案

    win7+iis7.5+asp.net下 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NE ...

  3. mysql 服务windows安装

    找到mysql的bin目录,下面有一个mysqld.exe程序,这是用来启动mysql服务的,直接运行即可启动. 还有安装windows服务方式: mysqld.exe -install -manua ...

  4. 关于Apache日志的统计

    统计apache日志文件里访问量前十的ip并按从多到少排列 五月 31, 2012 by FandLR   Filed under Linux Leave a comment 解法1: cat acc ...

  5. WIN8 平台应用隐私声明

    隐私权声明 本应用连接网络仅为控制硬件设备,不会收集你的个人信息,也不共享你个个人信息. 应用名称 雅典娜监控平台移动客户端 关于本应用 本应仅为控制设备应用,不关注任何配置相关信息,所有数据均来自服 ...

  6. python if

    根据用户从控制如输入数据,使用if语句实现用户登录功能 代码如下: name = "zy"password = "123"_name = input(" ...

  7. 彻底理解ThreadLocal一

    synchronized这类线程同步的机制可以解决多线程并发问题,在这种解决方案下,多个线程访问到的,都是同一份变量的内容.为了防止在多线程访问的过程中,可能会出现的并发错误.不得不对多个线程的访问进 ...

  8. App开发如何利用Fidder,在api接口还没有实现的情况下模拟数据,继续开发

    相信app开发很多时候,都是等后台出接口,拿到数据调试错误.殊不知,我们完全可以不用等,只要有约定好的接口定义文档,借助工具就能做到,自己模拟数据返回~      下面主要是在项目组开发过程中,使用F ...

  9. 让Entity Framework启动不再效验__MigrationHistory表

    Entity Framework中DbContext首次加载OnModelCreating会检查__MigrationHistory表,作为使用Code Frist编程模式,而实际先有数据库时,这种检 ...

  10. 依赖注入的威力,.NET Core的魅力:解决MVC视图中的中文被html编码的问题

    有园友在博问中提了这样一个问题 —— .NET Core 中文等非英文文字html编码输出问题,到我们的 ASP.NET Core 项目中一看,也是同样的问题. 比如下面的Razor视图代码: @{ ...