// File: 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:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x48(%rsp)
leaq 0x13613(%rip), %rdi     # 0x18266
leaq 0x1361b(%rip), %rsi     # 0x18275
callq 0x21f0
movq 0x48(%rsp), %rdi
movq %rax, 0xd8(%rsp)
movq $0x0, 0xc0(%rsp)
addq $0x13b8, %rdi           # imm = 0x13B8
callq 0x3970
movl %eax, 0xbc(%rsp)
movq 0xd8(%rsp), %rdi
leaq 0x135dd(%rip), %rsi     # 0x18277
movb $0x0, %al
callq 0x2250
movl $0x0, 0xb8(%rsp)
movq 0x48(%rsp), %rdi
movl 0xb8(%rsp), %eax
movl %eax, 0x44(%rsp)
addq $0x13b8, %rdi           # imm = 0x13B8
callq 0x3970
movq %rax, %rcx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x4d41
movq 0x48(%rsp), %rdi
addq $0x13b8, %rdi           # imm = 0x13B8
movslq 0xb8(%rsp), %rsi
callq 0x3990
movq %rax, 0xb0(%rsp)
movq 0xd8(%rsp), %rdi
movq 0xb0(%rsp), %rax
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0xb0(%rsp), %rax
movss 0x4(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x13568(%rip), %rsi     # 0x1828c
movb $0x2, %al
callq 0x2250
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x4cac
movl $0xffffffff, 0xac(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0xac(%rsp)
jg 0x4f2d
movl $0xffffffff, 0xa8(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0xa8(%rsp)
jg 0x4f15
cmpl $0x0, 0xa8(%rsp)
jne 0x4d8b
cmpl $0x0, 0xac(%rsp)
je 0x4efd
movl $0x0, 0xa4(%rsp)
movq 0x48(%rsp), %rdi
movl 0xa4(%rsp), %eax
movl %eax, 0x40(%rsp)
addq $0x13b8, %rdi           # imm = 0x13B8
callq 0x3970
movq %rax, %rcx
movl 0x40(%rsp), %eax
cmpl %ecx, %eax
jge 0x4efb
movq 0x48(%rsp), %rdi
addq $0x13b8, %rdi           # imm = 0x13B8
movslq 0xa4(%rsp), %rsi
callq 0x3990
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movss (%rax), %xmm0
movl 0xa8(%rsp), %eax
shll %eax
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
callq 0x6e40
movq 0x48(%rsp), %rax
movaps %xmm0, %xmm1
movss 0x116b4(%rip), %xmm0    # 0x164c8
subss %xmm0, %xmm1
movss 0x116ac(%rip), %xmm0    # 0x164cc
mulss 0x13d0(%rax), %xmm0
ucomiss %xmm1, %xmm0
ja 0x4e79
movq 0x98(%rsp), %rax
movss 0x4(%rax), %xmm0
movl 0xac(%rsp), %eax
shll %eax
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
callq 0x6e40
movq 0x48(%rsp), %rax
movaps %xmm0, %xmm1
movss 0x11668(%rip), %xmm0    # 0x164c8
subss %xmm0, %xmm1
movss 0x11660(%rip), %xmm0    # 0x164cc
mulss 0x13d0(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x4ee3
movq 0xd8(%rsp), %rdi
movq 0x98(%rsp), %rax
movss (%rax), %xmm0
movl 0xa8(%rsp), %eax
shll %eax
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x98(%rsp), %rax
movss 0x4(%rax), %xmm1
movl 0xac(%rsp), %eax
shll %eax
cvtsi2ss %eax, %xmm2
addss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x133c1(%rip), %rsi     # 0x1828c
movb $0x2, %al
callq 0x2250
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x4ee5
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x4d96
jmp 0x4efd
jmp 0x4eff
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x4d65
jmp 0x4f17
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x4d4c
movq 0xd8(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x21e0
movq 0xd8(%rsp), %rdi
movl 0xbc(%rsp), %edx
leaq 0x1333d(%rip), %rsi     # 0x18293
movb $0x0, %al
callq 0x2250
movq 0xd8(%rsp), %rdi
callq 0x2190
leaq 0x13348(%rip), %rdi     # 0x182b9
leaq 0x132fd(%rip), %rsi     # 0x18275
callq 0x21f0
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x2190
leaq 0x132ff(%rip), %rdi     # 0x18298
callq 0x2180
leaq 0x13314(%rip), %rdi     # 0x182b9
leaq 0x112ef(%rip), %rsi     # 0x1629b
callq 0x21f0
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
leaq 0x13301(%rip), %rsi     # 0x182c9
leaq 0xd4(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
movb $0x0, %al
callq 0x2310
cmpl $0x2, 0xd4(%rsp)
je 0x4ffc
leaq 0x132e0(%rip), %rdi     # 0x182d0
movb $0x0, %al
callq 0x2040
jmp 0x52b0
movslq 0xd0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, 0x28(%rsp)
callq 0x2030
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x38(%rsp)
cmpq $0x0, %rax
je 0x5089
movq 0x38(%rsp), %rax
movq 0x30(%rsp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x6e60
jmp 0x5071
movq 0x18(%rsp), %rcx
movq 0x10(%rsp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, 0x20(%rsp)
jne 0x5060
movq 0x38(%rsp), %rax
movq %rax, 0xc0(%rsp)
movl $0x0, 0x88(%rsp)
movl 0x88(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jge 0x5138
movq 0xd8(%rsp), %rdi
movq 0xc0(%rsp), %rdx
movslq 0x88(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdx
movq 0xc0(%rsp), %rcx
movslq 0x88(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rcx
addq $0x4, %rcx
leaq 0x13196(%rip), %rsi     # 0x1828c
movb $0x0, %al
callq 0x2310
movl 0x88(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x88(%rsp)
jmp 0x50a1
movq 0x28(%rsp), %rsi
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x90(%rsp)
movl %eax, 0x8c(%rsp)
callq 0x2330
jmp 0x52e5
movq 0xd8(%rsp), %rdi
leaq 0x13185(%rip), %rsi     # 0x182cc
leaq 0xcc(%rsp), %rdx
movb $0x0, %al
callq 0x2310
movl $0x0, 0x84(%rsp)
movq 0x48(%rsp), %rdi
movl 0x84(%rsp), %eax
movl %eax, 0xc(%rsp)
addq $0x13b8, %rdi           # imm = 0x13B8
callq 0x3970
movq %rax, %rcx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x52ae
leaq 0x7c(%rsp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x6b60
movl $0x0, 0x74(%rsp)
movq 0xd8(%rsp), %rdi
leaq 0x130e2(%rip), %rsi     # 0x18295
leaq 0x78(%rsp), %rdx
movb $0x0, %al
callq 0x2310
movl $0x0, 0x70(%rsp)
movl 0x70(%rsp), %eax
cmpl 0x78(%rsp), %eax
jge 0x5232
movq 0xd8(%rsp), %rdi
leaq 0x130b5(%rip), %rsi     # 0x18295
leaq 0x6c(%rsp), %rdx
movb $0x0, %al
callq 0x2310
cmpl $0x0, 0x6c(%rsp)
jge 0x51fd
movl $0x1, 0x74(%rsp)
jmp 0x5223
movq 0xc0(%rsp), %rax
movslq 0x6c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
leaq 0x7c(%rsp), %rdi
callq 0x6e70
jmp 0x5225
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0x51c7
cmpl $0x0, 0x74(%rsp)
jne 0x5296
cvtsi2ssl 0x78(%rsp), %xmm1
movss 0x11281(%rip), %xmm0    # 0x164c8
divss %xmm1, %xmm0
leaq 0x7c(%rsp), %rdi
callq 0x6ea0
movq 0x48(%rsp), %rdi
movq 0x7c(%rsp), %rax
movq %rax, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
callq 0x3d20
movq 0x48(%rsp), %rdi
movlpd %xmm0, 0x58(%rsp)
addq $0x13b8, %rdi           # imm = 0x13B8
movslq 0x84(%rsp), %rsi
callq 0x3990
movq 0x58(%rsp), %rcx
movq %rcx, (%rax)
jmp 0x5298
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x5161
jmp 0x52b0
cmpq $0x0, 0xc0(%rsp)
je 0x52dd
movq 0xc0(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x52db
movq (%rsp), %rdi
movl $0x8, %esi
callq 0x21b0
jmp 0x52dd
addq $0xe8, %rsp
retq
movq 0x90(%rsp), %rdi
callq 0x22f0
nopw %cs:(%rax,%rax)