// File: Luthaf[P]zeopp/feature.cc
// Function: FEATURE::segment_identify_connections()
void FEATURE::segment_identify_connections(){
int i,j,k;
int nfaces;
vector <int> segfaces;
vector <double> segfaces_max_r;

vector <int> nodes_in_segment; // list IDs of nodes assigned to segment
vector <int> face_nodes_in_segment; // list IDs of nodes assigned to segment and located on segment's "face", that is
                                    // nodes with connections to other segments
vector <int>  connected_segment_for_face_nodes_in_segment; // for each node, if it is a face node, store segment id to which it is connected
vector <SEGCONN> edges_from_segment; // list of edges that connect the current segment to other segments
SEGCONN segc; // inter-segment connection
vector <int> edges_in_face; // for each edge connecting to other semgent assign to a face, this svector stores face id for each edge

/* loop over all segments and identify all connections to other segments */

for(i=0;i<nsegments;i++)
   {
   nodes_in_segment.clear();
   edges_from_segment.clear();
   for(j=0;j<(int)nodes.size();j++)
      {
      if(segments[j]==i) nodes_in_segment.push_back(j);
      };
   face_nodes_in_segment.clear();
   connected_segment_for_face_nodes_in_segment.clear();
   face_nodes_in_segment.resize(nodes_in_segment.size(),0);
   connected_segment_for_face_nodes_in_segment.resize(nodes_in_segment.size(),-1);
   for(j=0;j<(int)nodes_in_segment.size();j++)
      {
      for(k=0;k<(int)nodes.at(nodes_in_segment[j]).connections.size();k++)
         {
         if(segments[nodes.at(nodes_in_segment[j]).connections.at(k).to]!=i) 
             {
             face_nodes_in_segment[j]++; 
             connected_segment_for_face_nodes_in_segment[j]=segments[nodes.at(nodes_in_segment[j]).connections.at(k).to];
        
             segc.from=nodes_in_segment[j]; // this is ID id in channel)
             segc.to=nodes.at(nodes_in_segment[j]).connections.at(k).to; // node id in channel
             segc.from_seg=i; 
             segc.to_seg=segments[nodes.at(nodes_in_segment[j]).connections.at(k).to];
             segc.max_radius=nodes.at(nodes_in_segment[j]).connections.at(k).max_radius;
             segc.deltaPos=nodes.at(nodes_in_segment[j]).connections.at(k).deltaPos;
             edges_from_segment.push_back(segc);
             };
         };
      };

// now connected_segment_for_face_nodes_in_segment has a list of segments each node in the current segment is conneed to (-1 if not) 
// we need now to find "clusters" of these nodes that will correspond to different connections;
// edges_from_segment stores all edges going out from a segment to other segments

   int nedges_tobe_assigned=edges_from_segment.size();
   edges_in_face.clear();
   edges_in_face.resize(nedges_tobe_assigned,-1);

   nfaces=0;
   int neigh_seg; // id of neighboring segment
//   while(nedges_tobe_assigned>-1)
     {
     for(k=0;k<(int)edges_from_segment.size();k++)
        {
        if(edges_in_face[k]==-1) // find first edge not assigned to face
          {
          neigh_seg=edges_from_segment[k].to_seg;
          edges_in_face[k]=nfaces;
          nedges_tobe_assigned--;
          segment_identify_connections_recurr(&nodes_in_segment,&edges_in_face,&nedges_tobe_assigned,nfaces,neigh_seg,&edges_from_segment,k);
          nfaces++;
          };
//        nfaces++;
        };
     };

/*
   cout << "Segment " << i <<": " << nodes_in_segment.size() << " nodes, " << edges_in_face.size() << " edges, " << nfaces << " faces, nodes-to-go " << nedges_tobe_assigned << "\n";
   for(k=0;k<(int)edges_from_segment.size();k++)
      {
      cout << "Edge " << edges_from_segment[k].from << "  " << edges_from_segment[k].to << "  Segments:" << edges_from_segment[k].from_seg << "  " << edges_from_segment[k].to_seg << "    "; edges_from_segment[k].deltaPos.print();
      };
*/


   }; // end of loop over all segments

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x240, %rsp            # imm = 0x240
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0x30(%rbp), %rdi
callq 0xd0d0
leaq -0x48(%rbp), %rdi
callq 0xf650
leaq -0x60(%rbp), %rdi
callq 0xd0d0
leaq -0x78(%rbp), %rdi
callq 0xd0d0
leaq -0x90(%rbp), %rdi
callq 0xd0d0
leaq -0xa8(%rbp), %rdi
callq 0xe0990
leaq -0xd8(%rbp), %rdi
callq 0x94d10
jmp 0xdc904
leaq -0x100(%rbp), %rdi
callq 0xd0d0
movl $0x0, -0xc(%rbp)
movq -0x120(%rbp), %rcx
movl -0xc(%rbp), %eax
cmpl 0x138(%rcx), %eax
jge 0xdcf9e
leaq -0x60(%rbp), %rdi
callq 0x163b0
leaq -0xa8(%rbp), %rdi
callq 0xe09b0
movl $0x0, -0x10(%rbp)
movq -0x120(%rbp), %rdi
movl -0x10(%rbp), %eax
movl %eax, -0x124(%rbp)
addq $0x60, %rdi
callq 0x10100
movq %rax, %rcx
movl -0x124(%rbp), %eax
cmpl %ecx, %eax
jge 0xdc9e8
movq -0x120(%rbp), %rdi
addq $0x140, %rdi            # imm = 0x140
movslq -0x10(%rbp), %rsi
callq 0xed60
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
jne 0xdc9d8
leaq -0x60(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0xd1b0
jmp 0xdc99c
jmp 0xdc9d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
jmp 0xdcfef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
leaq -0x100(%rbp), %rdi
callq 0xec80
jmp 0xdcfef
jmp 0xdc9da
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xdc949
leaq -0x78(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x163b0
leaq -0x90(%rbp), %rdi
callq 0x163b0
leaq -0x60(%rbp), %rdi
callq 0x10090
movq -0x130(%rbp), %rdi
movq %rax, %rsi
movl $0x0, -0x104(%rbp)
leaq -0x104(%rbp), %rdx
callq 0x84b00
jmp 0xdca2f
leaq -0x60(%rbp), %rdi
callq 0x10090
movq %rax, %rsi
movl $0xffffffff, -0x108(%rbp) # imm = 0xFFFFFFFF
leaq -0x90(%rbp), %rdi
leaq -0x108(%rbp), %rdx
callq 0x84b00
jmp 0xdca5a
movl $0x0, -0x10(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0x134(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x10090
movq %rax, %rcx
movl -0x134(%rbp), %eax
cmpl %ecx, %eax
jge 0xdce49
movl $0x0, -0x14(%rbp)
movq -0x120(%rbp), %rax
movl -0x14(%rbp), %ecx
movl %ecx, -0x14c(%rbp)
addq $0x60, %rax
movq %rax, -0x148(%rbp)
movslq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xed60
movq -0x148(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x140(%rbp)
jmp 0xdcacb
movq -0x140(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a9e0
movq %rax, %rcx
movl -0x14c(%rbp), %eax
cmpl %ecx, %eax
jge 0xdce39
movq -0x120(%rbp), %rax
movq %rax, %rcx
addq $0x140, %rcx            # imm = 0x140
movq %rcx, -0x168(%rbp)
addq $0x60, %rax
movq %rax, -0x160(%rbp)
movslq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xed60
movq -0x160(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x158(%rbp)
jmp 0xdcb34
movq -0x158(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x170(%rbp)
jmp 0xdcb51
movq -0x168(%rbp), %rdi
movq -0x170(%rbp), %rax
movslq 0x4(%rax), %rsi
callq 0xed60
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
je 0xdce29
movslq -0x10(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0xed60
movq %rax, %rcx
movq -0x120(%rbp), %rax
movl (%rcx), %edx
incl %edx
movl %edx, (%rcx)
movq %rax, %rcx
addq $0x140, %rcx            # imm = 0x140
movq %rcx, -0x188(%rbp)
addq $0x60, %rax
movq %rax, -0x180(%rbp)
movslq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xed60
movq -0x180(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x178(%rbp)
jmp 0xdcbd1
movq -0x178(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x190(%rbp)
jmp 0xdcbee
movq -0x188(%rbp), %rdi
movq -0x190(%rbp), %rax
movslq 0x4(%rax), %rsi
callq 0xed60
movl (%rax), %eax
movl %eax, -0x1ac(%rbp)
movslq -0x10(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0xed60
movl -0x1ac(%rbp), %ecx
movl %ecx, (%rax)
movslq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0xed60
movq -0x1a8(%rbp), %rdi
movq %rax, %rcx
movq -0x120(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0xd8(%rbp)
addq $0x60, %rax
movq %rax, -0x1a0(%rbp)
movslq -0x10(%rbp), %rsi
callq 0xed60
movq -0x1a0(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x198(%rbp)
jmp 0xdcc7e
movq -0x198(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x1b8(%rbp)
jmp 0xdcc9b
movq -0x120(%rbp), %rax
movq -0x1b8(%rbp), %rcx
movl 0x4(%rcx), %ecx
movl %ecx, -0xd4(%rbp)
movl -0xc(%rbp), %ecx
movl %ecx, -0xd0(%rbp)
movq %rax, %rcx
addq $0x140, %rcx            # imm = 0x140
movq %rcx, -0x1d0(%rbp)
addq $0x60, %rax
movq %rax, -0x1c8(%rbp)
movslq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xed60
movq -0x1c8(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x1c0(%rbp)
jmp 0xdccfc
movq -0x1c0(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x1d8(%rbp)
jmp 0xdcd19
movq -0x1d0(%rbp), %rdi
movq -0x1d8(%rbp), %rax
movslq 0x4(%rax), %rsi
callq 0xed60
movq %rax, %rcx
movq -0x120(%rbp), %rax
movl (%rcx), %ecx
movl %ecx, -0xcc(%rbp)
addq $0x60, %rax
movq %rax, -0x1e8(%rbp)
movslq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xed60
movq -0x1e8(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x1e0(%rbp)
jmp 0xdcd72
movq -0x1e0(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x1f0(%rbp)
jmp 0xdcd8f
movq -0x120(%rbp), %rax
movq -0x1f0(%rbp), %rcx
movsd 0x10(%rcx), %xmm0
movsd %xmm0, -0xc8(%rbp)
addq $0x60, %rax
movq %rax, -0x200(%rbp)
movslq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0xed60
movq -0x200(%rbp), %rdi
movslq (%rax), %rsi
callq 0x10130
movq %rax, -0x1f8(%rbp)
jmp 0xdcdda
movq -0x1f8(%rbp), %rdi
addq $0x20, %rdi
movslq -0x14(%rbp), %rsi
callq 0x64470
movq %rax, -0x208(%rbp)
jmp 0xdcdf7
movq -0x208(%rbp), %rax
movl 0x20(%rax), %ecx
movl %ecx, -0xac(%rbp)
movq 0x18(%rax), %rax
movq %rax, -0xb4(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0xe08b0
jmp 0xdce27
jmp 0xdce29
jmp 0xdce2b
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xdca8b
jmp 0xdce3b
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xdca61
leaq -0xa8(%rbp), %rdi
callq 0xe0920
movl %eax, -0x10c(%rbp)
leaq -0x100(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x163b0
movq -0x210(%rbp), %rdi
movslq -0x10c(%rbp), %rsi
movl $0xffffffff, -0x110(%rbp) # imm = 0xFFFFFFFF
leaq -0x110(%rbp), %rdx
callq 0x84b00
jmp 0xdce94
movl $0x0, -0x18(%rbp)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x214(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xe0920
movq %rax, %rcx
movl -0x214(%rbp), %eax
cmpl %ecx, %eax
jge 0xdcf8e
movslq -0x14(%rbp), %rsi
leaq -0x100(%rbp), %rdi
callq 0xed60
cmpl $-0x1, (%rax)
jne 0xdcf7e
movslq -0x14(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0xe09d0
movl 0xc(%rax), %eax
movl %eax, -0x114(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x22c(%rbp)
movslq -0x14(%rbp), %rsi
leaq -0x100(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0xed60
movl -0x22c(%rbp), %esi
movq -0x228(%rbp), %rcx
movq -0x120(%rbp), %rdi
movq -0x220(%rbp), %rdx
movl %esi, (%rax)
movl -0x10c(%rbp), %eax
decl %eax
movl %eax, -0x10c(%rbp)
movl -0x18(%rbp), %r8d
movl -0x114(%rbp), %r9d
movl -0x14(%rbp), %esi
movq %rsp, %rax
movl %esi, 0x8(%rax)
movq %rcx, (%rax)
leaq -0x60(%rbp), %rsi
leaq -0x10c(%rbp), %rcx
callq 0xdd040
jmp 0xdcf75
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xdcf80
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xdcea2
jmp 0xdcf90
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xdc917
leaq -0x100(%rbp), %rdi
callq 0xec80
leaq -0xa8(%rbp), %rdi
callq 0xe09f0
leaq -0x90(%rbp), %rdi
callq 0xec80
leaq -0x78(%rbp), %rdi
callq 0xec80
leaq -0x60(%rbp), %rdi
callq 0xec80
leaq -0x48(%rbp), %rdi
callq 0xf9d0
leaq -0x30(%rbp), %rdi
callq 0xec80
addq $0x240, %rsp            # imm = 0x240
popq %rbp
retq
leaq -0xa8(%rbp), %rdi
callq 0xe09f0
leaq -0x90(%rbp), %rdi
callq 0xec80
leaq -0x78(%rbp), %rdi
callq 0xec80
leaq -0x60(%rbp), %rdi
callq 0xec80
leaq -0x48(%rbp), %rdi
callq 0xf9d0
leaq -0x30(%rbp), %rdi
callq 0xec80
movq -0xe0(%rbp), %rdi
callq 0x58f0
nopw (%rax,%rax)