// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::SplitFace(gaps::R3MeshFace*, gaps::R3Point const&, gaps::R3MeshFace**, gaps::R3MeshFace**, gaps::R3MeshFace**)
R3MeshVertex *R3Mesh::
SplitFace(R3MeshFace *f, const R3Point& point, R3MeshFace **f0, R3MeshFace **f1, R3MeshFace **f2)
{
  // Invalidate GL buffer objects
  InvalidateGLBufferObjects();

  // Find vertices/edges bounding face
  R3MeshVertex *v0 = VertexOnFace(f, 0);
  R3MeshEdge *e0 = EdgeOnFace(f, v0, RN_CCW);
  R3MeshVertex *v1 = VertexAcrossEdge(e0, v0);
  R3MeshEdge *e1 = EdgeOnFace(f, v1, RN_CCW);
  R3MeshVertex *v2 = VertexAcrossEdge(e1, v1);
  R3MeshEdge *e2 = EdgeOnFace(f, v2, RN_CCW);
  int m = FaceMaterial(f);
  int s = FaceSegment(f);
  int c = FaceCategory(f);

  // Delete face
  DeleteFace(f);

  // Create new vertex at point
  R3MeshVertex *vertex = CreateVertex(point);

  // Create three new edges
  R3MeshEdge *s0 = CreateEdge(v0, vertex);
  R3MeshEdge *s1 = CreateEdge(v1, vertex);
  R3MeshEdge *s2 = CreateEdge(v2, vertex);

  // Create three new faces
  R3MeshFace *t0 = CreateFace(vertex, v0, v1, s0, e0, s1);
  R3MeshFace *t1 = CreateFace(vertex, v1, v2, s1, e1, s2);
  R3MeshFace *t2 = CreateFace(vertex, v2, v0, s2, e2, s0);
  
  // Set face materials
  SetFaceMaterial(t0, m);
  SetFaceMaterial(t1, m);
  SetFaceMaterial(t2, m);

  // Set face segments
  SetFaceSegment(t0, s);
  SetFaceSegment(t1, s);
  SetFaceSegment(t2, s);

  // Set face categories
  SetFaceCategory(t0, c);
  SetFaceCategory(t1, c);
  SetFaceCategory(t2, c);

  // Interpolate vertex properties
  RNScalar d0 = R3Distance(point, VertexPosition(v0));
  RNScalar d1 = R3Distance(point, VertexPosition(v1));
  RNScalar d2 = R3Distance(point, VertexPosition(v2));
  RNScalar dsum = d0 + d1 + d2;
  if (RNIsPositive(dsum)) {
    RNScalar t0 = (d1+d2)/dsum;   RNScalar t1 = (d0+d2)/dsum;  RNScalar t2 = (d0+d1)/dsum;
    SetVertexTextureCoords(vertex, t0*VertexTextureCoords(v0) + t1*VertexTextureCoords(v1) + t2*VertexTextureCoords(v2)); 
    SetVertexColor(vertex, t0*VertexColor(v0) + t1*VertexColor(v1) + t2*VertexColor(v2)); 
  }

  // Return created faces
  if (f0) *f0 = t0;
  if (f1) *f1 = t1;
  if (f2) *f2 = t2;

  // Return new vertex
  return vertex;
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x210(%rsp)
movq %rsi, 0x208(%rsp)
movq %rdx, 0x200(%rsp)
movq %rcx, 0x1f8(%rsp)
movq %r8, 0x1f0(%rsp)
movq %r9, 0x1e8(%rsp)
movq 0x210(%rsp), %rdi
movq %rdi, 0x60(%rsp)
movq (%rdi), %rax
callq *0x290(%rax)
movq 0x60(%rsp), %rdi
movq 0x208(%rsp), %rsi
xorl %edx, %edx
callq 0x13700
movq 0x60(%rsp), %rdi
movq %rax, 0x1e0(%rsp)
movq 0x208(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
xorl %ecx, %ecx
callq 0x37a60
movq 0x60(%rsp), %rdi
movq %rax, 0x1d8(%rsp)
movq 0x1d8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
callq 0x36f50
movq 0x60(%rsp), %rdi
movq %rax, 0x1d0(%rsp)
movq 0x208(%rsp), %rsi
movq 0x1d0(%rsp), %rdx
xorl %ecx, %ecx
callq 0x37a60
movq 0x60(%rsp), %rdi
movq %rax, 0x1c8(%rsp)
movq 0x1c8(%rsp), %rsi
movq 0x1d0(%rsp), %rdx
callq 0x36f50
movq 0x60(%rsp), %rdi
movq %rax, 0x1c0(%rsp)
movq 0x208(%rsp), %rsi
movq 0x1c0(%rsp), %rdx
xorl %ecx, %ecx
callq 0x37a60
movq 0x60(%rsp), %rdi
movq %rax, 0x1b8(%rsp)
movq 0x208(%rsp), %rsi
callq 0x369e0
movq 0x60(%rsp), %rdi
movl %eax, 0x1b4(%rsp)
movq 0x208(%rsp), %rsi
callq 0x36a20
movq 0x60(%rsp), %rdi
movl %eax, 0x1b0(%rsp)
movq 0x208(%rsp), %rsi
callq 0x36a60
movq 0x60(%rsp), %rdi
movl %eax, 0x1ac(%rsp)
movq 0x208(%rsp), %rsi
movq (%rdi), %rax
callq *0x70(%rax)
movq 0x60(%rsp), %rdi
movq 0x200(%rsp), %rsi
movq (%rdi), %rax
xorl %ecx, %ecx
movl %ecx, %edx
callq *0x20(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x1a0(%rsp)
movq 0x1e0(%rsp), %rsi
movq 0x1a0(%rsp), %rdx
movq (%rdi), %rax
xorl %ecx, %ecx
callq *0x40(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x198(%rsp)
movq 0x1d0(%rsp), %rsi
movq 0x1a0(%rsp), %rdx
movq (%rdi), %rax
xorl %ecx, %ecx
callq *0x40(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x190(%rsp)
movq 0x1c0(%rsp), %rsi
movq 0x1a0(%rsp), %rdx
movq (%rdi), %rax
xorl %ecx, %ecx
callq *0x40(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x188(%rsp)
movq 0x1a0(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movq 0x1d0(%rsp), %rcx
movq 0x198(%rsp), %r8
movq 0x1d8(%rsp), %r9
movq 0x190(%rsp), %r10
movq (%rdi), %rax
xorl %r11d, %r11d
movq %r10, (%rsp)
movq $0x0, 0x8(%rsp)
callq *0x58(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x180(%rsp)
movq 0x1a0(%rsp), %rsi
movq 0x1d0(%rsp), %rdx
movq 0x1c0(%rsp), %rcx
movq 0x190(%rsp), %r8
movq 0x1c8(%rsp), %r9
movq 0x188(%rsp), %r10
movq (%rdi), %rax
xorl %r11d, %r11d
movq %r10, (%rsp)
movq $0x0, 0x8(%rsp)
callq *0x58(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x178(%rsp)
movq 0x1a0(%rsp), %rsi
movq 0x1c0(%rsp), %rdx
movq 0x1e0(%rsp), %rcx
movq 0x188(%rsp), %r8
movq 0x1b8(%rsp), %r9
movq 0x198(%rsp), %r10
movq (%rdi), %rax
xorl %r11d, %r11d
movq %r10, (%rsp)
movq $0x0, 0x8(%rsp)
callq *0x58(%rax)
movq 0x60(%rsp), %rdi
movq %rax, 0x170(%rsp)
movq 0x180(%rsp), %rsi
movl 0x1b4(%rsp), %edx
callq 0x369c0
movq 0x60(%rsp), %rdi
movq 0x178(%rsp), %rsi
movl 0x1b4(%rsp), %edx
callq 0x369c0
movq 0x60(%rsp), %rdi
movq 0x170(%rsp), %rsi
movl 0x1b4(%rsp), %edx
callq 0x369c0
movq 0x60(%rsp), %rdi
movq 0x180(%rsp), %rsi
movl 0x1b0(%rsp), %edx
callq 0x36a00
movq 0x60(%rsp), %rdi
movq 0x178(%rsp), %rsi
movl 0x1b0(%rsp), %edx
callq 0x36a00
movq 0x60(%rsp), %rdi
movq 0x170(%rsp), %rsi
movl 0x1b0(%rsp), %edx
callq 0x36a00
movq 0x60(%rsp), %rdi
movq 0x180(%rsp), %rsi
movl 0x1ac(%rsp), %edx
callq 0x36a40
movq 0x60(%rsp), %rdi
movq 0x178(%rsp), %rsi
movl 0x1ac(%rsp), %edx
callq 0x36a40
movq 0x60(%rsp), %rdi
movq 0x170(%rsp), %rsi
movl 0x1ac(%rsp), %edx
callq 0x36a40
movq 0x60(%rsp), %rdi
movq 0x200(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x1e0(%rsp), %rsi
callq 0x13660
movq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movq 0x60(%rsp), %rdi
movsd %xmm0, 0x168(%rsp)
movq 0x200(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x1d0(%rsp), %rsi
callq 0x13660
movq 0x58(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movq 0x60(%rsp), %rdi
movsd %xmm0, 0x160(%rsp)
movq 0x200(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x1c0(%rsp), %rsi
callq 0x13660
movq 0x68(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movsd %xmm0, 0x158(%rsp)
movsd 0x168(%rsp), %xmm0
addsd 0x160(%rsp), %xmm0
addsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x150(%rsp)
movsd 0x150(%rsp), %xmm0
leaq 0x15a85e(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x1df00
movq 0x60(%rsp), %rdi
movsd 0x160(%rsp), %xmm0
addsd 0x158(%rsp), %xmm0
divsd 0x150(%rsp), %xmm0
movsd %xmm0, 0x148(%rsp)
movsd 0x168(%rsp), %xmm0
addsd 0x158(%rsp), %xmm0
divsd 0x150(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movsd 0x168(%rsp), %xmm0
addsd 0x160(%rsp), %xmm0
divsd 0x150(%rsp), %xmm0
movsd %xmm0, 0x138(%rsp)
movq 0x1a0(%rsp), %rax
movq %rax, 0x28(%rsp)
movsd 0x148(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x1e0(%rsp), %rsi
callq 0x368f0
movsd 0x10(%rsp), %xmm0
movq %rax, %rsi
leaq 0x108(%rsp), %rdi
callq 0x37910
movq 0x60(%rsp), %rdi
movsd 0x140(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x1d0(%rsp), %rsi
callq 0x368f0
movsd 0x18(%rsp), %xmm0
movq %rax, %rsi
leaq 0xf8(%rsp), %rdi
callq 0x37910
leaq 0x118(%rsp), %rdi
leaq 0x108(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
callq 0xc3750
movq 0x60(%rsp), %rdi
movsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x1c0(%rsp), %rsi
callq 0x368f0
movsd 0x20(%rsp), %xmm0
movq %rax, %rsi
leaq 0xe8(%rsp), %rdi
callq 0x37910
leaq 0x128(%rsp), %rdi
leaq 0x118(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
callq 0xc3750
movq 0x28(%rsp), %rsi
movq 0x60(%rsp), %rdi
leaq 0x128(%rsp), %rdx
callq 0x376a0
movq 0x60(%rsp), %rdi
movq 0x1a0(%rsp), %rax
movq %rax, 0x48(%rsp)
movsd 0x148(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x1e0(%rsp), %rsi
callq 0x368d0
movsd 0x30(%rsp), %xmm0
movq %rax, %rsi
leaq 0xa0(%rsp), %rdi
callq 0x37950
movq 0x60(%rsp), %rdi
movsd 0x140(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x1d0(%rsp), %rsi
callq 0x368d0
movsd 0x38(%rsp), %xmm0
movq %rax, %rsi
leaq 0x88(%rsp), %rdi
callq 0x37950
leaq 0xb8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x88(%rsp), %rdx
callq 0x37200
movq 0x60(%rsp), %rdi
movsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0x1c0(%rsp), %rsi
callq 0x368d0
movsd 0x40(%rsp), %xmm0
movq %rax, %rsi
leaq 0x70(%rsp), %rdi
callq 0x37950
leaq 0xd0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x37200
movq 0x60(%rsp), %rdi
movq 0x48(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0x37670
cmpq $0x0, 0x1f8(%rsp)
je 0x1df1e
movq 0x180(%rsp), %rcx
movq 0x1f8(%rsp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x1f0(%rsp)
je 0x1df3c
movq 0x178(%rsp), %rcx
movq 0x1f0(%rsp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x1e8(%rsp)
je 0x1df5a
movq 0x170(%rsp), %rcx
movq 0x1e8(%rsp), %rax
movq %rcx, (%rax)
movq 0x1a0(%rsp), %rax
addq $0x218, %rsp            # imm = 0x218
retq
nopw (%rax,%rax)