摘要Abstract:本文通过程序实例,将OpenCascade中的拓朴数据(边、面)离散化后在OpenSceneGraph中进行显示。有了这些离散数据,就可以不用OpenCascade的显示模块了,可以使用其他显示引擎对形状进行显示。即若要线框模式显示形状时,就绘制离散形状拓朴边后得到的多段线;若要实体渲染模式显示形状时,就绘制离散形状拓朴面得到的三角网格。理解这些概念也有助于理解显示模块的实现,及拓朴数据中包含的几何数据的意义。
Opencascade中的模型是Brep结构,可以通过三角化后,利用VTK的渲染引擎进行显示,从而能够利用几何内核和网格处理算法的共同优点。
以前看《计算机图形学》相关的书时,从数学概念到具体实现的桥梁总是无法衔接。现在,通过学习OpenCascade,终于把这些都串起来了。正如上面网友所说,面和边要在OpenGL中显示出来就需要离散化,即把边离散为多段线,把面离散为三角网格。这样就可以把用参数精确表示的几何数据在计算机布满像素点的屏幕上逼近显示了。
本文通过程序实例,将OpenCascade中的拓朴数据(边、面)离散化后在VTK中进行显示。有了这些离散数据,就可以不用OpenCascade的显示模块了,可以使用其他显示引擎对形状进行显示。即若要线框模式显示形状时,就绘制离散形状拓朴边后得到的多段线;若要实体渲染模式显示形状时,就绘制离散形状拓朴面得到的三角网格。理解这些概念也有助于理解显示模块的实现,及拓朴数据中包含的几何数据的意义。
以下通过一个具体程序实例,来对OpenCascade中的拓朴边和拓朴面进行离散化。
// sampleExample.cpp : Defines the entry point for the console application. // #include "stdafx.h" // OpenCascade library. #include <gp_Circ.hxx> #include <gp_Elips.hxx> #include <gp_Sphere.hxx> #include <Poly_Polygon3D.hxx> #include <Poly_Triangulation.hxx> #include <TopoDS_Edge.hxx> #include <TopoDS_Face.hxx> #include <BRep_Tool.hxx> #include <BRepMesh.hxx> #include <BRepBuilderAPI_MakeEdge.hxx> #include <BRepBuilderAPI_MakeFace.hxx> //vtk lib #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 <vtkTriangle.h> #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") /* * @breif Descret the shape: face. * For Face will be discreted to triangles; (BRepMesh_FastDiscret) * To get the triangles of the face, use BRep_Tool::Triangulation(Face, L); */ void BuildMesh(vtkRenderer* render, const TopoDS_Face& face, double deflection = 0.1) { TopLoc_Location location; BRepMesh::Mesh(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> cells = vtkSmartPointer<vtkCellArray>::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<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 array cells->InsertNextCell(triangle); id++; } polyData->SetPoints(points); polyData->SetPolys(cells); vtkSmartPointer<vtkPolyDataMapper> sourceMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); sourceMapper->SetInput(polyData); vtkSmartPointer<vtkActor> sourceActor = vtkSmartPointer<vtkActor>::New(); sourceActor->SetMapper(sourceMapper); sourceActor->GetProperty()->SetColor(1,0,0); render->AddActor(sourceActor); }
/* build the scene to visualization */ void BuildScene(vtkRenderer* render) { gp_Ax2 axis; // 1. Test sphere face while deflection is default 0.1. axis.SetLocation(gp_Pnt(26.0, 0.0, 0.0)); TopoDS_Face sphereFace1 = BRepBuilderAPI_MakeFace(gp_Sphere(axis, 8.0)); BuildMesh(render, sphereFace1); // 2. Test sphere face while deflection is 2.0. axis.SetLocation(gp_Pnt(26.0, 18.0, 0.0)); TopoDS_Face sphereFace2 = BRepBuilderAPI_MakeFace(gp_Sphere(axis, 8.0)); BuildMesh(render,sphereFace2, 2.0); // 3. Test sphere face while deflection is 0.001. axis.SetLocation(gp_Pnt(26.0, -18.0, 0.0)); TopoDS_Face sphereFace3 = BRepBuilderAPI_MakeFace(gp_Sphere(axis, 8.0)); BuildMesh(render, sphereFace3, 0.001); } int _tmain(int argc, _TCHAR* argv[]) { // Create a renderer, render window, and interactor vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow); //build mesh BuildScene(renderer); renderer->SetBackground(1,1,1); // Render and interact renderWindow->Render(); renderWindowInteractor->Start(); return 0; }