// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::R3Grid(gaps::R3Box const&, double, int, int, int)
R3Grid::
R3Grid(const R3Box& bbox, RNLength spacing, int min_resolution, int max_resolution, int min_border)
{
  // Check for empty bounding box
  if (bbox.IsEmpty() || (RNIsZero(spacing))) { *this = R3Grid(); return; }

  // Compute inflated bbox (with room for border)
  R3Box inflated_bbox = bbox;
  if (min_border > 0) {
    inflated_bbox[0] -= spacing * min_border * R3ones_vector;
    inflated_bbox[1] += spacing * min_border * R3ones_vector;
  }

  // Compute inflated box (if would otherwise have zero volume)
  for (int i = 0; i < 3; i++) {
    if (inflated_bbox[0][i] == inflated_bbox[1][i]) {
      inflated_bbox[0][i] -= RN_EPSILON;
      inflated_bbox[1][i] += RN_EPSILON;
    }
  }
  
  // Enforce max resolution
  if (max_resolution > 0) {
    if (inflated_bbox.XLength() / spacing > max_resolution) spacing = inflated_bbox.XLength() / max_resolution;
    if (inflated_bbox.YLength() / spacing > max_resolution) spacing = inflated_bbox.YLength() / max_resolution;
    if (inflated_bbox.ZLength() / spacing > max_resolution) spacing = inflated_bbox.ZLength() / max_resolution;
  }

  // Enforce min resolution
  if (min_resolution > 0) {
    if (inflated_bbox.XLength() / spacing < min_resolution) spacing = inflated_bbox.XLength() / min_resolution;
    if (inflated_bbox.YLength() / spacing < min_resolution) spacing = inflated_bbox.YLength() / min_resolution;
    if (inflated_bbox.ZLength() / spacing < min_resolution) spacing = inflated_bbox.ZLength() / min_resolution;
  }

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

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

  // 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(inflated_bbox);
}

// Assembly:
subq $0x408, %rsp            # imm = 0x408
movq %rdi, 0x400(%rsp)
movq %rsi, 0x3f8(%rsp)
movsd %xmm0, 0x3f0(%rsp)
movl %edx, 0x3ec(%rsp)
movl %ecx, 0x3e8(%rsp)
movl %r8d, 0x3e4(%rsp)
movq 0x400(%rsp), %rdi
movq %rdi, 0x100(%rsp)
movq %rdi, %rax
movq %rax, 0xf8(%rsp)
callq 0x4d310
movq 0x100(%rsp), %rdi
addq $0x110, %rdi            # imm = 0x110
movq %rdi, 0x108(%rsp)
callq 0x4d310
jmp 0x55ea4
movq 0x3f8(%rsp), %rdi
callq 0x44d80
movl %eax, 0xf4(%rsp)
jmp 0x55eba
movl 0xf4(%rsp), %eax
cmpl $0x0, %eax
jne 0x55ef8
movsd 0x3f0(%rsp), %xmm0
leaq 0x12260a(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
movl %eax, 0xf0(%rsp)
jmp 0x55ee8
movl 0xf0(%rsp), %eax
cmpl $0x0, %eax
je 0x55f8e
leaq 0x180(%rsp), %rdi
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x55680
jmp 0x55f0d
movq 0xf8(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x566d0
jmp 0x55f24
leaq 0x180(%rsp), %rdi
callq 0x56960
jmp 0x56696
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3d8(%rsp)
movl %eax, 0x3d4(%rsp)
jmp 0x566ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3d8(%rsp)
movl %eax, 0x3d4(%rsp)
jmp 0x5669e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3d8(%rsp)
movl %eax, 0x3d4(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x56960
jmp 0x5669e
movq 0x3f8(%rsp), %rsi
leaq 0x148(%rsp), %rdi
callq 0x428c0
jmp 0x55fa5
cmpl $0x0, 0x3e4(%rsp)
jle 0x56096
movsd 0x3f0(%rsp), %xmm0
cvtsi2sdl 0x3e4(%rsp), %xmm1
mulsd %xmm1, %xmm0
leaq 0x5235e8(%rip), %rsi    # 0x5795b8
leaq 0x130(%rsp), %rdi
callq 0x13800
jmp 0x55fdf
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0x44f40
movq %rax, 0xe8(%rsp)
jmp 0x55ff8
movq 0xe8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x6f890
jmp 0x5600f
movsd 0x3f0(%rsp), %xmm0
cvtsi2sdl 0x3e4(%rsp), %xmm1
mulsd %xmm1, %xmm0
leaq 0x52358c(%rip), %rsi    # 0x5795b8
leaq 0x118(%rsp), %rdi
callq 0x13800
jmp 0x5603b
leaq 0x148(%rsp), %rdi
movl $0x1, %esi
callq 0x44f40
movq %rax, 0xe0(%rsp)
jmp 0x56057
movq 0xe0(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x6f290
jmp 0x5606e
jmp 0x56096
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x3d8(%rsp)
movl %eax, 0x3d4(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x36a80
jmp 0x5669e
movl $0x0, 0x114(%rsp)
cmpl $0x3, 0x114(%rsp)
jge 0x5623d
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0x44f40
movq %rax, 0xd8(%rsp)
jmp 0x560c8
movq 0xd8(%rsp), %rdi
movl 0x114(%rsp), %esi
callq 0x36be0
movq %rax, 0xd0(%rsp)
jmp 0x560e6
movq 0xd0(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0xc0(%rsp)
leaq 0x148(%rsp), %rdi
movl $0x1, %esi
callq 0x44f40
movq %rax, 0xc8(%rsp)
jmp 0x56117
movq 0xc8(%rsp), %rdi
movl 0x114(%rsp), %esi
callq 0x36be0
movq %rax, 0xb8(%rsp)
jmp 0x56135
movsd 0xc0(%rsp), %xmm0
movq 0xb8(%rsp), %rax
ucomisd (%rax), %xmm0
jne 0x56225
jp 0x56225
leaq 0x122383(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm0
movsd %xmm0, 0xa8(%rsp)
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0x44f40
movq %rax, 0xb0(%rsp)
jmp 0x56183
movq 0xb0(%rsp), %rdi
movl 0x114(%rsp), %esi
callq 0x36be0
movq %rax, 0xa0(%rsp)
jmp 0x561a1
movq 0xa0(%rsp), %rax
movsd 0xa8(%rsp), %xmm1
movsd (%rax), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rax)
leaq 0x12231b(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm0
movsd %xmm0, 0x90(%rsp)
leaq 0x148(%rsp), %rdi
movl $0x1, %esi
callq 0x44f40
movq %rax, 0x98(%rsp)
jmp 0x561ee
movq 0x98(%rsp), %rdi
movl 0x114(%rsp), %esi
callq 0x36be0
movq %rax, 0x88(%rsp)
jmp 0x5620c
movq 0x88(%rsp), %rax
movsd 0x90(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x56227
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x560a1
cmpl $0x0, 0x3e8(%rsp)
jle 0x5637f
leaq 0x148(%rsp), %rdi
callq 0x37850
movsd %xmm0, 0x80(%rsp)
jmp 0x56263
movsd 0x80(%rsp), %xmm0
divsd 0x3f0(%rsp), %xmm0
cvtsi2sdl 0x3e8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x562b5
leaq 0x148(%rsp), %rdi
callq 0x37850
movsd %xmm0, 0x78(%rsp)
jmp 0x56299
movsd 0x78(%rsp), %xmm0
cvtsi2sdl 0x3e8(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x3f0(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x378a0
movsd %xmm0, 0x70(%rsp)
jmp 0x562ca
movsd 0x70(%rsp), %xmm0
divsd 0x3f0(%rsp), %xmm0
cvtsi2sdl 0x3e8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x56319
leaq 0x148(%rsp), %rdi
callq 0x378a0
movsd %xmm0, 0x68(%rsp)
jmp 0x562fd
movsd 0x68(%rsp), %xmm0
cvtsi2sdl 0x3e8(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x3f0(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x378f0
movsd %xmm0, 0x60(%rsp)
jmp 0x5632e
movsd 0x60(%rsp), %xmm0
divsd 0x3f0(%rsp), %xmm0
cvtsi2sdl 0x3e8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5637d
leaq 0x148(%rsp), %rdi
callq 0x378f0
movsd %xmm0, 0x58(%rsp)
jmp 0x56361
movsd 0x58(%rsp), %xmm0
cvtsi2sdl 0x3e8(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x3f0(%rsp)
jmp 0x5637f
cmpl $0x0, 0x3ec(%rsp)
jle 0x564bb
leaq 0x148(%rsp), %rdi
callq 0x37850
movsd %xmm0, 0x50(%rsp)
jmp 0x563a2
movsd 0x50(%rsp), %xmm1
divsd 0x3f0(%rsp), %xmm1
cvtsi2sdl 0x3ec(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x563f1
leaq 0x148(%rsp), %rdi
callq 0x37850
movsd %xmm0, 0x48(%rsp)
jmp 0x563d5
movsd 0x48(%rsp), %xmm0
cvtsi2sdl 0x3ec(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x3f0(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x378a0
movsd %xmm0, 0x40(%rsp)
jmp 0x56406
movsd 0x40(%rsp), %xmm1
divsd 0x3f0(%rsp), %xmm1
cvtsi2sdl 0x3ec(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x56455
leaq 0x148(%rsp), %rdi
callq 0x378a0
movsd %xmm0, 0x38(%rsp)
jmp 0x56439
movsd 0x38(%rsp), %xmm0
cvtsi2sdl 0x3ec(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x3f0(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x378f0
movsd %xmm0, 0x30(%rsp)
jmp 0x5646a
movsd 0x30(%rsp), %xmm1
divsd 0x3f0(%rsp), %xmm1
cvtsi2sdl 0x3ec(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x564b9
leaq 0x148(%rsp), %rdi
callq 0x378f0
movsd %xmm0, 0x28(%rsp)
jmp 0x5649d
movsd 0x28(%rsp), %xmm0
cvtsi2sdl 0x3ec(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x3f0(%rsp)
jmp 0x564bb
leaq 0x148(%rsp), %rdi
callq 0x37850
movsd %xmm0, 0x20(%rsp)
jmp 0x564d0
movq 0xf8(%rsp), %rax
movsd 0x20(%rsp), %xmm0
movsd 0x3f0(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0xdcb1d(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x238(%rax)
leaq 0x148(%rsp), %rdi
callq 0x378a0
movsd %xmm0, 0x18(%rsp)
jmp 0x56516
movq 0xf8(%rsp), %rax
movsd 0x18(%rsp), %xmm0
movsd 0x3f0(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0xdcad7(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x23c(%rax)
leaq 0x148(%rsp), %rdi
callq 0x378f0
movsd %xmm0, 0x10(%rsp)
jmp 0x5655c
movq 0xf8(%rsp), %rax
movsd 0x10(%rsp), %xmm0
divsd 0x3f0(%rsp), %xmm0
movsd 0xdca95(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, 0x240(%rax)
movl 0x238(%rax), %ecx
movl %ecx, 0x244(%rax)
movl 0x244(%rax), %ecx
imull 0x23c(%rax), %ecx
movl %ecx, 0x248(%rax)
movl 0x248(%rax), %ecx
imull 0x240(%rax), %ecx
movl %ecx, 0x24c(%rax)
cmpl $0x0, 0x24c(%rax)
jne 0x565d9
movq 0xf8(%rsp), %rax
movq $0x0, 0x230(%rax)
jmp 0x5661e
movq 0xf8(%rsp), %rax
movslq 0x24c(%rax), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xf030
movq %rax, 0x8(%rsp)
jmp 0x5660a
movq 0xf8(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0x230(%rax)
movl $0x0, 0x110(%rsp)
movq 0xf8(%rsp), %rcx
movl 0x110(%rsp), %eax
cmpl 0x24c(%rcx), %eax
jge 0x56672
movq 0xf8(%rsp), %rax
movq 0x230(%rax), %rax
movslq 0x110(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x110(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x110(%rsp)
jmp 0x56629
movq 0xf8(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x559e0
jmp 0x56689
leaq 0x148(%rsp), %rdi
callq 0x36a80
addq $0x408, %rsp            # imm = 0x408
retq
movq 0x108(%rsp), %rdi
callq 0x4d430
movq 0x100(%rsp), %rdi
callq 0x4d430
movq 0x3d8(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)