// 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::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, 0x18(%rsp)
movq %rsi, %r13
movq %rdi, 0x8(%rsp)
movslq 0x14(%rdi), %rbp
leaq (,%rbp,8), %rdi
testq %rbp, %rbp
movq $-0x1, %rbx
cmovsq %rbx, %rdi
callq 0xe030
movq %rax, %r14
movl $0x28, %ecx
movq %rbp, %rax
mulq %rcx
cmovnoq %rax, %rbx
movq %rbx, %rdi
callq 0xe030
movq %rax, %r12
testq %rbp, %rbp
jle 0x1c560
movq 0x8(%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, (%r14,%r8,8)
incq %r8
addq $0x28, %rcx
cmpq %r8, %rbp
jne 0x1c539
movq %r14, (%rsp)
movb 0xc9746(%rip), %al      # 0xe5cb0
testb %al, %al
je 0x1c6f6
cmpl $0x0, 0xc(%r13)
jle 0x1c5b4
xorl %r14d, %r14d
leaq 0xc96f5(%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 0x243f2
incq %r14
movslq 0xc(%r13), %rax
cmpq %rax, %r14
jl 0x1c583
cmpl $0x0, 0xc96c5(%rip)     # 0xe5c80
je 0x1c6db
movq %r15, 0x10(%rsp)
leaq 0xc96ab(%rip), %rdi     # 0xe5c78
callq 0x24506
movq %rax, %rbp
movsd 0x18(%rsp), %xmm1
ucomisd 0x85c05(%rip), %xmm1    # 0xa21e8
movq (%rax), %r14
movsd 0x20(%rax), %xmm0
jbe 0x1c5f7
ucomisd %xmm1, %xmm0
ja 0x1c6c8
movslq 0x70(%r14), %rax
movq (%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
testq %r15, %r15
je 0x1c611
movq 0x8(%rbp), %rcx
movq %rcx, (%r15,%rax,8)
cmpl $0x0, 0x14(%r14)
jle 0x1c6b4
xorl %r13d, %r13d
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %rbx
movq 0x8(%rbx), %rax
cmpq %r14, %rax
jne 0x1c634
movq 0x10(%rbx), %rax
movslq 0x70(%rax), %rax
leaq (%rax,%rax,4), %r15
movsd 0x20(%r12,%r15,8), %xmm1
testb $0x2, 0x38(%rbx)
jne 0x1c666
movq 0x8(%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 0x1c6a4
leaq (%r12,%r15,8), %rsi
movq %rbx, 0x8(%rsi)
movsd %xmm0, 0x20(%rsi)
movsd 0x859b5(%rip), %xmm0    # 0xa2040
leaq 0xc95e6(%rip), %rdi     # 0xe5c78
ucomisd %xmm1, %xmm0
jbe 0x1c69f
callq 0x245ac
jmp 0x1c6a4
callq 0x243f2
incq %r13
movslq 0x14(%r14), %rax
cmpq %rax, %r13
jl 0x1c61f
cmpl $0x0, 0xc95c5(%rip)     # 0xe5c80
movq 0x10(%rsp), %r15
jne 0x1c5c6
jmp 0x1c6db
cmpl $0x0, 0xc95b1(%rip)     # 0xe5c80
jle 0x1c6db
movl $0x0, 0xc95a5(%rip)     # 0xe5c80
movq %r12, %rdi
callq 0xe5b0
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0xe96d
jmp 0x1c572