// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movsd %xmm0, 0x28(%rsp)
movq %rsi, %rbp
movq %rdi, %rbx
movq %rdx, 0x18(%rsp)
movq %rdx, %rdi
xorl %esi, %esi
callq 0x66528
movq %rbx, 0x30(%rsp)
movslq 0x14(%rbx), %rbx
leaq (,%rbx,8), %rdi
testq %rbx, %rbx
pushq $-0x1
popq %r14
cmovsq %r14, %rdi
callq 0xe030
movq %rax, 0x10(%rsp)
pushq $0x28
popq %rcx
movq %rbx, %rax
mulq %rcx
cmovnoq %rax, %r14
movq %r14, %rdi
callq 0xe030
incl 0xbfe8b(%rip)           # 0xda4c0
movq %rax, 0x8(%rsp)
movb 0xc06c0(%rip), %al      # 0xdad00
testb %al, %al
je 0x1a7fc
xorl %r14d, %r14d
leaq 0xc0676(%rip), %rbx     # 0xdacc8
movslq 0xc(%rbp), %rax
cmpq %rax, %r14
jge 0x1a6a1
movq (%rbp), %rax
movq (%rax,%r14,8), %rax
movl 0xbfe57(%rip), %ecx     # 0xda4c0
movl %ecx, 0x88(%rax)
movslq 0x70(%rax), %rcx
imulq $0x28, %rcx, %rcx
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx), %rsi
movq %rax, (%rdx,%rcx)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x8(%rdx,%rcx)
movupd %xmm0, 0x18(%rdx,%rcx)
movq %rbx, %rdi
callq 0x21f7e
incq %r14
jmp 0x1a652
movq %r15, 0x20(%rsp)
cmpl $0x0, 0xc0623(%rip)     # 0xdacd0
je 0x1a7de
leaq 0xc060e(%rip), %rdi     # 0xdacc8
callq 0x22086
movq %rax, %r14
movsd 0x28(%rsp), %xmm1
ucomisd 0x78b18(%rip), %xmm1    # 0x931e8
movq (%rax), %r12
jbe 0x1a6e5
movsd 0x20(%r14), %xmm0
ucomisd %xmm1, %xmm0
ja 0x1a7ce
movq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x22bfc
movsd 0x20(%r14), %xmm0
movslq 0x70(%r12), %rax
movq 0x10(%rsp), %rcx
movsd %xmm0, (%rcx,%rax,8)
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
je 0x1a719
movq 0x8(%r14), %rcx
movq %rcx, (%rdx,%rax,8)
xorl %r15d, %r15d
movslq 0x14(%r12), %rax
cmpq %rax, %r15
jge 0x1a6a6
movq 0x8(%r12), %rax
movq (%rax,%r15,8), %rbx
movq 0x8(%rbx), %r13
cmpq %r12, %r13
jne 0x1a73c
movq 0x10(%rbx), %r13
movslq 0x70(%r13), %rax
imulq $0x28, %rax, %rbp
addq 0x8(%rsp), %rbp
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x10db2
addsd 0x20(%r14), %xmm0
movl 0xbfd5e(%rip), %eax     # 0xda4c0
cmpl %eax, 0x88(%r13)
jne 0x1a790
movsd 0x20(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1a7c6
movq %rbx, 0x8(%rbp)
movsd %xmm0, 0x20(%rbp)
leaq 0xc0542(%rip), %rdi     # 0xdacc8
movq %rbp, %rsi
callq 0x22126
jmp 0x1a7c6
movl %eax, 0x88(%r13)
movq %r13, (%rbp)
movq %rbx, 0x8(%rbp)
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
movq %rax, 0x10(%rbp)
andq $0x0, 0x18(%rbp)
movsd %xmm0, 0x20(%rbp)
leaq 0xc050a(%rip), %rdi     # 0xdacc8
movq %rbp, %rsi
callq 0x21f7e
incq %r15
jmp 0x1a71c
cmpl $0x0, 0xc04fb(%rip)     # 0xdacd0
jle 0x1a7de
andl $0x0, 0xc04f2(%rip)     # 0xdacd0
movq 0x8(%rsp), %rdi
callq 0xe5d0
movq 0x10(%rsp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc04fd(%rip), %rdi     # 0xdad00
callq 0xe840
testl %eax, %eax
je 0x1a648
xorpd %xmm0, %xmm0
movupd %xmm0, 0xc04ac(%rip)    # 0xdacc8
leaq 0xc04a5(%rip), %rsi     # 0xdacc8
andq $0x0, 0xc04c5(%rip)     # 0xdacf0
movupd %xmm0, 0xc04ad(%rip)    # 0xdace0
movl $0x1, 0xc04bb(%rip)     # 0xdacf8
movabsq $0x1800000020, %rax     # imm = 0x1800000020
movq %rax, 0xc048a(%rip)     # 0xdacd8
leaq 0x771b(%rip), %rdi      # 0x21f70
leaq 0xbfc3c(%rip), %rdx     # 0xda498
callq 0xe3c0
leaq 0xc0498(%rip), %rdi     # 0xdad00
callq 0xe2f0
jmp 0x1a648