// File: jingdao[P]PointCloudApp/pcd.cpp
// Function: PCD::writeToOFF(char const*)
void PCD::writeToOFF(const char* filename) {
	if (!filename)
		return;
	FILE* f = fopen(filename, "w");
	if (!f) {
		printf("Cannot write to file: %s\n", filename);
		return;
	}

	fprintf(f,"OFF\n8 6 12\n");
#define USE_OBB 1
#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];
		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");
	}
#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,"4 0 1 3 2 1 0 0 0\n"
	"4 0 4 5 1 0 1 0 0\n"
	"4 0 2 6 4 1 0 0 0\n"
	"4 1 3 7 5 0 1 0 0\n"
	"4 2 6 7 3 1 0 0 0\n"
	"4 4 5 7 6 0 1 0 0\n");
	fclose(f);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x140(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x56ea
jmp 0x594d
movq -0x10(%rbp), %rdi
leaq 0xdd7c(%rip), %rsi      # 0x13471
callq 0x2230
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x571c
movq -0x10(%rbp), %rsi
leaq 0xdd63(%rip), %rdi      # 0x13473
movb $0x0, %al
callq 0x2050
jmp 0x594d
movq -0x18(%rbp), %rdi
leaq 0xdf4e(%rip), %rsi      # 0x13675
xorl %eax, %eax
callq 0x2280
movq -0x140(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x148(%rbp)
callq 0xf840
movq -0x148(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
leaq -0x110(%rbp), %rdx
callq 0xfc00
jmp 0x5764
leaq -0xa8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
leaq -0x110(%rbp), %rdx
callq 0xfdb0
jmp 0x5780
movl $0x0, -0x120(%rbp)
cmpl $0x8, -0x120(%rbp)
jge 0x5924
movl $0x0, -0x130(%rbp)
cmpl $0x3, -0x130(%rbp)
jge 0x58fe
movslq -0x130(%rbp), %rax
movsd -0x30(%rbp,%rax,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movslq -0x130(%rbp), %rax
movss %xmm0, -0x12c(%rbp,%rax,4)
movl $0x0, -0x134(%rbp)
cmpl $0x3, -0x134(%rbp)
jge 0x58c4
movl -0x120(%rbp), %edx
movl -0x134(%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, -0x138(%rbp)
movss -0x138(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movslq -0x134(%rbp), %rax
mulsd -0x90(%rbp,%rax,8), %xmm0
movsd 0xda75(%rip), %xmm1     # 0x132b0
divsd %xmm1, %xmm0
leaq -0xa8(%rbp), %rax
addq $0x30, %rax
movslq -0x134(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movslq -0x130(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movslq -0x130(%rbp), %rax
movss -0x12c(%rbp,%rax,4), %xmm1
cvtss2sd %xmm1, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x12c(%rbp,%rax,4)
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
jmp 0x57d9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x118(%rbp)
movl %eax, -0x11c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xf870
jmp 0x5956
movq -0x18(%rbp), %rdi
movslq -0x130(%rbp), %rax
movss -0x12c(%rbp,%rax,4), %xmm0
cvtss2sd %xmm0, %xmm0
leaq 0xd884(%rip), %rsi      # 0x13167
movb $0x1, %al
callq 0x2280
movl -0x130(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x130(%rbp)
jmp 0x57a1
movq -0x18(%rbp), %rdi
leaq 0xe0dc(%rip), %rsi      # 0x139e5
movb $0x0, %al
callq 0x2280
movl -0x120(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x120(%rbp)
jmp 0x578a
movq -0x18(%rbp), %rdi
leaq 0xdd52(%rip), %rsi      # 0x13681
xorl %eax, %eax
callq 0x2280
movq -0x18(%rbp), %rdi
callq 0x21f0
jmp 0x5941
leaq -0xa8(%rbp), %rdi
callq 0xf870
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0x118(%rbp), %rdi
callq 0x2380
nopw %cs:(%rax,%rax)