// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1418, %rsp           # imm = 0x1418
cmpl $0x0, 0x34(%rdi)
je 0x405b5
movq %rdi, %r14
callq 0x3fbf6
movl %eax, %ebp
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x403e4
movq 0x8(%rax), %rax
jmp 0x403e6
xorl %eax, %eax
testq %rax, %rax
je 0x405b5
movq 0x10(%rax), %r12
testq %r12, %r12
je 0x405b5
movl 0x18(%rax), %eax
movl %eax, 0xc(%rsp)
movq 0x48(%r12), %rsi
testq %rsi, %rsi
je 0x40419
leaq 0x10(%rsp), %rdi
callq 0x27870
jmp 0x40431
movl 0x10(%r12), %edx
leaq 0x10e618(%rip), %rsi    # 0x14ea3d
leaq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0x27140
movq 0x8(%r12), %r13
testq %r13, %r13
je 0x405b5
cmpq $0x0, 0x28(%r13)
je 0x404e1
movq 0x28(%r13), %rdx
leaq 0x10ed67(%rip), %rsi    # 0x14f1bc
xorl %ebx, %ebx
leaq 0x410(%rsp), %r15
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
movl 0xc(%rsp), %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x27140
movq %r15, %rdi
callq 0xfc978
movb $0x1, %r15b
testl %eax, %eax
je 0x404d7
movl $0x148, %edi            # imm = 0x148
callq 0x27930
movq %rax, %rbx
movq %rax, %rdi
callq 0x43100
movq (%rbx), %rax
leaq 0x410(%rsp), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x2e0(%rax)
testl %eax, %eax
jne 0x404d7
leaq 0x10ed26(%rip), %rdi    # 0x14f1e2
xorl %r15d, %r15d
leaq 0x410(%rsp), %rsi
xorl %eax, %eax
callq 0xfd64a
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
testb %r15b, %r15b
jne 0x404e3
jmp 0x405b5
xorl %ebx, %ebx
testq %rbx, %rbx
jne 0x405b7
movl $0x148, %edi            # imm = 0x148
callq 0x27930
movq %rax, %rbx
movq %rax, %rdi
callq 0x43100
movsd 0x10db3f(%rip), %xmm0   # 0x14e048
movsd 0x10db77(%rip), %xmm1   # 0x14e088
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4344c
testl %eax, %eax
je 0x4059e
cmpq $0x0, 0x28(%r13)
je 0x405b7
leaq 0x10ece4(%rip), %rsi    # 0x14f216
movq %r13, %rdi
movq %r12, %rdx
callq 0x33130
testl %eax, %eax
je 0x405b7
movq 0x28(%r13), %rdx
leaq 0x10ec70(%rip), %rsi    # 0x14f1bc
leaq 0x410(%rsp), %r15
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
movl 0xc(%rsp), %r8d
movl %ebp, %r9d
xorl %eax, %eax
callq 0x27140
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movl $0x1, %ecx
callq *0x2e8(%rax)
testl %eax, %eax
jne 0x405b7
leaq 0x10ec96(%rip), %rdi    # 0x14f223
leaq 0x410(%rsp), %rsi
xorl %eax, %eax
callq 0xfd64a
jmp 0x405b7
leaq 0x10ec5a(%rip), %rdi    # 0x14f1ff
xorl %eax, %eax
callq 0xfd64a
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x1418, %rsp           # imm = 0x1418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x405ce
movq %rax, %r14
movl $0x148, %esi            # imm = 0x148
movq %rbx, %rdi
callq 0x27950
movq %r14, %rdi
callq 0x28160