// File: /workspace/github_cpp_makefile_star2_O2/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 0x3d5e3
movq %rdi, %r14
callq 0x3cecc
movl %eax, %ebp
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x3d5e3
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x3d5e3
movq 0x10(%rax), %r12
testq %r12, %r12
je 0x3d5e3
movl 0x18(%rax), %r15d
movq 0x48(%r12), %rsi
testq %rsi, %rsi
je 0x3d476
movq %rsp, %rdi
callq 0x27880
jmp 0x3d48c
movl 0x10(%r12), %edx
leaq 0xee5cb(%rip), %rsi     # 0x12ba4d
movq %rsp, %rdi
xorl %eax, %eax
callq 0x27140
movq 0x8(%r12), %r13
testq %r13, %r13
je 0x3d5e3
movq 0x28(%r13), %rdx
testq %rdx, %rdx
je 0x3d520
leaq 0xeed12(%rip), %rsi     # 0x12c1bc
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 0xe391c
testl %eax, %eax
je 0x3d520
movl $0x148, %edi            # imm = 0x148
callq 0x27940
movq %rax, %rbx
movq %rax, %rdi
callq 0x3fbd0
movq (%rbx), %rax
leaq 0x400(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x2e0(%rax)
testl %eax, %eax
jne 0x3d5e5
leaq 0xeecd6(%rip), %rdi     # 0x12c1e2
leaq 0x400(%rsp), %rsi
xorl %eax, %eax
callq 0xe434a
jmp 0x3d5da
movl $0x148, %edi            # imm = 0x148
callq 0x27940
movq %rax, %rbx
movq %rax, %rdi
callq 0x3fbd0
movsd 0xedb03(%rip), %xmm0    # 0x12b040
movsd 0xedb2b(%rip), %xmm1    # 0x12b070
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3ff18
testl %eax, %eax
je 0x3d5cc
cmpq $0x0, 0x28(%r13)
je 0x3d5e5
leaq 0xeecb0(%rip), %rsi     # 0x12c216
movq %r13, %rdi
movq %r12, %rdx
callq 0x318b8
testl %eax, %eax
je 0x3d5e5
movq 0x28(%r13), %rdx
leaq 0xeec3c(%rip), %rsi     # 0x12c1bc
leaq 0x400(%rsp), %r12
movq %rsp, %rcx
movq %r12, %rdi
movl %r15d, %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x27140
movq (%rbx), %rax
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq *0x2e8(%rax)
testl %eax, %eax
jne 0x3d5e5
leaq 0xeec68(%rip), %rdi     # 0x12c223
leaq 0x400(%rsp), %rsi
xorl %eax, %eax
callq 0xe434a
jmp 0x3d5e5
leaq 0xeec2c(%rip), %rdi     # 0x12c1ff
xorl %eax, %eax
callq 0xe434a
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 0x3d5fc
movq %rax, %r14
movl $0x148, %esi            # imm = 0x148
movq %rbx, %rdi
callq 0x27960
movq %r14, %rdi
callq 0x28170