// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::DijkstraDistances(gaps::RNArray<gaps::R3MeshVertex*> const&, double, gaps::R3MeshEdge**) const
RNLength *R3Mesh::
DijkstraDistances(const RNArray<R3MeshVertex *>& source_vertices, RNLength max_distance, R3MeshEdge **edges) const
{
  // Allocate array of distances (to return)
  RNLength *distances = new RNLength [ NVertices() ];
  if (!distances) {
    RNFail("Unable to allocate array of distances.\n");
    return NULL;
  }

  // Allocate temporary data
  DijkstraData *vertex_data = new DijkstraData [ NVertices() ];
  if (!vertex_data) {
    RNFail("Unable to allocate temporary data for geodesic distances.\n");
    delete [] distances;
    return NULL;
  }

  // Initialize all data
  for (int i = 0; i < NVertices(); i++) {
    vertex_data[i].vertex = Vertex(i);
    vertex_data[i].edge = NULL;
    vertex_data[i].distance_to_destination = RN_UNKNOWN;
    vertex_data[i].heappointer = NULL;
    vertex_data[i].distance = FLT_MAX;
    distances[i] = FLT_MAX;
  }

  // Initialize priority queue
  DijkstraData tmp;
  static RNHeap<DijkstraData *> heap(&tmp, &(tmp.distance), &(tmp.heappointer));
  for (int i = 0; i < source_vertices.NEntries(); i++) {
    R3MeshVertex *source_vertex = source_vertices[i];
    DijkstraData *data = &vertex_data[ VertexID(source_vertex) ];
    data->distance = 0;
    heap.Push(data);
  }

  // Visit vertices computing shortest distance to closest source vertex
  while (!heap.IsEmpty()) {
    DijkstraData *data = heap.Pop();
    R3MeshVertex *vertex = data->vertex;
    if ((max_distance > 0) && (data->distance > max_distance)) break;
    distances[ VertexID(vertex) ] = data->distance;
    if (edges) edges[ VertexID(vertex) ] = data->edge;
    for (int i = 0; i < VertexValence(vertex); i++) {
      R3MeshEdge *edge = EdgeOnVertex(vertex, i);
      R3MeshVertex *neighbor_vertex = VertexAcrossEdge(edge, vertex);
      DijkstraData *neighbor_data = &vertex_data [ VertexID(neighbor_vertex) ];
      RNScalar old_distance = neighbor_data->distance;
      RNScalar new_distance = EdgeLength(edge) + data->distance;
      if (new_distance < old_distance) {
        neighbor_data->edge = edge;
        neighbor_data->distance = new_distance;
        if (old_distance < FLT_MAX) heap.Update(neighbor_data);
        else heap.Push(neighbor_data);
      }
    }
  }

  // Delete temporary data
  heap.Empty();
  delete [] vertex_data;

  // Return distances
  return distances;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rsi, %r13
movq %rdi, %rbp
movslq 0x14(%rdi), %rbx
leaq (,%rbx,8), %rdi
testq %rbx, %rbx
pushq $-0x1
popq %r15
cmovsq %r15, %rdi
callq 0xe030
movq %rax, %r14
pushq $0x28
popq %rcx
movq %rbx, %rax
mulq %rcx
cmovnoq %rax, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r12
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
movq %rbp, 0x18(%rsp)
movq 0x8(%rbp), %rdx
movq %r14, %r15
leaq 0x20(%r12), %rsi
movabsq $-0x4010000000000000, %rdi # imm = 0xBFF0000000000000
movabsq $0x47efffffe0000000, %r8 # imm = 0x47EFFFFFE0000000
cmpq %rax, %rcx
je 0x1a36c
movq (%rdx,%rax,8), %r9
movq %r9, -0x20(%rsi)
andq $0x0, -0x18(%rsi)
movq %rdi, -0x10(%rsi)
andq $0x0, -0x8(%rsi)
movq %r8, (%rsi)
movq %r8, (%r15,%rax,8)
incq %rax
addq $0x28, %rsi
jmp 0x1a341
movb 0xc094e(%rip), %al      # 0xdacc0
testb %al, %al
je 0x1a4cd
xorl %r14d, %r14d
leaq 0xc0904(%rip), %rbx     # 0xdac88
movslq 0xc(%r13), %rax
cmpq %rax, %r14
jge 0x1a3b4
movq (%r13), %rax
movq (%rax,%r14,8), %rax
movslq 0x70(%rax), %rax
imulq $0x28, %rax, %rax
leaq (%r12,%rax), %rsi
andq $0x0, 0x20(%r12,%rax)
movq %rbx, %rdi
callq 0x21f7e
incq %r14
jmp 0x1a384
movq %r15, (%rsp)
cmpl $0x0, 0xc08d1(%rip)     # 0xdac90
je 0x1a4b2
leaq 0xc08bc(%rip), %rdi     # 0xdac88
callq 0x22086
movsd 0x10(%rsp), %xmm1
movq %rax, %rbp
ucomisd 0x78e06(%rip), %xmm1    # 0x931e8
movq (%rax), %r13
movsd 0x20(%rax), %xmm0
jbe 0x1a3f6
ucomisd %xmm1, %xmm0
ja 0x1a4a2
movslq 0x70(%r13), %rax
movq (%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
je 0x1a415
movq 0x8(%rbp), %rcx
movq %rcx, (%rdx,%rax,8)
xorl %r14d, %r14d
movslq 0x14(%r13), %rax
cmpq %rax, %r14
jge 0x1a3b8
movq 0x8(%r13), %rax
movq (%rax,%r14,8), %rbx
movq 0x8(%rbx), %rax
cmpq %r13, %rax
jne 0x1a436
movq 0x10(%rbx), %rax
movslq 0x70(%rax), %rax
imulq $0x28, %rax, %r15
movsd 0x20(%r12,%r15), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x10db2
movsd 0x20(%rsp), %xmm1
addsd 0x20(%rbp), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x1a49a
addq %r12, %r15
movq %rbx, 0x8(%r15)
movsd %xmm0, 0x20(%r15)
movsd 0x78b9a(%rip), %xmm0    # 0x93018
leaq 0xc0803(%rip), %rdi     # 0xdac88
movq %r15, %rsi
ucomisd %xmm1, %xmm0
jbe 0x1a495
callq 0x22126
jmp 0x1a49a
callq 0x21f7e
incq %r14
jmp 0x1a418
cmpl $0x0, 0xc07e7(%rip)     # 0xdac90
jle 0x1a4b2
andl $0x0, 0xc07de(%rip)     # 0xdac90
movq %r12, %rdi
callq 0xe5d0
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc07ec(%rip), %rdi     # 0xdacc0
callq 0xe840
testl %eax, %eax
je 0x1a37a
xorpd %xmm0, %xmm0
movupd %xmm0, 0xc079b(%rip)    # 0xdac88
leaq 0xc0794(%rip), %rsi     # 0xdac88
andq $0x0, 0xc07b4(%rip)     # 0xdacb0
movupd %xmm0, 0xc079c(%rip)    # 0xdaca0
movl $0x1, 0xc07aa(%rip)     # 0xdacb8
movabsq $0x1800000020, %rax     # imm = 0x1800000020
movq %rax, 0xc0779(%rip)     # 0xdac98
leaq 0x7a4a(%rip), %rdi      # 0x21f70
leaq 0xbff6b(%rip), %rdx     # 0xda498
callq 0xe3c0
leaq 0xc0787(%rip), %rdi     # 0xdacc0
callq 0xe2f0
jmp 0x1a37a
nop