// File: ddunbar[P]PDSample/PDSampling.cpp
// Function: PDSampler::findNeighborRanges(int, RangeList&)
void PDSampler::findNeighborRanges(int index, RangeList &rl)
{
	if (!m_grid) {
		printf("Internal error, sampler cannot search without grid.\n");
		exit(1);
	}

	Vec2 &candidate = points[index];
	float rangeSqrd = 4*4*radius*radius;
	int i, j, k, gx, gy, N = (int) ceil(4*radius/m_gridCellSize);
	if (N>(m_gridSize>>1)) N = m_gridSize>>1;
	
	getGridXY(candidate, &gx, &gy);

	int xSide = (candidate.x - (-1 + gx*m_gridCellSize))>m_gridCellSize*.5;
	int ySide = (candidate.y - (-1 + gy*m_gridCellSize))>m_gridCellSize*.5;
	int iy = 1;
	for (j=-N; j<=N; j++) {
		int ix = 1;

		if (j==0) iy = ySide;
		else if (j==1) iy = 0;

		for (i=-N; i<=N; i++) {
			if (i==0) ix = xSide;
			else if (i==1) ix = 0;

				// offset to closest cell point
			float dx = candidate.x - (-1 + (gx+i+ix)*m_gridCellSize);
			float dy = candidate.y - (-1 + (gy+j+iy)*m_gridCellSize);

			if (dx*dx+dy*dy<rangeSqrd) {
				int cx = (gx+i+m_gridSize)%m_gridSize;
				int cy = (gy+j+m_gridSize)%m_gridSize;
				int *cell = m_grid[cy*m_gridSize + cx];

				for (k=0; k<kMaxPointsPerCell; k++) {
					if (cell[k]==-1) {
						break;
					} else if (cell[k]!=index) {
						Vec2 &pt = points[cell[k]];
						Vec2 v = getTiled(pt-candidate);
						float distSqrd = v.x*v.x + v.y*v.y;

						if (distSqrd<rangeSqrd) {
							float dist = sqrt(distSqrd);
							float angle = atan2(v.y,v.x);
							float theta = acos(.25f*dist/radius);

							rl.subtract(angle-theta, angle+theta);
						}
					}
				}
			}
		}
	}
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0xa0(%rsp)
movl %esi, 0x9c(%rsp)
movq %rdx, 0x90(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x13a8(%rax)
jne 0x44ad
leaq 0x13d95(%rip), %rdi     # 0x18231
movb $0x0, %al
callq 0x2040
movl $0x1, %edi
callq 0x2200
movq 0x8(%rsp), %rdi
addq $0x13b8, %rdi           # imm = 0x13B8
movslq 0x9c(%rsp), %rsi
callq 0x3990
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x88(%rsp)
movss 0x11ff2(%rip), %xmm0    # 0x164d0
mulss 0x13d0(%rax), %xmm0
mulss 0x13d0(%rax), %xmm0
movss %xmm0, 0x84(%rsp)
movss 0x11fcd(%rip), %xmm0    # 0x164cc
mulss 0x13d0(%rax), %xmm0
divss 0x13b4(%rax), %xmm0
callq 0x6c00
movq 0x8(%rsp), %rcx
cvttss2si %xmm0, %eax
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl 0x13b0(%rcx), %ecx
sarl %ecx
cmpl %ecx, %eax
jle 0x4542
movq 0x8(%rsp), %rax
movl 0x13b0(%rax), %eax
sarl %eax
movl %eax, 0x6c(%rsp)
movq 0x8(%rsp), %rdi
movq 0x88(%rsp), %rsi
leaq 0x74(%rsp), %rdx
leaq 0x70(%rsp), %rcx
callq 0x3e20
movq 0x8(%rsp), %rax
movq 0x88(%rsp), %rcx
movss (%rcx), %xmm0
cvtsi2ssl 0x74(%rsp), %xmm1
movss 0x13b4(%rax), %xmm2
mulss %xmm2, %xmm1
movss 0x11f3b(%rip), %xmm2    # 0x164c4
addss %xmm2, %xmm1
subss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x13b4(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x11a6f(%rip), %xmm2    # 0x16018
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
seta %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, 0x68(%rsp)
movq 0x88(%rsp), %rcx
movss 0x4(%rcx), %xmm0
cvtsi2ssl 0x70(%rsp), %xmm1
movss 0x13b4(%rax), %xmm2
mulss %xmm2, %xmm1
movss 0x11edf(%rip), %xmm2    # 0x164c4
addss %xmm2, %xmm1
subss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x13b4(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x11a13(%rip), %xmm2    # 0x16018
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
seta %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x64(%rsp)
movl $0x1, 0x60(%rsp)
xorl %eax, %eax
subl 0x6c(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jg 0x4957
movl $0x1, 0x5c(%rsp)
cmpl $0x0, 0x7c(%rsp)
jne 0x4652
movl 0x64(%rsp), %eax
movl %eax, 0x60(%rsp)
jmp 0x4663
cmpl $0x1, 0x7c(%rsp)
jne 0x4661
movl $0x0, 0x60(%rsp)
jmp 0x4663
xorl %eax, %eax
subl 0x6c(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0x80(%rsp), %eax
cmpl 0x6c(%rsp), %eax
jg 0x4945
cmpl $0x0, 0x80(%rsp)
jne 0x4695
movl 0x68(%rsp), %eax
movl %eax, 0x5c(%rsp)
jmp 0x46a9
cmpl $0x1, 0x80(%rsp)
jne 0x46a7
movl $0x0, 0x5c(%rsp)
jmp 0x46a9
movq 0x8(%rsp), %rax
movq 0x88(%rsp), %rcx
movss (%rcx), %xmm0
movl 0x74(%rsp), %ecx
addl 0x80(%rsp), %ecx
addl 0x5c(%rsp), %ecx
cvtsi2ss %ecx, %xmm1
movss 0x13b4(%rax), %xmm2
mulss %xmm2, %xmm1
movss 0x11de3(%rip), %xmm2    # 0x164c4
addss %xmm2, %xmm1
subss %xmm1, %xmm0
movss %xmm0, 0x58(%rsp)
movq 0x88(%rsp), %rcx
movss 0x4(%rcx), %xmm0
movl 0x70(%rsp), %ecx
addl 0x7c(%rsp), %ecx
addl 0x60(%rsp), %ecx
cvtsi2ss %ecx, %xmm1
movss 0x13b4(%rax), %xmm2
mulss %xmm2, %xmm1
movss 0x11da4(%rip), %xmm2    # 0x164c4
addss %xmm2, %xmm1
subss %xmm1, %xmm0
movss %xmm0, 0x54(%rsp)
movss 0x58(%rsp), %xmm1
movss 0x58(%rsp), %xmm2
movss 0x54(%rsp), %xmm0
mulss 0x54(%rsp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x84(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x492d
movq 0x8(%rsp), %rcx
movl 0x74(%rsp), %eax
addl 0x80(%rsp), %eax
addl 0x13b0(%rcx), %eax
cltd
idivl 0x13b0(%rcx)
movl %edx, 0x50(%rsp)
movl 0x70(%rsp), %eax
addl 0x7c(%rsp), %eax
addl 0x13b0(%rcx), %eax
cltd
idivl 0x13b0(%rcx)
movl %edx, %eax
movq 0x8(%rsp), %rdx
movl %eax, 0x4c(%rsp)
movq 0x13a8(%rdx), %rax
movl 0x4c(%rsp), %ecx
imull 0x13b0(%rdx), %ecx
addl 0x50(%rsp), %ecx
movslq %ecx, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movl $0x0, 0x78(%rsp)
cmpl $0x9, 0x78(%rsp)
jge 0x492b
movq 0x40(%rsp), %rax
movslq 0x78(%rsp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x47ee
jmp 0x492b
movq 0x40(%rsp), %rax
movslq 0x78(%rsp), %rcx
movl (%rax,%rcx,4), %eax
cmpl 0x9c(%rsp), %eax
je 0x4917
movq 0x8(%rsp), %rdi
addq $0x13b8, %rdi           # imm = 0x13B8
movq 0x40(%rsp), %rax
movslq 0x78(%rsp), %rcx
movslq (%rax,%rcx,4), %rsi
callq 0x3990
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq 0x88(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
callq 0x6d70
movq 0x8(%rsp), %rdi
movlpd %xmm0, 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
callq 0x3d20
movlpd %xmm0, 0x30(%rsp)
movss 0x30(%rsp), %xmm0
movss 0x30(%rsp), %xmm2
movss 0x34(%rsp), %xmm1
mulss 0x34(%rsp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x1c(%rsp), %xmm1
movss 0x84(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x4915
movss 0x1c(%rsp), %xmm0
callq 0x6d50
movss %xmm0, 0x18(%rsp)
movss 0x34(%rsp), %xmm0
movss 0x30(%rsp), %xmm1
callq 0x6dc0
movq 0x8(%rsp), %rax
movss %xmm0, 0x14(%rsp)
movss 0x11bfd(%rip), %xmm0    # 0x164d4
mulss 0x18(%rsp), %xmm0
divss 0x13d0(%rax), %xmm0
callq 0x6de0
movss %xmm0, 0x10(%rsp)
movq 0x90(%rsp), %rdi
movss 0x14(%rsp), %xmm0
subss 0x10(%rsp), %xmm0
movss 0x14(%rsp), %xmm1
addss 0x10(%rsp), %xmm1
callq 0xff20
jmp 0x4917
jmp 0x4919
jmp 0x491b
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x47ce
jmp 0x492d
jmp 0x492f
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x4670
jmp 0x4947
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x462b
addq $0xa8, %rsp
retq
nop