+//================================================================================
+/*!
+ * \brief Fill netgen mesh with segments of a FACE
+ * \param ngMesh - netgen mesh
+ * \param geom - container of OCCT geometry to mesh
+ * \param wires - data of nodes on FACE boundary
+ * \param helper - mesher helper holding the FACE
+ * \param nodeVec - vector of nodes in which node index == netgen ID
+ * \retval SMESH_ComputeErrorPtr - error description
+ */
+//================================================================================
+
+SMESH_ComputeErrorPtr
+NETGENPlugin_Mesher::AddSegmentsToMesh(netgen::Mesh& ngMesh,
+ netgen::OCCGeometry& geom,
+ const TSideVector& wires,
+ SMESH_MesherHelper& helper,
+ vector< const SMDS_MeshNode* > & nodeVec)
+{
+ // ----------------------------
+ // Check wires and count nodes
+ // ----------------------------
+ int nbNodes = 0;
+ for ( int iW = 0; iW < wires.size(); ++iW )
+ {
+ StdMeshers_FaceSidePtr wire = wires[ iW ];
+ if ( wire->MissVertexNode() )
+ {
+ // Commented for issue 0020960. It worked for the case, let's wait for case where it doesn't.
+ // It seems that there is no reason for this limitation
+// return TError
+// (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH, "Missing nodes on vertices"));
+ }
+ const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
+ if ( uvPtVec.size() != wire->NbPoints() )
+ return SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
+ SMESH_Comment("Unexpected nb of points on wire ") << iW
+ << ": " << uvPtVec.size()<<" != "<<wire->NbPoints());
+ nbNodes += wire->NbPoints();
+ }
+ nodeVec.reserve( nodeVec.size() + nbNodes + 1 );
+ if ( nodeVec.empty() )
+ nodeVec.push_back( 0 );
+
+ // -----------------
+ // Fill netgen mesh
+ // -----------------
+
+ const bool wasNgMeshEmpty = ( ngMesh.GetNP() < 1 ); /* true => this method is called by
+ NETGENPlugin_NETGEN_2D_ONLY */
+
+ // map for nodes on vertices since they can be shared between wires
+ // ( issue 0020676, face_int_box.brep) and nodes built by NETGEN
+ map<const SMDS_MeshNode*, int > node2ngID;
+ if ( !wasNgMeshEmpty ) // fill node2ngID with nodes built by NETGEN
+ {
+ set< int > subIDs; // ids of sub-shapes of the FACE
+ for ( int iW = 0; iW < wires.size(); ++iW )
+ {
+ StdMeshers_FaceSidePtr wire = wires[ iW ];
+ for ( int iE = 0, nbE = wire->NbEdges(); iE < nbE; ++iE )
+ {
+ subIDs.insert( wire->EdgeID( iE ));
+ subIDs.insert( helper.GetMeshDS()->ShapeToIndex( wire->FirstVertex( iE )));
+ }
+ }
+ for ( size_t ngID = 1; ngID < nodeVec.size(); ++ngID )
+ if ( subIDs.count( nodeVec[ngID]->getshapeId() ))
+ node2ngID.insert( make_pair( nodeVec[ngID], ngID ));
+ }
+
+ const int solidID = 0, faceID = geom.fmap.FindIndex( helper.GetSubShape() );
+ if ( ngMesh.GetNFD() < 1 )
+ ngMesh.AddFaceDescriptor (netgen::FaceDescriptor(faceID, solidID, solidID, 0));
+
+ for ( int iW = 0; iW < wires.size(); ++iW )
+ {
+ StdMeshers_FaceSidePtr wire = wires[ iW ];
+ const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();
+ const int nbSegments = wire->NbPoints() - 1;
+
+ // assure the 1st node to be in node2ngID, which is needed to correctly
+ // "close chain of segments" (see below) in case if the 1st node is not
+ // onVertex because it is on a Viscous layer
+ node2ngID.insert( make_pair( uvPtVec[ 0 ].node, ngMesh.GetNP() + 1 ));
+
+ // compute length of every segment
+ vector<double> segLen( nbSegments );
+ for ( int i = 0; i < nbSegments; ++i )
+ segLen[i] = SMESH_TNodeXYZ( uvPtVec[ i ].node ).Distance( uvPtVec[ i+1 ].node );
+
+ int edgeID = 1, posID = -2;
+ bool isInternalWire = false;
+ double vertexNormPar = 0;
+ for ( int i = 0; i < nbSegments; ++i ) // loop on segments
+ {
+ // Add the first point of a segment
+
+ const SMDS_MeshNode * n = uvPtVec[ i ].node;
+ const int posShapeID = n->getshapeId();
+ bool onVertex = ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX );
+ bool onEdge = ( n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_EDGE );
+
+ // skip nodes on degenerated edges
+ if ( helper.IsDegenShape( posShapeID ) &&
+ helper.IsDegenShape( uvPtVec[ i+1 ].node->getshapeId() ))
+ continue;
+
+ int ngID1 = ngMesh.GetNP() + 1, ngID2 = ngID1+1;
+ if ( onVertex || ( !wasNgMeshEmpty && onEdge ))
+ ngID1 = node2ngID.insert( make_pair( n, ngID1 )).first->second;
+ if ( ngID1 > ngMesh.GetNP() )
+ {
+ netgen::MeshPoint mp( netgen::Point<3> (n->X(), n->Y(), n->Z()) );
+ ngMesh.AddPoint ( mp, 1, netgen::EDGEPOINT );
+ nodeVec.push_back( n );
+ }
+ else // n is in ngMesh already, and ngID2 in prev segment is wrong
+ {
+ ngID2 = ngMesh.GetNP() + 1;
+ if ( i > 0 ) // prev segment belongs to same wire
+ {
+ netgen::Segment& prevSeg = ngMesh.LineSegment( ngMesh.GetNSeg() );
+ prevSeg[1] = ngID1;
+ }
+ }
+
+ // Add the segment
+
+ netgen::Segment seg;
+
+ seg[0] = ngID1; // ng node id
+ seg[1] = ngID2; // ng node id
+ seg.edgenr = ngMesh.GetNSeg() + 1; // ng segment id
+ seg.si = faceID; // = geom.fmap.FindIndex (face);
+
+ for ( int iEnd = 0; iEnd < 2; ++iEnd)
+ {
+ const UVPtStruct& pnt = uvPtVec[ i + iEnd ];
+
+ seg.epgeominfo[ iEnd ].dist = pnt.param; // param on curve
+ seg.epgeominfo[ iEnd ].u = pnt.u;
+ seg.epgeominfo[ iEnd ].v = pnt.v;
+
+ // find out edge id and node parameter on edge
+ onVertex = ( pnt.normParam + 1e-10 > vertexNormPar );
+ if ( onVertex || posShapeID != posID )
+ {
+ // get edge id
+ double normParam = pnt.normParam;
+ if ( onVertex )
+ normParam = 0.5 * ( uvPtVec[ i ].normParam + uvPtVec[ i+1 ].normParam );
+ int edgeIndexInWire = wire->EdgeIndex( normParam );
+ vertexNormPar = wire->LastParameter( edgeIndexInWire );
+ const TopoDS_Edge& edge = wire->Edge( edgeIndexInWire );
+ edgeID = geom.emap.FindIndex( edge );
+ posID = posShapeID;
+ isInternalWire = ( edge.Orientation() == TopAbs_INTERNAL );
+ // if ( onVertex ) // param on curve is different on each of two edges
+ // seg.epgeominfo[ iEnd ].dist = helper.GetNodeU( edge, pnt.node );
+ }
+ seg.epgeominfo[ iEnd ].edgenr = edgeID; // = geom.emap.FindIndex(edge);
+ }
+
+ ngMesh.AddSegment (seg);
+ {
+ // restrict size of elements near the segment
+ SMESH_TNodeXYZ np1( n ), np2( uvPtVec[ i+1 ].node );
+ // get an average size of adjacent segments to avoid sharp change of
+ // element size (regression on issue 0020452, note 0010898)
+ int iPrev = SMESH_MesherHelper::WrapIndex( i-1, nbSegments );
+ int iNext = SMESH_MesherHelper::WrapIndex( i+1, nbSegments );
+ double avgH = ( segLen[ iPrev ] + segLen[ i ] + segLen[ iNext ]) / 3;
+
+ RestrictLocalSize( ngMesh, 0.5*(np1+np2), avgH );
+ }
+#ifdef DUMP_SEGMENTS
+ cout << "Segment: " << seg.edgenr << endl
+ << "\tp1: " << seg[0] << endl
+ << "\tp2: " << seg[1] << endl
+ << "\tp0 param: " << seg.epgeominfo[ 0 ].dist << endl
+ << "\tp0 uv: " << seg.epgeominfo[ 0 ].u <<", "<< seg.epgeominfo[ 0 ].v << endl
+ << "\tp0 edge: " << seg.epgeominfo[ 0 ].edgenr << endl
+ << "\tp1 param: " << seg.epgeominfo[ 1 ].dist << endl
+ << "\tp1 uv: " << seg.epgeominfo[ 1 ].u <<", "<< seg.epgeominfo[ 1 ].v << endl
+ << "\tp1 edge: " << seg.epgeominfo[ 1 ].edgenr << endl;
+#endif
+ if ( isInternalWire )
+ {
+ swap (seg[0], seg[1]);
+ swap( seg.epgeominfo[0], seg.epgeominfo[1] );
+ seg.edgenr = ngMesh.GetNSeg() + 1; // segment id
+ ngMesh.AddSegment (seg);
+#ifdef DUMP_SEGMENTS
+ cout << "Segment: " << seg.edgenr << endl << "\tis REVRESE of the previous one" << endl;
+#endif
+ }
+ } // loop on segments on a wire
+
+ // close chain of segments
+ if ( nbSegments > 0 )
+ {
+ netgen::Segment& lastSeg = ngMesh.LineSegment( ngMesh.GetNSeg() - int( isInternalWire));
+ const SMDS_MeshNode * lastNode = uvPtVec.back().node;
+ lastSeg[1] = node2ngID.insert( make_pair( lastNode, lastSeg[1] )).first->second;
+ if ( lastSeg[1] > ngMesh.GetNP() )
+ {
+ netgen::MeshPoint mp( netgen::Point<3> (lastNode->X(), lastNode->Y(), lastNode->Z()) );
+ ngMesh.AddPoint ( mp, 1, netgen::EDGEPOINT );
+ nodeVec.push_back( lastNode );
+ }
+ if ( isInternalWire )
+ {
+ netgen::Segment& realLastSeg = ngMesh.LineSegment( ngMesh.GetNSeg() );
+ realLastSeg[0] = lastSeg[1];
+ }
+ }
+
+ } // loop on WIREs of a FACE
+
+ // add a segment instead of an internal vertex
+ if ( wasNgMeshEmpty )
+ {
+ NETGENPlugin_Internals intShapes( *helper.GetMesh(), helper.GetSubShape(), /*is3D=*/false );
+ AddIntVerticesInFaces( geom, ngMesh, nodeVec, intShapes );
+ }
+ ngMesh.CalcSurfacesOfNode();
+
+ return TError();
+}
+