// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r13
movsd %xmm0, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movq %rdx, 0x28(%rsp)
movq %rdx, %rdi
xorl %esi, %esi
callq 0x7082c
movq %r14, 0x30(%rsp)
movslq 0x14(%r14), %r14
leaq (,%r14,8), %rdi
testq %r14, %r14
movq $-0x1, %r12
cmovsq %r12, %rdi
callq 0xe030
movq %rax, 0x8(%rsp)
movl $0x28, %ecx
movq %r14, %rax
mulq %rcx
cmovnoq %rax, %r12
movq %r12, %rdi
callq 0xe030
incl 0xc8cf4(%rip)           # 0xe54b0
movq %rax, %r12
movb 0xc952b(%rip), %al      # 0xe5cf0
testb %al, %al
je 0x1c9c9
cmpl $0x0, 0xc(%rbx)
jle 0x1c826
xorl %r15d, %r15d
leaq 0xc94db(%rip), %r14     # 0xe5cb8
movq (%rbx), %rax
movq (%rax,%r15,8), %rax
movl 0xc8cc6(%rip), %ecx     # 0xe54b0
movl %ecx, 0x88(%rax)
movslq 0x70(%rax), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%r12,%rcx,8), %rsi
movq %rax, (%r12,%rcx,8)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%r12,%rcx,8)
movupd %xmm0, 0x18(%r12,%rcx,8)
movq %r14, %rdi
callq 0x245e4
incq %r15
movslq 0xc(%rbx), %rax
cmpq %rax, %r15
jl 0x1c7dd
movq %r12, 0x18(%rsp)
cmpl $0x0, 0xc948e(%rip)     # 0xe5cc0
je 0x1c998
movq %r13, 0x20(%rsp)
leaq 0xc9474(%rip), %rdi     # 0xe5cb8
callq 0x246e0
movq %rax, %r12
movsd 0x10(%rsp), %xmm0
ucomisd 0x8598e(%rip), %xmm0    # 0xa21e8
movq (%rax), %rbx
jbe 0x1c874
movsd 0x20(%r12), %xmm0
movb $0x1, %al
ucomisd 0x10(%rsp), %xmm0
ja 0x1c987
movq 0x28(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %rbx, %rsi
callq 0x70682
movsd 0x20(%r12), %xmm0
movslq 0x70(%rbx), %rax
movq 0x8(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
testq %r13, %r13
je 0x1c8a8
movq 0x8(%r12), %rcx
movq %rcx, (%r13,%rax,8)
cmpl $0x0, 0x14(%rbx)
jle 0x1c985
xorl %ebp, %ebp
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %r14
movq 0x8(%r14), %r15
cmpq %rbx, %r15
jne 0x1c8c9
movq 0x10(%r14), %r15
movslq 0x70(%r15), %r13
testb $0x2, 0x38(%r14)
jne 0x1c8e5
movq 0x30(%rsp), %rdi
movq (%rdi), %rax
movq %r14, %rsi
callq *0x2b8(%rax)
leaq (,%r13,4), %rax
addq %r13, %rax
movsd 0x28(%r14), %xmm0
addsd 0x20(%r12), %xmm0
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movl 0xc8ba4(%rip), %eax     # 0xe54b0
cmpl %eax, 0x88(%r15)
jne 0x1c937
movsd 0x20(%rsi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1c96c
movq %r14, 0x8(%rsi)
movsd %xmm0, 0x20(%rsi)
leaq 0xc9388(%rip), %rdi     # 0xe5cb8
callq 0x2477a
jmp 0x1c96c
movl %eax, 0x88(%r15)
movq %r15, (%rsi)
movq %r14, 0x8(%rsi)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, 0x10(%rsi)
movq $0x0, 0x18(%rsi)
movsd %xmm0, 0x20(%rsi)
leaq 0xc9351(%rip), %rdi     # 0xe5cb8
callq 0x245e4
incq %rbp
movslq 0x14(%rbx), %rax
cmpq %rax, %rbp
jl 0x1c8b4
xorl %eax, %eax
movq 0x20(%rsp), %r13
jmp 0x1c987
xorl %eax, %eax
testb %al, %al
jne 0x1c998
cmpl $0x0, 0xc932e(%rip)     # 0xe5cc0
jne 0x1c83d
cmpl $0x0, 0xc9321(%rip)     # 0xe5cc0
jle 0x1c9ab
movl $0x0, 0xc9315(%rip)     # 0xe5cc0
movq 0x18(%rsp), %rdi
callq 0xe5b0
movq 0x8(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xe9ef
jmp 0x1c7cd
nop