// File: tomfunkhouser[P]gaps/pkgs/GSV/GSVScan.cpp
// Function: gaps::GSVScan::Mesh() const
GSVMesh *GSVScan::
Mesh(void) const
{
  // Get convenient variables (parameters)
  GSVScan *scan = (GSVScan *) this;
  if (NScanlines() == 0) return NULL;
  int scan_index = SegmentIndex();
  GSVSegment *segment = Segment();
  if (!segment) return NULL;
  int segment_index = segment->RunIndex();
  GSVRun *run = segment->Run();
  if (!run) return NULL;
  char run_name[1024];
  if (run->Name()) sprintf(run_name, "%s", run->Name());
  else sprintf(run_name, "Run%d", run->SceneIndex());
  GSVScene *scene = run->Scene();
  if (!scene) return NULL;

  // Initialize result
  GSVMesh *mesh = NULL;

  // Check if cached
  if (scene->CacheDataDirectoryName()) {
    // Construct mesh filename
    char mesh_name[4096];
    sprintf(mesh_name, "%s/laser_meshes/%s/%02d_%02d_Mesh.ply", 
      scene->CacheDataDirectoryName(), run_name, segment_index, scan_index);

    // Check if mesh file exists
    if (RNFileExists(mesh_name)) {
      // Allocate mesh
      mesh = new GSVMesh();
      if (!mesh) {
        RNFail("Unable to allocate mesh\n");
        return NULL;
      }
      
      // Read file 
      if (!mesh->ReadPlyFile(scan, mesh_name)) {
        RNFail("Unable to read mesh file %s\n", mesh_name);
        delete mesh;
        return NULL;
      }
    }
  }

  // If did not read from cache ...
  if (!mesh) {
    // Allocate mesh
    mesh = new GSVMesh();
    if (!mesh) {
      RNFail("Unable to allocate mesh\n");
      return NULL;
    }
      
    // Compute mesh 
    if (!mesh->LoadScan(scan)) {
      RNFail("Unable to create mesh\n");
      delete mesh;
      return NULL;
    }

    // Write mesh to file
    if (scene->CacheDataDirectoryName()) {
      if (scene->CreateCacheDataDirectory("laser_meshes", run)) {
        char mesh_name[4096];
        sprintf(mesh_name, "%s/laser_meshes/%s/%02d_%02d_Mesh.ply", 
          scene->CacheDataDirectoryName(), run_name, segment_index, scan_index);
        if (!mesh->WritePlyFile(scan, mesh_name, TRUE)) {
          RNFail("Unable to write mesh file %s\n", mesh_name);
        }
      }
    }
  }

  // Return mesh
  return mesh;
}

// Assembly:
subq $0x24a8, %rsp           # imm = 0x24A8
movq %rdi, 0x2498(%rsp)
movq 0x2498(%rsp), %rdi
movq %rdi, 0x48(%rsp)
movq %rdi, 0x2490(%rsp)
callq 0x3a0d0
cmpl $0x0, %eax
jne 0x5533f
movq $0x0, 0x24a0(%rsp)
jmp 0x55796
movq 0x48(%rsp), %rdi
callq 0x54480
movq 0x48(%rsp), %rdi
movl %eax, 0x248c(%rsp)
callq 0x542a0
movq %rax, 0x2480(%rsp)
cmpq $0x0, 0x2480(%rsp)
jne 0x5537e
movq $0x0, 0x24a0(%rsp)
jmp 0x55796
movq 0x2480(%rsp), %rdi
callq 0x4a640
movl %eax, 0x247c(%rsp)
movq 0x2480(%rsp), %rdi
callq 0x4f380
movq %rax, 0x2470(%rsp)
cmpq $0x0, 0x2470(%rsp)
jne 0x553c3
movq $0x0, 0x24a0(%rsp)
jmp 0x55796
movq 0x2470(%rsp), %rdi
callq 0x44310
cmpq $0x0, %rax
je 0x55408
leaq 0x2070(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x2470(%rsp), %rdi
callq 0x44310
movq 0x40(%rsp), %rdi
movq %rax, %rdx
leaq 0x22ae28(%rip), %rsi    # 0x280227
movb $0x0, %al
callq 0x2a280
jmp 0x55437
leaq 0x2070(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x2470(%rsp), %rdi
callq 0x443f0
movq 0x38(%rsp), %rdi
movl %eax, %edx
leaq 0x21171f(%rip), %rsi    # 0x266b4f
movb $0x0, %al
callq 0x2a280
movq 0x2470(%rsp), %rdi
callq 0x4a630
movq %rax, 0x2068(%rsp)
cmpq $0x0, 0x2068(%rsp)
jne 0x55468
movq $0x0, 0x24a0(%rsp)
jmp 0x55796
movq $0x0, 0x2060(%rsp)
movq 0x2068(%rsp), %rdi
callq 0x518e0
cmpq $0x0, %rax
je 0x555d3
leaq 0x1060(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x2068(%rsp), %rdi
callq 0x518e0
movq 0x30(%rsp), %rdi
movq %rax, %rdx
leaq 0x2070(%rsp), %rcx
movl 0x247c(%rsp), %r8d
movl 0x248c(%rsp), %r9d
leaq 0x211e2c(%rip), %rsi    # 0x2672f8
movb $0x0, %al
callq 0x2a280
leaq 0x1060(%rsp), %rdi
callq 0x1cadd0
cmpl $0x0, %eax
je 0x555d1
movl $0x148, %edi            # imm = 0x148
callq 0x292e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x28(%rsp)
callq 0x59ea0
jmp 0x55505
movq 0x28(%rsp), %rax
movq %rax, 0x2060(%rsp)
cmpq $0x0, 0x2060(%rsp)
jne 0x55564
leaq 0x211dfa(%rip), %rdi    # 0x26731e
movb $0x0, %al
callq 0x1cbff0
movq $0x0, 0x24a0(%rsp)
jmp 0x55796
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1058(%rsp)
movl %eax, 0x1054(%rsp)
movl $0x148, %esi            # imm = 0x148
callq 0x291f0
jmp 0x557a6
movq 0x2060(%rsp), %rdi
movq 0x2490(%rsp), %rsi
leaq 0x1060(%rsp), %rdx
movq (%rdi), %rax
callq *0x2e0(%rax)
cmpl $0x0, %eax
jne 0x555cf
leaq 0x1060(%rsp), %rsi
leaq 0x211d9e(%rip), %rdi    # 0x267337
movb $0x0, %al
callq 0x1cbff0
movq 0x2060(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x555be
movq 0x20(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0x24a0(%rsp)
jmp 0x55796
jmp 0x555d1
jmp 0x555d3
cmpq $0x0, 0x2060(%rsp)
jne 0x55786
movl $0x148, %edi            # imm = 0x148
callq 0x292e0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
callq 0x59ea0
jmp 0x555fe
movq 0x18(%rsp), %rax
movq %rax, 0x2060(%rsp)
cmpq $0x0, 0x2060(%rsp)
jne 0x5565d
leaq 0x211d01(%rip), %rdi    # 0x26731e
movb $0x0, %al
callq 0x1cbff0
movq $0x0, 0x24a0(%rsp)
jmp 0x55796
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1058(%rsp)
movl %eax, 0x1054(%rsp)
movl $0x148, %esi            # imm = 0x148
callq 0x291f0
jmp 0x557a6
movq 0x2060(%rsp), %rdi
movq 0x2490(%rsp), %rsi
movsd 0x210a0b(%rip), %xmm0   # 0x266080
movsd 0x210993(%rip), %xmm1   # 0x266010
callq 0x5a680
cmpl $0x0, %eax
jne 0x556c4
leaq 0x211cc6(%rip), %rdi    # 0x267354
movb $0x0, %al
callq 0x1cbff0
movq 0x2060(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x556b3
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0x24a0(%rsp)
jmp 0x55796
movq 0x2068(%rsp), %rdi
callq 0x518e0
cmpq $0x0, %rax
je 0x55784
movq 0x2068(%rsp), %rdi
movq 0x2470(%rsp), %rdx
leaq 0x211c79(%rip), %rsi    # 0x26736b
callq 0x3baa0
cmpl $0x0, %eax
je 0x55782
leaq 0x50(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x2068(%rsp), %rdi
callq 0x518e0
movq 0x8(%rsp), %rdi
movq %rax, %rdx
leaq 0x2070(%rsp), %rcx
movl 0x247c(%rsp), %r8d
movl 0x248c(%rsp), %r9d
leaq 0x211bba(%rip), %rsi    # 0x2672f8
movb $0x0, %al
callq 0x2a280
movq 0x2060(%rsp), %rdi
movq 0x2490(%rsp), %rsi
leaq 0x50(%rsp), %rdx
movq (%rdi), %rax
movl $0x1, %ecx
callq *0x2e8(%rax)
cmpl $0x0, %eax
jne 0x55780
leaq 0x50(%rsp), %rsi
leaq 0x211bff(%rip), %rdi    # 0x267378
movb $0x0, %al
callq 0x1cbff0
jmp 0x55782
jmp 0x55784
jmp 0x55786
movq 0x2060(%rsp), %rax
movq %rax, 0x24a0(%rsp)
movq 0x24a0(%rsp), %rax
addq $0x24a8, %rsp           # imm = 0x24A8
retq
movq 0x1058(%rsp), %rdi
callq 0x29c10
nopw %cs:(%rax,%rax)