// File: tomfunkhouser[P]gaps/apps/msh2pts/msh2pts.cpp
// Function: SelectPointsUniformInBBox(gaps::R3Mesh*, int)
static RNArray<Point *> *
SelectPointsUniformInBBox(R3Mesh *mesh, int npoints)
{
  // Get bounding box
  R3Box b = bbox;
  if (b.IsEmpty()) {
    b = mesh->BBox();
    b.Inflate(1.1);
  }
  
  // Create signed distance grid
  R3Grid *signed_distance_grid = CreateSignedDistanceGrid(mesh, b);
  if (!signed_distance_grid) {
    RNFail("Unable to create signed distance grid\n");
    return NULL;
  }

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

  // Sample points randomly from mesh bbox interior
  for (int i = 0; i < npoints; i++) {
    // Sample location
    RNScalar x = RNRandomScalar() * b.XLength() + b.XMin();
    RNScalar y = RNRandomScalar() * b.YLength() + b.YMin();
    RNScalar z = RNRandomScalar() * b.ZLength() + b.ZMin();
    R3Point position(x, y, z);

    // Compute sign 
    RNScalar sdf = signed_distance_grid->WorldValue(position);
    RNScalar sign = (sdf < 0) ? -1 : 1;
    
    // Compute distance to closest surface
    if (kdtree) {
      R3MeshIntersection closest;
      kdtree->FindClosest(position, closest);
      sdf = sign * closest.t;
    }

    // Create point
    Point *point = new Point();
    point->position = position;
    point->normal = R3zero_vector;
    point->sdf = sdf;
    points->Insert(point);
  }

  // Delete kdtree
  if (kdtree) delete kdtree;

  // Delete signed distance grid
  delete signed_distance_grid;

  // Return points
  return points;
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x168(%rsp)
movl %esi, 0x164(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x1789a3(%rip), %rsi    # 0x191598
callq 0x70db0
leaq 0x128(%rsp), %rdi
callq 0x1e010
cmpl $0x0, %eax
je 0x18c5d
movq 0x168(%rsp), %rdi
callq 0x1ba30
movq %rax, %rsi
leaq 0x128(%rsp), %rdi
callq 0x1f340
jmp 0x18c2b
leaq 0x128(%rsp), %rdi
movsd 0x13140d(%rip), %xmm0   # 0x14a048
callq 0x72880
jmp 0x18c42
jmp 0x18c5d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
jmp 0x190fe
movq 0x168(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x1aa10
movq %rax, 0x60(%rsp)
jmp 0x18c79
movq 0x60(%rsp), %rax
movq %rax, 0x110(%rsp)
cmpq $0x0, 0x110(%rsp)
jne 0x18cbd
leaq 0x131a39(%rip), %rdi    # 0x14a6d1
xorl %eax, %eax
callq 0xebe00
jmp 0x18ca1
movq $0x0, 0x170(%rsp)
movl $0x1, 0x10c(%rsp)
jmp 0x190e1
movl $0x10, %edi
callq 0xf4d0
movq %rax, 0x58(%rsp)
jmp 0x18cce
movq 0x58(%rsp), %rdi
callq 0x1b5a0
jmp 0x18cda
movq 0x58(%rsp), %rax
movq %rax, 0x100(%rsp)
cmpq $0x0, 0x100(%rsp)
jne 0x18d46
leaq 0x131916(%rip), %rdi    # 0x14a60f
xorl %eax, %eax
callq 0xebe00
jmp 0x18d02
movq $0x0, 0x170(%rsp)
movl $0x1, 0x10c(%rsp)
jmp 0x190e1
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
movl $0x10, %esi
callq 0xf4e0
jmp 0x190fe
movq $0x0, 0xf8(%rsp)
cmpl $0x0, 0x17889f(%rip)    # 0x1915f8
jne 0x18db7
movl $0x18, %edi
callq 0xf4d0
movq %rax, 0x50(%rsp)
jmp 0x18d6c
movq 0x50(%rsp), %rdi
movq 0x168(%rsp), %rsi
callq 0x1f840
jmp 0x18d80
movq 0x50(%rsp), %rax
movq %rax, 0xf8(%rsp)
jmp 0x18db7
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
movl $0x18, %esi
callq 0xf4e0
jmp 0x190fe
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
cmpl 0x164(%rsp), %eax
jge 0x19064
callq 0xeb5e0
movsd %xmm0, 0x48(%rsp)
jmp 0x18de3
leaq 0x128(%rsp), %rdi
callq 0x1f3a0
movsd %xmm0, 0x40(%rsp)
jmp 0x18df8
leaq 0x128(%rsp), %rdi
callq 0x1cfa0
movsd 0x40(%rsp), %xmm2
movaps %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xe8(%rsp)
callq 0xeb5e0
movsd %xmm0, 0x38(%rsp)
jmp 0x18e32
leaq 0x128(%rsp), %rdi
callq 0x1f3c0
movsd %xmm0, 0x30(%rsp)
jmp 0x18e47
leaq 0x128(%rsp), %rdi
callq 0x1cfe0
movsd 0x30(%rsp), %xmm2
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xe0(%rsp)
callq 0xeb5e0
movsd %xmm0, 0x28(%rsp)
jmp 0x18e81
leaq 0x128(%rsp), %rdi
callq 0x1f3e0
movsd %xmm0, 0x20(%rsp)
jmp 0x18e96
leaq 0x128(%rsp), %rdi
callq 0x1d020
movsd 0x20(%rsp), %xmm2
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0xe8(%rsp), %xmm0
movsd 0xe0(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm2
leaq 0xc0(%rsp), %rdi
callq 0x9cc60
jmp 0x18eed
movq 0x110(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x1f400
movsd %xmm0, 0x18(%rsp)
jmp 0x18f0a
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm1
xorps %xmm0, %xmm0
movl $0x1, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
ucomisd %xmm1, %xmm0
cmoval %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xb0(%rsp)
cmpq $0x0, 0xf8(%rsp)
je 0x18fa7
leaq 0x70(%rsp), %rdi
callq 0x1dff0
jmp 0x18f5a
movq 0xf8(%rsp), %rdi
leaq 0x1785b7(%rip), %rax    # 0x191520
movsd (%rax), %xmm1
xorl %eax, %eax
movl %eax, %r8d
leaq 0xc0(%rsp), %rsi
leaq 0x70(%rsp), %rdx
xorps %xmm0, %xmm0
movq %r8, %rcx
callq 0x22c30
jmp 0x18f8c
movsd 0xb0(%rsp), %xmm0
mulsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movl $0x48, %edi
callq 0xf4d0
movq %rax, 0x10(%rsp)
jmp 0x18fb8
movq 0x10(%rsp), %rdi
callq 0x1ba90
jmp 0x18fc4
movq 0x10(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
addq $0x10, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x9d2f0
jmp 0x18fe6
movq 0x68(%rsp), %rdi
addq $0x28, %rdi
leaq 0x5798aa(%rip), %rsi    # 0x5928a0
callq 0x9ea80
jmp 0x18ffd
movsd 0xb8(%rsp), %xmm0
movq 0x68(%rsp), %rax
movsd %xmm0, 0x40(%rax)
movq 0x100(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x1b540
jmp 0x19024
jmp 0x19026
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x18dc2
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x120(%rsp)
movl %eax, 0x11c(%rsp)
movl $0x48, %esi
callq 0xf4e0
jmp 0x190fe
cmpq $0x0, 0xf8(%rsp)
je 0x1909d
movq 0xf8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x1909b
movq 0x8(%rsp), %rdi
callq 0x1fa00
movq 0x8(%rsp), %rdi
movl $0x18, %esi
callq 0xf4e0
jmp 0x1909d
movq 0x110(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x190c6
movq (%rsp), %rdi
callq 0x849d0
movq (%rsp), %rdi
movl $0x250, %esi            # imm = 0x250
callq 0xf4e0
movq 0x100(%rsp), %rax
movq %rax, 0x170(%rsp)
movl $0x1, 0x10c(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x1b2c0
movq 0x170(%rsp), %rax
addq $0x178, %rsp            # imm = 0x178
retq
leaq 0x128(%rsp), %rdi
callq 0x1b2c0
movq 0x120(%rsp), %rdi
callq 0xf8a0
nopl (%rax,%rax)