2019/6/21

在OCCT中visualization中有现成的VTK接口 从Topo::DS_Shape到Mapper的接口如下所示:

IVtkOCC_Shape::Handle aShapeImpl = new IVtkOCC_Shape(MakeBottle(20,30,8));vtkSmartPointer<IVtkTools_ShapeDataSource> DS = vtkSmartPointer<IVtkTools_ShapeDataSource>::New();DS->SetShape(aShapeImpl);vtkSmartPointer<vtkPolyDataMapper> Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();Mapper->SetInputConnection(DS->GetOutputPort());

画图的例子采用的是OCCT官方教程中的二锅头的例子 图形的类型为Topo::DS_Shape

另外还有一个球的生成作为测试 图形的类型为Topo::DS_Face

#include <vtkAutoInit.h> //avoid error:0xC0000005
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);//Libs from OCCT
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKBRep.lib")
#pragma comment(lib, "TKMesh.lib")
#pragma comment(lib, "TKTopAlgo.lib")#include <TCollection_AsciiString.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Face.hxx>
#include <gp_Circ.hxx>
#include <gp_Elips.hxx>
#include <gp_Sphere.hxx>
#include <gp_Pnt.hxx>
#include <Geom_Line.hxx>
#include <Geom_Circle.hxx>
#include <Geom_Plane.hxx>
#include <Geom_CylindricalSurface.hxx>
#include <Geom2d_Ellipse.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <GC_MakeArcOfCircle.hxx>
#include <GC_MakeSegment.hxx>
#include <Poly_Polygon3D.hxx>
#include <Poly_Triangulation.hxx>
#include <BRep_Builder.hxx>
#include <BRepBuilderAPI.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepPrimAPI_MakePrism.hxx>
#include <BRepprimAPI_MakeCylinder.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRepAlgoAPI_Fuse.hxx>
#include <BRep_Tool.hxx>
#include <BRepMesh.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <BRepFilletAPI_MakeFillet.hxx>
#include <BRepOffsetAPI_MakeThickSolid.hxx>
#include <BRepLib.hxx>
#include <TopExp_Explorer.hxx>
#include <GCE2d_MakeSegment.hxx>
#include <BRepOffsetAPI_ThruSections.hxx>
#include <IVtkOCC_Shape.hxx>
#include <IVtkTools_ShapeDataSource.hxx>//Libs from VTK
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkLine.h>
#include <vtkTriangle.h>
#include <vtkPolyDataWriter.h>
#include <vtkVertexGlyphFilter.h>//OCC shape
//standard 基本类型
TopoDS_Shape MakeBottle(const Standard_Real myWidth, const Standard_Real myHeight,const Standard_Real myThickness)
{// Profile : Define Support Points//gp:几何元素gp_Pnt aPnt1(-myWidth / 2., 0, 0);gp_Pnt aPnt2(-myWidth / 2., -myThickness / 4., 0);gp_Pnt aPnt3(0, -myThickness / 2., 0);gp_Pnt aPnt4(myWidth / 2., -myThickness / 4., 0);gp_Pnt aPnt5(myWidth / 2., 0, 0);// Profile : Define the Geometry//geom构建几何类型 Handle为智能指针Handle(Geom_TrimmedCurve) anArcOfCircle = GC_MakeArcOfCircle(aPnt2, aPnt3, aPnt4);Handle(Geom_TrimmedCurve) aSegment1 = GC_MakeSegment(aPnt1, aPnt2);Handle(Geom_TrimmedCurve) aSegment2 = GC_MakeSegment(aPnt4, aPnt5);// Profile : Define the Topology//topods几何拓扑结构 包含几何信息 方向 位置//brepbuilderapi 使用几何类型构造几何拓扑结构TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(aSegment1);TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(anArcOfCircle);TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(aSegment2);TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge1, anEdge2, anEdge3);// Complete Profilegp_Ax1 xAxis = gp::OX();gp_Trsf aTrsf;aTrsf.SetMirror(xAxis);BRepBuilderAPI_Transform aBRepTrsf(aWire, aTrsf);TopoDS_Shape aMirroredShape = aBRepTrsf.Shape();TopoDS_Wire aMirroredWire = TopoDS::Wire(aMirroredShape);BRepBuilderAPI_MakeWire mkWire;mkWire.Add(aWire);mkWire.Add(aMirroredWire);TopoDS_Wire myWireProfile = mkWire.Wire();// Body : Prism the Profile// brepprimapi扫描构建几何体TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile);gp_Vec aPrismVec(0, 0, myHeight);TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile, aPrismVec);// Body : Apply Fillets//brepfilletapi倒角 圆角等//topexp——explorer用于遍历元素 for循环BRepFilletAPI_MakeFillet mkFillet(myBody);TopExp_Explorer anEdgeExplorer(myBody, TopAbs_EDGE);while (anEdgeExplorer.More()){TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExplorer.Current());//Add edge to fillet algorithmmkFillet.Add(myThickness / 12., anEdge);anEdgeExplorer.Next();}myBody = mkFillet.Shape();// Body : Add the Neck//brepalgo 算法包 布尔运算 裁剪等gp_Pnt neckLocation(0, 0, myHeight);gp_Dir neckAxis = gp::DZ();gp_Ax2 neckAx2(neckLocation, neckAxis);Standard_Real myNeckRadius = myThickness / 4.;Standard_Real myNeckHeight = myHeight / 10.;BRepPrimAPI_MakeCylinder MKCylinder(neckAx2, myNeckRadius, myNeckHeight);TopoDS_Shape myNeck = MKCylinder.Shape();myBody = BRepAlgoAPI_Fuse(myBody, myNeck);// Body : Create a Hollowed SolidTopoDS_Face   faceToRemove;Standard_Real zMax = -1;for (TopExp_Explorer aFaceExplorer(myBody, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next()){TopoDS_Face aFace = TopoDS::Face(aFaceExplorer.Current());// Check if <aFace> is the top face of the bottle's neck Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);if (aSurface->DynamicType() == STANDARD_TYPE(Geom_Plane)){Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurface);gp_Pnt aPnt = aPlane->Location();Standard_Real aZ = aPnt.Z();if (aZ > zMax){zMax = aZ;faceToRemove = aFace;}}}//toptools 数据结构类型 表 树等TopTools_ListOfShape facesToRemove;facesToRemove.Append(faceToRemove);//brepoffsetapi 通过偏移来生成实体BRepOffsetAPI_MakeThickSolid BodyMaker;BodyMaker.MakeThickSolidByJoin(myBody, facesToRemove, -myThickness / 50, 1.e-3);myBody = BodyMaker.Shape();// Threading : Create SurfacesHandle(Geom_CylindricalSurface) aCyl1 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 0.99);Handle(Geom_CylindricalSurface) aCyl2 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 1.05);// Threading : Define 2D Curvesgp_Pnt2d aPnt(2. * M_PI, myNeckHeight / 2.);gp_Dir2d aDir(2. * M_PI, myNeckHeight / 4.);gp_Ax2d anAx2d(aPnt, aDir);Standard_Real aMajor = 2. * M_PI;Standard_Real aMinor = myNeckHeight / 10;Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor);Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4);Handle(Geom2d_TrimmedCurve) anArc1 = new Geom2d_TrimmedCurve(anEllipse1, 0, M_PI);Handle(Geom2d_TrimmedCurve) anArc2 = new Geom2d_TrimmedCurve(anEllipse2, 0, M_PI);gp_Pnt2d anEllipsePnt1 = anEllipse1->Value(0);gp_Pnt2d anEllipsePnt2 = anEllipse1->Value(M_PI);Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2);// Threading : Build Edges and WiresTopoDS_Edge anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1, aCyl1);TopoDS_Edge anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment, aCyl1);TopoDS_Edge anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2, aCyl2);TopoDS_Edge anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment, aCyl2);TopoDS_Wire threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1, anEdge2OnSurf1);TopoDS_Wire threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2, anEdge2OnSurf2);BRepLib::BuildCurves3d(threadingWire1);BRepLib::BuildCurves3d(threadingWire2);// Create Threading BRepOffsetAPI_ThruSections aTool(Standard_True);aTool.AddWire(threadingWire1);aTool.AddWire(threadingWire2);aTool.CheckCompatibility(Standard_False);TopoDS_Shape myThreading = aTool.Shape();// Building the Resulting Compound TopoDS_Compound aRes;BRep_Builder aBuilder;aBuilder.MakeCompound(aRes);aBuilder.Add(aRes, myBody);aBuilder.Add(aRes, myThreading);return aRes;
}//Mesh&Data transmission
void BuildMesh(vtkRenderer* render, const TopoDS_Face& face, double deflection = 0.1)
{TopLoc_Location location;BRepMesh_IncrementalMesh(face, deflection);Handle_Poly_Triangulation triFace = BRep_Tool::Triangulation(face, location);Standard_Integer nTriangles = triFace->NbTriangles();gp_Pnt vertex1;gp_Pnt vertex2;gp_Pnt vertex3;Standard_Integer nVertexIndex1 = 0;Standard_Integer nVertexIndex2 = 0;Standard_Integer nVertexIndex3 = 0;TColgp_Array1OfPnt nodes(1, triFace->NbNodes());Poly_Array1OfTriangle triangles(1, triFace->NbTriangles());nodes = triFace->Nodes();triangles = triFace->Triangles();vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();vtkSmartPointer<vtkPolyData> pointData = vtkSmartPointer<vtkPolyData>::New();vtkSmartPointer<vtkPolyData> lineData = vtkSmartPointer<vtkPolyData>::New();vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();points->Allocate(nTriangles * 3);cells->Allocate(nTriangles);int id = 0;for (Standard_Integer i = 1; i <= nTriangles; i++){Poly_Triangle aTriangle = triangles.Value(i);aTriangle.Get(nVertexIndex1, nVertexIndex2, nVertexIndex3);vertex1 = nodes.Value(nVertexIndex1).Transformed(location.Transformation());vertex2 = nodes.Value(nVertexIndex2).Transformed(location.Transformation());vertex3 = nodes.Value(nVertexIndex3).Transformed(location.Transformation());points->InsertNextPoint(vertex1.X(), vertex1.Y(), vertex1.Z());points->InsertNextPoint(vertex2.X(), vertex2.Y(), vertex2.Z());points->InsertNextPoint(vertex3.X(), vertex3.Y(), vertex3.Z());vtkSmartPointer<vtkLine> line0 = vtkSmartPointer<vtkLine>::New();line0->GetPointIds()->SetId(0, id * 3);line0->GetPointIds()->SetId(1, id * 3 + 1);vtkSmartPointer<vtkLine> line1 = vtkSmartPointer<vtkLine>::New();line1->GetPointIds()->SetId(0, id * 3 + 1);line1->GetPointIds()->SetId(1, id * 3 + 2);vtkSmartPointer<vtkLine> line2 = vtkSmartPointer<vtkLine>::New();line2->GetPointIds()->SetId(0, id * 3 + 2);line2->GetPointIds()->SetId(1, id * 3);vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New();triangle->GetPointIds()->SetId(0, id * 3);triangle->GetPointIds()->SetId(1, id * 3 + 1);triangle->GetPointIds()->SetId(2, id * 3 + 2);// Add the triangle to a cell arraylines->InsertNextCell(line0);lines->InsertNextCell(line1);lines->InsertNextCell(line2);cells->InsertNextCell(triangle);id++;}pointData->SetPoints(points);vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =vtkSmartPointer<vtkVertexGlyphFilter>::New();glyphFilter->SetInputData(pointData);glyphFilter->Update();lineData->SetPoints(points);lineData->SetLines(lines);polyData->SetPoints(points);polyData->SetPolys(cells);/*vtkSmartPointer<vtkPolyDataWriter> writer = vtkSmartPointer<vtkPolyDataWriter>::New();writer->SetFileName("Mesh.vtk");writer->SetInputData(polyData);writer->Write();*/vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();pointMapper->SetInputData(glyphFilter->GetOutput());vtkSmartPointer<vtkPolyDataMapper> lineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();pointMapper->SetInputData(lineData);vtkSmartPointer<vtkPolyDataMapper> faceMapper = vtkSmartPointer<vtkPolyDataMapper>::New();faceMapper->SetInputData(polyData);vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();pointActor->SetMapper(pointMapper);pointActor->GetProperty()->SetPointSize(3);pointActor->GetProperty()->SetColor(1, 1, 1);pointActor->GetProperty()->SetOpacity(1); vtkSmartPointer<vtkActor> lineActor = vtkSmartPointer<vtkActor>::New();lineActor->SetMapper(lineMapper);lineActor->GetProperty()->SetLineWidth(3);lineActor->GetProperty()->SetColor(1, 0, 0);vtkSmartPointer<vtkActor> faceActor = vtkSmartPointer<vtkActor>::New();faceActor->SetMapper(faceMapper);faceActor->GetProperty()->SetColor(1, 0, 0);render->AddActor(pointActor);render->AddActor(lineActor);render->AddActor(faceActor);
}void TestScene(vtkRenderer* render)
{gp_Ax2 axis;axis.SetAxis(gp::OX());axis.SetLocation(gp_Pnt(0.0, 0.0, 0.0));TopoDS_Face sphereFace1 = BRepBuilderAPI_MakeFace(gp_Sphere(axis, 8.0));BuildMesh(render, sphereFace1);
}void BuildScene(vtkRenderer* render, TopoDS_Shape model,double deflection)
{for (TopExp_Explorer aFaceExplorer(model, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next()){TopoDS_Face aFace = TopoDS::Face(aFaceExplorer.Current());BuildMesh(render, aFace,deflection);}
}int main(int argc, char* argv[])
{// Create a renderer, render window, and interactorvtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();renderWindow->SetSize(1200, 900);renderWindow->AddRenderer(renderer);vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();renderWindowInteractor->SetRenderWindow(renderWindow);//build meshBuildScene(renderer, MakeBottle(20,30,8),0.01);TestScene(renderer);renderer->SetBackground(0, 0.2, 0.8);// Render and interactrenderWindow->Render();renderWindowInteractor->Start();return 0;
}

显示效果图:

VTK的管线配置还是比较简单的 建立相应的数据结构 将对应的数据传过去就行

比较麻烦的地方:

1.类型 TopoDS_Shape和TopoDS_Face的问题 即类型层次和转换的问题

2.三角剖分相关的源码

源码中间三角剖分最终都成为了Topo::DS_TFace中间的Trianglation 之后就没有办法看了

一层一层嵌套很难受

参考资料之后 觉得还是要从BRep_FastDiscretFace.cxx里面看

3.如何生成体网格

目前仅能实现一条参数轴(U)的离散

即仅能通过暴力方法 生成旋转和拉伸生成实体的面网格

OCCT 建模,VTK显示相关推荐

  1. VTK、ITK安装及ITK读取图片、VTK显示示例(CMake)

    VTK.ITK安装及ITK读取图片.VTK显示示例 文章目录 前言 一.配置 二.混合编程实例 1.创建工程 2.修改CMakeLists.txt 3.画界面mainwidow.ui 3.编程(~ 1 ...

  2. VTK显示CT图像视图

    VTK显示CT图像视图

  3. 【图形学】Bézier 曲面建模及显示

    [图形学]Bézier 曲面建模及显示 1.绘制目标 2.核心代码 3.运行结果 1.绘制目标 Bézier 曲面建模及显示 'x':绕x轴旋转 'y':绕y轴旋转 'z':绕z轴旋转 'r':复原 ...

  4. ITK读图VTK显示

    ITK 实现图像数据的读取,然后通过连接器把ITK读取的图像数据传输到VTK 然后进行显示. #include "itkImage.h" #include "itkIma ...

  5. DCMTK读取压缩格式的DICOM文件并使用Vtk显示

    想必看这篇文章的读者都对vtk有或多或少的认识,vtk中的vtkDICOMImageReader是用来读取DICOM文件的类,但是其只能读取未压缩格式的DICOM文件,本人手头上需要显示和处理的DIC ...

  6. occ+vtk显示igs模型

    使用Opencascade读取igs文件内模型,使用vtk进行显示. 本案例环境:Opencascade6.6.0 +  vtk-5.10 + VS2005(win32) 使用CMake管理工程. C ...

  7. 把pcl的VTK显示融合到MFC(代码找原作者)

    转自PCL中国,原文链接:http://www.pclcn.org/bbs/forum.php?mod=viewthread&tid=223&extra=page%3D1 本人做了少量 ...

  8. 使用vtk显示标准格式网格文件stl

    vtk有专用的stl文件读入接口 本链接主要为了学习pyvista,pyvista是一个非常强大的集成VTK的库,测试其网格读入相关接口 import vtk import pyvista as pv ...

  9. PCL库VTK显示点云法向时出错:no override found for vtkActor

    错误:no override found for vtkActor (我的环境是pcl1.8.1)解决办法: 1. 头文件中添加 #include <vtkAutoInit.h> VTK_ ...

  10. Vtk读取并显示保存图像

    (1):Vtk读取并显示图像:三种方法 转载:未知出处 转载:用VTK显示平面图片:http://blog.csdn.net/tonylk/article/details/464881 用到vtkJP ...

最新文章

  1. python爬虫教程网-python爬虫入门10分钟爬取一个网站
  2. 一文教你使用java开发一款推箱子游戏
  3. php js 循环对象属性,js 遍历对象的属性的代码_javascript技巧
  4. SQL Server 2005中NTEXT与NVARCHAR(MAX)
  5. 手把手叫你一台电脑配置两个Git账户
  6. Linux内核升级Broadcom网卡问题(bnx2)
  7. 简单python脚本实例-超实用的 30 段 Python 案例
  8. iOS开发之UILabel的使用
  9. MAC快捷键使用大全
  10. 模拟太阳系的html,three.js模拟实现太阳系行星体系功能
  11. 用c语言函数判断是否为水仙花数,输入三位数,判断是不是水仙花数c语言c语言...
  12. 就计算机应用领域而言航天器,婴幼儿耳道长耵聍,应该用()工具取出A.耳挖勺B.火柴棍C.棉签D.卡子...
  13. IPhone4S自定义铃声
  14. Excel文件加密的两种方式
  15. web期末大作业:基于html+css+js制作 学校班级网页制作我的校园
  16. Java HashMap双花括号初始化数据的代码解析,及带来的问题分析
  17. webview在android8.0,解决Android8.0系统应用打开webView报错
  18. 云监控介绍 - Amazon CloudWatch
  19. 自适应滤波器原理——新息过程
  20. 請問下需要一套繁體字多商戶商城+跑腿外賣系統

热门文章

  1. 必先利其器之 IDEA 磨刀好杀猪
  2. Problem : 闰年闰月
  3. 基于ISO27001的数据中心信息安全管理体系
  4. 计算机系统组成习题检测及答案
  5. pyinstaller 打包exe启动慢的问题
  6. Java入门第三季—简易扑克牌游戏
  7. 国产UOS系统之——安装N卡驱动(多屏显示)
  8. 【js高级 Day3】深入理解原型的方式继承,借用构造函数继承,组合继承,拷贝继承
  9. QGC 谷歌中国地图 火星坐标系 转换
  10. MM 移动类型-入门篇