// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/GSV/GSVMesh.cpp
// Function: gaps::GSVMesh::WritePlyFile(gaps::GSVScan*, char const*, int) const
int GSVMesh::
WritePlyFile(GSVScan *scan, const char *filename, RNBoolean binary) const
{
  typedef struct PlyVertex {
    float x, y, z;
    int scanline_index;
    int point_index;
    int boundary_type;
  } PlyVertex;

  typedef struct PlyFace {
    unsigned char nverts;
    int *verts;
    int segment;
  } PlyFace;

  // Element names
  char *elem_names[] = { (char *) "vertex", (char *) "face" };

  // List of property information for a vertex 
  static PlyProperty vert_props[] = { 
    {(char *) "x", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,x), 0, 0, 0, 0},
    {(char *) "y", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,y), 0, 0, 0, 0},
    {(char *) "z", PLY_FLOAT, PLY_FLOAT, offsetof(PlyVertex,z), 0, 0, 0, 0},
    {(char *) "scanline_index", PLY_INT, PLY_INT, offsetof(PlyVertex,scanline_index), 0, 0, 0, 0},
    {(char *) "point_index", PLY_INT, PLY_INT, offsetof(PlyVertex,point_index), 0, 0, 0, 0},
    {(char *) "boundary_type", PLY_INT, PLY_INT, offsetof(PlyVertex,boundary_type), 0, 0, 0, 0}
  };

  // List of property information for a vertex 
  static PlyProperty face_props[] = { 
    {(char *) "vertex_indices", PLY_INT, PLY_INT, offsetof(PlyFace,verts), 1, PLY_UCHAR, PLY_UCHAR, offsetof(PlyFace,nverts)},
    {(char *) "segment", PLY_INT, PLY_INT, offsetof(PlyFace,segment), 0, 0, 0, 0}
  };

  // Open ply file
  float version;
  int file_type = (binary) ? PLY_BINARY_NATIVE : PLY_ASCII;
  PlyFile *ply = ply_open_for_writing((char *) filename, 2, elem_names, file_type, &version);
  if (!ply) return -1;

  // Describe vertex properties
  ply_element_count(ply, (char *) "vertex", NVertices());
  for (unsigned int i = 0; i < sizeof(vert_props) / sizeof(PlyProperty); i++) {
    ply_describe_property(ply, (char *) "vertex", &vert_props[i]);
  }

  // Describe face properties
  ply_element_count(ply, (char *) "face", NFaces());
  for (unsigned int i = 0; i < sizeof(face_props) / sizeof(PlyProperty); i++) {
    ply_describe_property(ply, (char *) "face", &face_props[i]);
  }

  // Describe scan properties
  char buffer[1024];
  GSVSegment *segment = (scan) ? scan->Segment() : NULL;
  GSVRun *run = (segment) ? segment->Run() : NULL;
  sprintf(buffer, "run_name %s", (run) ? run->Name() : "None");
  ply_put_obj_info(ply, buffer);
  sprintf(buffer, "segment_index %d", (segment) ? segment->RunIndex() : -1);
  ply_put_obj_info(ply, buffer);
  sprintf(buffer, "scan_index %d", (scan) ? scan->SegmentIndex() : -1);
  ply_put_obj_info(ply, buffer);

  // Complete header
  ply_header_complete(ply);

  // Write vertices
  ply_put_element_setup(ply, (char *) "vertex");
  for (int i = 0; i < NVertices(); i++) {
    GSVMeshVertex *v = (GSVMeshVertex *) Vertex(i);
    const R3Point& position = VertexPosition(v);
    PlyVertex ply_vertex;
    ply_vertex.x = position.X();
    ply_vertex.y = position.Y();
    ply_vertex.z = position.Z();
    ply_vertex.scanline_index = (v->scanline) ? v->scanline->ScanIndex() : -1;
    ply_vertex.point_index = v->point_index;
    ply_vertex.boundary_type = v->boundary_type;
    ply_put_element(ply, (void *) &ply_vertex);
  }

  // Write faces
  ply_put_element_setup(ply, (char *) "face");
  for (int i = 0; i < NFaces(); i++) {
    R3MeshFace *f = Face(i);
    static int verts[3];
    static PlyFace ply_face = { 3, verts, 0 };
    ply_face.verts[0] = VertexID(VertexOnFace(f, 0));
    ply_face.verts[1] = VertexID(VertexOnFace(f, 1));
    ply_face.verts[2] = VertexID(VertexOnFace(f, 2));
    ply_face.segment = FaceSegment(f);
    ply_put_element(ply, (void *) &ply_face);
  }

  // Close the file 
  ply_close(ply);

  // Return number of faces written
  return NFaces();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movq %rdx, %rax
movq %rsi, %r15
movq %rdi, %rbx
movaps 0x18a2cb(%rip), %xmm0   # 0x1cee50
leaq 0x20(%rsp), %rdx
movaps %xmm0, (%rdx)
xorl %esi, %esi
testl %ecx, %ecx
setne %sil
leal 0x1(%rsi,%rsi,2), %ecx
leaq 0x4(%rsp), %r8
movq %rax, %rdi
movl $0x2, %esi
callq 0xda746
testq %rax, %rax
je 0x44c40
movq %rax, %r14
movl 0x14(%rbx), %edx
leaq 0x10a83f(%rip), %r12    # 0x14f400
movq %rax, %rdi
movq %r12, %rsi
callq 0xdab69
xorl %r13d, %r13d
leaq 0x19303a(%rip), %rbp    # 0x1d7c10
movq %rbp, %rdx
addq %r13, %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0xda947
addq $0x28, %r13
cmpq $0xf0, %r13
jne 0x44bd6
movl 0x34(%rbx), %edx
leaq 0x10a809(%rip), %r12    # 0x14f407
movq %r14, %rdi
movq %r12, %rsi
callq 0xdab69
xorl %r13d, %r13d
leaq 0x1930ed(%rip), %rbp    # 0x1d7d00
movq %rbp, %rdx
addq %r13, %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0xda947
addq $0x28, %r13
cmpq $0x28, %r13
je 0x44c13
testq %r15, %r15
je 0x44c4a
movq %r15, %rdi
callq 0x3faf8
movq %rax, %r12
jmp 0x44c4d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x44df5
xorl %r12d, %r12d
testq %r12, %r12
je 0x44c59
movq 0x10(%r12), %rax
jmp 0x44c5b
xorl %eax, %eax
testq %rax, %rax
je 0x44c66
movq 0x48(%rax), %rdx
jmp 0x44c6d
leaq 0x10a7ab(%rip), %rdx    # 0x14f418
leaq 0x10a798(%rip), %rsi    # 0x14f40c
leaq 0x30(%rsp), %r13
movq %r13, %rdi
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %r13, %rsi
callq 0xdb30b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
testq %r12, %r12
je 0x44ca2
movl 0x18(%r12), %edx
leaq 0x10a774(%rip), %rsi    # 0x14f41d
leaq 0x30(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %r12, %rsi
callq 0xdb30b
testq %r15, %r15
je 0x44cd2
movq %r15, %rdi
callq 0x3fbf6
movl %eax, %ebp
leaq 0x10a755(%rip), %rsi    # 0x14f42e
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %r15, %rsi
callq 0xdb30b
movq %r14, %rdi
callq 0xdab8f
leaq 0x10a6fc(%rip), %rsi    # 0x14f400
movq %r14, %rdi
callq 0xdada8
cmpl $0x0, 0x14(%rbx)
jle 0x44d7c
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movq 0x8(%rbx), %rax
movq (%rax,%r12,8), %rax
movupd 0x18(%rax), %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsd2ss 0x28(%rax), %xmm0
movss %xmm0, 0x10(%rsp)
movq 0x98(%rax), %rcx
testq %rcx, %rcx
je 0x44d50
movl 0x10(%rcx), %ecx
jmp 0x44d55
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x14(%rsp)
movq 0xa0(%rax), %rax
movq %rax, 0x18(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xdadd0
incq %r12
movslq 0x14(%rbx), %rax
cmpq %rax, %r12
jl 0x44d1a
leaq 0x10a684(%rip), %rsi    # 0x14f407
movq %r14, %rdi
callq 0xdada8
cmpl $0x0, 0x34(%rbx)
jle 0x44dea
xorl %r12d, %r12d
leaq 0x192fb5(%rip), %r15    # 0x1d7d50
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rcx
movl 0x70(%rcx), %ecx
movq 0x192fa7(%rip), %rdx    # 0x1d7d58
movl %ecx, (%rdx)
movq 0x10(%rax), %rcx
movl 0x70(%rcx), %ecx
movl %ecx, 0x4(%rdx)
movq 0x18(%rax), %rcx
movl 0x70(%rcx), %ecx
movl %ecx, 0x8(%rdx)
movl 0x9c(%rax), %eax
movl %eax, 0x192f8d(%rip)    # 0x1d7d60
movq %r14, %rdi
movq %r15, %rsi
callq 0xdadd0
incq %r12
movslq 0x34(%rbx), %rax
cmpq %rax, %r12
jl 0x44d9b
movq %r14, %rdi
callq 0xdc768
movl 0x34(%rbx), %eax
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop