// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %rbx
leaq 0x79a7(%rip), %rdi      # 0xb305
leaq 0x79af(%rip), %rsi      # 0xb314
callq 0x21e0
movq %rax, %r14
movq 0x13c0(%rbx), %r15
subq 0x13b8(%rbx), %r15
shrq $0x3, %r15
leaq 0x7990(%rip), %rdi      # 0xb316
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2330
xorl %r13d, %r13d
leaq 0x798d(%rip), %r12      # 0xb32b
movq 0x13b8(%rbx), %rax
movq 0x13c0(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %r13
jge 0x39dd
cvtss2sd (%rax,%r13,8), %xmm0
cvtss2sd 0x4(%rax,%r13,8), %xmm1
movq %r14, %rdi
movq %r12, %rsi
movb $0x2, %al
callq 0x2250
incq %r13
jmp 0x399e
pushq $-0x1
popq %rdx
movaps 0x5ac9(%rip), %xmm4     # 0x94b0
movss 0x5a7d(%rip), %xmm5     # 0x946c
leaq 0x7935(%rip), %r12      # 0xb32b
movl %edx, %esi
cmpl $0x2, %esi
je 0x3aec
leal (%rsi,%rsi), %edx
cvtsi2ss %edx, %xmm0
movss %xmm0, 0x8(%rsp)
pushq $-0x1
popq %rdx
movl %edx, %ebp
movq %rsi, 0x10(%rsp)
cmpl $0x2, %ebp
je 0x3ae5
movl %ebp, %edx
orl %esi, %edx
je 0x3ade
leal (,%rbp,2), %edx
cvtsi2ss %edx, %xmm0
movss %xmm0, 0x20(%rsp)
xorl %r13d, %r13d
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movslq %edx, %rdx
cmpq %rdx, %r13
jge 0x3ad9
movss (%rax,%r13,8), %xmm0
addss 0x20(%rsp), %xmm0
movaps %xmm0, %xmm2
andps %xmm4, %xmm2
addss %xmm5, %xmm2
movss 0x13d0(%rbx), %xmm1
mulss 0x5a01(%rip), %xmm1     # 0x947c
ucomiss %xmm2, %xmm1
movss 0x4(%rax,%r13,8), %xmm2
addss 0x8(%rsp), %xmm2
ja 0x3a9c
movaps %xmm2, %xmm3
andps %xmm4, %xmm3
addss %xmm5, %xmm3
ucomiss %xmm3, %xmm1
jbe 0x3ad1
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm2, %xmm1
movq %r14, %rdi
movq %r12, %rsi
movb $0x2, %al
callq 0x2250
movss 0x59b3(%rip), %xmm5     # 0x946c
movaps 0x59f0(%rip), %xmm4     # 0x94b0
incl %r15d
movq 0x13b8(%rbx), %rax
movq 0x13c0(%rbx), %rcx
incq %r13
jmp 0x3a3f
movq 0x10(%rsp), %rsi
incl %ebp
jmp 0x3a18
incl %esi
jmp 0x39f8
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21d0
leaq 0x7833(%rip), %rsi      # 0xb332
movq %r14, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0x2250
movq %r14, %rdi
callq 0x2180
leaq 0x783d(%rip), %r14      # 0xb358
leaq 0x77f2(%rip), %rsi      # 0xb314
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2180
leaq 0x77fe(%rip), %rdi      # 0xb337
callq 0x2170
leaq 0x5790(%rip), %rsi      # 0x92d5
movq %r14, %rdi
callq 0x21e0
movq %rax, %r14
leaq 0x7811(%rip), %rsi      # 0xb368
leaq 0x34(%rsp), %r15
leaq 0x4(%rsp), %rcx
movq %rax, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2320
cmpl $0x2, (%r15)
jne 0x3bcb
movslq 0x4(%rsp), %r15
leaq (,%r15,8), %rax
testq %r15, %r15
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %r13
leaq 0x7791(%rip), %r12      # 0xb32b
movq %rax, 0x8(%rsp)
xorl %ebp, %ebp
movslq %r15d, %rax
cmpq %rax, %rbp
jge 0x3bdc
leaq 0x4(%r13), %rcx
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
xorl %eax, %eax
callq 0x2320
incq %rbp
movl 0x4(%rsp), %r15d
addq $0x8, %r13
jmp 0x3ba1
leaq 0x7920(%rip), %rdi      # 0xb4f2
callq 0x2280
jmp 0x3cd7
leaq 0x7788(%rip), %rsi      # 0xb36b
leaq 0x30(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2320
leaq 0x773b(%rip), %r12      # 0xb334
leaq 0x18(%rsp), %rbp
xorl %r15d, %r15d
movq 0x13c0(%rbx), %rax
subq 0x13b8(%rbx), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r15
jge 0x3cca
movq %r15, 0x10(%rsp)
xorl %r13d, %r13d
movq %r14, %rdi
movq %r12, %rsi
leaq 0x1c(%rsp), %rdx
xorl %eax, %eax
callq 0x2320
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
xorl %r15d, %r15d
movl 0x1c(%rsp), %eax
cmpl %eax, %r15d
jge 0x3c88
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2320
movslq 0x18(%rsp), %rax
testq %rax, %rax
js 0x3c7f
movq 0x8(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm0
movaps 0x20(%rsp), %xmm1
addps %xmm0, %xmm1
movaps %xmm1, 0x20(%rsp)
jmp 0x3c83
pushq $0x1
popq %r13
incl %r15d
jmp 0x3c43
testl %r13d, %r13d
movq 0x10(%rsp), %r15
jne 0x3cc2
cvtsi2ss %eax, %xmm0
movss 0x57d2(%rip), %xmm1     # 0x9470
divss %xmm0, %xmm1
shufps $0x0, %xmm1, %xmm1      # xmm1 = xmm1[0,0,0,0]
movaps 0x20(%rsp), %xmm0
mulps %xmm1, %xmm0
movq %rbx, %rdi
callq 0x2fb6
movq 0x13b8(%rbx), %rax
movlps %xmm0, (%rax,%r15,8)
incq %r15
jmp 0x3c01
pushq $0x8
popq %rsi
movq 0x8(%rsp), %rdi
callq 0x21a0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq