// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::DijkstraDistances(gaps::RNArray<gaps::R3MeshVertex*> const&, double, gaps::RNArray<gaps::R3MeshVertex*>&, gaps::R3MeshEdge**) const
RNLength *R3Mesh::
DijkstraDistances(const RNArray<R3MeshVertex *>& source_vertices, RNLength max_distance, RNArray<R3MeshVertex *>& neighbor_vertices, R3MeshEdge **edges) const
{
  // Empty array of neighbor vertices to be returned
  neighbor_vertices.Empty();

  // 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;
  }

  // Increment mesh mark
  R3mesh_mark++;

  // 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];
    source_vertex->mark = R3mesh_mark;
    DijkstraData *data = &vertex_data[ VertexID(source_vertex) ];
    data->vertex = source_vertex;
    data->edge = NULL;
    data->distance_to_destination = 0;
    data->heappointer = NULL;
    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;
    neighbor_vertices.Insert(vertex);
    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 new_distance = EdgeLength(edge) + data->distance;
      if (VertexMark(neighbor_vertex) != R3mesh_mark) {
        neighbor_vertex->mark = R3mesh_mark;
        neighbor_data->vertex = neighbor_vertex;
        neighbor_data->edge = edge;
        neighbor_data->distance_to_destination = RN_UNKNOWN;
        neighbor_data->heappointer = NULL;
        neighbor_data->distance = new_distance;
        heap.Push(neighbor_data);
      }
      else {
        RNScalar old_distance = neighbor_data->distance;
        if (new_distance < old_distance) {
          neighbor_data->edge = edge;
          neighbor_data->distance = new_distance;
          heap.Update(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 %rcx, 0xf8(%rsp)
movq 0x118(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x100(%rsp), %rdi
xorl %esi, %esi
callq 0xd1d30
movq 0x58(%rsp), %rdi
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, 0xf0(%rsp)
cmpq $0x0, 0xf0(%rsp)
jne 0x2870b
leaq 0x10ac3a(%rip), %rdi    # 0x13332d
movb $0x0, %al
callq 0xd4f30
movq $0x0, 0x120(%rsp)
jmp 0x28c5d
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, 0xe8(%rsp)
cmpq $0x0, 0xe8(%rsp)
jne 0x28787
leaq 0x10aba0(%rip), %rdi    # 0x1332f2
movb $0x0, %al
callq 0xd4f30
movq 0xf0(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x28776
movq 0x50(%rsp), %rdi
callq 0xf670
movq $0x0, 0x120(%rsp)
jmp 0x28c5d
movl 0x146d93(%rip), %eax    # 0x16f520
addl $0x1, %eax
movl %eax, 0x146d8a(%rip)    # 0x16f520
cmpb $0x0, 0x14fea3(%rip)    # 0x178640
jne 0x28802
leaq 0x14fe9a(%rip), %rdi    # 0x178640
callq 0xf8e0
cmpl $0x0, %eax
je 0x28802
leaq 0xe0(%rsp), %rdx
leaq 0xd8(%rsp), %rcx
leaq 0x14fe41(%rip), %rdi    # 0x178608
leaq 0xc0(%rsp), %rsi
movl $0x1, %r8d
callq 0x396f0
jmp 0x287dc
leaq 0x10fad(%rip), %rdi     # 0x39790
leaq 0x14fe1e(%rip), %rsi    # 0x178608
leaq 0x146d07(%rip), %rdx    # 0x16f4f8
callq 0xf420
leaq 0x14fe43(%rip), %rdi    # 0x178640
callq 0xf310
movl $0x0, 0xb0(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x110(%rsp), %rdi
callq 0x13600
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2893d
movq 0x110(%rsp), %rdi
movl 0xb0(%rsp), %esi
callq 0x139a0
movq 0x58(%rsp), %rdi
movq %rax, 0xa8(%rsp)
movl 0x146cc6(%rip), %ecx    # 0x16f520
movq 0xa8(%rsp), %rax
movl %ecx, 0x88(%rax)
movq 0xe8(%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 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %rax
movq %rcx, (%rax)
movq 0xa0(%rsp), %rax
movq $0x0, 0x8(%rax)
movq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movq 0xa0(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rax)
movq 0xa0(%rsp), %rsi
leaq 0x14fd0b(%rip), %rdi    # 0x178608
callq 0x397d0
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x2880d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x14fd0d(%rip), %rdi    # 0x178640
callq 0xf300
jmp 0x28c6d
jmp 0x2893f
leaq 0x14fcc2(%rip), %rdi    # 0x178608
callq 0x399f0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2895c
jmp 0x28c24
leaq 0x14fca5(%rip), %rdi    # 0x178608
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 0x289b2
movq 0x98(%rsp), %rax
movsd 0x20(%rax), %xmm0
ucomisd 0x108(%rsp), %xmm0
jbe 0x289b2
jmp 0x28c24
movq 0x100(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x37410
movq 0x58(%rsp), %rdi
movq 0x98(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0xf0(%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, 0xf8(%rsp)
je 0x28a5c
movq 0x58(%rsp), %rdi
movq 0x98(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0xf8(%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 0x28c1f
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 0xe8(%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 0x80(%rsp), %rsi
callq 0x13cc0
movq 0x58(%rsp), %rdi
movq 0x98(%rsp), %rax
addsd 0x20(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x78(%rsp), %rsi
callq 0x37470
cmpl 0x1469e2(%rip), %eax    # 0x16f520
je 0x28bb1
movl 0x1469da(%rip), %ecx    # 0x16f520
movq 0x78(%rsp), %rax
movl %ecx, 0x88(%rax)
movq 0x78(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, (%rax)
movq 0x80(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x70(%rsp), %rax
movsd 0x10a49c(%rip), %xmm0   # 0x133018
movsd %xmm0, 0x10(%rax)
movq 0x70(%rsp), %rax
movq $0x0, 0x18(%rax)
movsd 0x68(%rsp), %xmm0
movq 0x70(%rsp), %rax
movsd %xmm0, 0x20(%rax)
movq 0x70(%rsp), %rsi
leaq 0x14fa5e(%rip), %rdi    # 0x178608
callq 0x397d0
jmp 0x28c07
movq 0x70(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x68(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x28c05
movq 0x80(%rsp), %rcx
movq 0x70(%rsp), %rax
movq %rcx, 0x8(%rax)
movsd 0x68(%rsp), %xmm0
movq 0x70(%rsp), %rax
movsd %xmm0, 0x20(%rax)
movq 0x70(%rsp), %rsi
leaq 0x14fa08(%rip), %rdi    # 0x178608
callq 0x39b20
jmp 0x28c07
jmp 0x28c09
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x28a67
jmp 0x2893f
leaq 0x14f9dd(%rip), %rdi    # 0x178608
callq 0x39c20
movq 0xe8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x28c4d
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0xf0(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
retq
movq 0xb8(%rsp), %rdi
callq 0xf890
nopw (%rax,%rax)