// File: jingdao[P]PointCloudApp/descriptor.cpp
// Function: Descriptor::setPCA(double*, double*)
void Descriptor::setPCA(double* lambda_real, double* v) {
	//sort by eigenvalue
	double tmpL,tmpV[3];
	if (lambda_real[0] < lambda_real[1]) {
		tmpL = lambda_real[1];
		lambda_real[1] = lambda_real[0];
		lambda_real[0] = tmpL;
		memcpy(tmpV, v+3, 3 * sizeof(double));
		memcpy(v+3, v, 3 * sizeof(double));
		memcpy(v, tmpV, 3 * sizeof(double));
	}
	if (lambda_real[1] < lambda_real[2]) {
		tmpL = lambda_real[2];
		lambda_real[2] = lambda_real[1];
		lambda_real[1] = tmpL;
		memcpy(tmpV, v+6, 3 * sizeof(double));
		memcpy(v+6, v+3, 3 * sizeof(double));
		memcpy(v+3, tmpV, 3 * sizeof(double));
	}
	if (lambda_real[0] < lambda_real[2]) {
		tmpL = lambda_real[2];
		lambda_real[2] = lambda_real[0];
		lambda_real[0] = tmpL;
		memcpy(tmpV, v+6, 3 * sizeof(double));
		memcpy(v+6, v, 3 * sizeof(double));
		memcpy(v, tmpV, 3 * sizeof(double));
	}
//	printf("lambda: %f %f %f\n",lambda_real[0],lambda_real[1],lambda_real[2]);
//	printf("v1: %f %f %f\n",v[0],v[1],v[2]);
//	printf("v2: %f %f %f\n",v[3],v[4],v[5]);
//	printf("v3: %f %f %f\n",v[6],v[7],v[8]);
	//compute projections
	float minScale[3], maxScale[3];
	for (int j=0;j<pointcloud->numPoints;j++) {
		for (int i=0;i<3;i++) {
			float dotProduct =
				pointcloud->float_data[j*4] * v[i*3] +
				pointcloud->float_data[j*4+1] * v[i*3+1] +
				pointcloud->float_data[j*4+2] * v[i*3+2];
			if (j==0 || dotProduct < minScale[i])
				minScale[i] = dotProduct;
			if (j==0 || dotProduct > maxScale[i])
				maxScale[i] = dotProduct;				
		}
	}
	float cx=0,cy=0,cz=0;
	for (int i=0;i<3;i++) {
		cx+=(minScale[i]+maxScale[i])/2 * v[i*3];
		cy+=(minScale[i]+maxScale[i])/2 * v[i*3+1];
		cz+=(minScale[i]+maxScale[i])/2 * v[i*3+2];
	}
	//set member variables
	this->bbCenter[0] = cx;
	this->bbCenter[1] = cy;
	this->bbCenter[2] = cz;
	for (int i=0;i<3;i++) {
		this->principalLengths[i] = maxScale[i] - minScale[i];
		for (int j=0;j<3;j++)
			this->principalAxes[i][j] = v[i * 3 + j];
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x10(%rbp), %rax
movsd (%rax), %xmm1
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfe6c
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movsd -0x20(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x20(%rax), %rcx
movq %rcx, -0x38(%rbp)
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x18(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x20(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm1
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xff0e
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movsd -0x20(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x38(%rax), %rcx
movq %rcx, -0x38(%rbp)
movq 0x40(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rdx
movq %rdx, 0x30(%rax)
movq 0x20(%rcx), %rdx
movq %rdx, 0x38(%rax)
movq 0x28(%rcx), %rcx
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x20(%rax)
movq -0x30(%rbp), %rcx
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movsd (%rax), %xmm1
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xffab
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, 0x10(%rax)
movsd -0x20(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd %xmm0, (%rax)
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x38(%rax), %rcx
movq %rcx, -0x38(%rbp)
movq 0x40(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, 0x30(%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x38(%rax)
movq 0x10(%rcx), %rcx
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, (%rax)
movq -0x38(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rcx
movq %rcx, 0x10(%rax)
movl $0x0, -0x5c(%rbp)
movq -0x88(%rbp), %rcx
movl -0x5c(%rbp), %eax
movq 0x10(%rcx), %rcx
cmpl 0x60(%rcx), %eax
jge 0x100f2
movl $0x0, -0x60(%rbp)
cmpl $0x3, -0x60(%rbp)
jge 0x100e2
movq -0x88(%rbp), %rax
movq 0x10(%rax), %rcx
movq 0x58(%rcx), %rcx
movl -0x5c(%rbp), %edx
shll $0x2, %edx
movslq %edx, %rdx
movss (%rcx,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm1
movq -0x18(%rbp), %rcx
imull $0x3, -0x60(%rbp), %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm2
movq 0x10(%rax), %rcx
movq 0x58(%rcx), %rcx
movl -0x5c(%rbp), %edx
shll $0x2, %edx
addl $0x1, %edx
movslq %edx, %rdx
movss (%rcx,%rdx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rcx
imull $0x3, -0x60(%rbp), %edx
addl $0x1, %edx
movslq %edx, %rdx
mulsd (%rcx,%rdx,8), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movq 0x10(%rax), %rax
movq 0x58(%rax), %rax
movl -0x5c(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
imull $0x3, -0x60(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x64(%rbp)
cmpl $0x0, -0x5c(%rbp)
je 0x1009e
movss -0x64(%rbp), %xmm1
movslq -0x60(%rbp), %rax
movss -0x4c(%rbp,%rax,4), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x100ad
movss -0x64(%rbp), %xmm0
movslq -0x60(%rbp), %rax
movss %xmm0, -0x4c(%rbp,%rax,4)
cmpl $0x0, -0x5c(%rbp)
je 0x100c3
movss -0x64(%rbp), %xmm0
movslq -0x60(%rbp), %rax
ucomiss -0x58(%rbp,%rax,4), %xmm0
jbe 0x100d2
movss -0x64(%rbp), %xmm0
movslq -0x60(%rbp), %rax
movss %xmm0, -0x58(%rbp,%rax,4)
jmp 0x100d4
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0xffd0
jmp 0x100e4
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0xffb2
xorps %xmm0, %xmm0
movss %xmm0, -0x68(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x6c(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
cmpl $0x3, -0x74(%rbp)
jge 0x10219
movslq -0x74(%rbp), %rax
movss -0x4c(%rbp,%rax,4), %xmm0
movslq -0x74(%rbp), %rax
addss -0x58(%rbp,%rax,4), %xmm0
movss 0x3235(%rip), %xmm1     # 0x1336c
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movss -0x68(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x68(%rbp)
movslq -0x74(%rbp), %rax
movss -0x4c(%rbp,%rax,4), %xmm0
movslq -0x74(%rbp), %rax
addss -0x58(%rbp,%rax,4), %xmm0
movss 0x31e7(%rip), %xmm1     # 0x1336c
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movss -0x6c(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x6c(%rbp)
movslq -0x74(%rbp), %rax
movss -0x4c(%rbp,%rax,4), %xmm0
movslq -0x74(%rbp), %rax
addss -0x58(%rbp,%rax,4), %xmm0
movss 0x3196(%rip), %xmm1     # 0x1336c
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x18(%rbp), %rax
imull $0x3, -0x74(%rbp), %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm2
movss -0x70(%rbp), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x70(%rbp)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x10111
movq -0x88(%rbp), %rax
movss -0x68(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x78(%rax)
movss -0x6c(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x80(%rax)
movss -0x70(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x88(%rax)
movl $0x0, -0x78(%rbp)
cmpl $0x3, -0x78(%rbp)
jge 0x102e4
movq -0x88(%rbp), %rax
movslq -0x78(%rbp), %rcx
movss -0x58(%rbp,%rcx,4), %xmm0
movslq -0x78(%rbp), %rcx
subss -0x4c(%rbp,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movslq -0x78(%rbp), %rcx
movsd %xmm0, 0x18(%rax,%rcx,8)
movl $0x0, -0x7c(%rbp)
cmpl $0x3, -0x7c(%rbp)
jge 0x102d4
movq -0x88(%rbp), %rax
movq -0x18(%rbp), %rcx
imull $0x3, -0x78(%rbp), %edx
addl -0x7c(%rbp), %edx
movslq %edx, %rdx
movsd (%rcx,%rdx,8), %xmm0
addq $0x30, %rax
movslq -0x78(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x7c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x10291
jmp 0x102d6
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x10257
addq $0x10, %rsp
popq %rbp
retq
nopw (%rax,%rax)