// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelFeatureEvaluation.cpp
// Function: gaps::R3SurfelOverheadGridFeature::R3SurfelOverheadGridFeature(char const*, char const*, double, double, double)
R3SurfelOverheadGridFeature::
R3SurfelOverheadGridFeature(const char *filename, const char *featurename, RNScalar minimum, RNScalar maximum, RNScalar weight)
  : R3SurfelFeature(featurename, minimum, maximum, weight),
    filename(NULL),
    world_to_grid_matrix(R3null_matrix),
    fp(NULL)
{
  // Initialize resolution
  grid_resolution[0] = 0;
  grid_resolution[1] = 0;

  // Open file
  fp = fopen(filename, "rb");
  if (!fp) {
    RNFail("Unable to open overhead grid file %s\n", filename);
    return;
  }

  // Read grid resolution from file
  if (fread(&grid_resolution, sizeof(int), 2, fp) != 2) {
    RNFail("Unable to read grid file %s", filename);
    fclose(fp);
    grid_resolution[0] = 0;
    grid_resolution[1] = 0;
    fp = NULL;
    return;
  }

  // Read world_to_grid transformation from file
  RNScalar m[9];
  if (fread(m, sizeof(RNScalar), 9, fp) != 9) {
    RNFail("Invalid format for grid file %s", filename);
    fclose(fp);
    grid_resolution[0] = 0;
    grid_resolution[1] = 0;
    fp = NULL;
    return;
  }

  // Create world_to_grid transformation matrix
  world_to_grid_matrix = R3Matrix(m);

  // Copy filename
  this->filename = RNStrdup(filename);
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq %rdx, 0xe0(%rsp)
movsd %xmm0, 0xd8(%rsp)
movsd %xmm1, 0xd0(%rsp)
movsd %xmm2, 0xc8(%rsp)
movq 0xf0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movq 0xe0(%rsp), %rsi
movsd 0xd8(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm1
movsd 0xc8(%rsp), %xmm2
callq 0x6bba0
movq 0x20(%rsp), %rdi
leaq 0x1d5ccd(%rip), %rax    # 0x1edca8
movq %rax, (%rdi)
movq $0x0, 0x48(%rdi)
addq $0x50, %rdi
leaq 0x5e6b6f(%rip), %rsi    # 0x5feb60
callq 0x12ff50
jmp 0x17ff8
movq 0x20(%rsp), %rax
movq $0x0, 0xa0(%rax)
movl $0x0, 0x98(%rax)
movl $0x0, 0x9c(%rax)
movq 0xe8(%rsp), %rdi
leaq 0x18c1ca(%rip), %rsi    # 0x1a41f5
callq 0x13690
movq %rax, 0x18(%rsp)
jmp 0x18037
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, 0xa0(%rax)
cmpq $0x0, 0xa0(%rax)
jne 0x18092
movq 0xe8(%rsp), %rsi
leaq 0x18c197(%rip), %rdi    # 0x1a41f8
xorl %eax, %eax
callq 0x1464b0
jmp 0x1806a
jmp 0x181e6
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
callq 0x6bd10
jmp 0x181ee
movq 0x20(%rsp), %rax
movq %rax, %rdi
addq $0x98, %rdi
movq 0xa0(%rax), %rcx
movl $0x4, %esi
movl $0x2, %edx
callq 0x13860
movq %rax, 0x10(%rsp)
jmp 0x180be
movq 0x10(%rsp), %rax
cmpq $0x2, %rax
je 0x1811d
movq 0xe8(%rsp), %rsi
leaq 0x18c146(%rip), %rdi    # 0x1a421e
xorl %eax, %eax
callq 0x1464b0
jmp 0x180e1
movq 0x20(%rsp), %rax
movq 0xa0(%rax), %rdi
callq 0x13560
jmp 0x180f4
movq 0x20(%rsp), %rax
movl $0x0, 0x98(%rax)
movl $0x0, 0x9c(%rax)
movq $0x0, 0xa0(%rax)
jmp 0x181e6
movq 0x20(%rsp), %rax
movq 0xa0(%rax), %rcx
leaq 0x70(%rsp), %rdi
movl $0x8, %esi
movl $0x9, %edx
callq 0x13860
movq %rax, 0x8(%rsp)
jmp 0x18144
movq 0x8(%rsp), %rax
cmpq $0x9, %rax
je 0x181a0
movq 0xe8(%rsp), %rsi
leaq 0x18c0dc(%rip), %rdi    # 0x1a423a
xorl %eax, %eax
callq 0x1464b0
jmp 0x18167
movq 0x20(%rsp), %rax
movq 0xa0(%rax), %rdi
callq 0x13560
jmp 0x1817a
movq 0x20(%rsp), %rax
movl $0x0, 0x98(%rax)
movl $0x0, 0x9c(%rax)
movq $0x0, 0xa0(%rax)
jmp 0x181e6
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x1300a0
jmp 0x181b1
movq 0x20(%rsp), %rdi
addq $0x50, %rdi
leaq 0x28(%rsp), %rsi
callq 0x130ee0
jmp 0x181c6
movq 0xe8(%rsp), %rdi
callq 0x1461b0
movq %rax, (%rsp)
jmp 0x181d9
movq 0x20(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, 0x48(%rax)
addq $0xf8, %rsp
retq
movq 0xc0(%rsp), %rdi
callq 0x13970
nopl (%rax,%rax)