// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movl %ecx, %ebp
movl %edx, %r12d
movapd %xmm0, (%rsp)
movq %rsi, %r15
movq %rdi, %rbx
callq 0x5d2fe
leaq 0x58(%rbx), %r14
movq %r14, %rdi
callq 0x5d2fe
movq %r15, %rdi
callq 0x50606
testl %eax, %eax
jne 0x51d3b
leaq 0x88e8f(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm0
movapd 0x4130b(%rip), %xmm1    # 0x93030
xorpd %xmm0, %xmm1
movapd (%rsp), %xmm2
ucomisd %xmm1, %xmm2
jb 0x51d72
ucomisd (%rsp), %xmm0
jb 0x51d72
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x51628
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x51ed6
leaq 0x18(%rsp), %rdi
callq 0x5216c
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x8(%r15), %xmm2
movsd 0x18(%r15), %xmm0
testl %ebp, %ebp
jle 0x51ddc
cvtsi2sd %ebp, %xmm1
movhpd 0x20(%r15), %xmm0       # xmm0 = xmm0[0],mem[0]
movhpd 0x10(%r15), %xmm2       # xmm2 = xmm2[0],mem[0]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
movapd (%rsp), %xmm5
divsd %xmm5, %xmm3
movapd %xmm0, %xmm4
divsd %xmm1, %xmm4
movapd %xmm1, %xmm2
cmpltsd %xmm3, %xmm2
andpd %xmm2, %xmm4
andnpd %xmm5, %xmm2
orpd %xmm4, %xmm2
movapd %xmm0, %xmm4
unpckhpd %xmm0, %xmm4            # xmm4 = xmm4[1],xmm0[1]
movapd %xmm4, %xmm3
divsd %xmm2, %xmm3
ucomisd %xmm1, %xmm3
jbe 0x51df3
divsd %xmm1, %xmm4
jmp 0x51df7
movhpd 0x20(%r15), %xmm0       # xmm0 = xmm0[0],mem[0]
movhpd 0x10(%r15), %xmm2       # xmm2 = xmm2[0],mem[0]
subpd %xmm2, %xmm0
movapd (%rsp), %xmm4
jmp 0x51df7
movapd %xmm2, %xmm4
unpcklpd %xmm4, %xmm4            # xmm4 = xmm4[0,0]
divpd %xmm4, %xmm0
addpd 0x41239(%rip), %xmm0    # 0x93040
cvttpd2dq %xmm0, %xmm0
movlpd %xmm0, 0xc0(%rbx)
movd %xmm0, %eax
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %ecx
testl %r12d, %r12d
jle 0x51e42
cmpl %r12d, %eax
jge 0x51e34
movl %r12d, 0xc0(%rbx)
movl %r12d, %eax
cmpl %r12d, %ecx
jge 0x51e42
movl %r12d, 0xc4(%rbx)
jmp 0x51e45
movl %ecx, %r12d
movl %eax, 0xc8(%rbx)
imull %eax, %r12d
movl %r12d, 0xcc(%rbx)
xorl %r13d, %r13d
testl %r12d, %r12d
je 0x51e76
movslq %r12d, %rax
shlq $0x3, %rax
testl %r12d, %r12d
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
jmp 0x51e78
xorl %eax, %eax
movq %rax, 0xb8(%rbx)
testl %r12d, %r12d
cmovlel %r13d, %r12d
cmpq %r13, %r12
je 0x51e95
andq $0x0, (%rax,%r13,8)
incq %r13
jmp 0x51e86
movq %rbx, %rdi
movq %r15, %rsi
callq 0x517bc
jmp 0x51d60
movq %rax, %r15
leaq 0x18(%rsp), %rdi
callq 0x5216c
jmp 0x51ebe
jmp 0x51ebb
movq %rax, %r15
jmp 0x51ec6
movq %rax, %r15
movq %r14, %rdi
callq 0x5d61e
movq %rbx, %rdi
callq 0x5d61e
movq %r15, %rdi
callq 0xe810