// File: tomfunkhouser[P]gaps/apps/msh2df/msh2df.cpp
// Function: CreatePoints(gaps::R3Grid*, gaps::R3Mesh*, gaps::RNArray<Point*>&)
static int
CreatePoints(R3Grid *grid, R3Mesh *mesh, RNArray<Point *>& points)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Create mesh search tree
  R3MeshSearchTree search_tree(mesh);

  // Create point samples on boundary
  RNLength grid_spacing = grid->GridToWorldScaleFactor();
  RNLength max_distance = refinement_radius * grid_spacing;
  if (max_distance < grid_spacing) max_distance = grid_spacing;
  for (int iz = 0; iz < grid->ZResolution(); iz++) {
    for (int iy = 0; iy < grid->YResolution(); iy++) {
      for (int ix = 0; ix < grid->XResolution(); ix++) {

        // Check if in swath on positive side
        RNScalar grid_distance = grid->GridValue(ix, iy, iz);
        if (grid_distance < 0) continue;
        if (grid_distance > max_distance) continue;

        // Create point samples on boundary
        RNArray<R3MeshIntersection *> hits;
        R3Point world_position = grid->WorldPosition(ix, iy, iz);
        search_tree.FindAll(world_position, hits, 0, grid_distance + RN_EPSILON);
        for (int k = 0; k < hits.NEntries(); k++) {
          R3MeshIntersection *hit = hits.Kth(k);
          Point *point = new Point(hit->point, mesh->FaceNormal(hit->face), grid_spacing);
          points.Insert(point);
          delete hit;
        }
      }
    }
  }
    
  // Create kdtree for computing radii
  Point tmp; int position_offset = (unsigned char *) &(tmp.position) - (unsigned char *) &tmp;
  R3Kdtree<Point *> kdtree(points, position_offset);
  
  // Compute radius for each point
  const int max_neighbors = 6;
  RNScalar neighbor_distances[max_neighbors];
  for (int i = 0; i < points.NEntries(); i++) {
    Point *point = points.Kth(i);
    RNArray<Point *> neighbors;
    point->radius = 0.5 * grid_spacing;
    if (kdtree.FindClosest(point->position, 0, 3*grid_spacing, max_neighbors, neighbors, neighbor_distances)) {
      if (!neighbors.IsEmpty()) {
        RNScalar radius = 0.5 * neighbor_distances[neighbors.NEntries()-1];
        if (radius < point->radius) {
          point->radius = radius;
        }
      }
    }
  }

  // Print statistics
  if (print_verbose) {
    printf("Sampled points on boundary ...\n");
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Points = %d\n", points.NEntries());
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x210(%rsp)
movq %rsi, 0x208(%rsp)
movq %rdx, 0x200(%rsp)
leaq 0x1f0(%rsp), %rdi
callq 0xddf30
leaq 0x1f0(%rsp), %rdi
callq 0x17860
movq 0x208(%rsp), %rsi
leaq 0x1d8(%rsp), %rdi
callq 0x1b120
movq 0x210(%rsp), %rdi
callq 0x17b20
movsd %xmm0, 0x1d0(%rsp)
cvtsi2sdl 0x1691a4(%rip), %xmm0 # 0x17d520
mulsd 0x1d0(%rsp), %xmm0
movsd %xmm0, 0x1c8(%rsp)
movsd 0x1c8(%rsp), %xmm1
movsd 0x1d0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x143b8
movsd 0x1d0(%rsp), %xmm0
movsd %xmm0, 0x1c8(%rsp)
movl $0x0, 0x1c4(%rsp)
movl 0x1c4(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0x210(%rsp), %rdi
callq 0x17b80
movl %eax, %ecx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x146e5
movl $0x0, 0x1c0(%rsp)
movl 0x1c0(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x210(%rsp), %rdi
callq 0x17b60
movl %eax, %ecx
movl 0x50(%rsp), %eax
cmpl %ecx, %eax
jge 0x146cd
movl $0x0, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0x210(%rsp), %rdi
callq 0x17b40
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x146b5
movq 0x210(%rsp), %rdi
movl 0x1bc(%rsp), %esi
movl 0x1c0(%rsp), %edx
movl 0x1c4(%rsp), %ecx
callq 0x17e80
movsd %xmm0, 0x1b0(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x1b0(%rsp), %xmm0
jbe 0x14489
jmp 0x1468d
movsd 0x1b0(%rsp), %xmm0
ucomisd 0x1c8(%rsp), %xmm0
jbe 0x144a2
jmp 0x1468d
leaq 0x1a0(%rsp), %rdi
callq 0x18360
jmp 0x144b1
movq 0x210(%rsp), %rsi
cvtsi2sdl 0x1bc(%rsp), %xmm0
cvtsi2sdl 0x1c0(%rsp), %xmm1
cvtsi2sdl 0x1c4(%rsp), %xmm2
leaq 0x178(%rsp), %rdi
callq 0x87100
jmp 0x144e3
movsd 0x1b0(%rsp), %xmm1
leaq 0x171ffd(%rip), %rax    # 0x1864f0
movsd (%rax), %xmm0
addsd %xmm0, %xmm1
xorl %eax, %eax
movl %eax, %r8d
leaq 0x1d8(%rsp), %rdi
leaq 0x178(%rsp), %rsi
leaq 0x1a0(%rsp), %rdx
xorps %xmm0, %xmm0
movq %r8, %rcx
callq 0x20050
jmp 0x14525
movl $0x0, 0x174(%rsp)
movl 0x174(%rsp), %eax
movl %eax, 0x48(%rsp)
leaq 0x1a0(%rsp), %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x48(%rsp), %eax
cmpl %ecx, %eax
jge 0x14680
movl 0x174(%rsp), %esi
leaq 0x1a0(%rsp), %rdi
callq 0x18370
movq %rax, 0x168(%rsp)
movl $0x38, %edi
callq 0xf4c0
movq %rax, 0x40(%rsp)
jmp 0x14583
movq 0x168(%rsp), %rax
movq %rax, %rcx
addq $0x20, %rcx
movq %rcx, 0x30(%rsp)
movq 0x208(%rsp), %rdi
movq 0x18(%rax), %rsi
callq 0x17fd0
movq %rax, 0x38(%rsp)
jmp 0x145af
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %rdi
movsd 0x1d0(%rsp), %xmm0
callq 0x184e0
jmp 0x145ce
movq 0x40(%rsp), %rax
movq %rax, 0x160(%rsp)
movq 0x200(%rsp), %rdi
movq 0x160(%rsp), %rsi
callq 0x18540
jmp 0x145f2
movq 0x168(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x14614
movq 0x28(%rsp), %rdi
movl $0x40, %esi
callq 0xf4d0
jmp 0x14616
movl 0x174(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x174(%rsp)
jmp 0x14530
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x198(%rsp)
movl %eax, 0x194(%rsp)
jmp 0x1495e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x198(%rsp)
movl %eax, 0x194(%rsp)
jmp 0x146a3
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x198(%rsp)
movl %eax, 0x194(%rsp)
movl $0x38, %esi
callq 0xf4d0
jmp 0x146a3
leaq 0x1a0(%rsp), %rdi
callq 0x183e0
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x14425
leaq 0x1a0(%rsp), %rdi
callq 0x183e0
jmp 0x1495e
jmp 0x146b7
movl 0x1c0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c0(%rsp)
jmp 0x143f4
jmp 0x146cf
movl 0x1c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c4(%rsp)
jmp 0x143c3
leaq 0x128(%rsp), %rdi
callq 0x18570
jmp 0x146f4
movl $0x0, 0x124(%rsp)
movq 0x200(%rsp), %rsi
movl 0x124(%rsp), %edx
leaq 0xc0(%rsp), %rdi
callq 0x185c0
jmp 0x1471d
movl $0x6, 0xbc(%rsp)
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x200(%rsp), %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x148b4
movq 0x200(%rsp), %rdi
movl 0x7c(%rsp), %esi
callq 0x18860
movq %rax, 0x18(%rsp)
jmp 0x1476b
movq 0x18(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x177c0
jmp 0x14781
movsd 0x1d0(%rsp), %xmm0
movsd 0x12b88e(%rip), %xmm1   # 0x140020
mulsd %xmm1, %xmm0
movq 0x70(%rsp), %rax
movsd %xmm0, 0x30(%rax)
movq 0x70(%rsp), %rsi
movsd 0x1d0(%rsp), %xmm1
movsd 0x12b872(%rip), %xmm0   # 0x140028
mulsd %xmm0, %xmm1
leaq 0xc0(%rsp), %rdi
xorps %xmm0, %xmm0
movl $0x6, %edx
leaq 0x60(%rsp), %rcx
leaq 0x80(%rsp), %r8
callq 0x18880
movl %eax, 0x14(%rsp)
jmp 0x147e2
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
je 0x1489a
leaq 0x60(%rsp), %rdi
callq 0x177d0
movl %eax, 0x10(%rsp)
jmp 0x147ff
movl 0x10(%rsp), %eax
cmpl $0x0, %eax
jne 0x14898
leaq 0x60(%rsp), %rdi
callq 0x178e0
subl $0x1, %eax
cltq
movsd 0x12b7fd(%rip), %xmm0   # 0x140020
mulsd 0x80(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm1
movq 0x70(%rsp), %rax
movsd 0x30(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14896
movsd 0x58(%rsp), %xmm0
movq 0x70(%rsp), %rax
movsd %xmm0, 0x30(%rax)
jmp 0x14896
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x198(%rsp)
movl %eax, 0x194(%rsp)
jmp 0x14951
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x198(%rsp)
movl %eax, 0x194(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x177f0
jmp 0x14951
jmp 0x14898
jmp 0x1489a
leaq 0x60(%rsp), %rdi
callq 0x177f0
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x14730
cmpl $0x0, 0x171d3d(%rip)    # 0x1865f8
je 0x1492a
leaq 0x12bb27(%rip), %rdi    # 0x1403eb
xorl %eax, %eax
callq 0xf070
jmp 0x148cd
leaq 0x1f0(%rsp), %rdi
callq 0xddff0
movsd %xmm0, 0x8(%rsp)
jmp 0x148e2
movsd 0x8(%rsp), %xmm0
leaq 0x12b934(%rip), %rdi    # 0x140223
movb $0x1, %al
callq 0xf070
jmp 0x148f8
movq 0x200(%rsp), %rdi
callq 0x178e0
movl %eax, %esi
leaq 0x12bafd(%rip), %rdi    # 0x14040b
xorl %eax, %eax
callq 0xf070
jmp 0x14917
movq 0x168672(%rip), %rax    # 0x17cf90
movq (%rax), %rdi
callq 0xf5d0
jmp 0x14928
jmp 0x1492a
leaq 0xc0(%rsp), %rdi
callq 0x18a40
leaq 0x1d8(%rsp), %rdi
callq 0x1b2e0
movl $0x1, %eax
addq $0x218, %rsp            # imm = 0x218
retq
leaq 0xc0(%rsp), %rdi
callq 0x18a40
leaq 0x1d8(%rsp), %rdi
callq 0x1b2e0
movq 0x198(%rsp), %rdi
callq 0xf890
nopl (%rax,%rax)