// File: 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
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x120(%rbp), %rdi
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %r8
movl $0x100, %esi            # imm = 0x100
leaq 0xe3c1(%rip), %rdx      # 0x136ee
movb $0x0, %al
callq 0x2310
leaq -0x120(%rbp), %rdi
leaq 0xe12f(%rip), %rsi      # 0x13471
callq 0x2230
movq %rax, -0x128(%rbp)
cmpq $0x0, -0x128(%rbp)
jne 0x5372
leaq -0x120(%rbp), %rsi
leaq 0xe10d(%rip), %rdi      # 0x13473
movb $0x0, %al
callq 0x2050
jmp 0x56a1
leaq -0x120(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0xe370(%rip), %rdx      # 0x136f9
movb $0x0, %al
callq 0x2310
leaq -0x120(%rbp), %rdi
leaq 0xe36f(%rip), %rsi      # 0x1370d
callq 0x2230
movq %rax, -0x130(%rbp)
cmpq $0x0, -0x130(%rbp)
jne 0x53ce
movq -0x130(%rbp), %rsi
leaq 0xe0b1(%rip), %rdi      # 0x13473
movb $0x0, %al
callq 0x2050
jmp 0x56a1
movq -0x268(%rbp), %rsi
leaq -0x1c0(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0xf840
movq -0x270(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
leaq -0x230(%rbp), %rdx
callq 0xfc00
jmp 0x5404
leaq -0x1c0(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
leaq -0x230(%rbp), %rdx
callq 0xfdb0
jmp 0x5420
movl $0x0, -0x240(%rbp)
cmpl $0x8, -0x240(%rbp)
jge 0x55e2
movq -0x128(%rbp), %rdi
leaq 0xe2ca(%rip), %rsi      # 0x1370f
movb $0x0, %al
callq 0x2280
movl $0x0, -0x250(%rbp)
cmpl $0x3, -0x250(%rbp)
jge 0x55b9
movslq -0x250(%rbp), %rax
movsd -0x148(%rbp,%rax,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movslq -0x250(%rbp), %rax
movss %xmm0, -0x24c(%rbp,%rax,4)
movl $0x0, -0x254(%rbp)
cmpl $0x3, -0x254(%rbp)
jge 0x557c
movl -0x240(%rbp), %edx
movl -0x254(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
andl %eax, %edx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl $0x1, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, -0x258(%rbp)
movss -0x258(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq -0x254(%rbp), %rax
mulsd -0x1a8(%rbp,%rax,8), %xmm0
movsd 0xddbd(%rip), %xmm1     # 0x132b0
divsd %xmm1, %xmm0
leaq -0x1c0(%rbp), %rax
addq $0x30, %rax
movslq -0x254(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x250(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movslq -0x250(%rbp), %rax
movss -0x24c(%rbp,%rax,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x24c(%rbp,%rax,4)
movl -0x254(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x254(%rbp)
jmp 0x5491
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0xf870
jmp 0x56aa
movq -0x128(%rbp), %rdi
movslq -0x250(%rbp), %rax
movss -0x24c(%rbp,%rax,4), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0xdbc9(%rip), %rsi      # 0x13167
movb $0x1, %al
callq 0x2280
movl -0x250(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x250(%rbp)
jmp 0x5456
movq -0x128(%rbp), %rdi
leaq 0xe41e(%rip), %rsi      # 0x139e5
movb $0x0, %al
callq 0x2280
movl -0x240(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x240(%rbp)
jmp 0x542a
movsd -0x190(%rbp), %xmm1
movsd -0x188(%rbp), %xmm0
callq 0x2060
movsd 0xdcd9(%rip), %xmm1     # 0x132d8
divsd %xmm1, %xmm0
movsd 0xdcc5(%rip), %xmm1     # 0x132d0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x260(%rbp)
movq -0x130(%rbp), %rdi
movsd -0x148(%rbp), %xmm0
movsd -0x140(%rbp), %xmm1
movsd -0x138(%rbp), %xmm2
movsd -0x1a8(%rbp), %xmm3
movsd -0x1a0(%rbp), %xmm4
movsd -0x198(%rbp), %xmm5
movsd -0x260(%rbp), %xmm6
leaq 0xe0b5(%rip), %rsi      # 0x13712
movb $0x7, %al
callq 0x2280
movq -0x128(%rbp), %rdi
leaq 0xe0b6(%rip), %rsi      # 0x13728
xorl %eax, %eax
callq 0x2280
movq -0x128(%rbp), %rdi
callq 0x21f0
jmp 0x5687
movq -0x130(%rbp), %rdi
callq 0x21f0
jmp 0x5695
leaq -0x1c0(%rbp), %rdi
callq 0xf870
addq $0x270, %rsp            # imm = 0x270
popq %rbp
retq
movq -0x238(%rbp), %rdi
callq 0x2380
nopw %cs:(%rax,%rax)