// File: /workspace/github_cpp_makefile_star2_O1/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 0x16b32
movq %rdx, %rbx
movq %rdi, %r14
movslq 0x10(%rdi), %rax
movq 0x8(%rdx), %rcx
movsd (%rcx,%rax,8), %xmm0
movl $0x1, %ebp
ucomisd 0xc7717(%rip), %xmm0    # 0xde168
jne 0x16b57
jp 0x16b57
cmpq $0x0, 0xa0(%r14)
je 0x16b36
movq %rsi, %rdi
callq 0x170be
movq (%rax), %rcx
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq *0x68(%rcx)
leaq 0x50(%r14), %r12
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movq %rsp, %r15
movq %r15, %rdi
callq 0xa3776
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xa2568
movsd (%r13), %xmm0
addsd 0xc7681(%rip), %xmm0    # 0xde140
cvttsd2si %xmm0, %eax
xorl %ebp, %ebp
testl %eax, %eax
js 0x16b57
movl 0x98(%r14), %ecx
cmpl %eax, %ecx
jle 0x16b57
movsd 0x18(%rsp), %xmm0
addsd 0xc765a(%rip), %xmm0    # 0xde140
cvttsd2si %xmm0, %edx
testl %edx, %edx
js 0x16b57
cmpl %edx, 0x9c(%r14)
jle 0x16b57
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 0x13570
testl %eax, %eax
je 0x16b68
movq 0x48(%r14), %rdx
leaq 0xc7761(%rip), %rdi     # 0xde287
movl %r15d, %esi
xorl %eax, %eax
callq 0xaddb2
jmp 0x16b46
xorl %ebp, %ebp
jmp 0x16b57
leaq 0xc7705(%rip), %rdi     # 0xde242
xorl %ebp, %ebp
xorl %eax, %eax
callq 0xaddb2
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
movl $0x8, %esi
movl $0x1, %edx
callq 0x13790
cmpq $0x1, %rax
jne 0x16ba0
movl 0x10(%r14), %esi
movsd (%rsp), %xmm0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x18(%rax)
movl $0x1, %ebp
jmp 0x16b57
movq 0x48(%r14), %rsi
leaq 0xc7709(%rip), %rdi     # 0xde2b4
xorl %ebp, %ebp
xorl %eax, %eax
callq 0xaddb2
jmp 0x16b46
nop