// File: /workspace/github_cpp_makefile_star2_O1/ddunbar[P]PDSample/PDSampling.cpp
// Function: PDSampler::relax()
void PDSampler::relax()
{
	FILE *tmp = fopen("relaxTmpIn.txt","w");
	int dim, numVerts, numFaces;
	Vec2 *verts = 0;
	int numPoints = (int) points.size();

		// will overwrite later
	fprintf(tmp, "2                  \n");
	for (int i=0; i<(int) points.size(); i++) {
		Vec2 &pt = points[i];
		fprintf(tmp, "%f %f\n", pt.x, pt.y);
	}
	for (int y=-1; y<=1; y++) {
		for (int x=-1; x<=1; x++) {
			if (x || y) {
				for (int i=0; i<(int) points.size(); i++) {
					Vec2 &pt = points[i];
					if (fabs(pt.x+x*2)-1<radius*4 || fabs(pt.y+y*2)-1<radius*4) {
						fprintf(tmp, "%f %f\n", pt.x+x*2, pt.y+y*2);
						numPoints++;
					}
				}
			}
		}
	}
	fseek(tmp, 0, 0);
	fprintf(tmp, "2 %d", numPoints);
	fclose(tmp);

	tmp = fopen("relaxTmpOut.txt", "w");
	fclose(tmp);
	system("qvoronoi p FN < relaxTmpIn.txt > relaxTmpOut.txt");

	tmp = fopen("relaxTmpOut.txt", "r");
	fscanf(tmp, "%d\n%d\n", &dim, &numVerts);

	if (dim!=2) {
		printf("Error calling out to qvoronoi, skipping relaxation.\n");
		goto exit;
	}

	verts = new Vec2[numVerts];
	for (int i=0; i<numVerts; i++) {
		fscanf(tmp, "%f %f\n", &verts[i].x, &verts[i].y);
	}

	fscanf(tmp, "%d\n", &numFaces);

	for (int i=0; i<(int) points.size(); i++) {
		Vec2 center(0,0);
		int N, skip=0;

		fscanf(tmp, "%d", &N);
		for (int j=0; j<N; j++) {
			int index;

			fscanf(tmp, "%d", &index);
			if (index<0) {
				skip = 1;
			} else {
				center += verts[index];
			}
		}

		if (!skip) {
			center *= (1.0f/N);
			points[i] = getTiled(center);
		}
	}

exit:
	if (verts) delete verts;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x8689(%rip), %rdi      # 0xc315
leaq 0x8691(%rip), %rsi      # 0xc324
callq 0x21f0
movq %rax, %r14
movq 0x13c0(%rbx), %r15
subq 0x13b8(%rbx), %r15
shrq $0x3, %r15
leaq 0x8672(%rip), %rdi      # 0xc326
movl $0x14, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2340
movq 0x13b8(%rbx), %rax
movq 0x13c0(%rbx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x3d29
xorl %r13d, %r13d
leaq 0x8652(%rip), %r12      # 0xc33b
xorps %xmm0, %xmm0
cvtss2sd (%rax,%r13,8), %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rax,%r13,8), %xmm1
movq %r14, %rdi
movq %r12, %rsi
movb $0x2, %al
callq 0x2260
incq %r13
movq 0x13b8(%rbx), %rax
movq 0x13c0(%rbx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jl 0x3ce9
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movaps 0x678b(%rip), %xmm3     # 0xa4c0
leaq 0x85ff(%rip), %r12      # 0xc33b
leal (%rcx,%rcx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x20(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rcx, 0x38(%rsp)
movl %ebp, %eax
orl %ecx, %eax
je 0x3e30
movq 0x13b8(%rbx), %rax
movq 0x13c0(%rbx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
testl %ecx, %ecx
jle 0x3e30
leal (,%rbp,2), %ecx
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
movss %xmm0, 0x10(%rsp)
xorl %r13d, %r13d
movss (%rax,%r13,8), %xmm0
addss 0x10(%rsp), %xmm0
movaps %xmm0, %xmm2
andps %xmm3, %xmm2
addss 0x66ce(%rip), %xmm2     # 0xa47c
movss 0x13d0(%rbx), %xmm1
mulss 0x66ce(%rip), %xmm1     # 0xa48c
ucomiss %xmm2, %xmm1
ja 0x3de0
movss 0x4(%rax,%r13,8), %xmm2
addss 0x20(%rsp), %xmm2
andps %xmm3, %xmm2
addss 0x66a1(%rip), %xmm2     # 0xa47c
ucomiss %xmm2, %xmm1
jbe 0x3e0c
cvtss2sd %xmm0, %xmm0
movss 0x4(%rax,%r13,8), %xmm1
addss 0x20(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movq %r14, %rdi
movq %r12, %rsi
movb $0x2, %al
callq 0x2260
movaps 0x66b7(%rip), %xmm3     # 0xa4c0
incl %r15d
incq %r13
movq 0x13b8(%rbx), %rax
movq 0x13c0(%rbx), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r13
jl 0x3d94
incl %ebp
cmpl $0x2, %ebp
movq 0x38(%rsp), %rcx
jne 0x3d56
incl %ecx
cmpl $0x2, %ecx
jne 0x3d3c
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21e0
leaq 0x84e4(%rip), %rsi      # 0xc342
movq %r14, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2260
movq %r14, %rdi
callq 0x2190
leaq 0x84ee(%rip), %r14      # 0xc368
leaq 0x84a3(%rip), %rsi      # 0xc324
movq %r14, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x2190
leaq 0x84af(%rip), %rdi      # 0xc347
callq 0x2180
leaq 0x624e(%rip), %rsi      # 0xa0f2
movq %r14, %rdi
callq 0x21f0
movq %rax, %r14
leaq 0x84c2(%rip), %rsi      # 0xc378
leaq 0x44(%rsp), %r15
leaq 0xc(%rsp), %rcx
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2330
cmpl $0x2, (%r15)
jne 0x40a1
movslq 0xc(%rsp), %r15
leaq (,%r15,8), %rax
testq %r15, %r15
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %rbp
testq %r15, %r15
jle 0x3f31
leaq 0x8435(%rip), %r12      # 0xc33b
movq %rbp, %r13
xorl %r15d, %r15d
leaq 0x4(%r13), %rcx
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2330
incq %r15
movslq 0xc(%rsp), %rax
addq $0x8, %r13
cmpq %rax, %r15
jl 0x3f0c
leaq 0x8443(%rip), %rsi      # 0xc37b
leaq 0x40(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2330
movq 0x13c0(%rbx), %rax
subq 0x13b8(%rbx), %rax
shrq $0x3, %rax
testl %eax, %eax
jle 0x40af
leaq 0x83dc(%rip), %r12      # 0xc344
xorl %r15d, %r15d
movq %r14, %rdi
movq %r12, %rsi
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x2330
movl 0x8(%rsp), %eax
testl %eax, %eax
jle 0x3fe8
movq %r15, 0x10(%rsp)
xorps %xmm2, %xmm2
xorl %r13d, %r13d
xorl %r15d, %r15d
movaps %xmm2, 0x20(%rsp)
movq %r14, %rdi
movq %r12, %rsi
leaq 0x1c(%rsp), %rdx
xorl %eax, %eax
callq 0x2330
movslq 0x1c(%rsp), %rax
testq %rax, %rax
js 0x3fc4
movsd (%rbp,%rax,8), %xmm0
movaps 0x20(%rsp), %xmm2
addps %xmm0, %xmm2
jmp 0x3fcf
movl $0x1, %r15d
movaps 0x20(%rsp), %xmm2
incl %r13d
movl 0x8(%rsp), %eax
cmpl %eax, %r13d
jl 0x3f93
testl %r15d, %r15d
sete %cl
movq 0x10(%rsp), %r15
jmp 0x3fed
movb $0x1, %cl
xorps %xmm2, %xmm2
testb %cl, %cl
je 0x407f
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0x647c(%rip), %xmm1     # 0xa480
divss %xmm0, %xmm1
movaps %xmm2, %xmm0
mulss %xmm1, %xmm0
shufps $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
mulss %xmm1, %xmm2
cmpb $0x1, 0x13d4(%rbx)
jne 0x4070
movss 0x6454(%rip), %xmm1     # 0xa47c
ucomiss %xmm0, %xmm1
jbe 0x4037
addss 0x6443(%rip), %xmm0     # 0xa478
jmp 0x4048
ucomiss 0x6442(%rip), %xmm0     # 0xa480
jbe 0x4048
addss 0x643c(%rip), %xmm0     # 0xa484
movss 0x642c(%rip), %xmm1     # 0xa47c
ucomiss %xmm2, %xmm1
jbe 0x405f
addss 0x641b(%rip), %xmm2     # 0xa478
jmp 0x4070
ucomiss 0x641a(%rip), %xmm2     # 0xa480
jbe 0x4070
addss 0x6414(%rip), %xmm2     # 0xa484
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movq 0x13b8(%rbx), %rax
movlps %xmm0, (%rax,%r15,8)
movq 0x13c0(%rbx), %rax
subq 0x13b8(%rbx), %rax
incq %r15
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jl 0x3f6b
jmp 0x40af
leaq 0x845a(%rip), %rdi      # 0xc502
callq 0x2290
xorl %ebp, %ebp
testq %rbp, %rbp
je 0x40c1
movl $0x8, %esi
movq %rbp, %rdi
callq 0x21b0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq