// File: jingdao[P]PointCloudApp/pcd.cpp
// Function: PCD::PCD(char const*)
PCD::PCD(const char* fileName) {
	numFields = 4;
	data = NULL;
	float_data = NULL;
	fields = NULL;
	numPoints = 0;
	capacity = 0;
	data_storage = NONE;
	kdtree = NULL;
	FILE* f = fopen(fileName, "r");
	if (!f) {
		printf("File not found: %s\n", fileName);
		return;
	}
	char buf[256];
	int pointsParsed = 0;
	while (fgets(buf, 256, f)) {
		if (sscanf(buf, "POINTS %d", &numPoints) == 1) {
			capacity = numPoints;
			float_data = (float*)malloc(4 * numPoints * sizeof(float));
		}
		else if (sscanf(buf, "VIEWPOINT %f %f %f %f %f %f %f", &viewpoint[0], &viewpoint[1], &viewpoint[2],
			&viewpoint[3], &viewpoint[4], &viewpoint[5], &viewpoint[6]) == 7) {
			camera.position.r = 0;
			camera.focal_length = DEFAULT_FOCAL_LENGTH;
			camera.position.i = (float)viewpoint[0];
			camera.position.j = (float)viewpoint[1];
			camera.position.k = (float)viewpoint[2];
			camera.rotation.r = (float)viewpoint[3];
			camera.rotation.i = (float)viewpoint[4];
			camera.rotation.j = (float)viewpoint[5];
			camera.rotation.k = (float)viewpoint[6];
		} else if (strncmp(buf,"DATA ascii",10)==0) {
			data_storage = ASCII;
		} else if (strncmp(buf,"DATA binary_compressed",23)==0) {
			data_storage = BINARY;
			fread(float_data,sizeof(float),numPoints*4,f);
			break;
		}
		else if (data_storage == ASCII) {
			if (sscanf(buf, "%f %f %f %f", &float_data[pointsParsed * 4], &float_data[pointsParsed * 4 + 1],
				&float_data[pointsParsed * 4 + 2], &float_data[pointsParsed * 4 + 3]) >= 3) {
				pointsParsed++;
			}
		}
	}
	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, -0x130(%rbp)
movl $0x4, 0x40(%rax)
movq $0x0, 0x50(%rax)
movq $0x0, 0x58(%rax)
movq $0x0, 0x48(%rax)
movl $0x0, 0x60(%rax)
movl $0x0, 0x64(%rax)
movl $0x2, 0x68(%rax)
movq $0x0, 0x70(%rax)
movq -0x10(%rbp), %rdi
leaq 0xf857(%rip), %rsi      # 0x1378c
callq 0x2230
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x3f5c
movq -0x10(%rbp), %rsi
leaq 0xf460(%rip), %rdi      # 0x133b0
movb $0x0, %al
callq 0x2050
jmp 0x41d3
movl $0x0, -0x124(%rbp)
leaq -0x120(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x2300
cmpq $0x0, %rax
je 0x41ca
movq -0x130(%rbp), %rdx
leaq -0x120(%rbp), %rdi
addq $0x60, %rdx
leaq 0xf426(%rip), %rsi      # 0x133c4
movb $0x0, %al
callq 0x21e0
cmpl $0x1, %eax
jne 0x3fdc
movq -0x130(%rbp), %rax
movl 0x60(%rax), %ecx
movl %ecx, 0x64(%rax)
movl 0x60(%rax), %eax
shll $0x2, %eax
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x2260
movq %rax, %rcx
movq -0x130(%rbp), %rax
movq %rcx, 0x58(%rax)
jmp 0x41c5
movq -0x130(%rbp), %rdx
leaq -0x120(%rbp), %rdi
movq %rdx, %rcx
addq $0x4, %rcx
movq %rdx, %r8
addq $0x8, %r8
movq %rdx, %r9
addq $0xc, %r9
movq %rdx, %r11
addq $0x10, %r11
movq %rdx, %r10
addq $0x14, %r10
movq %rdx, %rax
addq $0x18, %rax
leaq 0xf3b3(%rip), %rsi      # 0x133ce
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x21e0
cmpl $0x7, %eax
jne 0x409b
movq -0x130(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x1c(%rax)
movss 0xf314(%rip), %xmm0     # 0x13360
movss %xmm0, 0x3c(%rax)
movss (%rax), %xmm0
movss %xmm0, 0x20(%rax)
movss 0x4(%rax), %xmm0
movss %xmm0, 0x24(%rax)
movss 0x8(%rax), %xmm0
movss %xmm0, 0x28(%rax)
movss 0xc(%rax), %xmm0
movss %xmm0, 0x2c(%rax)
movss 0x10(%rax), %xmm0
movss %xmm0, 0x30(%rax)
movss 0x14(%rax), %xmm0
movss %xmm0, 0x34(%rax)
movss 0x18(%rax), %xmm0
movss %xmm0, 0x38(%rax)
jmp 0x41c3
leaq -0x120(%rbp), %rdi
leaq 0xf344(%rip), %rsi      # 0x133ed
movl $0xa, %edx
callq 0x20d0
cmpl $0x0, %eax
jne 0x40cb
movq -0x130(%rbp), %rax
movl $0x0, 0x68(%rax)
jmp 0x41c1
leaq -0x120(%rbp), %rdi
leaq 0xf31f(%rip), %rsi      # 0x133f8
movl $0x17, %edx
callq 0x20d0
cmpl $0x0, %eax
jne 0x4116
movq -0x130(%rbp), %rax
movl $0x1, 0x68(%rax)
movq 0x58(%rax), %rdi
movl 0x60(%rax), %eax
shll $0x2, %eax
movslq %eax, %rdx
movq -0x18(%rbp), %rcx
movl $0x4, %esi
callq 0x22a0
jmp 0x41ca
movq -0x130(%rbp), %rax
cmpl $0x0, 0x68(%rax)
jne 0x41bd
movq -0x130(%rbp), %rax
leaq -0x120(%rbp), %rdi
movq 0x58(%rax), %rdx
movl -0x124(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
shlq $0x2, %rcx
addq %rcx, %rdx
movq 0x58(%rax), %rcx
movl -0x124(%rbp), %esi
shll $0x2, %esi
addl $0x1, %esi
movslq %esi, %rsi
shlq $0x2, %rsi
addq %rsi, %rcx
movq 0x58(%rax), %r8
movl -0x124(%rbp), %esi
shll $0x2, %esi
addl $0x2, %esi
movslq %esi, %rsi
shlq $0x2, %rsi
addq %rsi, %r8
movq 0x58(%rax), %r9
movl -0x124(%rbp), %eax
shll $0x2, %eax
addl $0x3, %eax
cltq
shlq $0x2, %rax
addq %rax, %r9
leaq 0xf241(%rip), %rsi      # 0x133e1
movb $0x0, %al
callq 0x21e0
cmpl $0x3, %eax
jl 0x41bb
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x41bd
jmp 0x41bf
jmp 0x41c1
jmp 0x41c3
jmp 0x41c5
jmp 0x3f66
movq -0x18(%rbp), %rdi
callq 0x21f0
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopl (%rax)