// File: 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:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x118(%rsp)
movq %rsi, 0x110(%rsp)
movsd %xmm0, 0x108(%rsp)
movq %rdx, 0x100(%rsp)
movq 0x118(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x135e0
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, 0xf8(%rsp)
cmpq $0x0, 0xf8(%rsp)
jne 0x27fff
leaq 0x10b346(%rip), %rdi    # 0x13332d
movb $0x0, %al
callq 0xd4f30
movq $0x0, 0x120(%rsp)
jmp 0x28586
movq 0x58(%rsp), %rdi
callq 0x135e0
cltq
movl $0x28, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, 0xf0(%rsp)
cmpq $0x0, 0xf0(%rsp)
jne 0x2807b
leaq 0x10b2ac(%rip), %rdi    # 0x1332f2
movb $0x0, %al
callq 0xd4f30
movq 0xf8(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x2806a
movq 0x50(%rsp), %rdi
callq 0xf670
movq $0x0, 0x120(%rsp)
jmp 0x28586
movl $0x0, 0xec(%rsp)
movq 0x58(%rsp), %rdi
movl 0xec(%rsp), %eax
movl %eax, 0x4c(%rsp)
callq 0x135e0
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x28190
movq 0x58(%rsp), %rdi
movl 0xec(%rsp), %esi
callq 0x13630
movq %rax, %rcx
movq 0xf0(%rsp), %rax
movslq 0xec(%rsp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movq 0xf0(%rsp), %rax
movslq 0xec(%rsp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movq 0xf0(%rsp), %rax
movslq 0xec(%rsp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movsd 0x10af03(%rip), %xmm0   # 0x133018
movsd %xmm0, 0x10(%rax)
movq 0xf0(%rsp), %rax
movslq 0xec(%rsp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x18(%rax)
movq 0xf0(%rsp), %rax
movslq 0xec(%rsp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movsd 0x10aec8(%rip), %xmm0   # 0x133020
movsd %xmm0, 0x20(%rax)
movq 0xf8(%rsp), %rax
movslq 0xec(%rsp), %rcx
movsd 0x10aeab(%rip), %xmm0   # 0x133020
movsd %xmm0, (%rax,%rcx,8)
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x28086
cmpb $0x0, 0x150469(%rip)    # 0x178600
jne 0x281fc
leaq 0x150460(%rip), %rdi    # 0x178600
callq 0xf8e0
cmpl $0x0, %eax
je 0x281fc
leaq 0xe0(%rsp), %rdx
leaq 0xd8(%rsp), %rcx
leaq 0x150407(%rip), %rdi    # 0x1785c8
leaq 0xc0(%rsp), %rsi
movl $0x1, %r8d
callq 0x396f0
jmp 0x281d6
leaq 0x115b3(%rip), %rdi     # 0x39790
leaq 0x1503e4(%rip), %rsi    # 0x1785c8
leaq 0x14730d(%rip), %rdx    # 0x16f4f8
callq 0xf420
leaq 0x150409(%rip), %rdi    # 0x178600
callq 0xf310
movl $0x0, 0xb0(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x110(%rsp), %rdi
callq 0x13600
movl %eax, %ecx
movl 0x48(%rsp), %eax
cmpl %ecx, %eax
jge 0x282e0
movq 0x110(%rsp), %rdi
movl 0xb0(%rsp), %esi
callq 0x139a0
movq 0x58(%rsp), %rdi
movq %rax, 0xa8(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xa8(%rsp), %rsi
callq 0x36910
movl %eax, %ecx
movq 0x40(%rsp), %rax
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rax)
movq 0xa0(%rsp), %rsi
leaq 0x150328(%rip), %rdi    # 0x1785c8
callq 0x397d0
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x28207
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x15032a(%rip), %rdi    # 0x178600
callq 0xf300
jmp 0x28596
jmp 0x282e2
leaq 0x1502df(%rip), %rdi    # 0x1785c8
callq 0x399f0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x282ff
jmp 0x2854d
leaq 0x1502c2(%rip), %rdi    # 0x1785c8
callq 0x39a10
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x90(%rsp)
movsd 0x108(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x28355
movq 0x98(%rsp), %rax
movsd 0x20(%rax), %xmm0
ucomisd 0x108(%rsp), %xmm0
jbe 0x28355
jmp 0x2854d
movq 0x58(%rsp), %rdi
movq 0x98(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x90(%rsp), %rsi
callq 0x36910
movsd 0x30(%rsp), %xmm0
movl %eax, %ecx
movq 0x38(%rsp), %rax
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
cmpq $0x0, 0x100(%rsp)
je 0x283ea
movq 0x58(%rsp), %rdi
movq 0x98(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x90(%rsp), %rsi
callq 0x36910
movq 0x20(%rsp), %rdx
movl %eax, %ecx
movq 0x28(%rsp), %rax
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x0, 0x8c(%rsp)
movq 0x58(%rsp), %rdi
movl 0x8c(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x90(%rsp), %rsi
callq 0x36b50
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x28548
movq 0x58(%rsp), %rdi
movq 0x90(%rsp), %rsi
movl 0x8c(%rsp), %edx
callq 0x36ec0
movq 0x58(%rsp), %rdi
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rsi
movq 0x90(%rsp), %rdx
callq 0x36f50
movq 0x58(%rsp), %rdi
movq %rax, 0x78(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x78(%rsp), %rsi
callq 0x36910
movq 0x58(%rsp), %rdi
movl %eax, %ecx
movq 0x10(%rsp), %rax
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x80(%rsp), %rsi
callq 0x13cc0
movq 0x98(%rsp), %rax
addsd 0x20(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm1
movsd 0x68(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x28530
movq 0x80(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x8(%rax)
movsd 0x60(%rsp), %xmm0
movq 0x70(%rsp), %rax
movsd %xmm0, 0x20(%rax)
movsd 0x10ab1e(%rip), %xmm0   # 0x133020
ucomisd 0x68(%rsp), %xmm0
jbe 0x2851d
movq 0x70(%rsp), %rsi
leaq 0x1500b2(%rip), %rdi    # 0x1785c8
callq 0x39b20
jmp 0x2852e
movq 0x70(%rsp), %rsi
leaq 0x15009f(%rip), %rdi    # 0x1785c8
callq 0x397d0
jmp 0x28530
jmp 0x28532
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x283f5
jmp 0x282e2
leaq 0x150074(%rip), %rdi    # 0x1785c8
callq 0x39c20
movq 0xf0(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x28576
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0xf8(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xb8(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)