// File: jingdao[P]PointCloudApp/pcd.cpp
// Function: PCD::LoadFromPTS(char const*)
PCD* PCD::LoadFromPTS(const char* fileName) {
	FILE* f = fopen(fileName, "r");
	if (!f) {
		printf("File not found: %s\n", fileName);
		return NULL;
	}
	char buf[256];
	int pointsParsed = 0;
	PCD* pcd = new PCD(0);
	int r,g,b,d,rgb;
	while (fgets(buf, 256, f)) {
		int n = atoi(buf);
//		printf("n = %d\n",n);
		if (!pcd->expand(n + pointsParsed))
			break;
		for (int i=0;i<n;i++) {
			fgets(buf,256,f);
			if (sscanf(buf, "%f %f %f %d %d %d %d", pcd->float_data + pointsParsed * 4, pcd->float_data + pointsParsed * 4 + 1,
				pcd->float_data + pointsParsed * 4 + 2, &d, &r, &g, &b) == 7) {
				rgb = (r<<16) | (g<<8) | b;
				pcd->float_data[pointsParsed * 4 + 3] = (float) rgb;
				pointsParsed++;
			} else {
				printf("Error parsing %s\n",fileName);
				printf("Line %d: %s\n",pointsParsed,buf);
				break;
			}
		}
	}
	fclose(f);
	return pcd;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xbe62(%rip), %rsi      # 0x1378c
callq 0x2230
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x7959
movq -0x10(%rbp), %rsi
leaq 0xba6b(%rip), %rdi      # 0x133b0
movb $0x0, %al
callq 0x2050
movq $0x0, -0x8(%rbp)
jmp 0x7b95
movl $0x0, -0x124(%rbp)
movl $0x78, %edi
callq 0x2200
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x160(%rbp)
xorl %esi, %esi
callq 0x41e0
jmp 0x7983
movq -0x160(%rbp), %rax
movq %rax, -0x130(%rbp)
leaq -0x120(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x2300
cmpq $0x0, %rax
je 0x7b81
leaq -0x120(%rbp), %rdi
callq 0x22f0
movl %eax, -0x154(%rbp)
movq -0x130(%rbp), %rdi
movl -0x154(%rbp), %esi
addl -0x124(%rbp), %esi
callq 0x4330
testb $0x1, %al
jne 0x7a0b
jmp 0x7b81
movq -0x160(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x138(%rbp)
movl %eax, -0x13c(%rbp)
movl $0x78, %esi
callq 0x2210
jmp 0x7ba2
movl $0x0, -0x158(%rbp)
movl -0x158(%rbp), %eax
cmpl -0x154(%rbp), %eax
jge 0x7b7c
leaq -0x120(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x2300
leaq -0x120(%rbp), %rdi
movq -0x130(%rbp), %rax
movq 0x58(%rax), %rdx
movl -0x124(%rbp), %eax
shll $0x2, %eax
cltq
shlq $0x2, %rax
addq %rax, %rdx
movq -0x130(%rbp), %rax
movq 0x58(%rax), %rcx
movl -0x124(%rbp), %eax
shll $0x2, %eax
cltq
shlq $0x2, %rax
addq %rax, %rcx
addq $0x4, %rcx
movq -0x130(%rbp), %rax
movq 0x58(%rax), %r8
movl -0x124(%rbp), %eax
shll $0x2, %eax
cltq
shlq $0x2, %rax
addq %rax, %r8
addq $0x8, %r8
leaq 0xbd8f(%rip), %rsi      # 0x13838
leaq -0x14c(%rbp), %r9
leaq -0x140(%rbp), %r11
leaq -0x144(%rbp), %r10
leaq -0x148(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x21e0
cmpl $0x7, %eax
jne 0x7b37
movl -0x140(%rbp), %eax
shll $0x10, %eax
movl -0x144(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
orl -0x148(%rbp), %eax
movl %eax, -0x150(%rbp)
cvtsi2ssl -0x150(%rbp), %xmm0
movq -0x130(%rbp), %rax
movq 0x58(%rax), %rax
movl -0x124(%rbp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movss %xmm0, (%rax,%rcx,4)
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x7b66
movq -0x10(%rbp), %rsi
leaq 0xbc5e(%rip), %rdi      # 0x137a0
movb $0x0, %al
callq 0x2050
movl -0x124(%rbp), %esi
leaq -0x120(%rbp), %rdx
leaq 0xbc55(%rip), %rdi      # 0x137b2
movb $0x0, %al
callq 0x2050
jmp 0x7b7c
jmp 0x7b68
movl -0x158(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x158(%rbp)
jmp 0x7a15
jmp 0x7991
movq -0x18(%rbp), %rdi
callq 0x21f0
movq -0x130(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x138(%rbp), %rdi
callq 0x2380
nop