// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1408, %rsp           # imm = 0x1408
cmpl $0x0, 0x34(%rdi)
je 0x40157
movq %rdi, %r14
callq 0x3f79a
movl %eax, %ebp
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x40157
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x40157
movq 0x10(%rax), %r12
testq %r12, %r12
je 0x40157
movl 0x18(%rax), %r15d
movq 0x48(%r12), %rsi
testq %rsi, %rsi
je 0x3ffe8
movq %rsp, %rdi
callq 0x27870
jmp 0x3fffe
movl 0x10(%r12), %edx
leaq 0x10ca59(%rip), %rsi    # 0x14ca4d
movq %rsp, %rdi
xorl %eax, %eax
callq 0x27140
movq 0x8(%r12), %r13
testq %r13, %r13
je 0x40157
movq 0x28(%r13), %rdx
testq %rdx, %rdx
je 0x40092
leaq 0x10d1d0(%rip), %rsi    # 0x14d1ec
leaq 0x400(%rsp), %rbx
movq %rsp, %rcx
movq %rbx, %rdi
movl %r15d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x27140
movq %rbx, %rdi
callq 0xfaac4
testl %eax, %eax
je 0x40092
movl $0x148, %edi            # imm = 0x148
callq 0x27930
movq %rax, %rbx
movq %rax, %rdi
callq 0x42c38
movq (%rbx), %rax
leaq 0x400(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x2e0(%rax)
testl %eax, %eax
jne 0x40159
leaq 0x10d194(%rip), %rdi    # 0x14d212
leaq 0x400(%rsp), %rsi
xorl %eax, %eax
callq 0xfb6f2
jmp 0x4014e
movl $0x148, %edi            # imm = 0x148
callq 0x27930
movq %rax, %rbx
movq %rax, %rdi
callq 0x42c38
movsd 0x10bf99(%rip), %xmm0   # 0x14c048
movsd 0x10bfc1(%rip), %xmm1   # 0x14c078
movq %rbx, %rdi
movq %r14, %rsi
callq 0x42f84
testl %eax, %eax
je 0x40140
cmpq $0x0, 0x28(%r13)
je 0x40159
leaq 0x10d16e(%rip), %rsi    # 0x14d246
movq %r13, %rdi
movq %r12, %rdx
callq 0x32f6e
testl %eax, %eax
je 0x40159
movq 0x28(%r13), %rdx
leaq 0x10d0fa(%rip), %rsi    # 0x14d1ec
leaq 0x400(%rsp), %r12
movq %rsp, %rcx
movq %r12, %rdi
movl %r15d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x27140
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
movl $0x1, %ecx
callq *0x2e8(%rax)
testl %eax, %eax
jne 0x40159
leaq 0x10d124(%rip), %rdi    # 0x14d253
leaq 0x400(%rsp), %rsi
xorl %eax, %eax
callq 0xfb6f2
jmp 0x40159
leaq 0x10d0e8(%rip), %rdi    # 0x14d22f
xorl %eax, %eax
callq 0xfb6f2
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x1408, %rsp           # imm = 0x1408
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x40170
movq %rax, %r14
movl $0x148, %esi            # imm = 0x148
movq %rbx, %rdi
callq 0x27950
movq %r14, %rdi
callq 0x28150