// File: /workspace/github_cpp_makefile_star2_O3/jingdao[P]PointCloudApp/pcd.cpp
// Function: PCD::writeToOBJ(char const*, unsigned long)
void PCD::writeToOBJ(const char* dir,size_t index) {
	char filename[256];
	snprintf(filename,256,"%s/%lu.obj",dir,index);
	if (!filename)
		return;
	FILE* f = fopen(filename, "w");
	if (!f) {
		printf("Cannot write to file: %s\n", filename);
		return;
	}
	snprintf(filename,256,"%s/bounding_box.txt",dir);
	FILE* aggregate = fopen(filename,"a");
	if (!aggregate) {
		printf("Cannot write to file: %s\n", aggregate);
		return;
	}

#if USE_OBB
	Descriptor d(this);
	double lambda[3], v[9];
	d.getPCA_XY(lambda,v);
	d.setPCA(lambda,v);
	for (int i=0;i<8;i++) {
		float coords[3];
		fprintf(f,"v ");
		for (int j=0;j<3;j++) {
			coords[j] = d.bbCenter[j];
			for (int axis=0;axis<3;axis++) {
				float sign = (i & 1<<axis) ? 1 : -1;
				coords[j] += sign * d.principalLengths[axis] / 2 * d.principalAxes[axis][j];
			}
			fprintf(f,"%f ",coords[j]);
		}
		fprintf(f,"\n");
	}
	double theta = atan2(d.principalAxes[0][1],d.principalAxes[0][0]) / M_PI * 180;
	fprintf(aggregate,"%f %f %f %f %f %f %f\n",
		d.bbCenter[0],d.bbCenter[1],d.bbCenter[2],
		d.principalLengths[0],d.principalLengths[1],d.principalLengths[2],theta);
#else
	if (!kdtree) kdtree = new KdTree(this);
	KdTree::Cube bb = kdtree->getBoundingBox();
	fprintf(f,"%f %f %f\n",bb.x1,bb.y1,bb.z1);
	fprintf(f,"%f %f %f\n",bb.x2,bb.y1,bb.z1);
	fprintf(f,"%f %f %f\n",bb.x1,bb.y2,bb.z1);
	fprintf(f,"%f %f %f\n",bb.x2,bb.y2,bb.z1);
	fprintf(f,"%f %f %f\n",bb.x1,bb.y1,bb.z2);
	fprintf(f,"%f %f %f\n",bb.x2,bb.y1,bb.z2);
	fprintf(f,"%f %f %f\n",bb.x1,bb.y2,bb.z2);
	fprintf(f,"%f %f %f\n",bb.x2,bb.y2,bb.z2);
#endif
	fprintf(f,
	"l 1 2\n"
	"l 1 3\n"
	"l 2 4\n"
	"l 3 4\n"
	"l 1 5\n"
	"l 2 6\n"
	"l 3 7\n"
	"l 4 8\n"
	"l 5 6\n"
	"l 5 7\n"
	"l 6 8\n"
	"l 7 8\n");
	fclose(f);
	fclose(aggregate);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rdx, %r8
movq %rsi, %r14
movq %rdi, %r15
leaq 0x7969(%rip), %rdx      # 0xb7d6
leaq 0x110(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2340
leaq 0x76cb(%rip), %rsi      # 0xb559
movq %rbx, %rdi
callq 0x2230
testq %rax, %rax
je 0x408c
movq %rax, %rbx
leaq 0x7938(%rip), %rdx      # 0xb7e1
leaq 0x110(%rsp), %r12
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2340
leaq 0x792b(%rip), %rsi      # 0xb7f5
movq %r12, %rdi
callq 0x2230
testq %rax, %rax
je 0x40b4
movq %rax, 0x8(%rsp)
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x8ee0
leaq 0x10(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
movq %r12, %rdi
callq 0x9180
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x9270
xorl %r13d, %r13d
leaq 0x78d1(%rip), %r15      # 0xb7f7
leaq 0x726a(%rip), %r12      # 0xb197
movl $0x2, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x23d0
movl $0x30, %ebp
xorl %r14d, %r14d
xorps %xmm0, %xmm0
cvtsd2ss 0xa8(%rsp,%r14,8), %xmm0
movl $0x3, %eax
movq %rbp, %rcx
movsd 0x7379(%rip), %xmm2     # 0xb2e0
leal -0x3(%rax), %edx
movzbl %dl, %edx
btl %edx, %r13d
movl $0x0, %edx
adcl $-0x1, %edx
orl $0x1, %edx
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
cvtss2sd %xmm1, %xmm1
mulsd 0x30(%rsp,%rax,8), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x30(%rsp,%rcx), %xmm1
cvtss2sd %xmm0, %xmm0
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
incq %rax
addq $0x18, %rcx
cmpq $0x6, %rax
jne 0x3f67
cvtss2sd %xmm0, %xmm0
movq %rbx, %rdi
movq %r12, %rsi
movb $0x1, %al
callq 0x22a0
incq %r14
addq $0x8, %rbp
cmpq $0x3, %r14
jne 0x3f4a
movl $0xa, %edi
movq %rbx, %rsi
callq 0x2250
incl %r13d
cmpl $0x8, %r13d
jne 0x3f2d
leaq 0x30(%rsp), %r15
movsd 0x30(%r15), %xmm1
movsd 0x38(%r15), %xmm0
callq 0x2060
movapd %xmm0, %xmm6
divsd 0x7227(%rip), %xmm6     # 0xb238
mulsd 0x7217(%rip), %xmm6     # 0xb230
movsd 0x78(%r15), %xmm0
movsd 0x80(%r15), %xmm1
movsd 0x88(%r15), %xmm2
movsd 0x18(%r15), %xmm3
movsd 0x20(%r15), %xmm4
movsd 0x28(%r15), %xmm5
leaq 0x77b0(%rip), %rsi      # 0xb7fa
movq 0x8(%rsp), %r14
movq %r14, %rdi
movb $0x7, %al
callq 0x22a0
leaq 0x77b0(%rip), %rdi      # 0xb810
movl $0x48, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x23d0
movq %rbx, %rdi
callq 0x21f0
movq %r14, %rdi
callq 0x21f0
movq %r15, %rdi
callq 0x8ef4
jmp 0x40a2
leaq 0x74c8(%rip), %rdi      # 0xb55b
leaq 0x110(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x74a0(%rip), %rdi      # 0xb55b
xorl %esi, %esi
xorl %eax, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2050
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x8ef4
movq %rbx, %rdi
callq 0x23a0