// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::GenerateIsoSurface(double, gaps::R3Point*, int) const
int R3Grid::
GenerateIsoSurface(RNScalar isolevel, R3Point *points, int max_points) const
{
  // Initialize pointer into array of points
  R3Point *pointsp = points;

  // Fill array of points with triplets forming triangles of isosurface
  int npoints = 0;
  R3Point corner_points[8];
  RNScalar corner_levels[8];
  for (int k = 0; k < ZResolution()-1; k++) {
    corner_points[0][2] = corner_points[1][2] = corner_points[4][2] = corner_points[5][2] = k;
    corner_points[2][2] = corner_points[3][2] = corner_points[6][2] = corner_points[7][2] = k+1;
    for (int j = 0; j < YResolution()-1; j++) {
      corner_points[0][1] = corner_points[1][1] = corner_points[2][1] = corner_points[3][1] = j;
      corner_points[4][1] = corner_points[5][1] = corner_points[6][1] = corner_points[7][1] = j+1;
      for (int i = 0; i < XResolution()-1; i++) {
        corner_points[0][0] = corner_points[3][0] = corner_points[4][0] = corner_points[7][0] = i;
        corner_points[1][0] = corner_points[2][0] = corner_points[5][0] = corner_points[6][0] = i+1;

        // Check if points array is full (five tris is max for any cell)
        if (npoints >= max_points - 3*5) return npoints;

        // Fill in grid values
        corner_levels[0] = GridValue(i,j,k);
        corner_levels[1] = GridValue(i+1,j,k);
        corner_levels[2] = GridValue(i+1,j,k+1);
        corner_levels[3] = GridValue(i,j,k+1);
        corner_levels[4] = GridValue(i,j+1,k);
        corner_levels[5] = GridValue(i+1,j+1,k);
        corner_levels[6] = GridValue(i+1,j+1,k+1);
        corner_levels[7] = GridValue(i,j+1,k+1);

        // Create triangles
        int n = MarchingCubes(corner_points, corner_levels, isolevel, pointsp);
        pointsp += n;
        npoints += n;
      }
    }
  }

  // Just checking
  assert((npoints % 3) == 0);
  assert(npoints < max_points);

  // Return number of points
  return npoints;
}

// Assembly:
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rdi, 0x1a8(%rsp)
movsd %xmm0, 0x1a0(%rsp)
movq %rsi, 0x198(%rsp)
movl %edx, 0x194(%rsp)
movq 0x1a8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x188(%rsp)
movl $0x0, 0x184(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, %rcx
addq $0xc0, %rcx
movq %rcx, 0x60(%rsp)
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x6eb40
movq 0x50(%rsp), %rax
movq 0x60(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x68(%rsp)
jne 0x6a3cb
movl $0x0, 0x7c(%rsp)
movq 0x58(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, 0x4c(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0x4c(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x6a98a
cvtsi2sdl 0x7c(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0xc0(%rsp), %rdi
addq $0x78, %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x60, %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x18, %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x38(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0xc0(%rsp), %rdi
addq $0xa8, %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x90, %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x48, %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x30, %rdi
movl $0x2, %esi
callq 0x36be0
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x0, 0x78(%rsp)
movq 0x58(%rsp), %rdi
movl 0x78(%rsp), %eax
movl %eax, 0x34(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0x34(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x6a978
cvtsi2sdl 0x78(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xc0(%rsp), %rdi
addq $0x48, %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x30, %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x18, %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x78(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0xc0(%rsp), %rdi
addq $0xa8, %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x90, %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x78, %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x60, %rdi
movl $0x1, %esi
callq 0x36be0
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movl $0x0, 0x74(%rsp)
movq 0x58(%rsp), %rdi
movl 0x74(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x6a966
cvtsi2sdl 0x74(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xc0(%rsp), %rdi
addq $0xa8, %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x60, %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x48, %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x74(%rsp), %eax
addl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xc0(%rsp), %rdi
addq $0x90, %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x78, %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x30, %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0xc0(%rsp), %rdi
addq $0x18, %rdi
xorl %esi, %esi
callq 0x36be0
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movl 0x184(%rsp), %eax
movl 0x194(%rsp), %ecx
subl $0xf, %ecx
cmpl %ecx, %eax
jl 0x6a7e0
movl 0x184(%rsp), %eax
movl %eax, 0x1b4(%rsp)
jmp 0x6a998
movq 0x58(%rsp), %rdi
movl 0x74(%rsp), %esi
movl 0x78(%rsp), %edx
movl 0x7c(%rsp), %ecx
callq 0x6bad0
movq 0x58(%rsp), %rdi
movsd %xmm0, 0x80(%rsp)
movl 0x74(%rsp), %esi
addl $0x1, %esi
movl 0x78(%rsp), %edx
movl 0x7c(%rsp), %ecx
callq 0x6bad0
movq 0x58(%rsp), %rdi
movsd %xmm0, 0x88(%rsp)
movl 0x74(%rsp), %esi
addl $0x1, %esi
movl 0x78(%rsp), %edx
movl 0x7c(%rsp), %ecx
addl $0x1, %ecx
callq 0x6bad0
movq 0x58(%rsp), %rdi
movsd %xmm0, 0x90(%rsp)
movl 0x74(%rsp), %esi
movl 0x78(%rsp), %edx
movl 0x7c(%rsp), %ecx
addl $0x1, %ecx
callq 0x6bad0
movq 0x58(%rsp), %rdi
movsd %xmm0, 0x98(%rsp)
movl 0x74(%rsp), %esi
movl 0x78(%rsp), %edx
addl $0x1, %edx
movl 0x7c(%rsp), %ecx
callq 0x6bad0
movq 0x58(%rsp), %rdi
movsd %xmm0, 0xa0(%rsp)
movl 0x74(%rsp), %esi
addl $0x1, %esi
movl 0x78(%rsp), %edx
addl $0x1, %edx
movl 0x7c(%rsp), %ecx
callq 0x6bad0
movq 0x58(%rsp), %rdi
movsd %xmm0, 0xa8(%rsp)
movl 0x74(%rsp), %esi
addl $0x1, %esi
movl 0x78(%rsp), %edx
addl $0x1, %edx
movl 0x7c(%rsp), %ecx
addl $0x1, %ecx
callq 0x6bad0
movq 0x58(%rsp), %rdi
movsd %xmm0, 0xb0(%rsp)
movl 0x74(%rsp), %esi
movl 0x78(%rsp), %edx
addl $0x1, %edx
movl 0x7c(%rsp), %ecx
addl $0x1, %ecx
callq 0x6bad0
movsd %xmm0, 0xb8(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movsd 0x1a0(%rsp), %xmm0
movq 0x188(%rsp), %rdx
callq 0x61b60
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %ecx
movq 0x188(%rsp), %rax
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x188(%rsp)
movl 0x70(%rsp), %eax
addl 0x184(%rsp), %eax
movl %eax, 0x184(%rsp)
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x6a68e
jmp 0x6a968
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x6a544
jmp 0x6a97a
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x6a3fa
movl 0x184(%rsp), %eax
movl %eax, 0x1b4(%rsp)
movl 0x1b4(%rsp), %eax
addq $0x1b8, %rsp            # imm = 0x1B8
retq
nopw (%rax,%rax)