#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. MAC使用二进制方式安装Mysql 5.7

    一.参考文档: 二.基础环境: 系统:Centos7.4 mysql版本:percona mysql 5.7 三.部署mysql 1.初始化 mysqld --initialize --explici ...

  2. 【坑】不要使用各种框架提供的内部List

    为了代码简洁有时我们会使用一些框架提供的工具类.如 import org.apache.commons.collections.ListUtils; package java.util.Collect ...

  3. JVM 线上故障排查基本操作 (转)

    前言 对于后端程序员,特别是 Java 程序员来讲,排查线上问题是不可避免的.各种 CPU 飚高,内存溢出,频繁 GC 等等,这些都是令人头疼的问题.楼主同样也遇到过这些问题,那么,遇到这些问题该如何 ...

  4. redis 单线程的理解

    单线程模型 Redis客户端对服务端的每次调用都经历了发送命令,执行命令,返回结果三个过程.其中执行命令阶段,由于Redis是单线程来处理命令的,所有每一条到达服务端的命令不会立刻执行,所有的命令都会 ...

  5. shiro配置学习

    一.shiro的配置 1.shiro的web过滤 实例化ShiroFilterFactoryBean 设置securityManager.loginUrl.unauthorizedUrl.sucess ...

  6. js三级内联

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. springcloud(十一)-Zuul聚合微服务

    前言 我们接着上一节.在许多场景下,外部请求需要查询Zuul后端的多个微服务.比如一个电影售票手机APP,在购票订单页上,既需要查询“电影微服务”获得电影相关信息,又需要查询“用户微服务”获得当前用户 ...

  8. 企业QQ在线咨询接入

    普通QQ在线咨询接入   http://wpa.qq.com/msgrd?v=3&uin=4009603616&site=qq&menu=yes;   企业QQ在线咨询接入   ...

  9. C语言typedef详解

    原文链接 C语言允许用户使用 typedef 关键字来定义自己习惯的数据类型名称,来替代系统默认的基本类型名称.数组类型名称.指针类型名称与用户自定义的结构型名称.共用型名称.枚举型名称等.一旦用户在 ...

  10. java对象只有值传递,为什么?

    在开始深入讲解之前,有必要纠正一下大家以前的那些错误看法了.如果你有以下想法,那么你有必要好好阅读本文. 错误理解一:值传递和引用传递,区分的条件是传递的内容,如果是个值,就是值传递.如果是个引用,就 ...