// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::R2Grid(gaps::R2Box const&, double, int, int)
R2Grid::
R2Grid(const R2Box& bbox, RNLength spacing, int min_resolution, int max_resolution)
{
  // Check for empty bounding box
  if (bbox.IsEmpty() || (RNIsZero(spacing))) { *this = R2Grid(); return; }
  
  // Enforce max resolution
  if (max_resolution > 0) {
    if (bbox.XLength() / spacing > max_resolution) spacing = bbox.XLength() / max_resolution;
    if (bbox.YLength() / spacing > max_resolution) spacing = bbox.YLength() / max_resolution;
  }

  // Compute resolution
  grid_resolution[0] = (int) (bbox.XLength() / spacing + 0.5);
  grid_resolution[1] = (int) (bbox.YLength() / spacing + 0.5);

  // Enforce min resolution
  if (min_resolution > 0) {
    if (grid_resolution[0] < min_resolution) grid_resolution[0] = min_resolution;
    if (grid_resolution[1] < min_resolution) grid_resolution[1] = min_resolution;
  }

  // Set grid resolution
  grid_row_size = grid_resolution[0];
  grid_size = grid_row_size * grid_resolution[1];

  // Allocate grid values
  if (grid_size == 0) grid_values = NULL;
  else grid_values = new RNScalar [ grid_size ];
  assert(!grid_size || grid_values);

  // Set all values to zero
  for (int i = 0; i < grid_size; i++) grid_values[i] = 0;

  // Set transformations
  SetWorldToGridTransformation(bbox);
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movsd %xmm0, 0x150(%rsp)
movl %edx, 0x14c(%rsp)
movl %ecx, 0x148(%rsp)
movq 0x160(%rsp), %rdi
movq %rdi, 0x50(%rsp)
movq %rdi, %rax
movq %rax, 0x48(%rsp)
callq 0xbed00
movq 0x50(%rsp), %rdi
addq $0x58, %rdi
movq %rdi, 0x58(%rsp)
callq 0xbed00
jmp 0xa6e9d
movq 0x158(%rsp), %rdi
callq 0xa36c0
movl %eax, 0x44(%rsp)
jmp 0xa6eb0
movl 0x44(%rsp), %eax
cmpl $0x0, %eax
jne 0xa6ee5
movsd 0x150(%rsp), %xmm0
leaq 0xd1617(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
movl %eax, 0x40(%rsp)
jmp 0xa6ed8
movl 0x40(%rsp), %eax
cmpl $0x0, %eax
je 0xa6f6a
leaq 0x68(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0xa61e0
jmp 0xa6ef5
movq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0xa7220
jmp 0xa6f06
leaq 0x68(%rsp), %rdi
callq 0xa7690
jmp 0xa71ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
jmp 0xa71ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
jmp 0xa71f5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
leaq 0x68(%rsp), %rdi
callq 0xa7690
jmp 0xa71f5
cmpl $0x0, 0x148(%rsp)
jle 0xa7042
movq 0x158(%rsp), %rdi
callq 0xb9370
movsd %xmm0, 0x38(%rsp)
jmp 0xa6f8d
movsd 0x38(%rsp), %xmm0
divsd 0x150(%rsp), %xmm0
cvtsi2sdl 0x148(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0xa6fdc
movq 0x158(%rsp), %rdi
callq 0xb9370
movsd %xmm0, 0x30(%rsp)
jmp 0xa6fc0
movsd 0x30(%rsp), %xmm0
cvtsi2sdl 0x148(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x150(%rsp)
movq 0x158(%rsp), %rdi
callq 0xb9390
movsd %xmm0, 0x28(%rsp)
jmp 0xa6ff1
movsd 0x28(%rsp), %xmm0
divsd 0x150(%rsp), %xmm0
cvtsi2sdl 0x148(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0xa7040
movq 0x158(%rsp), %rdi
callq 0xb9390
movsd %xmm0, 0x20(%rsp)
jmp 0xa7024
movsd 0x20(%rsp), %xmm0
cvtsi2sdl 0x148(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x150(%rsp)
jmp 0xa7042
movq 0x158(%rsp), %rdi
callq 0xb9370
movsd %xmm0, 0x18(%rsp)
jmp 0xa7057
movq 0x48(%rsp), %rax
movsd 0x18(%rsp), %xmm0
movsd 0x150(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x8bf99(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0xc0(%rax)
movq 0x158(%rsp), %rdi
callq 0xb9390
movsd %xmm0, 0x10(%rsp)
jmp 0xa709a
movq 0x48(%rsp), %rax
movsd 0x10(%rsp), %xmm0
divsd 0x150(%rsp), %xmm0
movsd 0x8bf5a(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0xc4(%rax)
cmpl $0x0, 0x14c(%rsp)
jle 0xa711c
movq 0x48(%rsp), %rax
movl 0xc0(%rax), %eax
cmpl 0x14c(%rsp), %eax
jge 0xa70f4
movq 0x48(%rsp), %rax
movl 0x14c(%rsp), %ecx
movl %ecx, 0xc0(%rax)
movq 0x48(%rsp), %rax
movl 0xc4(%rax), %eax
cmpl 0x14c(%rsp), %eax
jge 0xa711a
movq 0x48(%rsp), %rax
movl 0x14c(%rsp), %ecx
movl %ecx, 0xc4(%rax)
jmp 0xa711c
movq 0x48(%rsp), %rax
movl 0xc0(%rax), %ecx
movl %ecx, 0xc8(%rax)
movl 0xc8(%rax), %ecx
imull 0xc4(%rax), %ecx
movl %ecx, 0xcc(%rax)
cmpl $0x0, 0xcc(%rax)
jne 0xa715b
movq 0x48(%rsp), %rax
movq $0x0, 0xb8(%rax)
jmp 0xa719a
movq 0x48(%rsp), %rax
movslq 0xcc(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xf030
movq %rax, 0x8(%rsp)
jmp 0xa7189
movq 0x48(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0xb8(%rax)
movl $0x0, 0x64(%rsp)
movq 0x48(%rsp), %rcx
movl 0x64(%rsp), %eax
cmpl 0xcc(%rcx), %eax
jge 0xa71d9
movq 0x48(%rsp), %rax
movq 0xb8(%rax), %rax
movslq 0x64(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0xa71a2
movq 0x48(%rsp), %rdi
movq 0x158(%rsp), %rsi
callq 0xa64f0
jmp 0xa71ed
addq $0x168, %rsp            # imm = 0x168
retq
movq 0x58(%rsp), %rdi
callq 0xb9250
movq 0x50(%rsp), %rdi
callq 0xb9250
movq 0x140(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)