// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, 0x68(%rsp)
movq %r8, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdx, %rbp
movq %rsi, %rbx
movq %rdi, %r15
movq (%rdi), %rax
callq *0x290(%rax)
movq 0x8(%rbx), %r14
movq 0x20(%rbx), %rcx
movq 0x8(%rcx), %r12
cmpq %r14, %r12
jne 0x153eb
movq 0x10(%rcx), %r12
cmpq %r12, %r14
je 0x15400
cmpq %r12, 0x10(%rbx)
je 0x15404
cmpq %r12, 0x18(%rbx)
je 0x15408
xorl %eax, %eax
jmp 0x1540f
pushq $0x20
jmp 0x1540a
pushq $0x28
jmp 0x1540a
pushq $0x30
popq %rax
movq (%rbx,%rax), %rax
movq 0x8(%rax), %r13
cmpq %r12, %r13
jne 0x1541c
movq 0x10(%rax), %r13
cmpq %r13, %r14
movq %rcx, (%rsp)
movq %rax, 0x8(%rsp)
je 0x15441
cmpq %r13, 0x10(%rbx)
je 0x15445
cmpq %r13, 0x18(%rbx)
je 0x15449
movq $0x0, 0x10(%rsp)
jmp 0x15455
pushq $0x20
jmp 0x1544b
pushq $0x28
jmp 0x1544b
pushq $0x30
popq %rax
movq (%rbx,%rax), %rax
movq %rax, 0x10(%rsp)
movl 0x98(%rbx), %eax
movl %eax, 0x20(%rsp)
movl 0x9c(%rbx), %eax
movl %eax, 0x28(%rsp)
movl 0xa0(%rbx), %eax
movl %eax, 0x18(%rsp)
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x70(%rax)
movq (%r15), %rax
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq *0x20(%rax)
movq %rbp, 0x50(%rsp)
movq %rax, %rbp
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %r14, 0x38(%rsp)
movq %rax, %r14
movq %rax, 0x48(%rsp)
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %rax, %rbx
movq (%r15), %rax
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
xorl %ecx, %ecx
callq *0x40(%rax)
movq %r13, 0x30(%rsp)
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
movq %rbp, %rsi
movq 0x38(%rsp), %rdx
movq %r12, %rcx
movq %r14, %r8
movq (%rsp), %r9
pushq $0x0
pushq %rbx
callq *0x58(%rax)
addq $0x10, %rsp
movq %rax, %r14
movq (%r15), %rax
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
movq 0x30(%rsp), %rcx
movq %rbx, %r8
movq 0x8(%rsp), %r9
pushq $0x0
movq %r13, %rbx
pushq %r13
callq *0x58(%rax)
addq $0x10, %rsp
movq %rax, %r13
movq (%r15), %rax
movq %r15, %rdi
movq %rbp, 0x40(%rsp)
movq %rbp, %rsi
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq %rbx, %r8
movq 0x30(%rsp), %rbp
movq 0x10(%rsp), %r9
pushq $0x0
pushq 0x50(%rsp)
movq 0x48(%rsp), %r15
callq *0x58(%rax)
addq $0x10, %rsp
movl 0x20(%rsp), %ecx
movl %ecx, 0x98(%r14)
movl %ecx, 0x98(%r13)
movl %ecx, 0x98(%rax)
movl 0x28(%rsp), %ecx
movl %ecx, 0x9c(%r14)
movl %ecx, 0x9c(%r13)
movl %ecx, 0x9c(%rax)
movq %r14, 0x28(%rsp)
movl 0x18(%rsp), %ecx
movl %ecx, 0xa0(%r14)
movq %r13, %rbx
movl %ecx, 0xa0(%r13)
movq %rax, 0x10(%rsp)
movl %ecx, 0xa0(%rax)
leaq 0x18(%r15), %rsi
movq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x4321a
movsd %xmm0, (%rsp)
leaq 0x18(%r12), %rsi
movq %r14, %rdi
callq 0x4321a
movsd %xmm0, 0x8(%rsp)
leaq 0x18(%rbp), %rsi
movq %r14, %rdi
callq 0x4321a
movsd (%rsp), %xmm2
movapd %xmm2, %xmm3
movsd 0x8(%rsp), %xmm1
movapd %xmm1, %xmm4
addsd %xmm1, %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm1
leaq 0xc5592(%rip), %rax     # 0xdaba8
ucomisd (%rax), %xmm1
jbe 0x15768
addsd %xmm0, %xmm4
divsd %xmm1, %xmm4
movsd %xmm4, 0x8(%rsp)
addsd %xmm0, %xmm3
divsd %xmm1, %xmm3
movsd %xmm3, (%rsp)
divsd %xmm1, %xmm2
movsd %xmm2, 0x18(%rsp)
leaq 0x48(%r15), %rsi
movq %rbx, 0x20(%rsp)
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movapd %xmm4, %xmm0
callq 0x5f833
leaq 0x48(%r12), %rsi
movq %r15, %r13
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movsd (%rsp), %xmm0
callq 0x5f833
leaq 0xb8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5f7a1
leaq 0x48(%rbp), %rsi
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x5f833
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x5f7a1
movq %r12, %rbx
movq 0x40(%rsp), %r12
leaq 0x48(%r12), %rdi
movq %r14, %rsi
callq 0x5f6c4
addq $0x58, %r13
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x223f6
addq $0x58, %rbx
leaq 0x88(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movsd (%rsp), %xmm0
callq 0x223f6
leaq 0xb8(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x213f3
addq $0x58, %rbp
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
movsd 0x18(%rsp), %xmm0
callq 0x223f6
leaq 0xd0(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq 0x20(%rsp), %rbx
callq 0x213f3
leaq 0x58(%r12), %rdi
movq %r14, %rsi
callq 0x66212
movq 0x58(%rsp), %rax
testq %rax, %rax
je 0x1577a
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0x60(%rsp), %rcx
testq %rcx, %rcx
movq 0x68(%rsp), %rax
je 0x1578c
movq %rbx, (%rcx)
testq %rax, %rax
je 0x15799
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
movq 0x40(%rsp), %rax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq