首页 > 其他分享 >解析OCC几何体(TopoDS_Shape)生成自定义off文件(转)

解析OCC几何体(TopoDS_Shape)生成自定义off文件(转)

时间:2022-08-29 10:48:00浏览次数:86  
标签:off 自定义 int iter 几何体 vt triFace Standard osg

void ParseTopoDSShapeToOff(const TopoDS_Shape& aShape)
{
    //0计算顶点和顶点索引//
    std::vector<osg::Vec3d> vertexArray;
    BRepMesh_IncrementalMesh(aShape, 0.001);//BRepMesh::Mesh(aShape, 3);//6.8.0后删除了
 
    gp_Pnt vertex1;
    gp_Pnt vertex2;
    gp_Pnt vertex3;
 
    Standard_Integer nVertexIndex1 = 0;
    Standard_Integer nVertexIndex2 = 0;
    Standard_Integer nVertexIndex3 = 0;
 
    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);
        if (triFace.IsNull())
        {
            continue;
        }
 
        Standard_Boolean hasNormal = triFace->HasNormals();
        Standard_Boolean hasuvNormal = triFace->HasUVNodes();
        Standard_Integer l = triFace->Nodes().Length();
        Standard_Integer nTriangles = triFace->NbTriangles();
 
        TColgp_Array1OfPnt nodes(1, triFace->NbNodes());
        Poly_Array1OfTriangle triangles(1, triFace->NbTriangles());
        nodes = triFace->Nodes();
        triangles = triFace->Triangles();
 
        for (Standard_Integer i = 1; 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);
 
            vertexArray.push_back(osg::Vec3(vertex1.X(), vertex1.Y(), vertex1.Z()));
            vertexArray.push_back(osg::Vec3(vertex2.X(), vertex2.Y(), vertex2.Z()));
            vertexArray.push_back(osg::Vec3(vertex3.X(), vertex3.Y(), vertex3.Z()));
        }
    }
 
    //顶点去重
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
    {
        int nums = vertexArray.size();
        for (unsigned int i = 0; i < nums; i++)
        {
            osg::Vec3d vt = vertexArray.at(i);
 
            bool bFind = false;//是否存在
            osg::Vec3Array::iterator iter = vertices->begin();
            for (; iter != vertices->end(); ++iter)
            {
                osg::Vec3d _vt = *iter;
                if (_vt == vt)
                {
                    bFind = true;
                    break;
                }
            }
 
            if (!bFind)
            {
                vertices->push_back(vt);
            }
        }
    }
    int nums = vertices->size();
 
    //获取索引
    osg::ref_ptr<osg::DrawElementsUInt> arrayIndexs = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES);
    {
        int nums = vertexArray.size();
        for (unsigned int i = 0; i < nums; i += 3)
        {
            bool bFind0 = false;
            bool bFind1 = false;
            bool bFind2 = false;
            int index0 = 0;
            int index1 = 0;
            int index2 = 0;
 
            osg::Vec3d vt0 = vertexArray.at(i);
            osg::Vec3d vt1 = vertexArray.at(i + 1);
            osg::Vec3d vt2 = vertexArray.at(i + 2);
 
            //从去重后的顶点中获取索引
            osg::Vec3Array::iterator iter = vertices->begin();
            for (int index = 0; iter != vertices->end(); ++iter, ++index)
            {
                osg::Vec3d _vt = *iter;
                if (_vt == vt0)
                {
                    bFind0 = true;
                    index0 = index;//第1个顶点索引
                }
                else if (_vt == vt1) {
                    bFind1 = true;
                    index1 = index;//第2个顶点索引
                }
                else if (_vt == vt2) {
                    bFind2 = true;
                    index2 = index;//第3个顶点索引
                }
 
                if (bFind0 && bFind1 && bFind2)
                {
                    arrayIndexs->push_back(index0);
                    arrayIndexs->push_back(index1);
                    arrayIndexs->push_back(index2);
                    break;
                }
            }
        }
    }
 
    {
        //写OFF
        WirteOffFile("D:data/off/test.off", vertices, arrayIndexs);
    }
}

转自:https://blog.csdn.net/weixin_43712770/article/details/100972804

标签:off,自定义,int,iter,几何体,vt,triFace,Standard,osg
From: https://www.cnblogs.com/mrliu0515/p/16635044.html

相关文章