Salome HOME
Enable C++0x/C++11 support
[modules/smesh.git] / src / SMESH / SMESH_MeshEditor.cxx
index ae309699d92f8cc18edc19114bb69569ca321e75..5e71716608b13cfd71def12c74f3216c6834d989 100644 (file)
@@ -4816,7 +4816,7 @@ void SMESH_MeshEditor::sweepElement(const SMDS_MeshElement*               elem,
         else if(nbSame==1) {
           // ---> pyramid + pentahedron - can not be created since it is needed
           // additional middle node at the center of face
-          INFOS( " Sweep for face " << elem->GetID() << " can not be created" );
+          //INFOS( " Sweep for face " << elem->GetID() << " can not be created" );
           return;
         }
         else if( nbSame == 2 ) {
@@ -5002,16 +5002,19 @@ void SMESH_MeshEditor::makeWalls (TNodeOfNodeListMap &     mapNewNodes,
     SMDSAbs_ElementType highType = SMDSAbs_Edge; // count most complex elements only
     while ( eIt->more() && nbInitElems < 2 ) {
       const SMDS_MeshElement* e = eIt->next();
-      SMDSAbs_ElementType type = e->GetType();
-      if ( type == SMDSAbs_Volume || type < highType ) continue;
+      SMDSAbs_ElementType  type = e->GetType();
+      if ( type == SMDSAbs_Volume ||
+           type < highType ||
+           !elemSet.count(e))
+        continue;
       if ( type > highType ) {
         nbInitElems = 0;
-        highType = type;
+        highType    = type;
       }
       el = e;
-      nbInitElems += elemSet.count(el);
+      ++nbInitElems;
     }
-    if ( nbInitElems < 2 ) {
+    if ( nbInitElems == 1 ) {
       bool NotCreateEdge = el && el->IsMediumNode(node);
       if(!NotCreateEdge) {
         vector<TNodeOfNodeListMapItr> newNodesItVec( 1, nList );
@@ -5983,6 +5986,10 @@ SMESH_MeshEditor::ExtrusionAlongTrack (TIDSortedElemSet     theElements[2],
   ASSERT( theTrack );
 
   SMESHDS_SubMesh* pSubMeshDS = theTrack->GetSubMeshDS();
+  if ( !pSubMeshDS )
+    return ExtrusionAlongTrack( theElements, theTrack->GetFather(), theN1,
+                                theHasAngles, theAngles, theLinearVariation,
+                                theHasRefPoint, theRefPoint, theMakeGroups );
 
   aItE = pSubMeshDS->GetElements();
   while ( aItE->more() ) {
@@ -6386,7 +6393,7 @@ SMESH_MeshEditor::ExtrusionAlongTrack (TIDSortedElemSet     theElements[2],
       SMESH_MeshEditor_PathPoint PP2 = currList.front();
       gp_Dir D1 = PP1.Tangent();
       gp_Dir D2 = PP2.Tangent();
-      gp_Dir Dnew( ( D1.XYZ() + D2.XYZ() ) / 2 );
+      gp_Dir Dnew( D1.XYZ() + D2.XYZ() );
       PP1.SetTangent(Dnew);
       fullList.push_back(PP1);
       fullList.splice( fullList.end(), currList, ++currList.begin(), currList.end() );
@@ -6451,7 +6458,7 @@ SMESH_MeshEditor::MakeEdgePathPoints(std::list<double>&                aPrms,
     aL2 = aVec.SquareMagnitude();
     if ( aL2 < aTolVec2 )
       return EXTR_CANT_GET_TANGENT;
-    gp_Dir aTgt( aVec );
+    gp_Dir aTgt( FirstIsStart ? aVec : -aVec );
     aPP.SetPnt( aP3D );
     aPP.SetTangent( aTgt );
     aPP.SetParameter( aT );
@@ -6476,9 +6483,11 @@ SMESH_MeshEditor::MakeExtrElements(TIDSortedElemSet                  theElemSets
                                    const bool                        theMakeGroups)
 {
   const int aNbTP = fullList.size();
+
   // Angles
   if( theHasAngles && !theAngles.empty() && theLinearVariation )
     LinearAngleVariation(aNbTP-1, theAngles);
+
   // fill vector of path points with angles
   vector<SMESH_MeshEditor_PathPoint> aPPs;
   list<SMESH_MeshEditor_PathPoint>::iterator itPP = fullList.begin();
@@ -6508,10 +6517,10 @@ SMESH_MeshEditor::MakeExtrElements(TIDSortedElemSet                  theElemSets
     {
       TIDSortedElemSet& theElements = theElemSets[ is2ndSet ];
       itElem = theElements.begin();
-      for ( ; itElem != theElements.end(); itElem++ ) {
+      for ( ; itElem != theElements.end(); itElem++ )
+      {
         const SMDS_MeshElement* elem = *itElem;
-
-        SMDS_ElemIteratorPtr itN = elem->nodesIterator();
+        SMDS_ElemIteratorPtr     itN = elem->nodesIterator();
         while ( itN->more() ) {
           const SMDS_MeshElement* node = itN->next();
           if ( newNodes.insert( node ).second )
@@ -6525,19 +6534,15 @@ SMESH_MeshEditor::MakeExtrElements(TIDSortedElemSet                  theElemSets
 
   // 4. Processing the elements
   SMESHDS_Mesh* aMesh = GetMeshDS();
+  list<const SMDS_MeshNode*> emptyList;
 
   setElemsFirst( theElemSets );
   for ( int is2ndSet = 0; is2ndSet < 2; ++is2ndSet )
   {
     TIDSortedElemSet& theElements = theElemSets[ is2ndSet ];
-    for ( itElem = theElements.begin(); itElem != theElements.end(); itElem++ ) {
-      // check element type
+    for ( itElem = theElements.begin(); itElem != theElements.end(); itElem++ )
+    {
       const SMDS_MeshElement* elem = *itElem;
-      if ( !elem )
-        continue;
-      // SMDSAbs_ElementType aTypeE = elem->GetType();
-      // if ( aTypeE != SMDSAbs_Face && aTypeE != SMDSAbs_Edge )
-      //   continue;
 
       vector<TNodeOfNodeListMapItr> & newNodesItVec = mapElemNewNodes[ elem ];
       newNodesItVec.reserve( elem->NbNodes() );
@@ -6549,13 +6554,11 @@ SMESH_MeshEditor::MakeExtrElements(TIDSortedElemSet                  theElemSets
       {
         ++nodeIndex;
         // check if a node has been already processed
-        const SMDS_MeshNode* node =
-          static_cast<const SMDS_MeshNode*>( itN->next() );
-        TNodeOfNodeListMap::iterator nIt = mapNewNodes.find( node );
-        if ( nIt == mapNewNodes.end() ) {
-          nIt = mapNewNodes.insert( make_pair( node, list<const SMDS_MeshNode*>() )).first;
-          list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
-
+        const SMDS_MeshNode* node = cast2Node( itN->next() );
+        TNodeOfNodeListMap::iterator nIt = mapNewNodes.insert( make_pair( node, emptyList )).first;
+        list<const SMDS_MeshNode*>& listNewNodes = nIt->second;
+        if ( listNewNodes.empty() )
+        {
           // make new nodes
           Standard_Real aAngle1x, aAngleT1T0, aTolAng;
           gp_Pnt aP0x, aP1x, aPN0, aPN1, aV0x, aV1x;
@@ -6570,7 +6573,6 @@ SMESH_MeshEditor::MakeExtrElements(TIDSortedElemSet                  theElemSets
           const SMESH_MeshEditor_PathPoint& aPP0 = aPPs[0];
           aP0x = aPP0.Pnt();
           aDT0x= aPP0.Tangent();
-          //cout<<"j = 0   PP: Pnt("<<aP0x.X()<<","<<aP0x.Y()<<","<<aP0x.Z()<<")"<<endl;
 
           for ( int j = 1; j < aNbTP; ++j ) {
             const SMESH_MeshEditor_PathPoint& aPP1 = aPPs[j];
@@ -6589,7 +6591,8 @@ SMESH_MeshEditor::MakeExtrElements(TIDSortedElemSet                  theElemSets
 
             // rotation 1 [ T1,T0 ]
             aAngleT1T0=-aDT1x.Angle( aDT0x );
-            if (fabs(aAngleT1T0) > aTolAng) {
+            if (fabs(aAngleT1T0) > aTolAng)
+            {
               aDT1T0=aDT1x^aDT0x;
               anAxT1T0.SetLocation( aV1x );
               anAxT1T0.SetDirection( aDT1T0 );
@@ -6608,13 +6611,11 @@ SMESH_MeshEditor::MakeExtrElements(TIDSortedElemSet                  theElemSets
             }
 
             // make new node
-            //MESSAGE("elem->IsQuadratic " << elem->IsQuadratic() << " " << elem->IsMediumNode(node));
-            if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
+            if ( elem->IsQuadratic() && !elem->IsMediumNode(node) )
+            {
               // create additional node
-              double x = ( aPN1.X() + aPN0.X() )/2.;
-              double y = ( aPN1.Y() + aPN0.Y() )/2.;
-              double z = ( aPN1.Z() + aPN0.Z() )/2.;
-              const SMDS_MeshNode* newNode = aMesh->AddNode(x,y,z);
+              gp_XYZ midP = 0.5 * ( aPN1.XYZ() + aPN0.XYZ() );
+              const SMDS_MeshNode* newNode = aMesh->AddNode( midP.X(), midP.Y(), midP.Z() );
               myLastCreatedNodes.Append(newNode);
               srcNodes.Append( node );
               listNewNodes.push_back( newNode );
@@ -6630,42 +6631,40 @@ SMESH_MeshEditor::MakeExtrElements(TIDSortedElemSet                  theElemSets
             aDT0x = aDT1x;
           }
         }
-
-        else {
+        else if( elem->IsQuadratic() && !elem->IsMediumNode(node) )
+        {
           // if current elem is quadratic and current node is not medium
           // we have to check - may be it is needed to insert additional nodes
-          if( elem->IsQuadratic() && !elem->IsMediumNode(node) ) {
-            list< const SMDS_MeshNode* > & listNewNodes = nIt->second;
-            if(listNewNodes.size()==aNbTP-1) {
-              vector<const SMDS_MeshNode*> aNodes(2*(aNbTP-1));
-              gp_XYZ P(node->X(), node->Y(), node->Z());
-              list< const SMDS_MeshNode* >::iterator it = listNewNodes.begin();
-              int i;
-              for(i=0; i<aNbTP-1; i++) {
-                const SMDS_MeshNode* N = *it;
-                double x = ( N->X() + P.X() )/2.;
-                double y = ( N->Y() + P.Y() )/2.;
-                double z = ( N->Z() + P.Z() )/2.;
-                const SMDS_MeshNode* newN = aMesh->AddNode(x,y,z);
-                srcNodes.Append( node );
-                myLastCreatedNodes.Append(newN);
-                aNodes[2*i] = newN;
-                aNodes[2*i+1] = N;
-                P = gp_XYZ(N->X(),N->Y(),N->Z());
-              }
-              listNewNodes.clear();
-              for(i=0; i<2*(aNbTP-1); i++) {
-                listNewNodes.push_back(aNodes[i]);
-              }
+          list< const SMDS_MeshNode* > & listNewNodes = nIt->second;
+          if ( listNewNodes.size() == aNbTP-1 )
+          {
+            vector<const SMDS_MeshNode*> aNodes(2*(aNbTP-1));
+            gp_XYZ P(node->X(), node->Y(), node->Z());
+            list< const SMDS_MeshNode* >::iterator it = listNewNodes.begin();
+            int i;
+            for(i=0; i<aNbTP-1; i++) {
+              const SMDS_MeshNode* N = *it;
+              double x = ( N->X() + P.X() )/2.;
+              double y = ( N->Y() + P.Y() )/2.;
+              double z = ( N->Z() + P.Z() )/2.;
+              const SMDS_MeshNode* newN = aMesh->AddNode(x,y,z);
+              srcNodes.Append( node );
+              myLastCreatedNodes.Append(newN);
+              aNodes[2*i] = newN;
+              aNodes[2*i+1] = N;
+              P = gp_XYZ(N->X(),N->Y(),N->Z());
+            }
+            listNewNodes.clear();
+            for(i=0; i<2*(aNbTP-1); i++) {
+              listNewNodes.push_back(aNodes[i]);
             }
           }
         }
 
         newNodesItVec.push_back( nIt );
       }
+
       // make new elements
-      //sweepElement( aMesh, elem, newNodesItVec, newElemsMap[elem],
-      //              newNodesItVec[0]->second.size(), myLastCreatedElems );
       sweepElement( elem, newNodesItVec, newElemsMap[elem], aNbTP-1, srcElems );
     }
   }
@@ -7244,19 +7243,17 @@ int SMESH_MeshEditor::SimplifyFace (const vector<const SMDS_MeshNode *>& faceNod
   set<const SMDS_MeshNode*> nodeSet;
 
   // get simple seq of nodes
-  //const SMDS_MeshNode* simpleNodes[ nbNodes ];
   vector<const SMDS_MeshNode*> simpleNodes( nbNodes );
-  int iSimple = 0, nbUnique = 0;
+  int iSimple = 0;
 
   simpleNodes[iSimple++] = faceNodes[0];
-  nbUnique++;
   for (int iCur = 1; iCur < nbNodes; iCur++) {
     if (faceNodes[iCur] != simpleNodes[iSimple - 1]) {
       simpleNodes[iSimple++] = faceNodes[iCur];
-      if (nodeSet.insert( faceNodes[iCur] ).second)
-        nbUnique++;
+      nodeSet.insert( faceNodes[iCur] );
     }
   }
+  int nbUnique = nodeSet.size();
   int nbSimple = iSimple;
   if (simpleNodes[nbSimple - 1] == simpleNodes[0]) {
     nbSimple--;
@@ -7370,8 +7367,8 @@ void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
   for ( ; eIt != elems.end(); eIt++ )
   {
     const SMDS_MeshElement* elem = *eIt;
-    int nbNodes = elem->NbNodes();
-    int aShapeId = FindShape( elem );
+    const           int  nbNodes = elem->NbNodes();
+    const           int aShapeId = FindShape( elem );
 
     nodeSet.clear();
     curNodes.resize( nbNodes );
@@ -7456,6 +7453,8 @@ void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
                     ( SMDS_MeshCell::interlacedSmdsOrder( SMDSEntity_Quad_Polygon,
                                                           nbNewNodes ), face_nodes );
               }
+              elemType.SetPoly(( nbNewNodes / ( elemType.myIsQuad + 1 ) > 4 ));
+
               SMDS_MeshElement* newElem = AddElement( face_nodes, elemType );
               if ( aShapeId )
                 aMesh->SetMeshElementOnShape(newElem, aShapeId);
@@ -7472,9 +7471,9 @@ void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
           }
           else {
             // each face has to be analyzed in order to check volume validity
-            const SMDS_VtkVolume* aPolyedre =
-              dynamic_cast<const SMDS_VtkVolume*>( elem );
-            if (aPolyedre) {
+            const SMDS_VtkVolume* aPolyedre = dynamic_cast<const SMDS_VtkVolume*>( elem );
+            if (aPolyedre)
+            {
               int nbFaces = aPolyedre->NbFaces();
 
               vector<const SMDS_MeshNode *> poly_nodes;
@@ -7502,7 +7501,6 @@ void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
 
               if (quantities.size() > 3)
               {
-                //aMesh->ChangePolyhedronNodes(elem, poly_nodes, quantities);
                 const SMDS_MeshElement* newElem =
                   aMesh->AddPolyhedralVolume(poly_nodes, quantities);
                 myLastCreatedElems.Append(newElem);
@@ -7910,17 +7908,21 @@ void SMESH_MeshEditor::MergeNodes (TListOfListOfNodes & theGroupsOfNodes)
 
     if ( isOk ) // the non-poly elem remains valid after sticking nodes
     {
-      elemType.Init( elem ).SetID( elem->GetID() );
+      if ( nbNodes != nbUniqueNodes ||
+           !aMesh->ChangeElementNodes( elem, & curNodes[0], nbNodes ))
+      {
+        elemType.Init( elem ).SetID( elem->GetID() );
 
-      SMESHDS_SubMesh * sm = aShapeId > 0 ? aMesh->MeshElements(aShapeId) : 0;
-      aMesh->RemoveFreeElement(elem, sm, /*fromGroups=*/false);
+        SMESHDS_SubMesh * sm = aShapeId > 0 ? aMesh->MeshElements(aShapeId) : 0;
+        aMesh->RemoveFreeElement(elem, sm, /*fromGroups=*/false);
 
-      uniqueNodes.resize(nbUniqueNodes);
-      SMDS_MeshElement* newElem = this->AddElement( uniqueNodes, elemType );
-      if ( sm && newElem )
-        sm->AddElement( newElem );
-      if ( elem != newElem )
-        ReplaceElemInGroups( elem, newElem, aMesh );
+        uniqueNodes.resize(nbUniqueNodes);
+        SMDS_MeshElement* newElem = this->AddElement( uniqueNodes, elemType );
+        if ( sm && newElem )
+          sm->AddElement( newElem );
+        if ( elem != newElem )
+          ReplaceElemInGroups( elem, newElem, aMesh );
+      }
     }
     else {
       // Remove invalid regular element or invalid polygon
@@ -7957,10 +7959,6 @@ public:
   const SMDS_MeshElement* Get() const
   { return myElem; }
 
-  void Set(const SMDS_MeshElement* e) const
-  { myElem = e; }
-
-
 private:
   mutable const SMDS_MeshElement* myElem;
 };
@@ -8267,6 +8265,8 @@ bool SMESH_MeshEditor::CheckFreeBorderNodes(const SMDS_MeshNode* theNode1,
 //=======================================================================
 //function : SewFreeBorder
 //purpose  :
+//warning  : for border-to-side sewing theSideSecondNode is considered as
+//           the last side node and theSideThirdNode is not used
 //=======================================================================
 
 SMESH_MeshEditor::Sew_Error
@@ -8528,7 +8528,7 @@ SMESH_MeshEditor::SewFreeBorder (const SMDS_MeshNode* theBordFirstNode,
   // sew the border to the side 2
   // ============================
 
-  int nbNodes[]  = { nSide[0].size(), nSide[1].size() };
+  int nbNodes[]  = { (int)nSide[0].size(), (int)nSide[1].size() };
   int maxNbNodes = Max( nbNodes[0], nbNodes[1] );
 
   bool toMergeConformal = ( nbNodes[0] == nbNodes[1] );
@@ -8743,9 +8743,20 @@ SMESH_MeshEditor::SewFreeBorder (const SMDS_MeshNode* theBordFirstNode,
     else
       newFaces.Append( myLastCreatedElems(i) );
   }
+  // get segments adjacent to merged nodes
+  TListOfListOfNodes::iterator groupIt = nodeGroupsToMerge.begin();
+  for ( ; groupIt != nodeGroupsToMerge.end(); groupIt++ )
+  {
+    const list<const SMDS_MeshNode*>& nodes = *groupIt;
+    SMDS_ElemIteratorPtr segIt = nodes.front()->GetInverseElementIterator( SMDSAbs_Edge );
+    while ( segIt->more() )
+      segments.insert( segIt->next() );
+  }
+
   // find coincident
   TListOfListOfElementsID equalGroups;
-  FindEqualElements( segments, equalGroups );
+  if ( !segments.empty() )
+    FindEqualElements( segments, equalGroups );
   if ( !equalGroups.empty() )
   {
     // remove from segments those that will be removed
@@ -11651,7 +11662,7 @@ bool SMESH_MeshEditor::DoubleNodesOnGroupBoundaries( const std::vector<TIDSorted
             }
           else
             {
-              INFOS("Quadratic multiple joints not implemented");
+              //INFOS("Quadratic multiple joints not implemented");
               // TODO quadratic nodes
             }
         }
@@ -12575,6 +12586,11 @@ int SMESH_MeshEditor::MakeBoundaryMesh(const TIDSortedElemSet& elements,
   TConnectivity tgtNodes;
   ElemFeatures elemKind( missType ), elemToCopy;
 
+  vector<const SMDS_MeshElement*> presentBndElems;
+  vector<TConnectivity>           missingBndElems;
+  vector<int>                     freeFacets;
+  TConnectivity nodes, elemNodes;
+
   SMDS_ElemIteratorPtr eIt;
   if (elements.empty()) eIt = aMesh->elementsIterator(elemType);
   else                  eIt = elemSetIterator( elements );
@@ -12588,18 +12604,24 @@ int SMESH_MeshEditor::MakeBoundaryMesh(const TIDSortedElemSet& elements,
     // ------------------------------------------------------------------------------------
     // 1. For an elem, get present bnd elements and connectivities of missing bnd elements
     // ------------------------------------------------------------------------------------
-    vector<const SMDS_MeshElement*> presentBndElems;
-    vector<TConnectivity>           missingBndElems;
-    TConnectivity nodes, elemNodes;
+    presentBndElems.clear();
+    missingBndElems.clear();
+    freeFacets.clear(); nodes.clear(); elemNodes.clear();
     if ( vTool.Set(elem, /*ignoreCentralNodes=*/true) ) // elem is a volume --------------
     {
-      vTool.SetExternalNormal();
       const SMDS_MeshElement* otherVol = 0;
       for ( int iface = 0, n = vTool.NbFaces(); iface < n; iface++ )
       {
         if ( !vTool.IsFreeFace(iface, &otherVol) &&
              ( !aroundElements || elements.count( otherVol )))
           continue;
+        freeFacets.push_back( iface );
+      }
+      if ( missType == SMDSAbs_Face )
+        vTool.SetExternalNormal();
+      for ( size_t i = 0; i < freeFacets.size(); ++i )
+      {
+        int                iface = freeFacets[i];
         const SMDS_MeshNode** nn = vTool.GetFaceNodes(iface);
         const size_t nbFaceNodes = vTool.NbFaceNodes (iface);
         if ( missType == SMDSAbs_Edge ) // boundary edges