// File: tomfunkhouser[P]gaps/apps/mshparam/mshparam.cpp
// Function: ParameterizeSegments(gaps::R3Mesh*, gaps::RNArray<Segment*> const&)
static int 
ParameterizeSegments(R3Mesh *mesh, const RNArray<Segment *>& segments)
{
  // Initialize location of segment in texture coordinates
  R2Point origin(0,0);
  RNLength max_xlength = 0;
  RNLength max_yorigin = mesh->BBox().LongestAxisLength();

  // Parameterize each segment
  for (int i = 0; i < segments.NEntries(); i++) {
    Segment *segment = segments.Kth(i);

    // Update matrix to start texcoords bbox for segment at origin
    R4Matrix matrix = segment->matrix;
    matrix[0][3] += origin.X() - segment->bbox.XMin();    
    matrix[1][3] += origin.Y() - segment->bbox.YMin();    
      
    // Assign texture coordinates to vertices
    for (int j = 0; j < segment->vertices.NEntries(); j++) {
      R3MeshVertex *vertex = segment->vertices.Kth(j);
      R3Point position = mesh->VertexPosition(vertex);
      position = matrix * position;
      R2Point texcoords(position.X(), position.Y());
      mesh->SetVertexTextureCoords(vertex, texcoords);
    }

    // Update max xlength
    if (segment->bbox.XLength() > max_xlength) max_xlength = segment->bbox.XLength();

    // Update origin
    origin[1] += segment->bbox.YLength();
    if (origin[1] > max_yorigin) {
      origin[0] += max_xlength;
      origin[1] = 0;
      max_xlength = 0;
    }
  }
  
  // Return success
  return 1;
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
leaq 0x138(%rsp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0xb2260
xorps %xmm0, %xmm0
movsd %xmm0, 0x130(%rsp)
movq 0x150(%rsp), %rdi
callq 0x199d0
movq %rax, %rdi
callq 0x4d450
movsd %xmm0, 0x128(%rsp)
movl $0x0, 0x124(%rsp)
movl 0x124(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0x148(%rsp), %rdi
callq 0x162c0
movl %eax, %ecx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x160d7
movq 0x148(%rsp), %rdi
movl 0x124(%rsp), %esi
callq 0x16850
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rsi
addq $0x30, %rsi
leaq 0x98(%rsp), %rdi
callq 0x5b290
leaq 0x138(%rsp), %rdi
callq 0x199f0
movsd %xmm0, 0x20(%rsp)
movq 0x118(%rsp), %rdi
addq $0xb0, %rdi
callq 0x19a00
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x98(%rsp), %rdi
xorl %esi, %esi
callq 0x19a20
movsd 0x28(%rsp), %xmm0
addsd 0x18(%rax), %xmm0
movsd %xmm0, 0x18(%rax)
leaq 0x138(%rsp), %rdi
callq 0x19a40
movsd %xmm0, 0x30(%rsp)
movq 0x118(%rsp), %rdi
addq $0xb0, %rdi
callq 0x19a50
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
leaq 0x98(%rsp), %rdi
movl $0x1, %esi
callq 0x19a20
movsd 0x38(%rsp), %xmm0
addsd 0x18(%rax), %xmm0
movsd %xmm0, 0x18(%rax)
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x118(%rsp), %rdi
addq $0x18, %rdi
callq 0x162c0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x15fd9
movq 0x118(%rsp), %rdi
addq $0x18, %rdi
movl 0x94(%rsp), %esi
callq 0x16890
movq %rax, 0x88(%rsp)
movq 0x150(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x16960
movq %rax, %rsi
leaq 0x70(%rsp), %rdi
callq 0x61810
leaq 0x58(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x5e980
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x61e60
leaq 0x70(%rsp), %rdi
callq 0x169a0
movsd %xmm0, 0x10(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x169b0
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
leaq 0x48(%rsp), %rdi
callq 0xb2260
movq 0x150(%rsp), %rdi
movq 0x88(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x19a70
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x15eea
movq 0x118(%rsp), %rdi
addq $0xb0, %rdi
callq 0x19870
ucomisd 0x130(%rsp), %xmm0
jbe 0x16015
movq 0x118(%rsp), %rdi
addq $0xb0, %rdi
callq 0x19870
movsd %xmm0, 0x130(%rsp)
movq 0x118(%rsp), %rdi
addq $0xb0, %rdi
callq 0x19aa0
movsd %xmm0, 0x8(%rsp)
leaq 0x138(%rsp), %rdi
movl $0x1, %esi
callq 0x19ac0
movsd 0x8(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
leaq 0x138(%rsp), %rdi
movl $0x1, %esi
callq 0x19ac0
movsd (%rax), %xmm0
ucomisd 0x128(%rsp), %xmm0
jbe 0x160bf
movsd 0x130(%rsp), %xmm0
movsd %xmm0, (%rsp)
leaq 0x138(%rsp), %rdi
xorl %esi, %esi
callq 0x19ac0
movsd (%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
leaq 0x138(%rsp), %rdi
movl $0x1, %esi
callq 0x19ac0
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
xorps %xmm0, %xmm0
movsd %xmm0, 0x130(%rsp)
jmp 0x160c1
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x15dcf
movl $0x1, %eax
addq $0x158, %rsp            # imm = 0x158
retq
nopw %cs:(%rax,%rax)