// File: /workspace/github_cpp_makefile_star2_O2/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:
movsd (%rsi), %xmm0
movsd 0x8(%rsi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x838d
movsd %xmm0, 0x8(%rsi)
movsd %xmm1, (%rsi)
movq 0x28(%rdx), %rax
movq %rax, -0x28(%rsp)
movups 0x18(%rdx), %xmm0
movaps %xmm0, -0x38(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x28(%rdx)
movups (%rdx), %xmm0
movups %xmm0, 0x18(%rdx)
movq -0x28(%rsp), %rax
movq %rax, 0x10(%rdx)
movapd -0x38(%rsp), %xmm0
movupd %xmm0, (%rdx)
movsd 0x8(%rsi), %xmm1
movsd 0x10(%rsi), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x83db
movsd %xmm1, 0x10(%rsi)
movsd %xmm0, 0x8(%rsi)
movq 0x40(%rdx), %rax
movq %rax, -0x28(%rsp)
movups 0x30(%rdx), %xmm0
movaps %xmm0, -0x38(%rsp)
movq 0x28(%rdx), %rax
movq %rax, 0x40(%rdx)
movups 0x18(%rdx), %xmm0
movups %xmm0, 0x30(%rdx)
movq -0x28(%rsp), %rax
movq %rax, 0x28(%rdx)
movaps -0x38(%rsp), %xmm0
movups %xmm0, 0x18(%rdx)
movsd 0x10(%rsi), %xmm0
movsd (%rsi), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x8422
movsd %xmm1, 0x10(%rsi)
movsd %xmm0, (%rsi)
movq 0x40(%rdx), %rax
movq %rax, -0x28(%rsp)
movups 0x30(%rdx), %xmm0
movaps %xmm0, -0x38(%rsp)
movups (%rdx), %xmm0
movups %xmm0, 0x30(%rdx)
movq 0x10(%rdx), %rax
movq %rax, 0x40(%rdx)
movapd -0x38(%rsp), %xmm0
movupd %xmm0, (%rdx)
movq -0x28(%rsp), %rax
movq %rax, 0x10(%rdx)
movq 0x10(%rdi), %rax
movl 0x60(%rax), %ecx
xorl %esi, %esi
testl %ecx, %ecx
cmovlel %esi, %ecx
leaq 0x10(%rdx), %r8
cmpq %rcx, %rsi
je 0x84d2
movq 0x58(%rax), %r9
movq %rsi, %r10
shlq $0x4, %r10
cvtss2sd (%r9,%r10), %xmm0
cvtss2sd 0x4(%r9,%r10), %xmm1
cvtss2sd 0x8(%r9,%r10), %xmm2
movq %r8, %r9
xorl %r10d, %r10d
cmpq $0x3, %r10
je 0x84ca
movsd -0x8(%r9), %xmm3
mulsd %xmm1, %xmm3
movsd -0x10(%r9), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm3, %xmm4
movsd (%r9), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm4, %xmm3
cvtsd2ss %xmm3, %xmm3
testq %rsi, %rsi
je 0x84b3
movss -0x18(%rsp,%r10,4), %xmm4
ucomiss %xmm3, %xmm4
jbe 0x84a9
movss %xmm3, -0x18(%rsp,%r10,4)
ucomiss -0xc(%rsp,%r10,4), %xmm3
ja 0x84ba
jmp 0x84c1
movss %xmm3, -0x18(%rsp,%r10,4)
movss %xmm3, -0xc(%rsp,%r10,4)
incq %r10
addq $0x18, %r9
jmp 0x8462
incq %rsi
jmp 0x8434
leaq 0x10(%rdx), %rax
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
xorl %ecx, %ecx
movss 0x1f6d(%rip), %xmm0     # 0xa454
cvtps2pd %xmm1, %xmm1
cvtss2sd %xmm2, %xmm2
cmpq $0x3, %rcx
je 0x8534
movss -0x18(%rsp,%rcx,4), %xmm3
addss -0xc(%rsp,%rcx,4), %xmm3
mulss %xmm0, %xmm3
cvtss2sd %xmm3, %xmm3
movupd -0x10(%rax), %xmm4
movaps %xmm3, %xmm5
movlhps %xmm3, %xmm5            # xmm5 = xmm5[0],xmm3[0]
mulpd %xmm4, %xmm5
addpd %xmm1, %xmm5
cvtpd2ps %xmm5, %xmm1
mulsd (%rax), %xmm3
addsd %xmm2, %xmm3
cvtsd2ss %xmm3, %xmm2
incq %rcx
addq $0x18, %rax
jmp 0x84e7
movupd %xmm1, 0x78(%rdi)
movsd %xmm2, 0x88(%rdi)
leaq 0x30(%rdi), %rax
xorl %ecx, %ecx
cmpq $0x3, %rcx
je 0x8587
movss -0xc(%rsp,%rcx,4), %xmm0
subss -0x18(%rsp,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x18(%rdi,%rcx,8)
xorl %esi, %esi
cmpq $0x3, %rsi
je 0x857a
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
jmp 0x8565
incq %rcx
addq $0x18, %rdx
addq $0x18, %rax
jmp 0x8547
retq