# Mesh Data Structure in OpenCascade

eryar@163.com

### 一、引言 Introduction

1．Wavefront OBJ(*.obj)

2．3D Max(*.max, *.3ds)

3．VRML(*.vrl)

4．Inventor(*.iv)

5．PLY(*.ply, *.ply2)

6．STL(*.stl)

7．Off(*.off) in CGAL library

Figure 1.1 Wavefront OBJ File Format

l Vertices

n 以‘V’开始；

n 其后为坐标值（x,y,z）；

l Faces

n 以‘F’开始；

n 其后为面的顶点索引值；

l Other properties

n Normal, texture coordinates, material, etc.

### 二、三角网格的表示 Mesh Data Structure

```struct Triangle
{
Vector3 p[3];
};

struct TriangleMesh
{
int triCount;
Triangle* triList;
};```

l 顶点。每个三角形有三个顶点，各顶点都有可能和其他三角形共享；

l 边。连接两个顶点的边，每个三角形有三条边；

l 面。每个三角形对应一个面。我们可以用顶点或边列表表示面；

n 顶点缓存；

n 三角带Triangle Strip；

n 三角扇Triangle Fan；

### 三、程序示例 Code Example

```/*
*
*        File    : Main.cpp
*        Author  : eryar@163.com
*        Date    : 2013-08-10 18:02
*        Version : V1.0
*
*    Description : Mesh Viewer for the general mesh file format.
*                  Poly_Triangulation data structure can save vertices and triangle index.
*
*/

// OpenSceneGraph library.
#include <osgViewer/Viewer>
#include <osgGA/StateSetManipulator>
#include <osgViewer/ViewerEventHandlers>

#pragma comment(lib, \"osgd.lib\")
#pragma comment(lib, \"osgDBd.lib\")
#pragma comment(lib, \"osgViewerd.lib\")

#include <TColgp_Array1OfPnt.hxx>
#include <Poly_Array1OfTriangle.hxx>
#include <Poly_Triangulation.hxx>

#pragma comment(lib, \"TKernel.lib\")
#pragma comment(lib, \"TKMath.lib\")

/**
* @breif Build the mesh from *.off file.
*/
osg::Node* buildMesh(const std::string& fileName)
{
std::ifstream offFile(fileName.c_str());
std::string strBuffer;

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();

Standard_Integer nbNodes = 0;
Standard_Integer nbTriangles = 0;

// Ignore \"OFF\"
offFile>>strBuffer;
offFile>>nbNodes>>nbTriangles>>strBuffer;

TColgp_Array1OfPnt nodes(0, nbNodes);
Poly_Array1OfTriangle triangles(0, nbTriangles);

// Read node coordinate and store them.
Standard_Real dx = 0.0;
Standard_Real dy = 0.0;
Standard_Real dz = 0.0;

for (Standard_Integer i = 0; i < nbNodes; i++)
{
offFile>>dx>>dy>>dz;

nodes(i).SetCoord(dx, dy, dz);
}

Standard_Integer ni = 0;
Standard_Integer n1 = 0;
Standard_Integer n2 = 0;
Standard_Integer n3 = 0;

for (Standard_Integer i = 0; i < nbTriangles; i++)
{
offFile>>ni>>n1>>n2>>n3;

triangles(i).Set(n1, n2, n3);
}

// Construct the mesh data by Poly_Triangulation.
gp_Pnt node1;
gp_Pnt node2;
gp_Pnt node3;
Poly_Triangle triangle;
Handle_Poly_Triangulation T = new Poly_Triangulation(nodes, triangles);

for (Standard_Integer i = 0; i < nbTriangles; i++)
{
triangle = triangles.Value(i);

triangle.Get(n1, n2, n3);

node1 = nodes.Value(n1);
node2 = nodes.Value(n2);
node3 = nodes.Value(n3);

gp_XYZ vector12(node2.XYZ() - node1.XYZ());
gp_XYZ vector13(node3.XYZ() - node1.XYZ());
gp_XYZ normal = vector12.Crossed(vector13);
Standard_Real rModulus = normal.Modulus();

if (rModulus > gp::Resolution())
{
normal.Normalize();
}
else
{
normal.SetCoord(0., 0., 0.);
}

vertices->push_back(osg::Vec3(node1.X(), node1.Y(), node1.Z()));
vertices->push_back(osg::Vec3(node2.X(), node2.Y(), node2.Z()));
vertices->push_back(osg::Vec3(node3.X(), node3.Y(), node3.Z()));

normals->push_back(osg::Vec3(normal.X(), normal.Y(),normal.Z()));
}

triGeom->setVertexArray(vertices.get());
triGeom->setNormalArray(normals);
triGeom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE);

return geode.release();
}

int main(int argc, char* argv[])
{
osgViewer::Viewer myViewer;

std::string strFile;

(argc > 1) ? strFile = argv[1] : strFile = \"ChineseDragon-10kv.off\";

myViewer.setSceneData(buildMesh(strFile));

return myViewer.run();
}```

Figure 3.1 ChineseDragon-10kv.off

Figure 3.2 Camel.off

Figure 3.3 cow.off

Figure 3.4 elephant.off

Figure 3.5 man.off

Figure 3.6 pinion.off

Figure 3.7 spool.off

Figure 3.8 bones.off

Figure 3.9 couplingdown.off

Figure 3.10 rotor.off

Figure 3.11 joint.off

Figure 3.12 knot1.off

Figure 3.13 anchor.off

Figure 3.14 mushroom.off

Figure 3.15 sphere.off

Figure 3.16 star.off