// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/cluster.cc
// Function: cluster_partition(ATOM_NETWORK*, float)
vector< vector<XYZ> > cluster_partition(ATOM_NETWORK* atmnet, const float cutoff_dist)
{
    //Algorithm:
    
    // Partition a high accuracy voronoi net into clusters.
    ATOM_NETWORK ha_atmnet = *atmnet;
    string acc_setting = "S30";
    setupHighAccuracyAtomNetwork(&ha_atmnet, acc_setting);

    vector<VOR_CELL> vcell;
    vector<BASIC_VCELL> bvcell;
    VORONOI_NETWORK vornet, ha_vornet;
    performVoronoiDecomp(true, atmnet, &vornet, &vcell, false, &bvcell);
    vcell.clear();
    bvcell.clear();
    performVoronoiDecomp(true, &ha_atmnet, &ha_vornet, &vcell, false, &bvcell);
    DIJKSTRA_NETWORK dnet;
    DIJKSTRA_NETWORK::buildDijkstraNetwork(&ha_vornet, &dnet);

    for (DnIt it = dnet.nodes.begin(); it != dnet.nodes.end(); ++it)
        it->active = false;
    
    for (VnIt it = vornet.nodes.begin(); it != vornet.nodes.end(); ++it)
        cout << it->x << " " << it->y << " " << it->z << endl;

    vector <vector<XYZ> > clusters;
    
    for (VnIt nodeit = vornet.nodes.begin(); nodeit != vornet.nodes.end(); ++nodeit) {
        // Get the first non assigned high accuracy voronoi node within the 
        // cutoff distance to current low accuracy voronoi node
        XYZ low_acc_node_loc(nodeit->x, nodeit->y, nodeit->z);
        pair<DnIt, DnIt> p = get_first_closer_nonassigned_node(   // Highaccuracy dijkstra
                low_acc_node_loc, *atmnet, dnet.nodes, cutoff_dist);

        // Initialize cluster and tovisit_nodes
        vector<XYZ> cluster;
        vector<int> tovisit_ids;
        vector<int> visited_ids;

        // Add the current high accuracy node to tovisit ids
        tovisit_ids.push_back(p.first->id);

        // Populate the cluster
        while (!tovisit_ids.empty()){
            //Work on the current node
            int curr_id = tovisit_ids.back();
            DIJKSTRA_NODE curr_nd = dnet.nodes.at(curr_id);
            cluster.push_back(XYZ(curr_nd.x, curr_nd.y, curr_nd.z));
            visited_ids.push_back(curr_nd.id);
            tovisit_ids.pop_back();

            //If any connected nodes are unvisited and are within cutoff distance,
            //push them to tovisit_ids
            vector<CONN>::iterator connit = curr_nd.connections.begin();
            for (; connit != curr_nd.connections.end(); ++connit){
                int to_nodeid = connit->to;
                DIJKSTRA_NODE to_node = dnet.nodes.at(to_nodeid);
                double dist_to_low_acc_node = atmnet->calcDistanceXYZ(
                        nodeit->x, nodeit->y, nodeit->z,
                        to_node.x, to_node.y, to_node.z);
                if (find(visited_ids.begin(), visited_ids.end(), to_nodeid) == visited_ids.end() 
                        && dist_to_low_acc_node < cutoff_dist)
                    tovisit_ids.push_back(to_nodeid); 
            }
        }
        
        clusters.push_back(cluster);
        tovisit_ids.clear();
        visited_ids.clear();
        cluster.clear();
        
    }
    // assert (clusters.size() == vornet.nodes.size());

    return clusters;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x7a8, %rsp            # imm = 0x7A8
movss %xmm0, 0x24(%rsp)
movq %rsi, %r14
movq %rdi, 0x28(%rsp)
leaq 0x348(%rsp), %rdi
callq 0x74b0e
leaq 0x9174f(%rip), %rsi     # 0x104cb7
leaq 0x178(%rsp), %rdi
leaq 0x1f8(%rsp), %rdx
callq 0x1b80e
leaq 0x158(%rsp), %rdi
leaq 0x178(%rsp), %rsi
callq 0x45d0
leaq 0x348(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x6da20
leaq 0x158(%rsp), %rdi
callq 0x4210
xorpd %xmm0, %xmm0
movapd %xmm0, 0xc0(%rsp)
andq $0x0, 0xd0(%rsp)
movapd %xmm0, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x4090e
leaq 0x2d0(%rsp), %rdi
callq 0x4090e
pushq $0x1
popq %rdi
leaq 0x1f8(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
leaq 0xa0(%rsp), %r9
movq %r14, %rsi
xorl %r8d, %r8d
callq 0x5827
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
callq 0xb9a6
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
callq 0xb99e
pushq $0x1
popq %rdi
leaq 0x348(%rsp), %rsi
leaq 0x2d0(%rsp), %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
movq %r15, %r9
callq 0x5827
leaq 0x270(%rsp), %rdi
callq 0x3d70e
leaq 0x2d0(%rsp), %rdi
leaq 0x270(%rsp), %rsi
callq 0x3d7e6
movq 0x270(%rsp), %rax
movq 0x278(%rsp), %rcx
cmpq %rcx, %rax
je 0x7369e
movb $0x0, 0x40(%rax)
addq $0x48, %rax
jmp 0x7368f
movq 0x240(%rsp), %r12
movq 0xdc913(%rip), %rbx     # 0x14ffc0
leaq 0x90e5e(%rip), %r15     # 0x104512
cmpq 0x248(%rsp), %r12
je 0x7370e
movsd (%r12), %xmm0
movq %rbx, %rdi
callq 0x46c0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4480
movsd 0x8(%r12), %xmm0
movq %rax, %rdi
callq 0x46c0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4480
movsd 0x10(%r12), %xmm0
movq %rax, %rdi
callq 0x46c0
movq %rax, %rdi
callq 0x4230
addq $0x40, %r12
jmp 0x736b4
xorpd %xmm0, %xmm0
movq 0x28(%rsp), %rax
movupd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x240(%rsp), %r13
cvtss2sd 0x24(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
leaq 0x270(%rsp), %rbx
leaq 0x198(%rsp), %rbp
leaq 0x5c(%rsp), %r15
cmpq %r12, %r13
je 0x739e0
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
leaq 0x140(%rsp), %rdi
callq 0x6402e
movq 0x150(%rsp), %rax
movq %rax, 0xf0(%rsp)
movupd 0x140(%rsp), %xmm0
movapd %xmm0, 0xe0(%rsp)
leaq 0x1e0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2a78c
movq 0xf0(%rsp), %rax
movq %rax, 0x10(%rsp)
movaps 0xe0(%rsp), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %r12, %rsi
movss 0x24(%rsp), %xmm0
callq 0x7349c
movq %rax, 0x50(%rsp)
movq %r12, %rdi
callq 0xae22
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
movapd %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0x30(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x9434
movq 0x38(%rsp), %rax
cmpq %rax, 0x30(%rsp)
je 0x73960
movslq -0x4(%rax), %rsi
movq %rbx, %rdi
callq 0xabe2
leaq 0xf8(%rsp), %rdi
movq %rax, %rsi
callq 0x29ebe
movsd 0x100(%rsp), %xmm0
movsd 0x108(%rsp), %xmm1
movsd 0x110(%rsp), %xmm2
movq %rbp, %rdi
callq 0x6402e
leaq 0x80(%rsp), %rdi
movq %rbp, %rsi
callq 0x75542
leaq 0x60(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x9434
addq $-0x4, 0x38(%rsp)
movq 0x118(%rsp), %r12
cmpq 0x120(%rsp), %r12
je 0x7394e
movslq 0x4(%r12), %rsi
movl %esi, 0x5c(%rsp)
movq %rbx, %rdi
callq 0xabe2
movq %rbp, %rdi
movq %rax, %rsi
callq 0x29ebe
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movsd 0x1a0(%rsp), %xmm3
movsd 0x1a8(%rsp), %xmm4
movsd 0x1b0(%rsp), %xmm5
movq %r14, %rdi
callq 0x3f636
movsd %xmm0, 0x50(%rsp)
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq %r15, %rdx
callq 0x7565c
movsd 0xd8(%rsp), %xmm0
ucomisd 0x50(%rsp), %xmm0
jbe 0x73938
cmpq 0x68(%rsp), %rax
jne 0x73938
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x9434
leaq 0x1b8(%rsp), %rdi
callq 0xaf14
addq $0x28, %r12
jmp 0x7389c
leaq 0x118(%rsp), %rdi
callq 0xaf14
jmp 0x7381d
movq 0x28(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x4bf4a
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x73983
movq %rax, 0x38(%rsp)
movq 0x60(%rsp), %rax
cmpq %rax, 0x68(%rsp)
je 0x73994
movq %rax, 0x68(%rsp)
movq 0x80(%rsp), %rax
cmpq %rax, 0x88(%rsp)
je 0x739ae
movq %rax, 0x88(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xb290
leaq 0x30(%rsp), %rdi
callq 0xb290
leaq 0x80(%rsp), %rdi
callq 0xb132
addq $0x40, %r13
movq 0x248(%rsp), %r12
jmp 0x7374c
leaq 0x270(%rsp), %rdi
callq 0xae22
leaq 0x2d0(%rsp), %rdi
callq 0xa95c
leaq 0x1f8(%rsp), %rdi
callq 0xa95c
leaq 0xa0(%rsp), %rdi
callq 0xaa94
leaq 0xc0(%rsp), %rdi
callq 0xaa72
leaq 0x178(%rsp), %rdi
callq 0x4210
leaq 0x348(%rsp), %rdi
callq 0xaab6
movq 0x28(%rsp), %rax
addq $0x7a8, %rsp            # imm = 0x7A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x73aae
jmp 0x73a8b
movq %rax, %rbx
jmp 0x73b25
movq %rax, %rbx
jmp 0x73b32
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x4210
jmp 0x73b4c
movq %rax, %rbx
jmp 0x73b4c
movq %rax, %rbx
jmp 0x73b59
movq %rax, %rbx
jmp 0x73b18
jmp 0x73a95
movq %rax, %rbx
jmp 0x73b01
movq %rax, %rbx
leaq 0x1e0(%rsp), %rdi
callq 0xae22
jmp 0x73b01
jmp 0x73ab7
movq %rax, %rbx
jmp 0x73b0b
jmp 0x73ad0
jmp 0x73ad0
movq %rax, %rbx
jmp 0x73ae0
jmp 0x73abe
movq %rax, %rbx
leaq 0x1b8(%rsp), %rdi
callq 0xaf14
jmp 0x73ad3
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0xaf14
leaq 0x60(%rsp), %rdi
callq 0xb290
leaq 0x30(%rsp), %rdi
callq 0xb290
leaq 0x80(%rsp), %rdi
callq 0xb132
movq 0x28(%rsp), %rdi
callq 0x4bf94
leaq 0x270(%rsp), %rdi
callq 0xae22
leaq 0x2d0(%rsp), %rdi
callq 0xa95c
leaq 0x1f8(%rsp), %rdi
callq 0xa95c
leaq 0xa0(%rsp), %rdi
callq 0xaa94
leaq 0xc0(%rsp), %rdi
callq 0xaa72
leaq 0x178(%rsp), %rdi
callq 0x4210
leaq 0x348(%rsp), %rdi
callq 0xaab6
movq %rbx, %rdi
callq 0x4780