// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq %rdi, %r14
leaq 0x6af1(%rip), %rdx      # 0xa77e
leaq 0x110(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x2350
leaq 0x6853(%rip), %rsi      # 0xa501
movq %rbx, %rdi
callq 0x2240
testq %rax, %rax
je 0x3e8f
movq %rax, %rbx
leaq 0x6ac0(%rip), %rdx      # 0xa789
leaq 0x110(%rsp), %r12
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x2350
leaq 0x6ab3(%rip), %rsi      # 0xa79d
movq %r12, %rdi
callq 0x2240
testq %rax, %rax
je 0x3eb7
movq %rax, 0x8(%rsp)
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x8056
leaq 0x10(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
movq %r15, %rdi
callq 0x8258
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x833c
xorl %r15d, %r15d
pushq $0xa
popq %rbp
leaq 0x644e(%rip), %r14      # 0xa197
cmpl $0x8, %r15d
je 0x3df6
leaq 0x6a45(%rip), %rdi      # 0xa79f
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x23e0
pushq $0x30
popq %r12
xorl %r13d, %r13d
cmpq $0x3, %r13
je 0x3de4
movsd 0xa8(%rsp,%r13,8), %xmm1
movq %r12, %rax
xorl %ecx, %ecx
movsd 0x6554(%rip), %xmm2     # 0xa2e0
cvtsd2ss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
cmpq $0x3, %rcx
je 0x3dce
btl %ecx, %r15d
movl $0x0, %edx
adcl $-0x1, %edx
orl $0x1, %edx
cvtsi2ss %edx, %xmm1
cvtss2sd %xmm1, %xmm1
mulsd 0x48(%rsp,%rcx,8), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x30(%rsp,%rax), %xmm1
addsd %xmm0, %xmm1
incq %rcx
addq $0x18, %rax
jmp 0x3d8c
movq %rbx, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x22b0
incq %r13
addq $0x8, %r12
jmp 0x3d6f
movl %ebp, %edi
movq %rbx, %rsi
callq 0x2260
incl %r15d
jmp 0x3d49
leaq 0x30(%rsp), %r14
movsd 0x30(%r14), %xmm1
movsd 0x38(%r14), %xmm0
callq 0x2060
movapd %xmm0, %xmm6
divsd 0x6420(%rip), %xmm6     # 0xa238
mulsd 0x6410(%rip), %xmm6     # 0xa230
movsd 0x78(%r14), %xmm0
movsd 0x80(%r14), %xmm1
movsd 0x88(%r14), %xmm2
movsd 0x18(%r14), %xmm3
movsd 0x20(%r14), %xmm4
movsd 0x28(%r14), %xmm5
leaq 0x6951(%rip), %rsi      # 0xa7a2
movq 0x8(%rsp), %r15
movq %r15, %rdi
movb $0x7, %al
callq 0x22b0
leaq 0x6951(%rip), %rdi      # 0xa7b8
pushq $0x48
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x23e0
movq %rbx, %rdi
callq 0x21f0
movq %r15, %rdi
callq 0x21f0
movq %r14, %rdi
callq 0x8066
jmp 0x3ea5
leaq 0x666d(%rip), %rdi      # 0xa503
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 0x6645(%rip), %rdi      # 0xa503
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 0x8066
movq %rbx, %rdi
callq 0x23b0
nop