// File: /workspace/github_cpp_makefile_star2_O1/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 $0x8a8, %rsp            # imm = 0x8A8
movl %ecx, %r15d
movsd %xmm0, 0x8(%rsp)
movl %edx, 0x4(%rsp)
movq %rdi, %rbx
leaq 0x460(%rsp), %r14
movq %r14, %rdi
callq 0x1a2b8
movq %r14, %rdi
callq 0x1bd9c
movslq 0x14(%rbx), %r12
movq %r12, %rax
shlq $0x4, %rax
testq %r12, %r12
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
testl %r12d, %r12d
jle 0x1531a
leaq 0x8(%r14), %rax
xorl %ecx, %ecx
movl %ecx, -0x8(%rax)
movq $0x0, (%rax)
incq %rcx
addq $0x10, %rax
cmpq %rcx, %r12
jne 0x15304
movq %rbx, %rdi
callq 0x28474
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x15332
sqrtsd %xmm0, %xmm0
jmp 0x15337
callq 0xe340
mulsd 0x98d19(%rip), %xmm0    # 0xae058
movsd %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
leaq 0x18(%rsp), %r12
leaq 0x460(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x1a2b8
movl 0x14(%rbx), %ecx
testl %ecx, %ecx
jle 0x1549a
xorl %ebp, %ebp
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %rcx
movslq 0x70(%rcx), %rax
movq 0x870(%rsp), %rdx
movsd (%rdx,%rax,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd 0x98c96(%rip), %xmm1    # 0xae028
jne 0x153b1
jp 0x153b1
movq %r12, %rdi
movl %ebp, %esi
callq 0x1bbf4
incq %rbp
movslq 0x14(%rbx), %rcx
cmpq %rcx, %rbp
jl 0x1536d
jmp 0x15492
movslq 0x14(%rcx), %rsi
testq %rsi, %rsi
jle 0x15422
movq 0x8(%rcx), %rdi
xorpd %xmm2, %xmm2
xorl %r8d, %r8d
xorl %eax, %eax
movsd 0x98c39(%rip), %xmm4    # 0xae008
movsd 0x98c89(%rip), %xmm3    # 0xae060
movq (%rdi,%r8,8), %r10
movq 0x8(%r10), %r9
cmpq %rcx, %r9
jne 0x153e8
movq 0x10(%r10), %r9
movslq 0x70(%r9), %r9
movsd (%rdx,%r9,8), %xmm5
ucomisd 0x98c2e(%rip), %xmm5    # 0xae028
jne 0x153fe
jnp 0x15418
movapd %xmm5, %xmm6
minsd %xmm4, %xmm6
addsd %xmm5, %xmm2
maxsd %xmm3, %xmm5
incl %eax
movapd %xmm5, %xmm3
movapd %xmm6, %xmm4
incq %r8
cmpq %r8, %rsi
jne 0x153d7
jmp 0x15438
xorl %eax, %eax
movsd 0x98c34(%rip), %xmm3    # 0xae060
movsd 0x98bd4(%rip), %xmm4    # 0xae008
xorpd %xmm2, %xmm2
testl %eax, %eax
je 0x15396
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 0x15396
orb %dl, %cl
je 0x15396
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm2
subsd %xmm2, %xmm1
andpd 0x98c17(%rip), %xmm1    # 0xae0a0
movapd %xmm1, %xmm0
jmp 0x15396
leaq 0x460(%rsp), %rbp
testl %ecx, %ecx
jle 0x154ca
movq 0x428(%rsp), %rax
movl %ecx, %ecx
shlq $0x3, %rcx
xorl %edx, %edx
movsd (%rax,%rdx), %xmm0
addsd 0x8(%r14,%rdx,2), %xmm0
movsd %xmm0, 0x8(%r14,%rdx,2)
addq $0x8, %rdx
cmpq %rdx, %rcx
jne 0x154ae
movq %rbp, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x1c3a2
movq %r12, %rdi
callq 0x1a36c
incl %r13d
cmpl $0x4, %r13d
jne 0x15355
cmpb $0x0, 0xdf7b8(%rip)     # 0xf4cac
je 0x15571
leaq 0x991a1(%rip), %rsi     # 0xae69e
leaq 0x460(%rsp), %rdi
callq 0x1cf16
leaq 0x9919a(%rip), %rdx     # 0xae6ab
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x1a1b0
cmpl $0x0, 0x14(%rbx)
jle 0x15556
movq %r14, %r13
addq $0x8, %r13
xorl %r15d, %r15d
leaq 0x18(%rsp), %r12
movsd (%r13), %xmm0
movq %r12, %rdi
movl %r15d, %esi
callq 0x1bbf4
incq %r15
movslq 0x14(%rbx), %rax
addq $0x10, %r13
cmpq %rax, %r15
jl 0x15535
leaq 0x9915e(%rip), %rsi     # 0xae6bb
leaq 0x18(%rsp), %rdi
callq 0x1cf16
leaq 0x18(%rsp), %rdi
callq 0x1a36c
movslq 0x14(%rbx), %rsi
leaq 0x411(%rip), %rcx       # 0x1598d
movl $0x10, %edx
movq %r14, %rdi
callq 0xe890
leaq 0xdef50(%rip), %r13     # 0xf44e0
incl (%r13)
leaq 0x18(%rsp), %rdi
callq 0x7c644
cmpl $0x0, 0x14(%rbx)
jle 0x15694
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %r15, %rax
shlq $0x4, %rax
movslq (%r14,%rax), %rax
movq 0x8(%rbx), %rcx
movq (%rcx,%rax,8), %r12
movl 0x88(%r12), %ecx
movl $0x13, %eax
cmpl (%r13), %ecx
je 0x1567b
cmpl $0x0, 0x14(%r12)
je 0x1567b
movl 0x24(%rsp), %edx
leaq 0x18(%rsp), %rdi
movq %r12, %rsi
callq 0x7c6da
movsd 0x8(%rsp), %xmm0
ucomisd 0x98a4e(%rip), %xmm0    # 0xae050
movl (%r13), %eax
movl %eax, 0x88(%r12)
jbe 0x15666
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x32b8c
movslq 0x14(%rbx), %rcx
testq %rcx, %rcx
movsd 0x8(%rsp), %xmm0
jle 0x15659
movq 0x8(%rbx), %rdx
movl (%r13), %esi
xorl %edi, %edi
movq (%rdx,%rdi,8), %r8
cmpl %esi, 0x88(%r8)
je 0x15651
ucomisd (%rax,%rdi,8), %xmm0
jb 0x15651
movl %esi, 0x88(%r8)
incq %rdi
cmpq %rdi, %rcx
jne 0x15636
testq %rax, %rax
je 0x15666
movq %rax, %rdi
callq 0xe5c0
movl 0x4(%rsp), %ecx
cmpl %ecx, 0x24(%rsp)
movl $0x11, %eax
cmovll %ebp, %eax
testl %ecx, %ecx
cmovlel %ebp, %eax
cmpl $0x13, %eax
je 0x15684
testl %eax, %eax
jne 0x15694
incq %r15
movslq 0x14(%rbx), %rax
cmpq %rax, %r15
jl 0x155ad
movq %r14, %rdi
callq 0xe5c0
movl $0x10, %edi
callq 0xe450
movq %rax, %r14
movq %rax, %rdi
callq 0x7c644
cmpl $0x0, 0x24(%rsp)
jle 0x156fb
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq (%rax,%r13,8), %r12
movl $0x48, %edi
callq 0xe450
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x159b2
movl 0xc(%r14), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x7c6da
incq %r13
movslq 0x24(%rsp), %rax
cmpq %rax, %r13
jl 0x156bb
leaq 0x18(%rsp), %rdi
callq 0x7c6ae
leaq 0x460(%rsp), %rdi
callq 0x1a36c
movq %r14, %rax
addq $0x8a8, %rsp            # imm = 0x8A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15770
jmp 0x1575c
movq %rax, %rbx
movl $0x10, %esi
movq %r14, %rdi
jmp 0x15753
jmp 0x15761
jmp 0x1575c
jmp 0x1575c
jmp 0x1575c
jmp 0x15761
jmp 0x1575c
jmp 0x15761
jmp 0x15770
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
callq 0xe460
jmp 0x15764
jmp 0x15770
movq %rax, %rbx
jmp 0x1577d
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x7c6ae
jmp 0x1577d
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x1a36c
leaq 0x460(%rsp), %rdi
callq 0x1a36c
movq %rbx, %rdi
callq 0xe7f0