// File: /workspace/github_cpp_makefile_star2_O1/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 0x9302
movsd %xmm0, 0x8(%rsi)
movsd %xmm1, (%rsi)
movq 0x28(%rdx), %rax
movq %rax, -0x28(%rsp)
movups 0x18(%rdx), %xmm0
movaps %xmm0, -0x38(%rsp)
movups (%rdx), %xmm0
movups %xmm0, 0x18(%rdx)
movq 0x10(%rdx), %rax
movq %rax, 0x28(%rdx)
movapd -0x38(%rsp), %xmm0
movupd %xmm0, (%rdx)
movq -0x28(%rsp), %rax
movq %rax, 0x10(%rdx)
movsd 0x8(%rsi), %xmm0
movsd 0x10(%rsi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x9352
movsd %xmm0, 0x10(%rsi)
movsd %xmm1, 0x8(%rsi)
movq 0x40(%rdx), %rax
movq %rax, -0x28(%rsp)
movups 0x30(%rdx), %xmm0
movaps %xmm0, -0x38(%rsp)
movups 0x18(%rdx), %xmm0
movups %xmm0, 0x30(%rdx)
movq 0x28(%rdx), %rax
movq %rax, 0x40(%rdx)
movapd -0x38(%rsp), %xmm0
movupd %xmm0, 0x18(%rdx)
movq -0x28(%rsp), %rax
movq %rax, 0x28(%rdx)
movsd (%rsi), %xmm0
movsd 0x10(%rsi), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x939e
movsd %xmm0, 0x10(%rsi)
movsd %xmm1, (%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 0x9452
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 0x941e
movss -0xc(%rsp,%r10,4), %xmm4
ucomiss %xmm3, %xmm4
jbe 0x9425
movss %xmm3, -0xc(%rsp,%r10,4)
testq %r8, %r8
je 0x9432
ucomiss -0x18(%rsp,%r10,4), %xmm3
jbe 0x9439
movss %xmm3, -0x18(%rsp,%r10,4)
incq %r10
addq $0x18, %r9
cmpq $0x3, %r10
jne 0x93e4
incq %r8
cmpq %rax, %r8
jne 0x93ba
leaq 0x10(%rdx), %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movss 0x1f70(%rip), %xmm1     # 0xb3d4
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
movss -0xc(%rsp,%rcx,4), %xmm4
addss -0x18(%rsp,%rcx,4), %xmm4
mulss %xmm1, %xmm4
cvtss2sd %xmm4, %xmm4
cvtss2sd %xmm3, %xmm3
movsd -0x10(%rax), %xmm5
mulsd %xmm4, %xmm5
addsd %xmm3, %xmm5
xorps %xmm3, %xmm3
cvtsd2ss %xmm5, %xmm3
cvtss2sd %xmm2, %xmm2
movsd -0x8(%rax), %xmm5
mulsd %xmm4, %xmm5
addsd %xmm2, %xmm5
xorps %xmm2, %xmm2
cvtsd2ss %xmm5, %xmm2
mulsd (%rax), %xmm4
cvtss2sd %xmm0, %xmm0
addsd %xmm0, %xmm4
xorps %xmm0, %xmm0
cvtsd2ss %xmm4, %xmm0
incq %rcx
addq $0x18, %rax
cmpq $0x3, %rcx
jne 0x946a
xorps %xmm1, %xmm1
cvtss2sd %xmm3, %xmm1
movsd %xmm1, 0x78(%rdi)
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
movsd %xmm1, 0x80(%rdi)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x88(%rdi)
leaq 0x30(%rdi), %rax
xorl %ecx, %ecx
movss -0x18(%rsp,%rcx,4), %xmm0
subss -0xc(%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 0x9513
incq %rcx
addq $0x18, %rdx
addq $0x18, %rax
cmpq $0x3, %rcx
jne 0x94fb
retq