// File: tomfunkhouser[P]gaps/apps/msh2prp/msh2prp.cpp
// Function: CreateGrid(gaps::R3Mesh*)
static R3Grid *
CreateGrid(R3Mesh *mesh)
{
  // Determine grid resolution
  int max_resolution = 256;
  R3Box bbox = mesh->BBox();
  RNScalar spacing = bbox.LongestAxisLength() / max_resolution;
  if (spacing == 0) return NULL;
  int xres = (int) (bbox.XLength() / spacing); if (xres == 0) xres = 1;
  int yres = (int) (bbox.YLength() / spacing); if (yres == 0) yres = 1;
  int zres = (int) (bbox.ZLength() / spacing); if (zres == 0) zres = 1;

  // Create grid
  R3Grid *grid = new R3Grid(xres, yres, zres, bbox);
  if (!grid) {
    RNFail("Unable to allocate grid\n");
    exit(-1);
  }

  // Rasterize each triangle into grid
  for (int i = 0; i < mesh->NFaces(); i++) {
    R3MeshFace *face = mesh->Face(i);
    const R3Point& p0 = mesh->VertexPosition(mesh->VertexOnFace(face, 0));
    const R3Point& p1 = mesh->VertexPosition(mesh->VertexOnFace(face, 1));
    const R3Point& p2 = mesh->VertexPosition(mesh->VertexOnFace(face, 2));
    grid->RasterizeWorldTriangle(p0, p1, p2, 1.0);
  }

  // Make sure every pixel is set only once
  grid->Threshold(0.5, 0, 1);

  // Return grid
  return grid;
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movl $0x100, 0xf4(%rsp) # imm = 0x100
movq 0xf8(%rsp), %rdi
callq 0x1fad0
movq %rax, %rsi
leaq 0xb8(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x5c060
movq 0x50(%rsp), %rdi
callq 0x5d450
movsd %xmm0, 0x58(%rsp)
jmp 0x1cafe
movsd 0x58(%rsp), %xmm0
cvtsi2sdl 0xf4(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xb0(%rsp)
movsd 0xb0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1cb63
jp 0x1cb63
movq $0x0, 0x100(%rsp)
movl $0x1, 0xa0(%rsp)
jmp 0x1ce2a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
jmp 0x1ce47
leaq 0xb8(%rsp), %rdi
callq 0x1faf0
movsd %xmm0, 0x48(%rsp)
jmp 0x1cb78
movsd 0x48(%rsp), %xmm0
divsd 0xb0(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x9c(%rsp)
cmpl $0x0, 0x9c(%rsp)
jne 0x1cba7
movl $0x1, 0x9c(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x1fb10
movsd %xmm0, 0x40(%rsp)
jmp 0x1cbbc
movsd 0x40(%rsp), %xmm0
divsd 0xb0(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x98(%rsp)
cmpl $0x0, 0x98(%rsp)
jne 0x1cbeb
movl $0x1, 0x98(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x1fb30
movsd %xmm0, 0x38(%rsp)
jmp 0x1cc00
movsd 0x38(%rsp), %xmm0
divsd 0xb0(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x94(%rsp)
cmpl $0x0, 0x94(%rsp)
jne 0x1cc2f
movl $0x1, 0x94(%rsp)
movl $0x250, %edi            # imm = 0x250
callq 0x11680
movq %rax, 0x30(%rsp)
jmp 0x1cc40
movq 0x30(%rsp), %rdi
movl 0x9c(%rsp), %esi
movl 0x98(%rsp), %edx
movl 0x94(%rsp), %ecx
leaq 0xb8(%rsp), %r8
callq 0x6ef30
jmp 0x1cc69
movq 0x30(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
jne 0x1ccc3
leaq 0x1577bb(%rip), %rdi    # 0x174443
xorl %eax, %eax
callq 0xedb00
jmp 0x1cc91
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x11820
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa8(%rsp)
movl %eax, 0xa4(%rsp)
movl $0x250, %esi            # imm = 0x250
callq 0x11690
jmp 0x1ce47
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x1f2a0
movl %eax, 0x2c(%rsp)
jmp 0x1ccec
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x1cded
movq 0xf8(%rsp), %rdi
movl 0x84(%rsp), %esi
callq 0x1f640
movq %rax, 0x20(%rsp)
jmp 0x1cd17
movq 0x20(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0xf8(%rsp), %rdi
movq %rdi, (%rsp)
movq 0x78(%rsp), %rsi
xorl %edx, %edx
movl %edx, 0x1c(%rsp)
callq 0x1f670
movq (%rsp), %rdi
movq %rax, %rsi
callq 0x1f4b0
movq %rax, 0x70(%rsp)
movq 0xf8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movq 0x78(%rsp), %rsi
movl $0x1, %edx
callq 0x1f670
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x1f4b0
movq %rax, 0x68(%rsp)
movq 0xf8(%rsp), %rdi
movq %rdi, 0x10(%rsp)
movq 0x78(%rsp), %rsi
movl $0x2, %edx
callq 0x1f670
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x1f4b0
movl 0x1c(%rsp), %r8d
movq %rax, 0x60(%rsp)
movq 0x88(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x60(%rsp), %rcx
movsd 0x15724a(%rip), %xmm0   # 0x174018
callq 0x1fb50
jmp 0x1cdd5
jmp 0x1cdd7
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x1ccce
movq 0x88(%rsp), %rdi
movsd 0x157223(%rip), %xmm0   # 0x174020
xorps %xmm1, %xmm1
movsd 0x157210(%rip), %xmm2   # 0x174018
callq 0x73030
jmp 0x1ce0f
movq 0x88(%rsp), %rax
movq %rax, 0x100(%rsp)
movl $0x1, 0xa0(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x1fbf0
movq 0x100(%rsp), %rax
addq $0x108, %rsp            # imm = 0x108
retq
leaq 0xb8(%rsp), %rdi
callq 0x1fbf0
movq 0xa8(%rsp), %rdi
callq 0x11bb0
nopw %cs:(%rax,%rax)