// File: /workspace/github_cpp_makefile_star2_O3/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, 0x28(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movq %rdx, 0x20(%rsp)
movq %rdx, %rdi
xorl %esi, %esi
callq 0x6fa6c
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 0xc8cb6(%rip)           # 0xe54b0
movq %rax, %r12
movb 0xc94ed(%rip), %al      # 0xe5cf0
testb %al, %al
je 0x1c9fb
cmpl $0x0, 0xc(%rbx)
jle 0x1c864
xorl %r15d, %r15d
leaq 0xc949d(%rip), %r14     # 0xe5cb8
movq (%rbx), %rax
movq (%rax,%r15,8), %rax
movl 0xc8c88(%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 0x243f2
incq %r15
movslq 0xc(%rbx), %rax
cmpq %rax, %r15
jl 0x1c81b
movq %r12, 0x10(%rsp)
cmpl $0x0, 0xc9450(%rip)     # 0xe5cc0
je 0x1c9dd
movq %r13, 0x18(%rsp)
leaq 0xc9436(%rip), %rdi     # 0xe5cb8
callq 0x24506
movq %rax, %r12
movsd 0x28(%rsp), %xmm1
ucomisd 0x85950(%rip), %xmm1    # 0xa21e8
movq (%rax), %rbx
jbe 0x1c8ae
movsd 0x20(%r12), %xmm0
ucomisd %xmm1, %xmm0
ja 0x1c9ca
movq 0x20(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %rbx, %rsi
callq 0x6f8a2
movsd 0x20(%r12), %xmm0
movslq 0x70(%rbx), %rax
movq 0x8(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
testq %r13, %r13
je 0x1c8e2
movq 0x8(%r12), %rcx
movq %rcx, (%r13,%rax,8)
cmpl $0x0, 0x14(%rbx)
jle 0x1c9b6
xorl %ebp, %ebp
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %r14
movq 0x8(%r14), %r15
cmpq %rbx, %r15
jne 0x1c903
movq 0x10(%r14), %r15
movslq 0x70(%r15), %r13
testb $0x2, 0x38(%r14)
jne 0x1c91f
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 0x10(%rsp), %rcx
leaq (%rcx,%rax,8), %rsi
movl 0xc8b6a(%rip), %eax     # 0xe54b0
cmpl %eax, 0x88(%r15)
jne 0x1c971
movsd 0x20(%rsi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1c9a6
movq %r14, 0x8(%rsi)
movsd %xmm0, 0x20(%rsi)
leaq 0xc934e(%rip), %rdi     # 0xe5cb8
callq 0x245ac
jmp 0x1c9a6
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 0xc9317(%rip), %rdi     # 0xe5cb8
callq 0x243f2
incq %rbp
movslq 0x14(%rbx), %rax
cmpq %rax, %rbp
jl 0x1c8ee
cmpl $0x0, 0xc9303(%rip)     # 0xe5cc0
movq 0x18(%rsp), %r13
jne 0x1c87b
jmp 0x1c9dd
cmpl $0x0, 0xc92ef(%rip)     # 0xe5cc0
jle 0x1c9dd
movl $0x0, 0xc92e3(%rip)     # 0xe5cc0
movq 0x10(%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 0xe9db
jmp 0x1c80b
nop