// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::InterpolatedVertex(gaps::R3Grid const*, int, int, int, int, gaps::R3Mesh*, gaps::R3MeshVertex**, double)
static R3MeshVertex *
InterpolatedVertex(const R3Grid *grid, int ix0, int iy0, int iz0, int dim,
  R3Mesh *mesh, R3MeshVertex **vertices, RNScalar isolevel)
{
  // Check indices
  assert(dim < 3);
  int grid_index;
  int ix1 = (dim == 0) ? ix0+1 : ix0;
  int iy1 = (dim == 1) ? iy0+1 : iy0;
  int iz1 = (dim == 2) ? iz0+1 : iz0;
  assert(ix0 < grid->XResolution());
  assert(iy0 < grid->YResolution());
  assert(iz0 < grid->ZResolution());
  assert(ix1 < grid->XResolution());
  assert(iy1 < grid->YResolution());
  assert(iz1 < grid->ZResolution());
  grid->IndicesToIndex(ix0, iy0, iz0, grid_index);
  if (!vertices[3*grid_index+dim]) {
    RNScalar value0 = grid->GridValue(ix0, iy0, iz0);
    RNScalar value1 = grid->GridValue(ix1, iy1, iz1);
    RNScalar delta0 = fabs(value0 - isolevel);
    RNScalar delta1 = fabs(value1 - isolevel);
    RNScalar denom = delta0 + delta1;
    RNScalar t = (RNIsNotZero(denom)) ? delta0 / denom : 0.5;
    R3Point p0 = grid->WorldPosition(ix0, iy0, iz0);
    R3Point p1 = grid->WorldPosition(ix1, iy1, iz1);
    R3Point p = (1.0-t)*p0 + t*p1;
    vertices[3*grid_index+dim] = mesh->CreateVertex(p);
  }

  // Return vertex
  return vertices[3*grid_index+dim];
}

// Assembly:
subq $0xf8, %rsp
movq 0x100(%rsp), %rax
movq %rdi, 0xf0(%rsp)
movl %esi, 0xec(%rsp)
movl %edx, 0xe8(%rsp)
movl %ecx, 0xe4(%rsp)
movl %r8d, 0xe0(%rsp)
movq %r9, 0xd8(%rsp)
movsd %xmm0, 0xd0(%rsp)
cmpl $0x0, 0xe0(%rsp)
jne 0x6a07f
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x6a08a
movl 0xec(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0xc8(%rsp)
cmpl $0x1, 0xe0(%rsp)
jne 0x6a0af
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x6a0ba
movl 0xe8(%rsp), %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0xc4(%rsp)
cmpl $0x2, 0xe0(%rsp)
jne 0x6a0df
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0x6a0ea
movl 0xe4(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0xc0(%rsp)
movq 0xf0(%rsp), %rdi
movl 0xec(%rsp), %esi
movl 0xe8(%rsp), %edx
movl 0xe4(%rsp), %ecx
leaq 0xcc(%rsp), %r8
callq 0x6bd30
movq 0x100(%rsp), %rax
imull $0x3, 0xcc(%rsp), %ecx
addl 0xe0(%rsp), %ecx
movslq %ecx, %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x6a333
movq 0xf0(%rsp), %rdi
movl 0xec(%rsp), %esi
movl 0xe8(%rsp), %edx
movl 0xe4(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0xb8(%rsp)
movq 0xf0(%rsp), %rdi
movl 0xc8(%rsp), %esi
movl 0xc4(%rsp), %edx
movl 0xc0(%rsp), %ecx
callq 0x6bad0
movsd %xmm0, 0xb0(%rsp)
movsd 0xb8(%rsp), %xmm0
subsd 0xd0(%rsp), %xmm0
movaps 0xc90bd(%rip), %xmm1    # 0x133270
pand %xmm1, %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xb0(%rsp), %xmm0
subsd 0xd0(%rsp), %xmm0
movaps 0xc9097(%rip), %xmm1    # 0x133270
pand %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa8(%rsp), %xmm0
addsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
leaq 0x10e2cf(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x555a0
cmpl $0x0, %eax
je 0x6a238
movsd 0xa8(%rsp), %xmm0
divsd 0x98(%rsp), %xmm0
movsd %xmm0, (%rsp)
jmp 0x6a247
movsd 0xc8dd0(%rip), %xmm0    # 0x133010
movsd %xmm0, (%rsp)
jmp 0x6a247
movsd (%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0xf0(%rsp), %rsi
cvtsi2sdl 0xec(%rsp), %xmm0
cvtsi2sdl 0xe8(%rsp), %xmm1
cvtsi2sdl 0xe4(%rsp), %xmm2
leaq 0x78(%rsp), %rdi
callq 0x62cd0
movq 0xf0(%rsp), %rsi
cvtsi2sdl 0xc8(%rsp), %xmm0
cvtsi2sdl 0xc4(%rsp), %xmm1
cvtsi2sdl 0xc0(%rsp), %xmm2
leaq 0x60(%rsp), %rdi
callq 0x62cd0
movsd 0xc8d51(%rip), %xmm0    # 0x133008
subsd 0x90(%rsp), %xmm0
leaq 0x30(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x36b10
movsd 0x90(%rsp), %xmm0
leaq 0x18(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x36b10
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x6fa40
movq 0xd8(%rsp), %rdi
movq (%rdi), %rax
leaq 0x48(%rsp), %rsi
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq %rax, %rdx
movq 0x100(%rsp), %rax
imull $0x3, 0xcc(%rsp), %ecx
addl 0xe0(%rsp), %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x100(%rsp), %rax
imull $0x3, 0xcc(%rsp), %ecx
addl 0xe0(%rsp), %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
addq $0xf8, %rsp
retq
nopl (%rax)