#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>
#include <osg/Vec3>
#include <osg/Array>
#include <osg/Geode>
#include <osg/Group>
#include <osg/MatrixTransform>
#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>
// Open Cascade library.
#include <gp_Pnt.hxx>
#include <gp_Pln.hxx>
#include <BRep_Tool.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Face.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepPrimAPI_MakeCone.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepPrimApI_MakeSphere.hxx>
#include <BRepAlgoAPI_Cut.hxx>
#include <BRepAlgoAPI_Common.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <TopExp_Explorer.hxx>
#include <Poly_Triangulation.hxx>
#include <TShort_Array1OfShortReal.hxx>
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKBRep.lib")
#pragma comment(lib, "TKPrim.lib")
#pragma comment(lib, "TKMesh.lib")
#pragma comment(lib, "TKTopAlgo.lib")
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKG3d.lib")
#pragma comment(lib, "TKGeomBase.lib")
#pragma comment(lib, "TkBO.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(0.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.
// 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)
{
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, 0.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();
} osg::Node* BuildShapeMesh(const TopoDS_Shape& aShape)
{
osg::ref_ptr<osg::Group> root = new osg::Group();
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
osg::ref_ptr<osg::Geometry> triGeom = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array();
BRepMesh_IncrementalMesh(aShape, );
TopExp_Explorer faceExplorer;
for (faceExplorer.Init(aShape, TopAbs_FACE); faceExplorer.More(); faceExplorer.Next())
{
TopLoc_Location loc;
TopoDS_Face aFace = TopoDS::Face(faceExplorer.Current());
Handle_Poly_Triangulation triFace = BRep_Tool::Triangulation(aFace, loc); Standard_Boolean hasNormal = triFace->HasNormals();
Standard_Boolean hasuvNormal = triFace->HasUVNodes();
Standard_Integer l = triFace->Nodes().Length();
Standard_Integer nTriangles = triFace->NbTriangles(); gp_Pnt vertex1;
gp_Pnt vertex2;
gp_Pnt vertex3; Standard_Integer nVertexIndex1 = ;
Standard_Integer nVertexIndex2 = ;
Standard_Integer nVertexIndex3 = ;
TColgp_Array1OfPnt nodes(, triFace->NbNodes());
Poly_Array1OfTriangle triangles(, triFace->NbTriangles());
nodes = triFace->Nodes();
triangles = triFace->Triangles(); for (Standard_Integer i = ; i <= nTriangles; i++)
{
Poly_Triangle aTriangle = triangles.Value(i);
aTriangle.Get(nVertexIndex1, nVertexIndex2, nVertexIndex3); vertex1 = nodes.Value(nVertexIndex1);
vertex2 = nodes.Value(nVertexIndex2);
vertex3 = nodes.Value(nVertexIndex3);
gp_XYZ vector12(vertex2.XYZ() - vertex1.XYZ());
gp_XYZ vector13(vertex3.XYZ() - vertex1.XYZ());
gp_XYZ normal = vector12.Crossed(vector13);
Standard_Real rModulus = normal.Modulus(); if (rModulus > gp::Resolution())
{
normal.Normalize();
}
else
{
normal.SetCoord(., ., .);
} if (aFace.Orientation()!= TopAbs_FORWARD)
{
normal.Reverse();
} vertices->push_back(osg::Vec3(vertex1.X(), vertex1.Y(), vertex1.Z()));
vertices->push_back(osg::Vec3(vertex2.X(), vertex2.Y(), vertex2.Z()));
vertices->push_back(osg::Vec3(vertex3.X(), vertex3.Y(), vertex3.Z()));
normals->push_back(osg::Vec3(normal.X(), normal.Y(), normal.Z()));
}
} triGeom->setVertexArray(vertices.get());
triGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, , vertices->size()));
triGeom->setNormalArray(normals);
triGeom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET); geode->addDrawable(triGeom);
root->addChild(geode);
return root.release();
}
/* @breif Test geometry surfaces of OpenCascade.
138 */ osg::Node* buildScene(void)
{
osg::ref_ptr<osg::Group> root = new osg::Group();
// Test Plane.
Geom_Plane plane(gp::XOY());
Geom_Plane plane2(gp::YOZ());
root->addChild(buildSurface(plane));
root->addChild(buildSurface(plane2));
// 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();
}*/ int main(int argc, char* argv[])
{
osgViewer::Viewer myViewer;
osg::ref_ptr<osg::Group> root = new osg::Group(); TopoDS_Shape theBox = BRepPrimAPI_MakeBox(,,);
TopoDS_Shape theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(,,),);
TopoDS_Shape ShapeCut = BRepAlgoAPI_Common(theSphere,theBox);
root->addChild(BuildShapeMesh(ShapeCut)); myViewer.setSceneData(root);
myViewer.addEventHandler(new osgGA::StateSetManipulator(myViewer.getCamera()->getOrCreateStateSet()));
myViewer.addEventHandler(new osgViewer::StatsHandler);
myViewer.addEventHandler(new osgViewer::WindowSizeHandler); return myViewer.run();
}

Open Cascade 转化为OpenSceneGraph中的Mesh的更多相关文章

  1. unity中的mesh合并

    在分析shadowgun时,无意中发现所有的环境建筑运行后,都被合并成一个叫做 "Combined Mesha (root: scene)" 的mesh了,但是没有发现任何合并的脚 ...

  2. gem5: 使用ruby memory system中的mesh结构 出现AssertionError错误

    问题:在使用ruby memory system中的mesh结构測试时,出现例如以下错误: Traceback (most recent call last): File "<stri ...

  3. 在微服务架构中service mesh是什么?

    在微服务架构中service mesh是什么 什么是 service mesh ? 微服务架构将软件功能隔离为多个独立的服务,这些服务可独立部署,高度可维护和可测试,并围绕特定业务功能进行组织. 这些 ...

  4. Unity 中关于 BuildSetting 中 “Optimize Mesh Data” 选项的“坑”

    Unity 在底层默认希望为你做尽可能多的优化,降低使用门槛,比如 BuildSetting 中的 Optimize Mesh Data 选项就是一个典型的例子. 这个选项到底有什么用呢?文档描述为: ...

  5. 关于Unity中的Mesh Collider碰撞器

    原来我的场景中有一个平面Plane带Mesh Collider碰撞器组件,一个主角Hero带有一个Box Collider碰撞器和有重力的Rigidbody刚体组件,主角可以放在平面上. 在导入场景后 ...

  6. 网络流中的图像转化为OpenCV中的Mat类型

    1,从网络中读取到的图像流,不支持查找,不能直接转化为Mat类型 2,例子如下: string Url = "http://192.168.0.110/cgi-bin/camera?reso ...

  7. 按照相应的格式获取系统时间并将其转化为SQL中匹配的(date)时间格式

    在获取时间时需要对时间格式进行设置,此时就需要用到SimpleDateFormat 类 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM ...

  8. java对接c++发布的webservice接口,其中参数类型有base64Binary格式(无需将图片数据转化为c++中的结构体)

    接口名称: std::string SendVehiclePass(std::string VehiclePassInfo, struct xsd__base64Binary PlatePicData ...

  9. Unity中Mesh分解与边缘高亮加上深度检测

    一个比较简单的需求,不过遇到些坑,记录下. 房间有多个模型,每个模型可能多个SubMesh,点击后,需要能具体到是那个SubMesh,并且在这个SubMesh上显示边缘高光,以及能个性这单个SubMe ...

随机推荐

  1. Linux基础训练题型(下)

    8.在题3的基础上,使用命令调换passwd文件里root位置和/bin/bash位置?即将所有的第一列和最后一列位置调换? 例: 默认:root:x:0:0:root:/root:/bin/bash ...

  2. MySql在win10上的安装(压缩版)

    一.下载: 二.下载zip版,免安装版的. 三.解压缩后,注意:文件夹名称不能包含[空格] C:\MySQL 四.增加环境变量 五.手动在安装目录  C:\MySQL 下新建一个my.ini写入以下内 ...

  3. MySQL Explain命令详解--表的读取顺序,数据读取操作的类型等

    表示索引中使用的字节数,可通过该列计算查询中使用的索引的长度(key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的) 不损失精确 ...

  4. 初次shell编程

    虽然说的是初次shell写xhell脚本,但是其实我也写了三.四个简单的脚本了.现在总结下写简单的shell脚本中遇到的一些小问题备忘一下吧. 首先是脚本文件是已.sh作为后缀的,可以先建一个.sh的 ...

  5. Kick Start 2019 Round A Contention

    $\DeclareMathOperator*{\argmax}{arg\,max}$ 题目链接 题目大意 一排 $N$ 个座位,从左到右编号 $1$ 到 $N$ . 有 $Q$ 个预定座位的请求,第 ...

  6. 03:linux文件操作四剑客

    1.1 find查找命令 1.find命令说明 1. Linux find命令用来在指定目录下查找文件. 2. 任何位于参数之前的字符串都将被视为欲查找的目录名. 3. 如果使用该命令时,不设置任何参 ...

  7. 从入门到自闭之Python高阶函数

    高阶函数:内部帮忙做了一个for循环 filter:筛选过滤 语法: filter(function,iterable) function: 1.指定过滤规则(函数的内存地址) 2.用来筛选的函数,在 ...

  8. Django 中事务的使用

    目录 Django 中事务的使用 Django默认的事务行为 在HTTP请求上加事务 在View中实现事务控制 使用装饰器 使用context manager autocommit() commit_ ...

  9. python网络爬虫(8)多媒体文件抽取

    目的 批量下载网页图片 导入库 urllib中的request中的urlretrieve方法,可以下载图片 lxml用于解析网页 requests用于获取网站信息 import urllib from ...

  10. C# 面向对象4 构造函数

    构造函数 1.构造函数用来创建对象,并且可以在构造函数中对对象进行初始化. (给对象的每个属性依次的赋值) 2.构造函数是用来创建对象的特殊方法: 1.方法名和类名一样. 2.没有返回值,连void都 ...