// File: /workspace/github_cpp_makefile_star2_O3/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 0x875d(%rip), %rdi      # 0xc305
leaq 0x8765(%rip), %rsi      # 0xc314
callq 0x21f0
movq %rax, %r14
movq 0x13c0(%rbx), %r15
subq 0x13b8(%rbx), %r15
shrq $0x3, %r15
leaq 0x8746(%rip), %rdi      # 0xc316
movl $0x14, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2340
movq 0x13b8(%rbx), %rax
movq 0x13c0(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
testl %edx, %edx
jle 0x3c4b
xorl %r13d, %r13d
leaq 0x8723(%rip), %r12      # 0xc32b
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
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %r13
jl 0x3c08
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movaps 0x6859(%rip), %xmm5     # 0xa4b0
movss 0x680d(%rip), %xmm4     # 0xa46c
leaq 0x86c5(%rip), %r12      # 0xc32b
leal (%rsi,%rsi), %edx
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movss %xmm0, 0x10(%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movq %rsi, 0x20(%rsp)
movl %ebp, %edx
orl %esi, %edx
je 0x3d4b
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
testl %edx, %edx
jle 0x3d4b
leal (,%rbp,2), %edx
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movss %xmm0, 0x30(%rsp)
xorl %r13d, %r13d
movss (%rax,%r13,8), %xmm0
addss 0x30(%rsp), %xmm0
movaps %xmm0, %xmm2
andps %xmm5, %xmm2
addss %xmm4, %xmm2
movss 0x13d0(%rbx), %xmm1
mulss 0x67a3(%rip), %xmm1     # 0xa47c
ucomiss %xmm2, %xmm1
movss 0x4(%rax,%r13,8), %xmm2
addss 0x10(%rsp), %xmm2
ja 0x3cfa
movaps %xmm2, %xmm3
andps %xmm5, %xmm3
addss %xmm4, %xmm3
ucomiss %xmm3, %xmm1
jbe 0x3d32
cvtss2sd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
movq %r14, %rdi
movq %r12, %rsi
movb $0x2, %al
callq 0x2260
movaps 0x6797(%rip), %xmm5     # 0xa4b0
movss 0x674b(%rip), %xmm4     # 0xa46c
incl %r15d
movq 0x13b8(%rbx), %rax
movq 0x13c0(%rbx), %rcx
incq %r13
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %r13
jl 0x3cb3
incl %ebp
cmpl $0x2, %ebp
movq 0x20(%rsp), %rsi
jne 0x3c80
incl %esi
cmpl $0x2, %esi
jne 0x3c66
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21e0
leaq 0x85b9(%rip), %rsi      # 0xc332
movq %r14, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2260
movq %r14, %rdi
callq 0x2190
leaq 0x85c3(%rip), %r14      # 0xc358
leaq 0x8578(%rip), %rsi      # 0xc314
movq %r14, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x2190
leaq 0x8584(%rip), %rdi      # 0xc337
callq 0x2180
leaq 0x6516(%rip), %rsi      # 0xa2d5
movq %r14, %rdi
callq 0x21f0
movq %rax, %r14
leaq 0x8597(%rip), %rsi      # 0xc368
leaq 0x44(%rsp), %r15
leaq 0x1c(%rsp), %rcx
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2330
cmpl $0x2, (%r15)
jne 0x3fd1
movslq 0x1c(%rsp), %r12
leaq (,%r12,8), %rax
testq %r12, %r12
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %r15
testq %r12, %r12
jle 0x3e4b
leaq 0x850a(%rip), %r12      # 0xc32b
movq %r15, %r13
xorl %ebp, %ebp
leaq 0x4(%r13), %rcx
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2330
incq %rbp
movslq 0x1c(%rsp), %rax
addq $0x8, %r13
cmpq %rax, %rbp
jl 0x3e26
leaq 0x8519(%rip), %rsi      # 0xc36b
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 0x3fc2
leaq 0x84b2(%rip), %r12      # 0xc334
leaq 0x2c(%rsp), %rbp
movq $0x0, 0x10(%rsp)
movq %r14, %rdi
movq %r12, %rsi
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x2330
movl 0xc(%rsp), %eax
xorps %xmm2, %xmm2
testl %eax, %eax
jle 0x3f12
movb $0x1, %al
xorl %r13d, %r13d
movl %eax, 0x20(%rsp)
movaps %xmm2, 0x30(%rsp)
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2330
movslq 0x2c(%rsp), %rax
testq %rax, %rax
js 0x3ef1
movsd (%r15,%rax,8), %xmm0
movaps 0x30(%rsp), %xmm2
addps %xmm0, %xmm2
incl %r13d
movl 0xc(%rsp), %eax
cmpl %eax, %r13d
jl 0x3eb6
jmp 0x3f07
incl %r13d
xorl %eax, %eax
cmpl 0xc(%rsp), %r13d
movaps 0x30(%rsp), %xmm2
jl 0x3eb2
jmp 0x3f98
testb $0x1, 0x20(%rsp)
je 0x3f98
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss 0x654f(%rip), %xmm1     # 0xa470
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 0x3f85
movss 0x6527(%rip), %xmm1     # 0xa46c
ucomiss %xmm0, %xmm1
jbe 0x3f54
addss 0x6516(%rip), %xmm0     # 0xa468
jmp 0x3f65
ucomiss 0x6515(%rip), %xmm0     # 0xa470
jbe 0x3f65
addss 0x650f(%rip), %xmm0     # 0xa474
ucomiss %xmm2, %xmm1
jbe 0x3f74
addss 0x64f6(%rip), %xmm2     # 0xa468
jmp 0x3f85
ucomiss 0x64f5(%rip), %xmm2     # 0xa470
jbe 0x3f85
addss 0x64ef(%rip), %xmm2     # 0xa474
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
movq 0x13b8(%rbx), %rax
movq 0x10(%rsp), %rcx
movlps %xmm0, (%rax,%rcx,8)
movq 0x13c0(%rbx), %rax
subq 0x13b8(%rbx), %rax
movq 0x10(%rsp), %rdx
incq %rdx
shrq $0x3, %rax
cltq
movq %rdx, 0x10(%rsp)
cmpq %rax, %rdx
jl 0x3e90
movl $0x8, %esi
movq %r15, %rdi
callq 0x21b0
jmp 0x3fdd
leaq 0x851a(%rip), %rdi      # 0xc4f2
callq 0x2290
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq