// File: jingdao[P]PointCloudApp/kdtree.cpp
// Function: KdTree::buildKdTreeNode(int*, int, int)
KdTree::KdTreeNode* KdTree::buildKdTreeNode(int* pointList, int n, int depth) {
	if (depth > kdtreeDepth)
		kdtreeDepth = depth;
	if (n == 1) //only one point
		return kdtreeLeaves + *pointList; 
	int dimension = depth % 3;

	//find median element
	findMedian(pointList,n,n/2,dimension); //partitions around median
	int medianIndex = n / 2;
	int median = pointList[medianIndex];
	float medianValue = float_data[median * 4 + dimension];
	KdTreeNode* currentNode = (KdTreeNode*) calloc(1,sizeof(KdTreeNode));
	currentNode->value = medianValue;
	kdtreeBranches.push_back(currentNode);

	//partition around median
	int leftSize = medianIndex, *leftPoints = pointList;
	int rightSize = n - leftSize, *rightPoints = pointList + leftSize;
//	printf("n %d i,j %d %d left %d right %d median %d\n",n,i,j,leftSize,rightSize,medianIndex);

	//get left and right children
	if (leftSize > 0)
		currentNode->left = buildKdTreeNode(leftPoints,leftSize,depth+1);
	if (rightSize > 0)
		currentNode->right = buildKdTreeNode(rightPoints,rightSize,depth+1);

//	printf("depth %d left %d right %d median %d %f\n",depth,leftSize,rightSize,medianIndex,medianValue);
	return currentNode;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, -0x60(%rbp)
movl -0x20(%rbp), %eax
cmpl 0x28(%rcx), %eax
jle 0xe000
movq -0x60(%rbp), %rax
movl -0x20(%rbp), %ecx
movl %ecx, 0x28(%rax)
cmpl $0x1, -0x1c(%rbp)
jne 0xe024
movq -0x60(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movslq (%rcx), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0xe147
movq -0x60(%rbp), %rdi
movl -0x20(%rbp), %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %edx, -0x24(%rbp)
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl -0x64(%rbp), %edx
movl %eax, %ecx
movl -0x24(%rbp), %r8d
callq 0xe1f0
movl -0x1c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x60(%rbp), %rax
movl %ecx, -0x28(%rbp)
movq -0x18(%rbp), %rcx
movslq -0x28(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x2c(%rbp)
movq 0x30(%rax), %rax
movl -0x2c(%rbp), %ecx
shll $0x2, %ecx
addl -0x24(%rbp), %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, -0x30(%rbp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x2120
movq -0x60(%rbp), %rdi
movq %rax, -0x38(%rbp)
movss -0x30(%rbp), %xmm0
movq -0x38(%rbp), %rax
movss %xmm0, 0x4(%rax)
addq $0x8, %rdi
leaq -0x38(%rbp), %rsi
callq 0xecb0
movl -0x28(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
movl -0x1c(%rbp), %eax
subl -0x3c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
cmpl $0x0, -0x3c(%rbp)
jle 0xe118
movq -0x60(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl -0x3c(%rbp), %edx
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
callq 0xdfd0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpl $0x0, -0x4c(%rbp)
jle 0xe13f
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
movl -0x4c(%rbp), %edx
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
callq 0xdfd0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x38(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)