// File: /workspace/github_cpp_makefile_star2_O3/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 0x1a0a8
movq %r14, %rdi
callq 0x1bba2
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 0x151b5
leaq 0x8(%r14), %rax
xorl %ecx, %ecx
movl %ecx, -0x8(%rax)
movq $0x0, (%rax)
incq %rcx
addq $0x10, %rax
cmpq %rcx, %r12
jne 0x1519f
movq %rbx, %rdi
callq 0x27ca6
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x151cd
sqrtsd %xmm0, %xmm0
jmp 0x151d2
callq 0xe340
mulsd 0x98e7e(%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 0x1a0a8
cmpl $0x0, 0x14(%rbx)
jle 0x1534e
xorl %ebp, %ebp
movq 0x8(%rbx), %rax
movq (%rax,%rbp,8), %rcx
movq 0x870(%rsp), %rdx
movslq 0x70(%rcx), %rax
movsd (%rdx,%rax,8), %xmm1
xorpd %xmm0, %xmm0
ucomisd 0x98dfc(%rip), %xmm1    # 0xae028
jne 0x1524b
jp 0x1524b
movq %r12, %rdi
movl %ebp, %esi
callq 0x1b9fa
incq %rbp
movslq 0x14(%rbx), %rax
cmpq %rax, %rbp
jl 0x15207
jmp 0x15318
movslq 0x14(%rcx), %rsi
testq %rsi, %rsi
jle 0x15230
movq 0x8(%rcx), %rdi
xorpd %xmm2, %xmm2
xorl %r8d, %r8d
xorl %eax, %eax
movsd 0x98d9f(%rip), %xmm4    # 0xae008
movsd 0x98def(%rip), %xmm3    # 0xae060
movq (%rdi,%r8,8), %r10
movq 0x8(%r10), %r9
cmpq %rcx, %r9
jne 0x15282
movq 0x10(%r10), %r9
movslq 0x70(%r9), %r9
movsd (%rdx,%r9,8), %xmm0
ucomisd 0x98d94(%rip), %xmm0    # 0xae028
jne 0x15298
jnp 0x152b2
movapd %xmm0, %xmm5
minsd %xmm4, %xmm5
addsd %xmm0, %xmm2
maxsd %xmm3, %xmm0
incl %eax
movapd %xmm0, %xmm3
movapd %xmm5, %xmm4
incq %r8
cmpq %r8, %rsi
jne 0x15271
xorpd %xmm0, %xmm0
testl %eax, %eax
je 0x15230
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 0x15230
orb %dl, %cl
je 0x15230
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd %xmm0, %xmm2
subsd %xmm2, %xmm1
andpd 0x98d91(%rip), %xmm1    # 0xae0a0
movapd %xmm1, %xmm0
jmp 0x15230
testl %eax, %eax
leaq 0x460(%rsp), %rbp
jle 0x1534e
movq 0x428(%rsp), %rcx
shlq $0x3, %rax
xorl %edx, %edx
movsd (%rcx,%rdx), %xmm0
addsd 0x8(%r14,%rdx,2), %xmm0
movsd %xmm0, 0x8(%r14,%rdx,2)
addq $0x8, %rdx
cmpq %rdx, %rax
jne 0x15332
movq %rbp, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x1c17e
movq %r12, %rdi
callq 0x1a158
incl %r13d
cmpl $0x4, %r13d
jne 0x151f0
cmpb $0x0, 0xdf934(%rip)     # 0xf4cac
je 0x153f5
leaq 0x9931a(%rip), %rsi     # 0xae69b
leaq 0x460(%rsp), %rdi
callq 0x1ccdc
leaq 0x99313(%rip), %rdx     # 0xae6a8
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x19fa0
cmpl $0x0, 0x14(%rbx)
jle 0x153da
movq %r14, %r13
addq $0x8, %r13
xorl %r15d, %r15d
leaq 0x18(%rsp), %r12
movsd (%r13), %xmm0
movq %r12, %rdi
movl %r15d, %esi
callq 0x1b9fa
incq %r15
movslq 0x14(%rbx), %rax
addq $0x10, %r13
cmpq %rax, %r15
jl 0x153b9
leaq 0x992d7(%rip), %rsi     # 0xae6b8
leaq 0x18(%rsp), %rdi
callq 0x1ccdc
leaq 0x18(%rsp), %rdi
callq 0x1a158
movslq 0x14(%rbx), %rsi
leaq 0x402(%rip), %rcx       # 0x15802
movl $0x10, %edx
movq %r14, %rdi
callq 0xe890
leaq 0xdf0cc(%rip), %r13     # 0xf44e0
incl (%r13)
leaq 0x18(%rsp), %rdi
callq 0x7b688
movl 0x14(%rbx), %eax
testl %eax, %eax
jle 0x15509
xorl %ebp, %ebp
leaq 0x18(%rsp), %r15
movq %rbp, %rcx
shlq $0x4, %rcx
movslq (%r14,%rcx), %rcx
movq 0x8(%rbx), %rdx
movq (%rdx,%rcx,8), %r12
movl 0x88(%r12), %ecx
cmpl (%r13), %ecx
je 0x154fa
cmpl $0x0, 0x14(%r12)
je 0x154fa
movl 0x24(%rsp), %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x7b71e
movsd 0x8(%rsp), %xmm0
ucomisd 0x98bce(%rip), %xmm0    # 0xae050
movl (%r13), %eax
movl %eax, 0x88(%r12)
jbe 0x154e6
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x32456
movslq 0x14(%rbx), %rcx
testq %rcx, %rcx
movsd 0x8(%rsp), %xmm0
jle 0x154d9
movq 0x8(%rbx), %rdx
movl (%r13), %esi
xorl %edi, %edi
movq (%rdx,%rdi,8), %r8
cmpl %esi, 0x88(%r8)
je 0x154d1
ucomisd (%rax,%rdi,8), %xmm0
jb 0x154d1
movl %esi, 0x88(%r8)
incq %rdi
cmpq %rdi, %rcx
jne 0x154b6
testq %rax, %rax
je 0x154e6
movq %rax, %rdi
callq 0xe5c0
cmpl $0x0, 0x4(%rsp)
jle 0x154f7
movl 0x4(%rsp), %eax
cmpl %eax, 0x24(%rsp)
jge 0x15509
movl 0x14(%rbx), %eax
incq %rbp
movslq %eax, %rcx
cmpq %rcx, %rbp
jl 0x15434
movq %r14, %rdi
callq 0xe5c0
movl $0x10, %edi
callq 0xe450
movq %rax, %r14
movq %rax, %rdi
callq 0x7b688
cmpl $0x0, 0x24(%rsp)
jle 0x15570
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 0x15826
movl 0xc(%r14), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x7b71e
incq %r13
movslq 0x24(%rsp), %rax
cmpq %rax, %r13
jl 0x15530
leaq 0x18(%rsp), %rdi
callq 0x7b6f2
leaq 0x460(%rsp), %rdi
callq 0x1a158
movq %r14, %rax
addq $0x8a8, %rsp            # imm = 0x8A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x155e5
jmp 0x155d1
movq %rax, %rbx
movl $0x10, %esi
movq %r14, %rdi
jmp 0x155c8
jmp 0x155d6
jmp 0x155d1
jmp 0x155d1
jmp 0x155d1
jmp 0x155d6
jmp 0x155d1
jmp 0x155d6
jmp 0x155e5
movq %rax, %rbx
movl $0x48, %esi
movq %r15, %rdi
callq 0xe460
jmp 0x155d9
jmp 0x155e5
movq %rax, %rbx
jmp 0x155f2
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x7b6f2
jmp 0x155f2
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x1a158
leaq 0x460(%rsp), %rdi
callq 0x1a158
movq %rbx, %rdi
callq 0xe7f0