Esempio n. 1
0
bool NETGENPlugin_NETGEN_3D::Compute(SMESH_Mesh& aMesh,
                                     SMESH_MesherHelper* aHelper)
{
    MESSAGE("NETGENPlugin_NETGEN_3D::Compute with maxElmentsize = " << _maxElementVolume);
    const int invalid_ID = -1;
    bool _quadraticMesh = false;
    typedef map< const SMDS_MeshNode*, int, TIDCompare > TNodeToIDMap;
    TNodeToIDMap nodeToNetgenID;
    list< const SMDS_MeshElement* > triangles;
    SMESHDS_Mesh* MeshDS = aHelper->GetMeshDS();

    SMESH_MesherHelper::MType MeshType = aHelper->IsQuadraticMesh();

    if(MeshType == SMESH_MesherHelper::COMP)
        return error( COMPERR_BAD_INPUT_MESH,
                      SMESH_Comment("Mesh with linear and quadratic elements given."));
    else if (MeshType == SMESH_MesherHelper::QUADRATIC)
        _quadraticMesh = true;

    StdMeshers_QuadToTriaAdaptor Adaptor;
    Adaptor.Compute(aMesh);

    SMDS_FaceIteratorPtr fIt = MeshDS->facesIterator();
    TIDSortedElemSet sortedFaces; //  0020279: control the "random" use when using mesh algorithms
    while( fIt->more()) sortedFaces.insert( fIt->next() );

    TIDSortedElemSet::iterator itFace = sortedFaces.begin(), fEnd = sortedFaces.end();
    for ( ; itFace != fEnd; ++itFace ) {
        // check element
        const SMDS_MeshElement* elem = *itFace;
        if ( !elem )
            return error( COMPERR_BAD_INPUT_MESH, "Null element encounters");
        bool isTraingle = ( elem->NbNodes()==3 || (_quadraticMesh && elem->NbNodes()==6 ));
        if ( !isTraingle ) {
            //return error( COMPERR_BAD_INPUT_MESH,
            //              SMESH_Comment("Not triangle element ")<<elem->GetID());
            // using adaptor
            const list<const SMDS_FaceOfNodes*>* faces = Adaptor.GetTriangles(elem);
            if(faces==0) {
                return error( COMPERR_BAD_INPUT_MESH,
                              SMESH_Comment("Not triangles in adaptor for element ")<<elem->GetID());
            }
            list<const SMDS_FaceOfNodes*>::const_iterator itf = faces->begin();
            for(; itf!=faces->end(); itf++ ) {
                triangles.push_back( (*itf) );
                // put triange's nodes to nodeToNetgenID map
                SMDS_ElemIteratorPtr triangleNodesIt = (*itf)->nodesIterator();
                while ( triangleNodesIt->more() ) {
                    const SMDS_MeshNode * node =
                        static_cast<const SMDS_MeshNode *>(triangleNodesIt->next());
                    if(aHelper->IsMedium(node))
                        continue;
                    nodeToNetgenID.insert( make_pair( node, invalid_ID ));
                }
            }
        }
        else {
            // keep a triangle
            triangles.push_back( elem );
            // put elem nodes to nodeToNetgenID map
            SMDS_ElemIteratorPtr triangleNodesIt = elem->nodesIterator();
            while ( triangleNodesIt->more() ) {
                const SMDS_MeshNode * node =
                    static_cast<const SMDS_MeshNode *>(triangleNodesIt->next());
                if(aHelper->IsMedium(node))
                    continue;
                nodeToNetgenID.insert( make_pair( node, invalid_ID ));
            }
        }
    }

    // ---------------------------------
    // Feed the Netgen with surface mesh
    // ---------------------------------

    int Netgen_NbOfNodes = 0;
    int Netgen_param2ndOrder = 0;
    double Netgen_paramFine = 1.;
    double Netgen_paramSize = pow( 72, 1/6. ) * pow( _maxElementVolume, 1/3. );

    double Netgen_point[3];
    int Netgen_triangle[3];
    int Netgen_tetrahedron[4];

    Ng_Init();

    Ng_Mesh * Netgen_mesh = Ng_NewMesh();

    // set nodes and remember thier netgen IDs

    TNodeToIDMap::iterator n_id = nodeToNetgenID.begin();
    for ( ; n_id != nodeToNetgenID.end(); ++n_id )
    {
        const SMDS_MeshNode* node = n_id->first;

        Netgen_point [ 0 ] = node->X();
        Netgen_point [ 1 ] = node->Y();
        Netgen_point [ 2 ] = node->Z();
        Ng_AddPoint(Netgen_mesh, Netgen_point);
        n_id->second = ++Netgen_NbOfNodes; // set netgen ID

    }

    // set triangles
    list< const SMDS_MeshElement* >::iterator tria = triangles.begin();
    for ( ; tria != triangles.end(); ++tria)
    {
        int i = 0;
        SMDS_ElemIteratorPtr triangleNodesIt = (*tria)->nodesIterator();
        while ( triangleNodesIt->more() ) {
            const SMDS_MeshNode * node =
                static_cast<const SMDS_MeshNode *>(triangleNodesIt->next());
            if(aHelper->IsMedium(node))
                continue;
            Netgen_triangle[ i ] = nodeToNetgenID[ node ];
            ++i;
        }

        Ng_AddSurfaceElement(Netgen_mesh, NG_TRIG, Netgen_triangle);
    }

    // -------------------------
    // Generate the volume mesh
    // -------------------------

    Ng_Meshing_Parameters Netgen_param;

    Netgen_param.secondorder = Netgen_param2ndOrder;
    Netgen_param.fineness = Netgen_paramFine;
    Netgen_param.maxh = Netgen_paramSize;

    Ng_Result status;

    try {
#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
        OCC_CATCH_SIGNALS;
#endif
        status = Ng_GenerateVolumeMesh(Netgen_mesh, &Netgen_param);
    }
    catch (Standard_Failure& exc) {
        error(COMPERR_OCC_EXCEPTION, exc.GetMessageString());
        status = NG_VOLUME_FAILURE;
    }
    catch (...) {
        error("Bad mesh input!!!");
        status = NG_VOLUME_FAILURE;
    }
    if ( GetComputeError()->IsOK() ) {
        error( status, "Bad mesh input!!!");
    }

    int Netgen_NbOfNodesNew = Ng_GetNP(Netgen_mesh);

    int Netgen_NbOfTetra = Ng_GetNE(Netgen_mesh);

    MESSAGE("End of Volume Mesh Generation. status=" << status <<
            ", nb new nodes: " << Netgen_NbOfNodesNew - Netgen_NbOfNodes <<
            ", nb tetra: " << Netgen_NbOfTetra);

    // -------------------------------------------------------------------
    // Feed back the SMESHDS with the generated Nodes and Volume Elements
    // -------------------------------------------------------------------

    bool isOK = ( Netgen_NbOfTetra > 0 );// get whatever built
    if ( isOK )
    {
        // vector of nodes in which node index == netgen ID
        vector< const SMDS_MeshNode* > nodeVec ( Netgen_NbOfNodesNew + 1 );
        // insert old nodes into nodeVec
        for ( n_id = nodeToNetgenID.begin(); n_id != nodeToNetgenID.end(); ++n_id ) {
            nodeVec.at( n_id->second ) = n_id->first;
        }
        // create and insert new nodes into nodeVec
        int nodeIndex = Netgen_NbOfNodes + 1;

        for ( ; nodeIndex <= Netgen_NbOfNodesNew; ++nodeIndex )
        {
            Ng_GetPoint( Netgen_mesh, nodeIndex, Netgen_point );
            SMDS_MeshNode * node = aHelper->AddNode(Netgen_point[0],
                                                    Netgen_point[1],
                                                    Netgen_point[2]);
            nodeVec.at(nodeIndex) = node;
        }

        // create tetrahedrons
        for ( int elemIndex = 1; elemIndex <= Netgen_NbOfTetra; ++elemIndex )
        {
            Ng_GetVolumeElement(Netgen_mesh, elemIndex, Netgen_tetrahedron);
            aHelper->AddVolume (nodeVec.at( Netgen_tetrahedron[0] ),
                                nodeVec.at( Netgen_tetrahedron[1] ),
                                nodeVec.at( Netgen_tetrahedron[2] ),
                                nodeVec.at( Netgen_tetrahedron[3] ));
        }
    }

    Ng_DeleteMesh(Netgen_mesh);
    Ng_Exit();

    NETGENPlugin_Mesher::RemoveTmpFiles();

    return (status == NG_OK);
}
Esempio n. 2
0
void ViewProviderFEMMeshBuilder::createMesh(const App::Property* prop, SoCoordinate3* coords, SoIndexedFaceSet* faces) const
{
    const Fem::PropertyFemMesh* mesh = static_cast<const Fem::PropertyFemMesh*>(prop);

    SMESHDS_Mesh* data = const_cast<SMESH_Mesh*>(mesh->getValue().getSMesh())->GetMeshDS();
    const SMDS_MeshInfo& info = data->GetMeshInfo();
    int numNode = info.NbNodes();
    int numTria = info.NbTriangles();
    int numQuad = info.NbQuadrangles();
    //int numPoly = info.NbPolygons();
    //int numVolu = info.NbVolumes();
    int numTetr = info.NbTetras();
    //int numHexa = info.NbHexas();
    //int numPyrd = info.NbPyramids();
    //int numPris = info.NbPrisms();
    //int numHedr = info.NbPolyhedrons();

    int index=0;
    std::map<const SMDS_MeshNode*, int> mapNodeIndex;

    // set the point coordinates
    coords->point.setNum(numNode);
    SMDS_NodeIteratorPtr aNodeIter = data->nodesIterator();
    unsigned int i=0;
    SbVec3f* verts = coords->point.startEditing();
    for (;aNodeIter->more();) {
        const SMDS_MeshNode* aNode = aNodeIter->next();
        verts[i++].setValue((float)aNode->X(),(float)aNode->Y(),(float)aNode->Z());
        mapNodeIndex[aNode] = index++;
    }
    coords->point.finishEditing();

    // set the face indices
    index=0;
    faces->coordIndex.setNum(4*numTria + 5*numQuad + 16*numTetr);
    int32_t* indices = faces->coordIndex.startEditing();
    SMDS_FaceIteratorPtr aFaceIter = data->facesIterator();
    for (;aFaceIter->more();) {
        const SMDS_MeshFace* aFace = aFaceIter->next();
        int num = aFace->NbNodes();
        if (num != 3 && num != 4)
            continue;
        for (int j=0; j<num;j++) {
            const SMDS_MeshNode* node = aFace->GetNode(j);
            indices[index++] = mapNodeIndex[node];
        }
        indices[index++] = SO_END_FACE_INDEX;
    }
    SMDS_VolumeIteratorPtr aVolIter = data->volumesIterator();
    for (;aVolIter->more();) {
        const SMDS_MeshVolume* aVol = aVolIter->next();
        int num = aVol->NbNodes();
        if (num != 4)
            continue;
        int i1 = mapNodeIndex[aVol->GetNode(0)];
        int i2 = mapNodeIndex[aVol->GetNode(1)];
        int i3 = mapNodeIndex[aVol->GetNode(2)];
        int i4 = mapNodeIndex[aVol->GetNode(3)];
        indices[index++] = i1;
        indices[index++] = i3;
        indices[index++] = i2;
        indices[index++] = SO_END_FACE_INDEX;
        indices[index++] = i1;
        indices[index++] = i2;
        indices[index++] = i4;
        indices[index++] = SO_END_FACE_INDEX;
        indices[index++] = i1;
        indices[index++] = i4;
        indices[index++] = i3;
        indices[index++] = SO_END_FACE_INDEX;
        indices[index++] = i2;
        indices[index++] = i3;
        indices[index++] = i4;
        indices[index++] = SO_END_FACE_INDEX;
    }
    faces->coordIndex.finishEditing();
}