// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelFeatureEvaluation.cpp
// Function: gaps::R3SurfelOverheadGridFeature::UpdateFeatureVector(gaps::R3SurfelObject*, gaps::R3SurfelFeatureVector&) const
int R3SurfelOverheadGridFeature::
UpdateFeatureVector(R3SurfelObject *object, R3SurfelFeatureVector& vector) const
{
  // Get/check scene
  R3SurfelScene *scene = Scene();
  int scene_index = SceneIndex();
  if (!scene) return 0;
  assert(scene == object->Scene());
  assert(scene_index >= 0);

  // Check if feature value is already up to date
  if (vector.Value(scene_index) != RN_UNKNOWN) return 1;

  // Check if file is open
  if (!fp) {
    RNFail("Can't evaluate overhead grid feature because grid file is not open.\n");
    vector.SetValue(SceneIndex(), 0);
    return 0;
  }

  // Get object centroid
  R3Point centroid = object->Centroid();

  // Compute grid coordinates
  R2Point grid_position = world_to_grid_matrix * R2Point(centroid.X(), centroid.Y());
  int ix = (int) (grid_position[0] + 0.5);
  if ((ix < 0) || (ix >= grid_resolution[0])) return 0;
  int iy = (int) (grid_position[1] + 0.5);
  if ((iy < 0) || (iy >= grid_resolution[1])) return 0;

  // Seek to correct position in file
  int grid_index = grid_resolution[0] * iy + ix;
  unsigned int file_offset = 2*sizeof(int) + 9*sizeof(RNScalar) + grid_index*sizeof(RNScalar);
  if (fseek(fp, file_offset, SEEK_SET)) {
    RNFail("Unable to seek to offset %d in grid file %s\n", file_offset, filename);
    vector.SetValue(SceneIndex(), 0);
    return 0;
  }

  // Read value
  RNScalar value;
  if (fread(&value, sizeof(RNScalar), 1, fp) != (unsigned int) 1) {
    RNFail("Unable to read value from grid file %s\n", filename);
    vector.SetValue(SceneIndex(), 0);
    return 0;
  }

  // Set vector values 
  vector.SetValue(SceneIndex(), value);

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpq $0x0, 0x8(%rdi)
je 0x161c6
movq %rdx, %rbx
movq %rdi, %r14
movslq 0x10(%rdi), %rax
movq 0x8(%rdx), %rcx
movsd (%rcx,%rax,8), %xmm0
ucomisd 0xb1fa6(%rip), %xmm0    # 0xc8160
jne 0x161be
jnp 0x161cd
pushq $0x1
popq %rbp
jmp 0x162b7
xorl %ebp, %ebp
jmp 0x162b7
cmpq $0x0, 0xa0(%r14)
je 0x16296
leaq 0x20(%rsp), %r15
movq %r15, %rdi
callq 0x16342
leaq 0x50(%r14), %r12
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movq %rsp, %r15
movq %r15, %rdi
callq 0x93a30
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x928a0
movsd (%r13), %xmm0
addsd 0xb1f15(%rip), %xmm0    # 0xc8138
cvttsd2si %xmm0, %eax
xorl %ebp, %ebp
testl %eax, %eax
js 0x162b7
movl 0x98(%r14), %ecx
cmpl %eax, %ecx
jle 0x162b7
movsd 0x18(%rsp), %xmm0
addsd 0xb1eee(%rip), %xmm0    # 0xc8138
cvttsd2si %xmm0, %edx
testl %edx, %edx
js 0x162b7
cmpl %edx, 0x9c(%r14)
jle 0x162b7
imull %edx, %ecx
addl %eax, %ecx
leal 0x50(,%rcx,8), %r15d
movq 0xa0(%r14), %rdi
xorl %ebp, %ebp
movq %r15, %rsi
xorl %edx, %edx
callq 0x13590
testl %eax, %eax
je 0x162c8
movq 0x48(%r14), %rdx
leaq 0xb1ff5(%rip), %rdi     # 0xc827f
movl %r15d, %esi
xorl %eax, %eax
callq 0x9ce26
jmp 0x162a6
leaq 0xb1f9d(%rip), %rdi     # 0xc823a
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x9ce26
movl 0x10(%r14), %esi
movq (%rbx), %rax
xorpd %xmm0, %xmm0
movq %rbx, %rdi
callq *0x18(%rax)
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa0(%r14), %rcx
movq %rsp, %rdi
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
callq 0x137d0
cmpq $0x1, %rax
jne 0x162fa
movl 0x10(%r14), %esi
movsd (%rsp), %xmm0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
jmp 0x161be
movq 0x48(%r14), %rsi
leaq 0xb1fa7(%rip), %rdi     # 0xc82ac
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x9ce26
jmp 0x162a6