// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/../../pkgs/R3Shapes/R3Kdtree.cpp
// Function: gaps::R3Kdtree<gaps::R3SurfelPoint*>::R3Kdtree(gaps::RNArray<gaps::R3SurfelPoint*> const&, gaps::R3Point (*)(gaps::R3SurfelPoint*, void*), void*)
R3Kdtree<PtrType>::
R3Kdtree(const RNArray<PtrType>& points, R3Point (*position_callback)(PtrType, void *), void *position_callback_data)
  : position_offset(-1),
    position_callback(position_callback),
    position_callback_data(position_callback_data),
    npoints(points.NEntries()),
    nnodes(1)
{
  // Create root node
  root = new R3KdtreeNode<PtrType>(NULL);
  assert(root);

  // Determine bounding box 
  bbox = R3null_box;
  for (int i = 0; i < points.NEntries(); i++) {
    bbox.Union(Position(points[i]));
  }

  // Allocate copy of points array (so that it can be sorted)
  PtrType *copy = new PtrType [ points.NEntries() ];
  assert(copy);

  // Copy points
  for (int i = 0; i < points.NEntries(); i++) 
    copy[i] = points[i];

  // Insert points into root
  InsertPoints(root, bbox, copy, points.NEntries());

  // Delete copy of points array
  delete [] copy;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x98(%rsp)
movq 0xb0(%rsp), %rdi
movq %rdi, 0x50(%rsp)
movq %rdi, %rax
movq %rax, 0x48(%rsp)
callq 0xc9d70
movq 0x50(%rsp), %rax
movl $0xffffffff, 0x38(%rax) # imm = 0xFFFFFFFF
movq 0xa0(%rsp), %rcx
movq %rcx, 0x40(%rax)
movq 0x98(%rsp), %rcx
movq %rcx, 0x48(%rax)
movq 0xa8(%rsp), %rdi
callq 0x16d50
movl %eax, %ecx
movq 0x50(%rsp), %rax
movl %ecx, 0x58(%rax)
movl $0x1, 0x5c(%rax)
movl $0x130, %edi            # imm = 0x130
callq 0x13570
movq %rax, 0x58(%rsp)
jmp 0x5b3c4
movq 0x58(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5b7b0
jmp 0x5b3d4
movq 0x48(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rax, 0x50(%rdi)
leaq 0x1a1f1f(%rip), %rsi    # 0x1fd308
callq 0x1bd40
jmp 0x5b3f0
movl $0x0, 0x88(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x5b4c6
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xa8(%rsp), %rdi
movl 0x88(%rsp), %esi
callq 0x5b8a0
movq %rax, 0x38(%rsp)
jmp 0x5b446
movq 0x38(%rsp), %rdx
movq 0x48(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0x5b800
jmp 0x5b45c
movq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0xcb9d0
jmp 0x5b46d
jmp 0x5b46f
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x5b3fb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
jmp 0x5b5c1
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
movl $0x130, %esi            # imm = 0x130
callq 0x13580
jmp 0x5b5c1
movq 0xa8(%rsp), %rdi
callq 0x16d50
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x13030
movq %rax, 0x28(%rsp)
jmp 0x5b4f7
movq 0x28(%rsp), %rax
movq %rax, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x5b560
movq 0xa8(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x5b8a0
movq %rax, 0x18(%rsp)
jmp 0x5b540
movq 0x18(%rsp), %rdx
movq 0x68(%rsp), %rax
movslq 0x64(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x5b509
movq 0x48(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x16d50
movq 0x8(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq 0x10(%rsp), %rcx
movl %eax, %r8d
movq %rdx, %rdi
callq 0x5b8c0
jmp 0x5b5a1
movq 0x68(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x5b5b9
movq (%rsp), %rdi
callq 0x13750
addq $0xb8, %rsp
retq
movq 0x50(%rsp), %rdi
callq 0x1b990
movq 0x90(%rsp), %rdi
callq 0x13970
nopl (%rax,%rax)