// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %edx, 0x10(%rsp)
movq %rsi, %r14
movsd %xmm0, 0x20(%rsp)
movq %rdi, %r15
xorl %ebx, %ebx
leaq (%rsp,%rbx), %rdi
addq $0x70, %rdi
callq 0x37b4a
addq $0x18, %rbx
cmpq $0xc0, %rbx
jne 0x35cc5
addl $-0xf, 0x10(%rsp)
xorl %ebp, %ebp
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
xorl %ecx, %ecx
movl 0x240(%r15), %eax
decl %eax
cmpl %eax, %ecx
jge 0x35f60
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0xf8(%rsp)
movsd %xmm0, 0xe0(%rsp)
movsd %xmm0, 0x98(%rsp)
movsd %xmm0, 0x80(%rsp)
movq %rcx, 0x28(%rsp)
leal 0x1(%rcx), %eax
movl %eax, 0x14(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x128(%rsp)
movsd %xmm0, 0x110(%rsp)
movsd %xmm0, 0xc8(%rsp)
movsd %xmm0, 0xb0(%rsp)
xorl %ebx, %ebx
pushq $0x1
popq %r12
movl 0x23c(%r15), %edx
decl %edx
cmpl %edx, %ebx
jge 0x35f4c
movl %edx, 0x18(%rsp)
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd %xmm0, 0xa8(%rsp)
movsd %xmm0, 0x90(%rsp)
movsd %xmm0, 0x78(%rsp)
leal 0x1(%rbx), %eax
movl %eax, 0x1c(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x120(%rsp)
movsd %xmm0, 0x108(%rsp)
movsd %xmm0, 0xf0(%rsp)
movsd %xmm0, 0xd8(%rsp)
xorl %eax, %eax
movl 0x238(%r15), %ecx
decl %ecx
cmpl %ecx, %eax
jge 0x35f36
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x118(%rsp)
movsd %xmm0, 0xd0(%rsp)
movsd %xmm0, 0xb8(%rsp)
movsd %xmm0, 0x70(%rsp)
leal 0x1(%rax), %r13d
cvtsi2sd %r13d, %xmm0
movsd %xmm0, 0x100(%rsp)
movsd %xmm0, 0xe8(%rsp)
movsd %xmm0, 0xa0(%rsp)
movsd %xmm0, 0x88(%rsp)
cmpl 0x10(%rsp), %ebp
jge 0x35f44
movq 0x230(%r15), %rcx
movl 0x248(%r15), %esi
movl 0x244(%r15), %edx
movq 0x28(%rsp), %rdi
imull %esi, %edi
movl %ebx, %r8d
imull %edx, %r8d
leal (%rdi,%r8), %r9d
leal (%rax,%r9), %r10d
movslq %r10d, %r10
movsd (%rcx,%r10,8), %xmm0
movsd %xmm0, 0x30(%rsp)
leal 0x1(%rax,%r9), %r9d
movslq %r9d, %r9
movsd (%rcx,%r9,8), %xmm0
movsd %xmm0, 0x38(%rsp)
imull 0xc(%rsp), %esi
addl %esi, %r8d
leal (%rax,%r8), %r9d
addl %eax, %r8d
incl %r8d
movslq %r8d, %r8
movsd (%rcx,%r8,8), %xmm0
movsd %xmm0, 0x40(%rsp)
movslq %r9d, %r8
movsd (%rcx,%r8,8), %xmm0
movsd %xmm0, 0x48(%rsp)
imull %r12d, %edx
addl %edx, %edi
leal (%rax,%rdi), %r8d
movslq %r8d, %r8
movsd (%rcx,%r8,8), %xmm0
movsd %xmm0, 0x50(%rsp)
leal 0x1(%rax,%rdi), %edi
movslq %edi, %rdi
movsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, 0x58(%rsp)
addl %esi, %edx
movl %eax, %esi
addl %edx, %esi
addl %edx, %eax
incl %eax
cltq
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x60(%rsp)
movslq %esi, %rax
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x30(%rsp), %rsi
movsd 0x20(%rsp), %xmm0
movq %r14, %rdx
callq 0x31225
cltq
imulq $0x18, %rax, %rcx
addq %rcx, %r14
addl %ebp, %eax
movl %eax, %ebp
movl %r13d, %eax
jmp 0x35dcd
incl %r12d
movl 0x1c(%rsp), %eax
movl %eax, %ebx
jmp 0x35d62
movl %ebp, 0x8(%rsp)
movl 0x18(%rsp), %edx
incl 0xc(%rsp)
movl 0x14(%rsp), %eax
movl %eax, %ecx
cmpl %edx, %ebx
jge 0x35cef
jmp 0x35f64
movl %ebp, 0x8(%rsp)
movl 0x8(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq