// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshProperty.cpp
// Function: gaps::R3MeshProperty::NonExtremumSuppression(double, int, int)
void R3MeshProperty::
NonExtremumSuppression(RNLength radius, RNBoolean keep_local_minima, RNBoolean keep_local_maxima)
{
  // Set every value to unknown if it has a neighbor with a more extreme value
  R3MeshProperty copy(*this);
  for (int i = 0; i < mesh->NVertices(); i++) {
    if (copy.VertexValue(i) == RN_UNKNOWN) continue;
    R3MeshVertex *vertex = mesh->Vertex(i);
    RNScalar vertex_value = copy.VertexValue(i);

    // Find neighborhood of vertex
    RNArray<R3MeshVertex *> neighbor_vertices;
    RNLength *distances = mesh->DijkstraDistances(vertex, radius, neighbor_vertices); 
    for (int j = 0; j < mesh->VertexValence(vertex); j++) neighbor_vertices.Insert(mesh->VertexOnVertex(vertex, j));
    delete [] distances;

    // Check value of vertices within neighborhood
    RNBoolean local_minimum = TRUE;
    RNBoolean local_maximum = TRUE;
    for (int j = 0; j < neighbor_vertices.NEntries(); j++) {
      R3MeshVertex *neighbor_vertex = neighbor_vertices.Kth(j);
      RNScalar neighbor_value = copy.VertexValue(neighbor_vertex);
      if (neighbor_value == RN_UNKNOWN) continue;
      if (neighbor_value < vertex_value) local_minimum = FALSE;
      if (neighbor_value > vertex_value) local_maximum = FALSE;
    }

    // Mask this vertex if not extremum
    if (keep_local_minima && local_minimum) continue;
    if (keep_local_maxima && local_maximum) continue;
    SetVertexValue(i, RN_UNKNOWN);
  }

  // Reset statistics
  ResetStatistics();
}

// Assembly:
subq $0x528, %rsp            # imm = 0x528
movq %rdi, 0x520(%rsp)
movsd %xmm0, 0x518(%rsp)
movl %esi, 0x514(%rsp)
movl %edx, 0x510(%rsp)
movq 0x520(%rsp), %rsi
movq %rsi, 0x58(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x6c4c0
movl $0x0, 0xc4(%rsp)
movq 0x58(%rsp), %rax
movl 0xc4(%rsp), %ecx
movl %ecx, 0x50(%rsp)
movq (%rax), %rdi
callq 0x2abd0
movl %eax, 0x54(%rsp)
jmp 0x6ee89
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x6f1d1
movl 0xc4(%rsp), %esi
leaq 0xc8(%rsp), %rdi
callq 0x2b0a0
movsd %xmm0, 0x48(%rsp)
jmp 0x6eeb5
movsd 0x48(%rsp), %xmm0
movsd 0x12715d(%rip), %xmm1   # 0x196020
ucomisd %xmm1, %xmm0
jne 0x6eee9
jp 0x6eee9
jmp 0x6f1bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
jmp 0x6f1f2
movq 0x58(%rsp), %rax
movq (%rax), %rdi
movl 0xc4(%rsp), %esi
callq 0x2abf0
movq %rax, 0x40(%rsp)
jmp 0x6ef04
movq 0x40(%rsp), %rax
movq %rax, 0xa8(%rsp)
movl 0xc4(%rsp), %esi
leaq 0xc8(%rsp), %rdi
callq 0x2b0a0
movsd %xmm0, 0x38(%rsp)
jmp 0x6ef2d
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x712b0
jmp 0x6ef4b
movq 0x58(%rsp), %rax
movq (%rax), %rdi
movq 0xa8(%rsp), %rsi
movsd 0x518(%rsp), %xmm0
xorl %eax, %eax
movl %eax, %ecx
leaq 0x90(%rsp), %rdx
callq 0x9a840
movq %rax, 0x30(%rsp)
jmp 0x6ef7c
movq 0x30(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movq 0x58(%rsp), %rax
movl 0x84(%rsp), %ecx
movl %ecx, 0x28(%rsp)
movq (%rax), %rdi
movq 0xa8(%rsp), %rsi
callq 0x6c0d0
movl %eax, 0x2c(%rsp)
jmp 0x6efba
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x6f038
movq 0x58(%rsp), %rax
movq (%rax), %rdi
movq 0xa8(%rsp), %rsi
movl 0x84(%rsp), %edx
callq 0x714c0
movq %rax, 0x20(%rsp)
jmp 0x6efe9
movq 0x20(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x71490
jmp 0x6effd
jmp 0x6efff
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x6ef94
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x712e0
jmp 0x6f1f2
movq 0x88(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x6f055
movq 0x18(%rsp), %rdi
callq 0x22d80
movl $0x1, 0x80(%rsp)
movl $0x1, 0x7c(%rsp)
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x10(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x2ac70
movl %eax, 0x14(%rsp)
jmp 0x6f08b
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jge 0x6f144
movl 0x78(%rsp), %esi
leaq 0x90(%rsp), %rdi
callq 0x712c0
movq %rax, 0x8(%rsp)
jmp 0x6f0b3
movq 0x8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x2b020
movsd %xmm0, (%rsp)
jmp 0x6f0d6
movsd (%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
movsd 0x126f31(%rip), %xmm1   # 0x196020
ucomisd %xmm1, %xmm0
jne 0x6f0f9
jp 0x6f0f9
jmp 0x6f134
movsd 0x68(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6f119
movl $0x0, 0x80(%rsp)
movsd 0x68(%rsp), %xmm0
ucomisd 0xa0(%rsp), %xmm0
jbe 0x6f132
movl $0x0, 0x7c(%rsp)
jmp 0x6f134
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x6f070
cmpl $0x0, 0x514(%rsp)
je 0x6f162
cmpl $0x0, 0x80(%rsp)
je 0x6f162
movl $0x4, 0x64(%rsp)
jmp 0x6f1a0
cmpl $0x0, 0x510(%rsp)
je 0x6f17d
cmpl $0x0, 0x7c(%rsp)
je 0x6f17d
movl $0x4, 0x64(%rsp)
jmp 0x6f1a0
movq 0x58(%rsp), %rdi
movl 0xc4(%rsp), %esi
movsd 0x126e8f(%rip), %xmm0   # 0x196020
callq 0x6f210
jmp 0x6f198
movl $0x0, 0x64(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x712e0
movl 0x64(%rsp), %eax
testl %eax, %eax
je 0x6f1b9
jmp 0x6f1b7
jmp 0x6f1bb
jmp 0x6f1bb
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x6ee6b
movq 0x58(%rsp), %rdi
callq 0x6c450
jmp 0x6f1dd
leaq 0xc8(%rsp), %rdi
callq 0x6c640
addq $0x528, %rsp            # imm = 0x528
retq
leaq 0xc8(%rsp), %rdi
callq 0x6c640
movq 0xb8(%rsp), %rdi
callq 0x23270
nopl (%rax)