// File: tomfunkhouser[P]gaps/apps/msh2prp/msh2prp.cpp
// Function: ComputeDijkstraHistogramProperties(gaps::R3Mesh*)
static R3MeshPropertySet *
ComputeDijkstraHistogramProperties(R3Mesh *mesh)
{
  // Parameters
  int nbins = 32;
  int nsamples_in_smallest_bin = 32;
  int nsamples = nsamples_in_smallest_bin * (nbins * nbins) / 2;
  if (nsamples < 3*mesh->NVertices()/2) nsamples = mesh->NVertices();
  if (mesh->NVertices() < 60000) nsamples = mesh->NVertices();

  // Start statistics
  RNTime start_time;
  start_time.Read();
  if (print_verbose) {
    printf("Computing dijkstra histogram properties ...\n");
    fflush(stdout);
  }

  // Allocate property set
  R3MeshPropertySet *properties = new R3MeshPropertySet(mesh);
  if (!properties) {
    RNFail("Unable to allocate property set.\n");
    return NULL;
  }

  // Allocate/insert properties
  for (int i = 0; i < nbins; i++) {
    char name[1024];
    sprintf(name, "DijkstraHistogramBin%d", i);
    R3MeshProperty *property = new R3MeshProperty(mesh, name);
    properties->Insert(property);
  }
 
  // Create a sampled set of vertices
  RNScalar *weights = new RNScalar [ mesh->NVertices() ];
  RNArray<R3MeshVertex *> *samples = CreateVertexSampling(mesh, nsamples, weights);
  if (!samples) {
    RNFail("Unable to sample vertices\n");
    return NULL;
  }

  // Compute normalization factors
  RNScalar area = mesh->Area();
  RNScalar normalization = (area > 0) ? nbins / (1.5 * sqrt(area)) : 1;

  // Compute histogram of distances
  RNScalar total_vote = 0;
  for (int i = 0; i < samples->NEntries(); i++) {
    R3MeshVertex *vertex = samples->Kth(i);
    RNScalar vote = weights[i];

    // Compute distances to other vertices
    RNLength *distances = mesh->DijkstraDistances(vertex);

    // Add values to histogram
    for (int j = 0; j < mesh->NVertices(); j++) {
      RNScalar bin = normalization * distances[j];
      int bin1 = (int) bin;
      int bin2 = bin1 + 1;
      RNScalar t = bin - bin1;
      if (bin1 >= nbins) bin1 = nbins-1;
      if (bin2 >= nbins) bin2 = nbins-1;
      properties->Property(bin1)->AddVertexValue(j, (1-t) * vote);
      properties->Property(bin2)->AddVertexValue(j, t * vote);
    }

    // Delete distances
    delete [] distances;
  }

  // Normalize distribution by total_vote
  if (total_vote > 0) {
    for (int i = 1; i < nbins; i++) {
      R3MeshProperty *property = properties->Property(i);
      property->Divide(total_vote);
    }
  }

  // Make a cumulative distribution 
  for (int i = 1; i < nbins; i++) {
    R3MeshProperty *prev_property = properties->Property(i-1);
    R3MeshProperty *property = properties->Property(i);
    for (int j = 0; j < mesh->NVertices(); j++) {
      property->AddVertexValue(j, prev_property->VertexValue(j));
    }
  }

  // Print statistics
  if (print_verbose) {
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Properties = %d\n", properties->NProperties());
    printf("  # Samples = %d\n", samples->NEntries());
    fflush(stdout);
  }

  // Delete sample points
  delete samples;
  delete [] weights;

  // Return property set
  return properties;
}

// Assembly:
subq $0x548, %rsp            # imm = 0x548
movq %rdi, 0x538(%rsp)
movl $0x20, 0x534(%rsp)
movl $0x20, 0x530(%rsp)
movl 0x530(%rsp), %eax
movl 0x534(%rsp), %ecx
imull 0x534(%rsp), %ecx
imull %ecx, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, 0x52c(%rsp)
movl 0x52c(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0x538(%rsp), %rdi
callq 0x1f2e0
imull $0x3, %eax, %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl 0x60(%rsp), %eax
cmpl %ecx, %eax
jge 0x1a22e
movq 0x538(%rsp), %rdi
callq 0x1f2e0
movl %eax, 0x52c(%rsp)
movq 0x538(%rsp), %rdi
callq 0x1f2e0
cmpl $0xea60, %eax           # imm = 0xEA60
jge 0x1a256
movq 0x538(%rsp), %rdi
callq 0x1f2e0
movl %eax, 0x52c(%rsp)
leaq 0x518(%rsp), %rdi
callq 0xe98e0
leaq 0x518(%rsp), %rdi
callq 0x1f280
cmpl $0x0, 0x1b04c9(%rip)    # 0x1ca740
je 0x1a296
leaq 0x15a2ca(%rip), %rdi    # 0x17454a
movb $0x0, %al
callq 0x11090
movq 0x1a6d02(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movl $0x18, %edi
callq 0x11680
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x58(%rsp)
movq 0x538(%rsp), %rsi
callq 0x24ce0
jmp 0x1a2ba
movq 0x58(%rsp), %rax
movq %rax, 0x510(%rsp)
cmpq $0x0, 0x510(%rsp)
jne 0x1a319
leaq 0x159f7d(%rip), %rdi    # 0x174256
movb $0x0, %al
callq 0xedb00
movq $0x0, 0x540(%rsp)
jmp 0x1a91e
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x508(%rsp)
movl %eax, 0x504(%rsp)
movl $0x18, %esi
callq 0x11690
jmp 0x1a92e
movl $0x0, 0x500(%rsp)
movl 0x500(%rsp), %eax
cmpl 0x534(%rsp), %eax
jge 0x1a3ed
movl 0x500(%rsp), %edx
leaq 0x15a231(%rip), %rsi    # 0x174577
xorl %eax, %eax
movl %eax, 0x4c(%rsp)
leaq 0x100(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x11120
movl $0x448, %edi            # imm = 0x448
callq 0x11680
movq 0x40(%rsp), %rdx
movq %rax, %rdi
movl 0x4c(%rsp), %eax
movq %rdi, %rcx
movq %rcx, 0x50(%rsp)
movq 0x538(%rsp), %rsi
movl %eax, %ecx
callq 0x27ef0
jmp 0x1a38d
movq 0x50(%rsp), %rax
movq %rax, 0xf8(%rsp)
movq 0x510(%rsp), %rdi
movq 0xf8(%rsp), %rsi
callq 0x24e20
movl 0x500(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x500(%rsp)
jmp 0x1a324
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x508(%rsp)
movl %eax, 0x504(%rsp)
movl $0x448, %esi            # imm = 0x448
callq 0x11690
jmp 0x1a92e
movq 0x538(%rsp), %rdi
callq 0x1f2e0
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x11040
movq %rax, 0xf0(%rsp)
movq 0x538(%rsp), %rdi
movl 0x52c(%rsp), %esi
movq 0xf0(%rsp), %rdx
callq 0x1d1a0
movq %rax, 0xe8(%rsp)
cmpq $0x0, 0xe8(%rsp)
jne 0x1a473
leaq 0x15a133(%rip), %rdi    # 0x17458e
movb $0x0, %al
callq 0xedb00
movq $0x0, 0x540(%rsp)
jmp 0x1a91e
movq 0x538(%rsp), %rdi
callq 0x2f940
movsd %xmm0, 0xe0(%rsp)
movsd 0xe0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1a4d9
cvtsi2sdl 0x534(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0xe0(%rsp), %xmm0
callq 0x114f0
movaps %xmm0, %xmm2
movsd 0x30(%rsp), %xmm0
movsd 0x159b87(%rip), %xmm1   # 0x174050
mulsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
jmp 0x1a4e9
movsd 0x159b37(%rip), %xmm0   # 0x174018
movsd %xmm0, 0x38(%rsp)
jmp 0x1a4e9
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x1f300
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1a72a
movq 0xe8(%rsp), %rdi
movl 0xcc(%rsp), %esi
callq 0x1ffb0
movq %rax, 0xc0(%rsp)
movq 0xf0(%rsp), %rax
movslq 0xcc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x538(%rsp), %rdi
movq 0xc0(%rsp), %rsi
xorps %xmm0, %xmm0
xorl %eax, %eax
movl %eax, %edx
callq 0x42780
movq %rax, 0xb0(%rsp)
movl $0x0, 0xac(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x538(%rsp), %rdi
callq 0x1f2e0
movl %eax, %ecx
movl 0x28(%rsp), %eax
cmpl %ecx, %eax
jge 0x1a6f5
movsd 0xd8(%rsp), %xmm0
movq 0xb0(%rsp), %rax
movslq 0xac(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0xa0(%rsp)
cvttsd2si 0xa0(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
movsd 0xa0(%rsp), %xmm0
cvtsi2sdl 0x9c(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0x534(%rsp), %eax
jl 0x1a64c
movl 0x534(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x9c(%rsp)
movl 0x98(%rsp), %eax
cmpl 0x534(%rsp), %eax
jl 0x1a66d
movl 0x534(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x98(%rsp)
movq 0x510(%rsp), %rdi
movl 0x9c(%rsp), %esi
callq 0x1f460
movq %rax, %rdi
movl 0xac(%rsp), %esi
movsd 0x159985(%rip), %xmm0   # 0x174018
subsd 0x90(%rsp), %xmm0
mulsd 0xb8(%rsp), %xmm0
callq 0x2b010
movq 0x510(%rsp), %rdi
movl 0x98(%rsp), %esi
callq 0x1f460
movq %rax, %rdi
movl 0xac(%rsp), %esi
movsd 0x90(%rsp), %xmm0
mulsd 0xb8(%rsp), %xmm0
callq 0x2b010
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x1a59e
movq 0xb0(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x1a712
movq 0x20(%rsp), %rdi
callq 0x118b0
jmp 0x1a714
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x1a50f
movsd 0xd0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1a79e
movl $0x1, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
cmpl 0x534(%rsp), %eax
jge 0x1a79c
movq 0x510(%rsp), %rdi
movl 0x8c(%rsp), %esi
callq 0x1f460
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
movsd 0xd0(%rsp), %xmm0
callq 0x2a4f0
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x1a747
jmp 0x1a79e
movl $0x1, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x534(%rsp), %eax
jge 0x1a85a
movq 0x510(%rsp), %rdi
movl 0x7c(%rsp), %esi
subl $0x1, %esi
callq 0x1f460
movq %rax, 0x70(%rsp)
movq 0x510(%rsp), %rdi
movl 0x7c(%rsp), %esi
callq 0x1f460
movq %rax, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x538(%rsp), %rdi
callq 0x1f2e0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1a848
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x70(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x1ffd0
movq 0x10(%rsp), %rdi
movl 0x18(%rsp), %esi
callq 0x2b010
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x1a7ee
jmp 0x1a84a
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x1a7a6
cmpl $0x0, 0x1afedf(%rip)    # 0x1ca740
je 0x1a8c7
leaq 0x518(%rsp), %rdi
callq 0xe99a0
leaq 0x159995(%rip), %rdi    # 0x17420c
movb $0x1, %al
callq 0x11090
movq 0x510(%rsp), %rdi
callq 0x1f400
movl %eax, %esi
leaq 0x159a23(%rip), %rdi    # 0x1742b7
movb $0x0, %al
callq 0x11090
movq 0xe8(%rsp), %rdi
callq 0x1f300
movl %eax, %esi
leaq 0x159cf8(%rip), %rdi    # 0x1745a9
movb $0x0, %al
callq 0x11090
movq 0x1a66d1(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movq 0xe8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x1a8f3
movq 0x8(%rsp), %rdi
callq 0x1ff40
movq 0x8(%rsp), %rdi
movl $0x10, %esi
callq 0x11690
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x1a90e
movq (%rsp), %rdi
callq 0x118b0
movq 0x510(%rsp), %rax
movq %rax, 0x540(%rsp)
movq 0x540(%rsp), %rax
addq $0x548, %rsp            # imm = 0x548
retq
movq 0x508(%rsp), %rdi
callq 0x11bb0
nopl (%rax,%rax)