// File: 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:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq 0xa8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x18620
movq 0x30(%rsp), %rdi
movq %rax, 0x90(%rsp)
callq 0x18630
movl %eax, 0x8c(%rsp)
cmpq $0x0, 0x90(%rsp)
jne 0x18325
movl $0x0, 0xb4(%rsp)
jmp 0x185db
movq 0x98(%rsp), %rdi
movl 0x8c(%rsp), %esi
callq 0x18640
movsd 0x18be3f(%rip), %xmm1   # 0x1a4180
ucomisd %xmm1, %xmm0
jne 0x1834b
jp 0x1834b
jmp 0x1835b
movl $0x1, 0xb4(%rsp)
jmp 0x185db
movq 0x30(%rsp), %rax
cmpq $0x0, 0xa0(%rax)
jne 0x183af
leaq 0x18bee9(%rip), %rdi    # 0x1a425a
movb $0x0, %al
callq 0x1464b0
movq 0x30(%rsp), %rdi
movq 0x98(%rsp), %rax
movq %rax, 0x28(%rsp)
callq 0x18630
movq 0x28(%rsp), %rdi
movl %eax, %esi
movq (%rdi), %rax
xorps %xmm0, %xmm0
callq *0x18(%rax)
movl $0x0, 0xb4(%rsp)
jmp 0x185db
movq 0xa0(%rsp), %rsi
leaq 0x70(%rsp), %rdi
callq 0x187e0
movq 0x30(%rsp), %rax
addq $0x50, %rax
movq %rax, 0x20(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x187b0
movsd %xmm0, 0x18(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x187c0
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
leaq 0x50(%rsp), %rdi
callq 0x133960
movq 0x20(%rsp), %rsi
leaq 0x60(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x131720
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x18820
movsd 0x18bd34(%rip), %xmm0   # 0x1a4158
addsd (%rax), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x4c(%rsp)
jl 0x18448
movq 0x30(%rsp), %rcx
movl 0x4c(%rsp), %eax
cmpl 0x98(%rcx), %eax
jl 0x18458
movl $0x0, 0xb4(%rsp)
jmp 0x185db
leaq 0x60(%rsp), %rdi
movl $0x1, %esi
callq 0x18820
movsd 0x18bce9(%rip), %xmm0   # 0x1a4158
addsd (%rax), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x48(%rsp)
cmpl $0x0, 0x48(%rsp)
jl 0x18493
movq 0x30(%rsp), %rcx
movl 0x48(%rsp), %eax
cmpl 0x9c(%rcx), %eax
jl 0x184a3
movl $0x0, 0xb4(%rsp)
jmp 0x185db
movq 0x30(%rsp), %rax
movl 0x98(%rax), %ecx
imull 0x48(%rsp), %ecx
addl 0x4c(%rsp), %ecx
movl %ecx, 0x44(%rsp)
movslq 0x44(%rsp), %rcx
shlq $0x3, %rcx
addq $0x50, %rcx
movl %ecx, 0x40(%rsp)
movq 0xa0(%rax), %rdi
movl 0x40(%rsp), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x13640
cmpl $0x0, %eax
je 0x18537
movq 0x30(%rsp), %rax
movl 0x40(%rsp), %esi
movq 0x48(%rax), %rdx
leaq 0x18bda6(%rip), %rdi    # 0x1a429f
movb $0x0, %al
callq 0x1464b0
movq 0x30(%rsp), %rdi
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
callq 0x18630
movq 0x10(%rsp), %rdi
movl %eax, %esi
movq (%rdi), %rax
xorps %xmm0, %xmm0
callq *0x18(%rax)
movl $0x0, 0xb4(%rsp)
jmp 0x185db
movq 0x30(%rsp), %rax
movq 0xa0(%rax), %rcx
leaq 0x38(%rsp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x13860
cmpq $0x1, %rax
je 0x185a8
movq 0x30(%rsp), %rax
movq 0x48(%rax), %rsi
leaq 0x18bd5f(%rip), %rdi    # 0x1a42cc
movb $0x0, %al
callq 0x1464b0
movq 0x30(%rsp), %rdi
movq 0x98(%rsp), %rax
movq %rax, 0x8(%rsp)
callq 0x18630
movq 0x8(%rsp), %rdi
movl %eax, %esi
movq (%rdi), %rax
xorps %xmm0, %xmm0
callq *0x18(%rax)
movl $0x0, 0xb4(%rsp)
jmp 0x185db
movq 0x30(%rsp), %rdi
movq 0x98(%rsp), %rax
movq %rax, (%rsp)
callq 0x18630
movq (%rsp), %rdi
movl %eax, %esi
movsd 0x38(%rsp), %xmm0
movq (%rdi), %rax
callq *0x18(%rax)
movl $0x1, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nopw (%rax,%rax)