Render OpenCascade Geometry Surfaces in OpenSceneGraph
在OpenSceneGraph中绘制OpenCascade的曲面
Render OpenCascade Geometry Surfaces in OpenSceneGraph
摘要Abstract:本文对OpenCascade中的几何曲面数据进行简要说明,并结合OpenSceneGraph将这些曲面显示。
关键字Key Words:OpenCascade、OpenSceneGraph、Geometry Surface、NURBS
一、引言 Introduction
《BRep Format Description White Paper》中对OpenCascade的几何数据结构进行了详细说明。BRep文件中用到的曲面总共有11种:
1.Plane 平面;
2.Cylinder 圆柱面;
3.Cone 圆锥面;
4.Sphere 球面;
5.Torus 圆环面;
6.Linear Extrusion 线性拉伸面;
7.Revolution Surface 旋转曲面;
8.Bezier Surface 贝塞尔面;
9.B-Spline Surface B样条曲面;
10.Rectangle Trim Surface 矩形裁剪曲面;
11.Offset Surface 偏移曲面;
曲面的几何数据类都有一个共同的基类Geom_Surface,类图如下所示:
![]()
Figure 1.1 Geometry Surface class diagram
抽象基类Geom_Surface有几个纯虚函数Bounds()、Value()等,可用来计算曲面上的点。类图如下所示:
![]()
Figure 1.2 Geom_Surface class diagram
与另一几何内核sgCore中的几何的概念一致,几何(geometry)是用参数方程对曲线曲面精确表示的。
每种曲面都对纯虚函数进行实现,使计算曲面上点的方式统一。
曲线C(u)是单参数的矢值函数,它是由直线段到三维欧几里得空间的映射。曲面是关于两个参数u和v的矢值函数,它表示由uv平面上的二维区域R到三维欧几里得空间的映射。把曲面表示成双参数的形式为:
![]()
它的参数方程为:
![]()
u,v参数形成了一个参数平面,参数的变化区间在参数平面上构成一个矩形区域。正常情况下,参数域内的点(u,v)与曲面上的点r(u,v)是一一对应的映射关系。
给定一个具体的曲面方程,称之为给定了一个曲面的参数化。它既决定了所表示的曲面的形状,也决定了该曲面上的点与其参数域内的点的一种对应关系。同样地,曲面的参数化不是唯一的。
曲面双参数u,v的变化范围往往取为单位正方形,即u∈[0,1],v∈[0,1]。这样讨论曲面方程时,即简单、方便,又不失一般性。
二、程序示例 Code Example
使用函数Value(u, v)根据参数计算出曲面上的点,将点分u,v方向连成线,可以绘制出曲面的线框模型。程序如下所示:
/*
* Copyright (c) 2013 eryar All Rights Reserved.
*
* File : Main.cpp
* Author : eryar@163.com
* Date : 2013-08-11 10:36
* Version : V1.0
*
* Description : Draw OpenCascade Geometry Surfaces in OpenSceneGraph.
*
*/ // OpenSceneGraph
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers> #pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgViewerd.lib") // OpenCascade
#define WNT
#include <TColgp_Array2OfPnt.hxx>
#include <TColStd_HArray1OfInteger.hxx>
#include <TColGeom_Array2OfBezierSurface.hxx>
#include <GeomConvert_CompBezierSurfacesToBSplineSurface.hxx> #include <Geom_Surface.hxx>
#include <Geom_BezierSurface.hxx>
#include <Geom_BSplineSurface.hxx>
#include <Geom_ConicalSurface.hxx>
#include <Geom_CylindricalSurface.hxx>
#include <Geom_Plane.hxx>
#include <Geom_ToroidalSurface.hxx>
#include <Geom_SphericalSurface.hxx> #pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKG3d.lib")
#pragma comment(lib, "TKGeomBase.lib") // Approximation Delta.
const double APPROXIMATION_DELTA = 0.1; /**
* @breif Build geometry surface.
*/
osg::Node* buildSurface(const Geom_Surface& surface)
{
osg::ref_ptr<osg::Geode> geode = new osg::Geode(); gp_Pnt point;
Standard_Real uFirst = 0.0;
Standard_Real vFirst = 0.0;
Standard_Real uLast = 0.0;
Standard_Real vLast = 0.0; surface.Bounds(uFirst, uLast, vFirst, vLast); Precision::IsNegativeInfinite(uFirst) ? uFirst = -1.0 : uFirst;
Precision::IsInfinite(uLast) ? uLast = 1.0 : uLast; Precision::IsNegativeInfinite(vFirst) ? vFirst = -1.0 : vFirst;
Precision::IsInfinite(vLast) ? vLast = 1.0 : vLast; // Approximation in v direction.
for (Standard_Real u = uFirst; u <= uLast; u += APPROXIMATION_DELTA)
{
osg::ref_ptr<osg::Geometry> linesGeom = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> pointsVec = new osg::Vec3Array(); for (Standard_Real v = vFirst; v <= vLast; v += APPROXIMATION_DELTA)
{
point = surface.Value(u, v); 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());
} // Approximation in u direction.
for (Standard_Real v = vFirst; v <= vLast; v += APPROXIMATION_DELTA)
{
osg::ref_ptr<osg::Geometry> linesGeom = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> pointsVec = new osg::Vec3Array(); for (Standard_Real u = vFirst; u <= uLast; u += APPROXIMATION_DELTA)
{
point = surface.Value(u, v); 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 Test geometry surfaces of OpenCascade.
*/
osg::Node* buildScene(void)
{
osg::ref_ptr<osg::Group> root = new osg::Group(); // Test Plane.
Geom_Plane plane(gp::XOY());
root->addChild(buildSurface(plane)); // Test Bezier Surface and B-Spline Surface.
TColgp_Array2OfPnt array1(,,,);
TColgp_Array2OfPnt array2(,,,);
TColgp_Array2OfPnt array3(,,,);
TColgp_Array2OfPnt array4(,,,); array1.SetValue(,,gp_Pnt(,,));
array1.SetValue(,,gp_Pnt(,,));
array1.SetValue(,,gp_Pnt(,,));
array1.SetValue(,,gp_Pnt(,,));
array1.SetValue(,,gp_Pnt(,,));
array1.SetValue(,,gp_Pnt(,,));
array1.SetValue(,,gp_Pnt(,,));
array1.SetValue(,,gp_Pnt(,,));
array1.SetValue(,,gp_Pnt(,,)); array2.SetValue(,,gp_Pnt(,,));
array2.SetValue(,,gp_Pnt(,,));
array2.SetValue(,,gp_Pnt(,,));
array2.SetValue(,,gp_Pnt(,,));
array2.SetValue(,,gp_Pnt(,,));
array2.SetValue(,,gp_Pnt(,,));
array2.SetValue(,,gp_Pnt(,,));
array2.SetValue(,,gp_Pnt(,,));
array2.SetValue(,,gp_Pnt(,,)); array3.SetValue(,,gp_Pnt(,,));
array3.SetValue(,,gp_Pnt(,,));
array3.SetValue(,,gp_Pnt(,,));
array3.SetValue(,,gp_Pnt(,,));
array3.SetValue(,,gp_Pnt(,,));
array3.SetValue(,,gp_Pnt(,,));
array3.SetValue(,,gp_Pnt(,,));
array3.SetValue(,,gp_Pnt(,,));
array3.SetValue(,,gp_Pnt(,,)); array4.SetValue(,,gp_Pnt(,,));
array4.SetValue(,,gp_Pnt(,,));
array4.SetValue(,,gp_Pnt(,,));
array4.SetValue(,,gp_Pnt(,,));
array4.SetValue(,,gp_Pnt(,,));
array4.SetValue(,,gp_Pnt(,,));
array4.SetValue(,,gp_Pnt(,,));
array4.SetValue(,,gp_Pnt(,,));
array4.SetValue(,,gp_Pnt(,,)); Geom_BezierSurface BZ1(array1);
Geom_BezierSurface BZ2(array2);
Geom_BezierSurface BZ3(array3);
Geom_BezierSurface BZ4(array4);
root->addChild(buildSurface(BZ1));
root->addChild(buildSurface(BZ2));
root->addChild(buildSurface(BZ3));
root->addChild(buildSurface(BZ4)); Handle_Geom_BezierSurface BS1 = new Geom_BezierSurface(array1);
Handle_Geom_BezierSurface BS2 = new Geom_BezierSurface(array2);
Handle_Geom_BezierSurface BS3 = new Geom_BezierSurface(array3);
Handle_Geom_BezierSurface BS4 = new Geom_BezierSurface(array4);
TColGeom_Array2OfBezierSurface bezierarray(,,,);
bezierarray.SetValue(,,BS1);
bezierarray.SetValue(,,BS2);
bezierarray.SetValue(,,BS3);
bezierarray.SetValue(,,BS4); GeomConvert_CompBezierSurfacesToBSplineSurface BB (bezierarray); if (BB.IsDone())
{
Geom_BSplineSurface BSPLSURF(
BB.Poles()->Array2(),
BB.UKnots()->Array1(),
BB.VKnots()->Array1(),
BB.UMultiplicities()->Array1(),
BB.VMultiplicities()->Array1(),
BB.UDegree(),
BB.VDegree() ); BSPLSURF.Translate(gp_Vec(,,)); root->addChild(buildSurface(BSPLSURF));
} // Test Spherical Surface.
Geom_SphericalSurface sphericalSurface(gp::XOY(), 1.0);
sphericalSurface.Translate(gp_Vec(2.5, 0.0, 0.0));
root->addChild(buildSurface(sphericalSurface)); // Test Conical Surface.
Geom_ConicalSurface conicalSurface(gp::XOY(), M_PI/, 1.0);
conicalSurface.Translate(gp_Vec(5.0, 0.0, 0.0));
root->addChild(buildSurface(conicalSurface)); // Test Cylindrical Surface.
Geom_CylindricalSurface cylindricalSurface(gp::XOY(), 1.0);
cylindricalSurface.Translate(gp_Vec(8.0, 0.0, 0.0));
root->addChild(buildSurface(cylindricalSurface)); // Test Toroidal Surface.
Geom_ToroidalSurface toroidalSurface(gp::XOY(), 1.0, 0.2);
toroidalSurface.Translate(gp_Vec(11.0, 0.0, 0.0));
root->addChild(buildSurface(toroidalSurface)); 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();
}
程序效果如下图所示:
![]()
Figure 2.1 OpenCascade Geometry Surfaces in OpenSceneGraph
三、结论 Conclusion
根据OpenCascade中的几何曲面的函数Value(u, v)可以计算出曲面上的点。分u方向和v方向分别绘制曲面上的点,并将之连接成线,即可以表示出曲面的线框模型。因为这样的模型没有面的信息,所以不能有光照效果、材质效果等。要有光照、材质的信息,必须将曲面进行三角剖分。相关的剖分算法有Delaunay三角剖分等。
PDF Version: Draw OpenCascade Geometry Surfaces in OpenSceneGraph
Render OpenCascade Geometry Surfaces in OpenSceneGraph的更多相关文章
- Render OpenCascade Geometry Curves in OpenSceneGraph
在OpenSceneGraph中绘制OpenCascade的曲线 Render OpenCascade Geometry Curves in OpenSceneGraph eryar@163.com ...
- OpenCascade Shape Representation in OpenSceneGraph
OpenCascade Shape Representation in OpenSceneGraph eryar@163.com 摘要Abstract:本文通过程序实例,将OpenCascade中的拓 ...
- Opencascade、OpenGL和OpenSceneGraph的区别与联系
OpenGL只是三维显示 Openscenegraph基于场景图的概念,它提供一个在OpenGL之上的面向对象的框架,从而能把开发者从实现和优化底层图形的调用中解脱出来 Opencascade更适合算 ...
- OpenCASCADE Linear Extrusion Surface
OpenCASCADE Linear Extrusion Surface eryar@163.com Abstract. OpenCASCADE linear extrusion surface is ...
- Polynomial Library in OpenCascade
Polynomial Library in OpenCascade eryar@163.com 摘要Abstract:分析幂基曲线即多项式曲线在OpenCascade中的计算方法,以及利用OpenSc ...
- Representation Data in OpenCascade BRep
Representation Data in OpenCascade BRep eryar@163.com 摘要Abstract:现在的显示器大多数是光栅显示器,即可以看做一个像素的矩阵.在光栅显示器 ...
- Open Cascade 转化为OpenSceneGraph中的Mesh
#include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osgGA/StateSetManipul ...
- OpenCASCADE Data Exchange - 3D PDF
OpenCASCADE Data Exchange - 3D PDF eryar@163.com Abstract. Today most 3D engineering model data are ...
- Visualize Surface by Delaunay Triangulator
Visualize Surface by Delaunay Triangulator eryar@163.com Abstract. Delaunay Triangulation is the cor ...
随机推荐
- Android服务开机自启动
新任务需要Android程序开机跑一个服务,查找资料得出如下方法: 用广播的方法监听系统启动事件:android.intent.action.BOOT_COMPLETED 并在AndroidManif ...
- 基于Linux的WebSphere性能调优与故障诊断
一.关于was数据源等问题的配置 (1)关于was数据源连接池的最大.最小配置多大合适?怎样去计算? (2)关于JVM的配置,64位系统,64位WAS,最值小和最大配置多大最优?怎样去计算? (3)应 ...
- Unity3d中Update()方法的替身
在网上看到一些资料说Unity3d的Update方法是如何如何不好,影响性能.作为一个菜鸟,之前我还觉得挺好用的,完全没用什么影响性能的问题存在.现在发现确实有很大的问题,我习惯把一大堆检测判断放在U ...
- SQL入门经典(十) 之事务
事务是什么?事务关键在与其原子性.原子性概念是指可以把一些事情当作一个执行单元来看待.从数据库角度看待.他是指应该全部执行或者全部不执行一条或多条语句的最小组合.当处理数据时候经常确保一件事发生另一件 ...
- C#中使用OpenSSL的公钥加密/私钥解密
在C#中进行公钥加密/私钥解密,需要用RSACryptoServiceProvider,但是它不支持由OpenSSL生成的公钥/私钥字符串. 比如这样的公钥/私钥对( 公私钥生成方法见 http:// ...
- 坑爹的BufferManager
特别记录一下 国内外各种关于 Socket 的例子或开源项目,大部分都采用了 BufferManager.cs(代码类似). 也不知道是哪一个坑货写的.有一定几率会导致内存无法复用,导致数据是上一个的 ...
- 修改nw.js的exe文件使其请求管理员权限
修改nw.js的exe文件使其请求管理员权限 默认情况下,nw.js发布的nw.exe文件请求的是普通权限,当我们的应用需要访问一些特殊目录或者注册表等,就需要程序启动的时候以管理员权限运行.那么此时 ...
- memcache(三)内存管理
memcached(三)内存管理 memcached使用预申请的方式来管理内存的分配,从而避免内存碎片化的问题.如果采用mallo和free来动态的申请和销毁内存,必然会产生大量的内存碎片. 基本知识 ...
- STSdb,最强纯C#开源NoSQL和虚拟文件系统 4.0 RC2 支持C/S架构
STSdb是什么 再来说明一下STSdb是什么:STSdb是C#写的开源嵌入式数据库和虚拟文件系统,支持实时索引,性能是同类产品的几倍到几十倍,访问官方网站. 温故知新 之前发了文章<STSdb ...
- Office 2016 正式发布——新特性预览
今天微软又发生了一件大事!Windows Office 2016正式发布,这标志着Windows Office 又达到了一个新的里程碑! 全新的Office 发布为Office 365 用户带来了新的 ...