static int ngNodeId( const SMDS_MeshNode* node,
                     netgen::Mesh&        ngMesh,
                     TNode2IdMap&         nodeNgIdMap)
{
  int newNgId = ngMesh.GetNP() + 1;

  pair< TNode2IdMap::iterator, bool > it_isNew = nodeNgIdMap.insert( make_pair( node, newNgId ));

  if ( it_isNew.second ) {
    netgen::MeshPoint p( netgen::Point<3> (node->X(), node->Y(), node->Z()) );
    ngMesh.AddPoint( p );
  }
  return it_isNew.first->second;
}
static TError AddSegmentsToMesh(netgen::Mesh&                    ngMesh,
                                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() )
      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 TError
        (new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,
                                SMESH_Comment("Unexpected nb of points on wire ") << iW
                                << ": " << uvPtVec.size()<<" != "<<wire->NbPoints()));
    nbNodes += wire->NbSegments();
  }
  nodeVec.reserve( nbNodes );

  // -----------------
  // Fill netgen mesh
  // -----------------

//   netgen::Box<3> bb = geom.GetBoundingBox();
//   bb.Increase (bb.Diam()/10);
//   ngMesh.SetLocalH (bb.PMin(), bb.PMax(), 0.5); // set grading

  const int faceID = 1, solidID = 0;
  ngMesh.AddFaceDescriptor (FaceDescriptor(faceID, solidID, solidID, 0));

  for ( int iW = 0; iW < wires.size(); ++iW )
  {
    StdMeshers_FaceSidePtr wire = wires[ iW ];
    const vector<UVPtStruct>& uvPtVec = wire->GetUVPtStruct();

    int firstPointID = ngMesh.GetNP() + 1;
    int edgeID = 1, posID = -2;
    for ( int i = 0; i < wire->NbSegments(); ++i ) // loop on segments
    {
      // Add the first point of a segment
      const SMDS_MeshNode * n = uvPtVec[ i ].node;
      const int posShapeID = n->GetPosition()->GetShapeId();

      // skip nodes on degenerated edges
      if ( helper.IsDegenShape( posShapeID ) &&
           helper.IsDegenShape( uvPtVec[ i+1 ].node->GetPosition()->GetShapeId() ))
        continue;

      nodeVec.push_back( n );

      MeshPoint mp( Point<3> (n->X(), n->Y(), n->Z()) );
      ngMesh.AddPoint ( mp, 1, EDGEPOINT );

      // Add the segment
      Segment seg;

      seg.pnums[0] = ngMesh.GetNP();          // ng node id
      seg.pnums[1] = seg.pnums[0] + 1;              // ng node id
      seg.edgenr = ngMesh.GetNSeg() + 1;// 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
        bool onVertex = ( pnt.node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX );
        if ( onVertex || posShapeID != posID )
        {
          // get edge id
          double normParam = pnt.normParam;
          if ( onVertex )
            normParam = 0.5 * ( uvPtVec[ i ].normParam + uvPtVec[ i+1 ].normParam );
          const TopoDS_Edge& edge = wire->Edge( wire->EdgeIndex( normParam ));
          edgeID = geom.emap.FindIndex( edge );
          posID  = posShapeID;
          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);

//       cout << "Segment: " << seg.edgenr << endl
//            << "\tp1: " << seg.p1 << endl
//            << "\tp2: " << seg.p2 << 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;
    }
    Segment& seg = ngMesh.LineSegment( ngMesh.GetNSeg() );
    seg.pnums[1] = firstPointID;
  }

  ngMesh.CalcSurfacesOfNode();  

  return TError();
}