// 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::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, %r15
movsd %xmm0, 0x8(%rsp)
movq %rsi, %r13
movq %rdi, 0x10(%rsp)
movslq 0x14(%rdi), %rbp
leaq (,%rbp,8), %rdi
testq %rbp, %rbp
movq $-0x1, %rbx
cmovsq %rbx, %rdi
callq 0xe030
movq %rax, %rsi
movl $0x28, %ecx
movq %rbp, %rax
mulq %rcx
cmovnoq %rax, %rbx
movq %rbx, %rdi
movq %rsi, %rbx
callq 0xe030
movq %rax, %r12
testq %rbp, %rbp
jle 0x1c50d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
leaq 0x20(%r12), %rcx
xorl %edx, %edx
movabsq $-0x4010000000000000, %rsi # imm = 0xBFF0000000000000
movabsq $0x47efffffe0000000, %rdi # imm = 0x47EFFFFFE0000000
xorl %r8d, %r8d
movq (%rax,%r8,8), %r9
movq %r9, -0x20(%rcx)
movq %rdx, -0x18(%rcx)
movq %rsi, -0x10(%rcx)
movq %rdx, -0x8(%rcx)
movq %rdi, (%rcx)
movq %rdi, (%rbx,%r8,8)
incq %r8
addq $0x28, %rcx
cmpq %r8, %rbp
jne 0x1c4e6
movq %rbx, (%rsp)
movb 0xc9799(%rip), %al      # 0xe5cb0
testb %al, %al
je 0x1c6b7
cmpl $0x0, 0xc(%r13)
jle 0x1c561
xorl %r14d, %r14d
leaq 0xc9748(%rip), %rbx     # 0xe5c78
movq (%r13), %rax
movq (%rax,%r14,8), %rax
movslq 0x70(%rax), %rax
leaq (%rax,%rax,4), %rax
leaq (%r12,%rax,8), %rsi
movq $0x0, 0x20(%r12,%rax,8)
movq %rbx, %rdi
callq 0x245e4
incq %r14
movslq 0xc(%r13), %rax
cmpq %rax, %r14
jl 0x1c530
cmpl $0x0, 0xc9718(%rip)     # 0xe5c80
movq (%rsp), %rbx
je 0x1c68a
movq %r15, 0x18(%rsp)
leaq 0xc96fa(%rip), %rdi     # 0xe5c78
callq 0x246e0
movq %rax, %rbp
movsd 0x8(%rsp), %xmm0
ucomisd 0x85c54(%rip), %xmm0    # 0xa21e8
movq (%rax), %r14
jbe 0x1c5ac
movsd 0x20(%rbp), %xmm0
movb $0x1, %al
ucomisd 0x8(%rsp), %xmm0
ja 0x1c679
movsd 0x20(%rbp), %xmm0
movslq 0x70(%r14), %rax
movsd %xmm0, (%rbx,%rax,8)
testq %r15, %r15
je 0x1c5c7
movq 0x8(%rbp), %rcx
movq %rcx, (%r15,%rax,8)
cmpl $0x0, 0x14(%r14)
jle 0x1c677
xorl %r13d, %r13d
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rbx
movq 0x8(%rbx), %rax
cmpq %r14, %rax
jne 0x1c5ea
movq 0x10(%rbx), %rax
movslq 0x70(%rax), %rax
leaq (%rax,%rax,4), %r15
movsd 0x20(%r12,%r15,8), %xmm1
testb $0x2, 0x38(%rbx)
jne 0x1c61c
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movsd %xmm1, 0x20(%rsp)
callq *0x2b8(%rax)
movsd 0x20(%rsp), %xmm1
movsd 0x28(%rbx), %xmm0
addsd 0x20(%rbp), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x1c65a
leaq (%r12,%r15,8), %rsi
movq %rbx, 0x8(%rsi)
movsd %xmm0, 0x20(%rsi)
movsd 0x859ff(%rip), %xmm0    # 0xa2040
leaq 0xc9630(%rip), %rdi     # 0xe5c78
ucomisd %xmm1, %xmm0
jbe 0x1c655
callq 0x2477a
jmp 0x1c65a
callq 0x245e4
incq %r13
movslq 0x14(%r14), %rax
cmpq %rax, %r13
jl 0x1c5d5
xorl %eax, %eax
movq 0x18(%rsp), %r15
movq (%rsp), %rbx
jmp 0x1c679
xorl %eax, %eax
testb %al, %al
jne 0x1c68a
cmpl $0x0, 0xc95fc(%rip)     # 0xe5c80
jne 0x1c577
cmpl $0x0, 0xc95ef(%rip)     # 0xe5c80
jle 0x1c69d
movl $0x0, 0xc95e3(%rip)     # 0xe5c80
movq %r12, %rdi
callq 0xe5b0
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xe977
jmp 0x1c51f
nop