// File: /workspace/github_cpp_makefile_star2_O3/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 0x92c1
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 0x930f
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 0x9356
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), %rcx
movslq 0x60(%rcx), %rax
testq %rax, %rax
jle 0x940e
movq 0x58(%rcx), %rcx
leaq 0x10(%rdx), %rsi
xorl %r8d, %r8d
movq %r8, %r9
shlq $0x4, %r9
xorps %xmm0, %xmm0
cvtss2sd (%rcx,%r9), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rcx,%r9), %xmm1
xorps %xmm2, %xmm2
cvtss2sd 0x8(%rcx,%r9), %xmm2
movq %rsi, %r9
xorl %r10d, %r10d
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 %r8, %r8
je 0x93e7
movss -0x18(%rsp,%r10,4), %xmm4
ucomiss %xmm3, %xmm4
jbe 0x93dd
movss %xmm3, -0x18(%rsp,%r10,4)
ucomiss -0xc(%rsp,%r10,4), %xmm3
ja 0x93ee
jmp 0x93f5
movss %xmm3, -0x18(%rsp,%r10,4)
movss %xmm3, -0xc(%rsp,%r10,4)
incq %r10
addq $0x18, %r9
cmpq $0x3, %r10
jne 0x939c
incq %r8
cmpq %rax, %r8
jne 0x9372
leaq 0x10(%rdx), %rax
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movss 0x2080(%rip), %xmm1     # 0xb4a4
movss -0x18(%rsp,%rcx,4), %xmm3
addss -0xc(%rsp,%rcx,4), %xmm3
mulss %xmm1, %xmm3
cvtss2sd %xmm3, %xmm3
cvtps2pd %xmm2, %xmm2
movupd -0x10(%rax), %xmm4
movaps %xmm3, %xmm5
movlhps %xmm3, %xmm5            # xmm5 = xmm5[0],xmm3[0]
mulpd %xmm4, %xmm5
addpd %xmm2, %xmm5
cvtpd2ps %xmm5, %xmm2
mulsd (%rax), %xmm3
cvtss2sd %xmm0, %xmm0
addsd %xmm0, %xmm3
xorps %xmm0, %xmm0
cvtsd2ss %xmm3, %xmm0
incq %rcx
addq $0x18, %rax
cmpq $0x3, %rcx
jne 0x9424
cvtps2pd %xmm2, %xmm1
movups %xmm1, 0x78(%rdi)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x88(%rdi)
leaq 0x30(%rdi), %rax
xorl %ecx, %ecx
movss -0xc(%rsp,%rcx,4), %xmm0
subss -0x18(%rsp,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x18(%rdi,%rcx,8)
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
cmpq $0x3, %rsi
jne 0x94a3
incq %rcx
addq $0x18, %rdx
addq $0x18, %rax
cmpq $0x3, %rcx
jne 0x948b
retq