// File: /workspace/github_cpp_makefile_star2_O2/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 $0x448, %rsp            # imm = 0x448
movq %rdx, %rax
movq %rsi, %rbp
movq %rdi, %rbx
movaps 0x174a49(%rip), %xmm0   # 0x1b5e80
leaq 0x30(%rsp), %rdx
movaps %xmm0, (%rdx)
xorl %esi, %esi
testl %ecx, %ecx
setne %sil
leal 0x1(%rsi,%rsi,2), %ecx
pushq $0x2
popq %rsi
leaq 0x14(%rsp), %r8
movq %rax, %rdi
callq 0xc4acd
testq %rax, %rax
je 0x41691
movq %rax, %r14
movl 0x14(%rbx), %edx
leaq 0xeaf7f(%rip), %r12     # 0x12c3f0
movq %rax, %rdi
movq %r12, %rsi
callq 0xc4f07
xorl %r15d, %r15d
leaq 0x17d79a(%rip), %r13    # 0x1bec20
cmpq $0xf0, %r15
je 0x414a4
leaq (%r15,%r13), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0xc4ceb
addq $0x28, %r15
jmp 0x41486
movl 0x34(%rbx), %edx
leaq 0xeaf49(%rip), %r12     # 0x12c3f7
movq %r14, %rdi
movq %r12, %rsi
callq 0xc4f07
xorl %r15d, %r15d
leaq 0x17d84d(%rip), %r13    # 0x1bed10
cmpq $0x50, %r15
je 0x414de
leaq (%r15,%r13), %rdx
movq %r14, %rdi
movq %r12, %rsi
callq 0xc4ceb
addq $0x28, %r15
jmp 0x414c3
leaq 0xeaf23(%rip), %r13     # 0x12c408
movb $0x1, %r15b
testq %rbp, %rbp
movq %rbp, 0x8(%rsp)
je 0x41514
movq %rbp, %rdi
callq 0x3cdf6
testq %rax, %rax
je 0x41514
movq %rax, %r12
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x4150f
movq 0x48(%rax), %r13
xorl %r15d, %r15d
jmp 0x41517
xorl %r12d, %r12d
leaq 0xeaede(%rip), %rsi     # 0x12c3fc
leaq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %rbp, %rsi
callq 0xc56ff
pushq $-0x1
popq %rbp
movl %ebp, %edx
testb %r15b, %r15b
jne 0x4154a
movl 0x18(%r12), %edx
leaq 0xeaebc(%rip), %rsi     # 0x12c40d
leaq 0x40(%rsp), %r12
movq %r12, %rdi
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %r12, %rsi
callq 0xc56ff
movl %ebp, %edx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x4157e
callq 0x3cecc
movl %eax, %edx
leaq 0xeae99(%rip), %rsi     # 0x12c41e
xorl %r12d, %r12d
leaq 0x40(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0x27140
movq %r14, %rdi
movq %r15, %rsi
callq 0xc56ff
movq %r14, %rdi
callq 0xc4f46
leaq 0xeae3f(%rip), %rsi     # 0x12c3f0
movq %r14, %rdi
callq 0xc516a
leaq 0x18(%rsp), %r15
movslq 0x14(%rbx), %rax
cmpq %rax, %r12
jge 0x4161a
movq 0x8(%rbx), %rax
movq (%rax,%r12,8), %rax
movupd 0x18(%rax), %xmm0
cvtpd2ps %xmm0, %xmm0
movlpd %xmm0, 0x18(%rsp)
cvtsd2ss 0x28(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movq 0x98(%rax), %rdx
movl %ebp, %ecx
testq %rdx, %rdx
je 0x415fa
movl 0x10(%rdx), %ecx
movl %ecx, 0x24(%rsp)
movq 0xa0(%rax), %rax
movq %rax, 0x28(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xc51ad
incq %r12
jmp 0x415be
leaq 0xeadd6(%rip), %rsi     # 0x12c3f7
movq %r14, %rdi
callq 0xc516a
xorl %r12d, %r12d
leaq 0x17d72d(%rip), %r15    # 0x1bed60
movslq 0x34(%rbx), %rax
cmpq %rax, %r12
jge 0x41684
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rax
movq 0x8(%rax), %rcx
movl 0x70(%rcx), %ecx
movq 0x17d716(%rip), %rdx    # 0x1bed68
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, 0x17d6fc(%rip)    # 0x1bed70
movq %r14, %rdi
movq %r15, %rsi
callq 0xc51ad
incq %r12
jmp 0x41633
movq %r14, %rdi
callq 0xc69df
movl 0x34(%rbx), %eax
jmp 0x41694
pushq $-0x1
popq %rax
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq