// File: tomfunkhouser[P]gaps/apps/msh2df/../../pkgs/R3Shapes/R3Kdtree.cpp
// Function: gaps::R3Kdtree<Point*>::R3Kdtree(gaps::RNArray<Point*> const&, int)
R3Kdtree<PtrType>::
R3Kdtree(const RNArray<PtrType>& points, int position_offset)
  : position_offset(position_offset),
    position_callback(NULL),
    position_callback_data(NULL),
    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)
movl %edx, 0xa4(%rsp)
movq 0xb0(%rsp), %rdi
movq %rdi, 0x58(%rsp)
movq %rdi, %rax
movq %rax, 0x50(%rsp)
callq 0x67090
movq 0x58(%rsp), %rax
movl 0xa4(%rsp), %ecx
movl %ecx, 0x38(%rax)
movq $0x0, 0x40(%rax)
movq $0x0, 0x48(%rax)
movq 0xa8(%rsp), %rdi
callq 0x178e0
movl %eax, %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0x58(%rax)
movl $0x1, 0x5c(%rax)
movl $0x130, %edi            # imm = 0x130
callq 0xf4c0
movq %rax, 0x60(%rsp)
jmp 0x18646
movq 0x60(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x18b70
jmp 0x18656
movq 0x50(%rsp), %rdi
movq 0x60(%rsp), %rax
movq %rax, 0x50(%rdi)
leaq 0x16e3ad(%rip), %rsi    # 0x186a18
callq 0x17760
jmp 0x18672
movl $0x0, 0x90(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x4c(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jge 0x18748
movq 0x50(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xa8(%rsp), %rdi
movl 0x90(%rsp), %esi
callq 0x18c60
movq %rax, 0x40(%rsp)
jmp 0x186c8
movq 0x40(%rsp), %rdx
movq 0x50(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0x18bc0
jmp 0x186de
movq 0x38(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x68cf0
jmp 0x186ef
jmp 0x186f1
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x1867d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0x18845
movq 0x60(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
movl $0x130, %esi            # imm = 0x130
callq 0xf4d0
jmp 0x18845
movq 0xa8(%rsp), %rdi
callq 0x178e0
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xf030
movq %rax, 0x30(%rsp)
jmp 0x18779
movq 0x30(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x187e2
movq 0xa8(%rsp), %rdi
movl 0x6c(%rsp), %esi
callq 0x18c60
movq %rax, 0x20(%rsp)
jmp 0x187c2
movq 0x20(%rsp), %rdx
movq 0x70(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x1878b
movq 0x50(%rsp), %rax
movq 0x50(%rax), %rax
movq %rax, 0x10(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x178e0
movq 0x10(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x18(%rsp), %rcx
movl %eax, %r8d
movq %rdx, %rdi
callq 0x18c80
jmp 0x18823
movq 0x70(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x1883d
movq 0x8(%rsp), %rdi
callq 0xf670
addq $0xb8, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x17670
movq 0x98(%rsp), %rdi
callq 0xf890
nopl (%rax)