// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/msh2pts/msh2pts.cpp
// Function: SelectScaleSpaceExtrema(gaps::R3Mesh*, gaps::R3MeshProperty*, int, double, int)
static RNArray<Point *> *
SelectScaleSpaceExtrema(R3Mesh *mesh, R3MeshProperty *prp,
  int npoints, double min_spacing, int extrema_type)
{
  // Copy property 
  R3MeshProperty property(*prp);

  // Normalize property
  property.Normalize();

  // Allocate scores
  ScaleSpaceScore *scores = new ScaleSpaceScore [ mesh->NVertices() ];
  for (int i = 0; i < mesh->NVertices(); i++) {
    scores[i].vertex_index = i;
    scores[i].score = 0;
  }

  // Compute scale space scores
  int nblurs = 4;
  RNScalar weight = 1;
  RNScalar sigma = 0.01 * sqrt(mesh->Area());
  for (int blur = 0; blur < nblurs; blur++) { 
    // Compute strength of every vertex
    R3MeshProperty vote(property);
    for (int i = 0; i < mesh->NVertices(); i++) {
      R3MeshVertex *vertex = mesh->Vertex(i);
      RNScalar value = VoteValue(mesh, vertex, &property, extrema_type);
      vote.SetVertexValue(i, value); 
    }

    // Vote for vertex with its normalized strength value (augmented if local extremum)
    for (int i = 0; i < mesh->NVertices(); i++) {
      scores[i].score += weight * vote.VertexValue(i);
    }

    // Blur the property and iterate
    property.Blur(sigma);
    // weight *= 2;
    // sigma *= 2;
  }


  if (print_debug) {
    property.Write("property.val");
    R3MeshProperty foo(mesh, "ScaleSpaceScore");
    for (int i = 0; i < mesh->NVertices(); i++) foo.SetVertexValue(i, scores[i].score);
    foo.Write("scores.val");
  }

  // Sort vertices by score
  qsort(scores, mesh->NVertices(), sizeof(ScaleSpaceScore), CompareScaleSpaceScores);

  // Select vertices taking into account min_spacing
  R3mesh_mark++;
  RNArray<R3MeshVertex *> selected_vertices;
  for (int i = 0; i < mesh->NVertices(); i++) {
    R3MeshVertex *vertex = mesh->Vertex(scores[i].vertex_index);
    
    // Check if vertex is marked (i.e., too close to vertex previously selected)
    if (mesh->VertexMark(vertex) == R3mesh_mark) continue;

    // Check if vertex is disconnected completely
    if (mesh->VertexValence(vertex) == 0) continue;
    
    // Select vertex
    selected_vertices.Insert(vertex);
    
    // Mark nearby vertices
    mesh->SetVertexMark(vertex, R3mesh_mark);
    if (min_spacing > 0) {
      RNLength *distances = mesh->DijkstraDistances(vertex, min_spacing);
      for (int j = 0; j < mesh->NVertices(); j++) {
        R3MeshVertex *nearby_vertex = mesh->Vertex(j);
        if (mesh->VertexMark(nearby_vertex) != R3mesh_mark) {
          if (distances[j] <= min_spacing) {
            mesh->SetVertexMark(nearby_vertex, R3mesh_mark);
          }
        }
      } 
      delete [] distances;
    }

    // Check if found all points
    if ((npoints > 0) && (selected_vertices.NEntries() >= npoints)) break;
  }

  // Delete scores
  delete [] scores;

  // Allocate array of points
  RNArray<Point *> *points = new RNArray<Point *>();
  if (!points) {
    RNFail("Unable to allocate array of points\n");
    return NULL;
  }

  // Create points
  for (int i = 0; i < selected_vertices.NEntries(); i++) {
    R3MeshVertex *vertex = selected_vertices[i];
    Point *point = new Point(mesh, vertex);
    points->Insert(point);
  }

  // Return points
  return points;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8b8, %rsp            # imm = 0x8B8
movl %ecx, %r15d
movsd %xmm0, 0x10(%rsp)
movl %edx, 0xc(%rsp)
movq %rdi, %rbx
leaq 0x470(%rsp), %r14
movq %r14, %rdi
callq 0x191b8
movq %r14, %rdi
callq 0x1aa74
movslq 0x14(%rbx), %r12
movq %r12, %rax
shlq $0x4, %rax
testq %r12, %r12
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
xorl %eax, %eax
testl %r12d, %r12d
cmovlel %eax, %r12d
leaq 0x8(%r14), %rcx
cmpq %rax, %r12
je 0x1425b
movl %eax, -0x8(%rcx)
andq $0x0, (%rcx)
incq %rax
addq $0x10, %rcx
jmp 0x14246
movq %rbx, %rdi
callq 0x25642
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x14273
sqrtsd %xmm0, %xmm0
jmp 0x14278
callq 0xe350
mulsd 0x89dd8(%rip), %xmm0    # 0x9e058
movsd %xmm0, 0x20(%rsp)
leaq 0x28(%rsp), %r12
leaq 0x470(%rsp), %rbp
xorl %eax, %eax
movl %eax, 0x1c(%rsp)
cmpl $0x4, %eax
je 0x1441b
movq %r12, %rdi
movq %rbp, %r13
movq %rbp, %rsi
callq 0x191b8
xorl %ebp, %ebp
movslq 0x14(%rbx), %rax
cmpq %rax, %rbp
jge 0x143c1
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %rcx
movslq 0x70(%rcx), %rax
movq 0x880(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd 0x89d3c(%rip), %xmm1    # 0x9e020
jne 0x142f7
jp 0x142f7
movq %r12, %rdi
movl %ebp, %esi
callq 0x1a8fc
incq %rbp
jmp 0x142b2
movl 0x14(%rcx), %esi
xorl %edi, %edi
testl %esi, %esi
cmovlel %edi, %esi
xorpd %xmm2, %xmm2
movsd 0x89d53(%rip), %xmm3    # 0x9e060
movsd 0x89cf3(%rip), %xmm4    # 0x9e008
xorl %eax, %eax
cmpq %rdi, %rsi
je 0x14366
movq 0x8(%rcx), %r8
movq (%r8,%rdi,8), %r9
movq 0x8(%r9), %r8
cmpq %rcx, %r8
jne 0x14331
movq 0x10(%r9), %r8
movslq 0x70(%r8), %r8
movsd (%rdx,%r8,8), %xmm0
ucomisd 0x89cdd(%rip), %xmm0    # 0x9e020
jne 0x14347
jnp 0x14361
movapd %xmm0, %xmm5
minsd %xmm4, %xmm5
addsd %xmm0, %xmm2
maxsd %xmm3, %xmm0
incl %eax
movapd %xmm0, %xmm3
movapd %xmm5, %xmm4
incq %rdi
jmp 0x14317
xorpd %xmm0, %xmm0
testl %eax, %eax
je 0x142e8
cmpl $0xa, %r15d
setne %cl
cmpl $0xb, %r15d
setne %dl
ucomisd %xmm4, %xmm1
setb %sil
orb %dl, %sil
ucomisd %xmm1, %xmm3
setb %dl
cmpb $0x1, %sil
jne 0x142e8
orb %dl, %cl
je 0x142e8
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm2
subsd %xmm2, %xmm1
andpd 0x89cd8(%rip), %xmm1    # 0x9e090
movapd %xmm1, %xmm0
jmp 0x142e8
movq 0x438(%rsp), %rcx
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
shlq $0x3, %rax
xorl %edx, %edx
movq %r13, %rbp
cmpq %rdx, %rax
je 0x143fa
movsd (%rcx,%rdx), %xmm0
addsd 0x8(%r14,%rdx,2), %xmm0
movsd %xmm0, 0x8(%r14,%rdx,2)
addq $0x8, %rdx
jmp 0x143dc
movq %rbp, %rdi
movsd 0x20(%rsp), %xmm0
callq 0x1afa0
movq %r12, %rdi
callq 0x1925c
movl 0x1c(%rsp), %eax
incl %eax
jmp 0x14295
cmpb $0x0, 0xd48aa(%rip)     # 0xe8ccc
je 0x1449b
leaq 0x8a270(%rip), %rsi     # 0x9e69b
leaq 0x470(%rsp), %rdi
callq 0x1b9aa
leaq 0x8a269(%rip), %rdx     # 0x9e6a8
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x190c0
movq %r14, %r13
addq $0x8, %r13
xorl %r15d, %r15d
leaq 0x28(%rsp), %r12
movslq 0x14(%rbx), %rax
cmpq %rax, %r15
jge 0x14480
movsd (%r13), %xmm0
movq %r12, %rdi
movl %r15d, %esi
callq 0x1a8fc
incq %r15
addq $0x10, %r13
jmp 0x1445d
leaq 0x8a231(%rip), %rsi     # 0x9e6b8
leaq 0x28(%rsp), %rdi
callq 0x1b9aa
leaq 0x28(%rsp), %rdi
callq 0x1925c
movslq 0x14(%rbx), %rsi
leaq 0x3b7(%rip), %rcx       # 0x1485d
pushq $0x10
popq %rdx
movq %r14, %rdi
callq 0xe8c0
leaq 0xd4048(%rip), %r13     # 0xe8500
incl (%r13)
leaq 0x28(%rsp), %rdi
callq 0x7132e
leaq 0x28(%rsp), %r15
xorl %ebp, %ebp
movslq 0x14(%rbx), %rax
cmpq %rax, %rbp
jge 0x145ab
movq %rbp, %rax
shlq $0x4, %rax
movslq (%r14,%rax), %rax
movq 0x8(%rbx), %rcx
movq (%rcx,%rax,8), %r12
movl 0x88(%r12), %eax
cmpl (%r13), %eax
je 0x145a3
cmpl $0x0, 0x14(%r12)
je 0x145a3
movq %r15, %rdi
movq %r12, %rsi
callq 0x14bcc
movsd 0x10(%rsp), %xmm0
ucomisd 0x89b2c(%rip), %xmm0    # 0x9e050
movl (%r13), %eax
movl %eax, 0x88(%r12)
jbe 0x14592
movq %rbx, %rdi
movq %r12, %rsi
movsd 0x10(%rsp), %xmm0
xorl %edx, %edx
callq 0x2e39e
movl 0x14(%rbx), %ecx
movl (%r13), %edx
testl %ecx, %ecx
movl $0x0, %esi
cmovlel %esi, %ecx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x14585
movq 0x8(%rbx), %rdi
movq (%rdi,%rsi,8), %rdi
cmpl %edx, 0x88(%rdi)
je 0x14580
movsd 0x10(%rsp), %xmm0
ucomisd (%rax,%rsi,8), %xmm0
jb 0x14580
movl %edx, 0x88(%rdi)
incq %rsi
jmp 0x14558
testq %rax, %rax
je 0x14592
movq %rax, %rdi
callq 0xe5e0
cmpl $0x0, 0xc(%rsp)
jle 0x145a3
movl 0xc(%rsp), %eax
cmpl %eax, 0x34(%rsp)
jge 0x145ab
incq %rbp
jmp 0x144cd
movq %r14, %rdi
callq 0xe5e0
pushq $0x10
popq %rdi
callq 0xe460
movq %rax, %r14
movq %rax, %rdi
callq 0x7132e
xorl %ebp, %ebp
pushq $0x48
popq %r15
movslq 0x34(%rsp), %rax
cmpq %rax, %rbp
jge 0x14608
movq 0x28(%rsp), %rax
movq (%rax,%rbp,8), %r13
movq %r15, %rdi
callq 0xe460
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x148c6
movq %r14, %rdi
movq %r12, %rsi
callq 0x14bcc
incq %rbp
jmp 0x145cc
leaq 0x28(%rsp), %rdi
callq 0x7139c
leaq 0x470(%rsp), %rdi
callq 0x1925c
movq %r14, %rax
addq $0x8b8, %rsp            # imm = 0x8B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x14679
jmp 0x14665
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r14, %rdi
jmp 0x1465c
jmp 0x1466a
jmp 0x14665
jmp 0x14665
jmp 0x14665
jmp 0x14665
jmp 0x1466a
jmp 0x1466a
jmp 0x14679
movq %rax, %rbx
pushq $0x48
popq %rsi
movq %r12, %rdi
callq 0xe470
jmp 0x1466d
jmp 0x14679
movq %rax, %rbx
jmp 0x14686
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x7139c
jmp 0x14686
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x1925c
leaq 0x470(%rsp), %rdi
callq 0x1925c
movq %rbx, %rdi
callq 0xe820