// File: ddunbar[P]PDSample/PDSampling.cpp
// Function: PDSampler::findNeighbors(Vec2&, float)
int PDSampler::findNeighbors(Vec2 &pt, float distance)
{
	if (!m_grid) {
		printf("Internal error, sampler cannot search without grid.\n");
		exit(1);
	}

	float distanceSqrd = distance*distance;
	int i, j, k, gx, gy, N = (int) ceil(distance/m_gridCellSize);
	if (N>(m_gridSize>>1)) N = m_gridSize>>1;
	
	m_neighbors.clear();
	getGridXY(pt, &gx, &gy);
	for (j=-N; j<=N; j++) {
		for (i=-N; i<=N; i++) {
			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 (getDistanceSquared(pt, points[cell[k]])<distanceSqrd)
						m_neighbors.push_back(cell[k]);
				}
			}
		}
	}

	return (int) m_neighbors.size();
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq %rsi, 0x48(%rsp)
movss %xmm0, 0x44(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x13a8(%rax)
jne 0x4050
leaq 0x141f2(%rip), %rdi     # 0x18231
movb $0x0, %al
callq 0x2040
movl $0x1, %edi
callq 0x2200
movq 0x10(%rsp), %rax
movss 0x44(%rsp), %xmm0
mulss 0x44(%rsp), %xmm0
movss %xmm0, 0x40(%rsp)
movss 0x44(%rsp), %xmm0
divss 0x13b4(%rax), %xmm0
callq 0x6c00
movq 0x10(%rsp), %rcx
cvttss2si %xmm0, %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl 0x13b0(%rcx), %ecx
sarl %ecx
cmpl %ecx, %eax
jle 0x40a8
movq 0x10(%rsp), %rax
movl 0x13b0(%rax), %eax
sarl %eax
movl %eax, 0x28(%rsp)
movq 0x10(%rsp), %rdi
addq $0x1390, %rdi           # imm = 0x1390
callq 0x6c20
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
callq 0x3e20
xorl %eax, %eax
subl 0x28(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x38(%rsp), %eax
cmpl 0x28(%rsp), %eax
jg 0x422d
xorl %eax, %eax
subl 0x28(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
cmpl 0x28(%rsp), %eax
jg 0x421b
movq 0x10(%rsp), %rcx
movl 0x30(%rsp), %eax
addl 0x3c(%rsp), %eax
addl 0x13b0(%rcx), %eax
cltd
idivl 0x13b0(%rcx)
movl %edx, 0x24(%rsp)
movl 0x2c(%rsp), %eax
addl 0x38(%rsp), %eax
addl 0x13b0(%rcx), %eax
cltd
idivl 0x13b0(%rcx)
movl %edx, %eax
movq 0x10(%rsp), %rdx
movl %eax, 0x20(%rsp)
movq 0x13a8(%rdx), %rax
movl 0x20(%rsp), %ecx
imull 0x13b0(%rdx), %ecx
addl 0x24(%rsp), %ecx
movslq %ecx, %rcx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x34(%rsp)
cmpl $0x9, 0x34(%rsp)
jge 0x4209
movq 0x18(%rsp), %rax
movslq 0x34(%rsp), %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x418a
jmp 0x4209
movq 0x10(%rsp), %rdi
movq 0x48(%rsp), %rax
movq %rax, 0x8(%rsp)
addq $0x13b8, %rdi           # imm = 0x13B8
movq 0x18(%rsp), %rax
movslq 0x34(%rsp), %rcx
movslq (%rax,%rcx,4), %rsi
callq 0x3990
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rax, %rdx
callq 0x6c40
movaps %xmm0, %xmm1
movss 0x40(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x41f5
movq 0x10(%rsp), %rdi
addq $0x1390, %rdi           # imm = 0x1390
movq 0x18(%rsp), %rsi
movslq 0x34(%rsp), %rax
shlq $0x2, %rax
addq %rax, %rsi
callq 0x6cc0
jmp 0x41f7
jmp 0x41f9
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x416d
jmp 0x420b
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x40f4
jmp 0x421d
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x40dc
movq 0x10(%rsp), %rdi
addq $0x1390, %rdi           # imm = 0x1390
callq 0x6d30
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)