// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Mesh.cpp
// Function: gaps::R3Mesh::TracePath(gaps::R3MeshVertex const*, gaps::R3Vector const&, double, gaps::R3Point*, gaps::R3MeshFace**, gaps::R3MeshIntersection*, int*) const
RNLength R3Mesh::
TracePath(const R3MeshVertex *source_vertex, const R3Vector& tangent_direction, RNLength max_distance,
  R3Point *final_position, R3MeshFace **final_face, R3MeshIntersection *intersections, int *nintersections) const
{
  // Increment mesh mark (used to avoid visiting same edge/face twice in recursive function)
  R3Mesh *mesh = (R3Mesh *) this;
  R3mesh_mark++;

  // Initialize result
  if (final_position) *final_position = VertexPosition(source_vertex);
  if (final_face) *final_face = NULL;
  if (nintersections) *nintersections = 0;
  if (max_distance <= 0) return 0;

  // Get convenient variables
  const R3Point& source_position = VertexPosition(source_vertex);
  R3Vector source_normal = VertexNormal(source_vertex);
  R3Vector split_direction = tangent_direction % source_normal;
  RNLength split_direction_length = split_direction.Length();
  if (split_direction_length == 0) return 0;
  split_direction /= split_direction_length;
  R3Plane split_plane(source_position, split_direction);

  // Find first face, edge, and position
  R3MeshFace *cur_face = NULL;
  R3MeshEdge *cur_edge = NULL;
  R3Point cur_position = R3zero_point;
  for (int i = 0; i < VertexValence(source_vertex); i++) {
    R3MeshEdge *cw_edge = EdgeOnVertex(source_vertex, i);
    R3MeshEdge *ccw_edge = EdgeOnVertex(source_vertex, cw_edge, RN_CCW);
    R3MeshFace *face = FaceOnVertex(source_vertex, cw_edge, RN_CCW);
    if (!face) continue;
    R3MeshEdge *edge = EdgeAcrossFace(face, source_vertex);

    // Check position of cw_vertex
    R3MeshVertex *cw_vertex = VertexAcrossEdge(cw_edge, source_vertex);
    const R3Point& cw_position = VertexPosition(cw_vertex);
    RNScalar cw_d = R3SignedDistance(split_plane, cw_position);
    if (cw_d == 0) {
      cur_position = cw_position;
      cur_face = face;
      cur_edge = edge;
      break;
    }

    // Check position of ccw_vertex
    R3MeshVertex *ccw_vertex = VertexAcrossEdge(ccw_edge, source_vertex);
    const R3Point& ccw_position = VertexPosition(ccw_vertex);
    RNScalar ccw_d = R3SignedDistance(split_plane, ccw_position);
    if (ccw_d == 0) {
      cur_position = ccw_position;
      cur_face = face;
      cur_edge = edge;
      break;
    }

    // Check if source direction splits cw and ccw vertices
    if ((cw_d > 0)  && (ccw_d < 0)) {
      RNScalar t = cw_d / (cw_d - ccw_d);
      cur_position = cw_position + t * (ccw_position - cw_position);
      cur_face = face;
      cur_edge = edge;
      break;
    }
  }      

  // Get next distance and direction
  RNLength cur_distance = R3Distance(cur_position, source_position);
  R3Vector cur_direction = cur_position - source_position; cur_direction.Normalize();

  // Check if found next face
  if (!cur_face) return 0;

  // Check if max_distance lies within next face
  if (cur_distance >= max_distance) {
    RNScalar t = max_distance / cur_distance;
    if (final_position) *final_position = (1-t)*source_position + t*cur_position;
    if (final_face) *final_face = cur_face;
    return max_distance;
  }

  // Trace path
  while (TRUE) {
    // Mark cur edge and face
    mesh->SetEdgeMark(cur_edge, R3mesh_mark);
    mesh->SetFaceMark(cur_face, R3mesh_mark);

    // Add intersection
    if (intersections && nintersections) {
      intersections[*nintersections].type = R3_MESH_EDGE_TYPE;
      intersections[*nintersections].vertex = NULL;
      intersections[*nintersections].edge = cur_edge;
      intersections[*nintersections].face = cur_face;
      intersections[*nintersections].point = cur_position;
      intersections[*nintersections].t = cur_distance;
      (*nintersections)++;
    }

    // Get next face
    R3MeshFace *next_face = FaceAcrossEdge(cur_edge, cur_face);
    if (!next_face) {
      if (final_position) *final_position = cur_position;
      if (final_face) *final_face = cur_face;
      return cur_distance;
    }
    
    // Get next direction 
    R3Vector cur_face_normal = FaceNormal(cur_face);
    R3Vector next_face_normal = FaceNormal(next_face);
    R4Matrix matrix = R4identity_matrix;
    matrix.Rotate(cur_face_normal, next_face_normal);
    R3Vector next_direction = matrix * cur_direction;
    next_direction.Normalize();
    
    // Get plane splitting next face along next_direction
    R3Vector split_plane_normal = next_face_normal % next_direction;  
    split_plane_normal.Normalize();
    R3Plane split_plane(cur_position, split_plane_normal);
    
    // Get next edge and position
    R3MeshVertex *vertex = VertexAcrossFace(next_face, cur_edge);
    R3Point next_position = VertexPosition(vertex);
    RNScalar d = R3SignedDistance(split_plane, next_position);
    R3MeshEdge *next_edge = EdgeOnFace(next_face, cur_edge, (d < 0) ? RN_CW : RN_CCW);
    if (d != 0) {
      // Split does not hit vertex
      R3Vector next_edge_vector = EdgeVector(next_edge);
      RNScalar denom = split_plane.Normal().Dot(next_edge_vector);
      if (denom != 0) {
        R3Point next_edge_start = VertexPosition(VertexOnEdge(next_edge, 0));
        RNScalar t = -(R3SignedDistance(split_plane, next_edge_start)) / denom;
        next_position = next_edge_start + next_edge_vector * t;
      }
    }

    // Get next distance
    RNLength next_distance = cur_distance + R3Distance(cur_position, next_position);
    if (next_distance >= max_distance) {
      assert(next_distance > cur_distance);
      RNScalar t = (max_distance - cur_distance) / (next_distance - cur_distance);
      if (final_position) *final_position = (1-t)*cur_position + t*next_position;
      if (final_face) *final_face = next_face;
      return max_distance;
    }

    // Go to next face
    cur_edge = next_edge;
    cur_face = next_face;
    cur_position = next_position;
    cur_direction = next_direction;
    cur_distance = next_distance;
  }

  // Return error
  return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %r9, 0x68(%rsp)
movq %r8, %rbx
movq %rcx, %r14
movsd %xmm0, 0x28(%rsp)
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, 0x48(%rsp)
incl 0xc8aa9(%rip)           # 0xe54b0
testq %rcx, %rcx
je 0x1ca18
leaq 0x18(%r15), %rsi
movq %r14, %rdi
callq 0x3cd94
movq %r14, 0x40(%rsp)
movq 0x290(%rsp), %rax
testq %rbx, %rbx
je 0x1ca31
movq $0x0, (%rbx)
testq %rax, %rax
je 0x1ca3c
movl $0x0, (%rax)
xorpd %xmm0, %xmm0
ucomisd 0x28(%rsp), %xmm0
jae 0x1d21f
testb $0x2, 0x78(%r15)
jne 0x1ca64
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x2b0(%rax)
leaq 0x30(%r15), %rsi
leaq 0x1c0(%rsp), %r13
movq %r13, %rdi
callq 0x3d4ce
leaq 0x148(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3de12
movq %rbp, %rdi
callq 0x3d5c4
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
movsd %xmm1, 0x18(%rsp)
ucomisd %xmm2, %xmm0
jne 0x1cab0
jnp 0x1d219
movq %rbx, 0x38(%rsp)
leaq 0x18(%r15), %rbx
leaq 0x148(%rsp), %r12
movq %r12, %rdi
callq 0x3e0ec
leaq 0xe0(%rsp), %rdi
movq %rbx, 0x18(%rsp)
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3b5a6
leaq 0x4c9f78(%rip), %rsi    # 0x4e6a60
leaq 0x50(%rsp), %rdi
callq 0x3ca36
cmpl $0x0, 0x14(%r15)
jle 0x1ccc7
xorl %r14d, %r14d
xorl %edx, %edx
xorl %esi, %esi
movq 0x8(%r15), %rax
movq (%rax,%r14,8), %rax
movq 0x8(%rax), %rbp
xorl %ecx, %ecx
cmpq %r15, %rbp
setne %cl
movq 0x18(%rax,%rcx,8), %r12
testq %r12, %r12
je 0x1cb37
cmpq %r15, 0x8(%r12)
je 0x1cb3c
cmpq %r15, 0x10(%r12)
je 0x1cb43
cmpq %r15, 0x18(%r12)
je 0x1cb4a
xorl %r13d, %r13d
jmp 0x1cb53
movl $0x30, %ecx
jmp 0x1cb4f
movl $0x20, %ecx
jmp 0x1cb4f
movl $0x28, %ecx
movq (%r12,%rcx), %r13
testq %r12, %r12
je 0x1cb94
movq 0x20(%r12), %rbx
cmpq %r15, 0x8(%rbx)
je 0x1cb69
cmpq %r15, 0x10(%rbx)
jne 0x1cba0
movq 0x28(%r12), %rbx
cmpq %r15, 0x8(%rbx)
je 0x1cb7a
cmpq %r15, 0x10(%rbx)
jne 0x1cba0
movq 0x30(%r12), %rcx
cmpq %r15, 0x8(%rcx)
je 0x1cb9e
cmpq %r15, 0x10(%rcx)
movl $0x0, %ebx
cmovneq %rcx, %rbx
jmp 0x1cba0
movl $0x4, %eax
jmp 0x1cca5
xorl %ebx, %ebx
movq %rsi, 0x10(%rsp)
movq %rdx, 0x30(%rsp)
cmpq %r15, %rbp
jne 0x1cbb3
movq 0x10(%rax), %rbp
addq $0x18, %rbp
leaq 0xe0(%rsp), %rdi
movq %rbp, %rsi
callq 0x4a10f
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1cbe0
jp 0x1cbe0
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
jmp 0x1cc95
movsd %xmm0, 0x20(%rsp)
movq %r13, %rax
movq 0x8(%r13), %r13
cmpq %r15, %r13
jne 0x1cbf6
movq 0x10(%rax), %r13
addq $0x18, %r13
leaq 0xe0(%rsp), %rdi
movq %r13, %rsi
callq 0x4a10f
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1cc16
jnp 0x1cc8d
xorl %eax, %eax
movsd 0x20(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x1ccbb
ucomisd %xmm0, %xmm1
movq 0x30(%rsp), %rdx
movq 0x10(%rsp), %rsi
jbe 0x1cca5
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x20(%rsp)
leaq 0x78(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3cc88
leaq 0xa8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
movsd 0x20(%rsp), %xmm0
callq 0x3de8e
leaq 0x1d8(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
leaq 0xa8(%rsp), %rdx
callq 0x3cd71
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0x3cd94
movl $0x2, %eax
movq %rbx, %rsi
movq %r12, %rdx
testb $0x3, %al
jne 0x1cccb
incq %r14
movslq 0x14(%r15), %rax
cmpq %rax, %r14
jl 0x1cb04
jmp 0x1cccb
movq 0x30(%rsp), %rdx
movq 0x10(%rsp), %rsi
jmp 0x1cca5
xorl %esi, %esi
xorl %edx, %edx
movq %rsi, %rbx
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %rdx, %r12
movq 0x18(%rsp), %rbp
movq %rbp, %rsi
callq 0x49efc
movsd %xmm0, 0x20(%rsp)
leaq 0x78(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x3cc88
movq %r13, %rdi
callq 0x3d7e0
testq %r12, %r12
je 0x1d17f
movq %r12, %rdx
movsd 0x20(%rsp), %xmm2
ucomisd 0x28(%rsp), %xmm2
jae 0x1d188
cmpq $0x0, 0x290(%rsp)
setne %al
cmpq $0x0, 0x68(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0xf(%rsp)
leaq 0x178(%rsp), %r15
leaq 0x160(%rsp), %r12
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %rbp
movq %rbx, %rsi
movl 0xc874d(%rip), %eax     # 0xe54b0
movl %eax, 0x48(%rsi)
movl %eax, 0xb8(%rdx)
cmpb $0x0, 0xf(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x10(%rsp)
movsd %xmm2, 0x20(%rsp)
je 0x1cde6
movq 0x290(%rsp), %r13
movslq (%r13), %rax
shlq $0x6, %rax
movq 0x68(%rsp), %rbx
leaq (%rbx,%rax), %rdi
addq $0x20, %rdi
movl $0x2, -0x20(%rdi)
movq $0x0, -0x18(%rdi)
movq %rsi, -0x10(%rdi)
movq %rdx, -0x8(%rdi)
leaq 0x50(%rsp), %rsi
callq 0x3cd94
movsd 0x20(%rsp), %xmm2
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %rdx
movslq (%r13), %rax
leal 0x1(%rax), %ecx
shlq $0x6, %rax
movsd %xmm2, 0x38(%rbx,%rax)
movl %ecx, (%r13)
movq 0x18(%rsi), %r13
cmpq %rdx, %r13
jne 0x1cdf3
movq 0x20(%rsi), %r13
testq %r13, %r13
je 0x1cf6b
movq %r12, %rbp
testb $0x4, 0xa8(%rdx)
jne 0x1ce20
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq 0x30(%rsp), %rsi
callq *0x2c8(%rax)
movq 0x30(%rsp), %rdx
movq %r15, %r12
leaq 0x38(%rdx), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x3d4ce
testb $0x4, 0xa8(%r13)
jne 0x1ce4f
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x2c8(%rax)
leaq 0x38(%r13), %rsi
leaq 0x1a8(%rsp), %r14
movq %r14, %rdi
callq 0x3d4ce
leaq 0x1d8(%rsp), %r15
movq %r15, %rdi
leaq 0x85fcb(%rip), %rsi     # 0xa2e40
callq 0x2c5ba
movq %r15, %rdi
leaq 0xc8(%rsp), %rsi
movq %r14, %rdx
callq 0x2dee0
leaq 0x130(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
leaq 0x78(%rsp), %rdx
callq 0x2e63e
movq %rbx, %rdi
callq 0x3d7e0
leaq 0x190(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3de12
movq %r15, %rdi
callq 0x3d7e0
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x50(%rsp), %rsi
movq %r15, %rdx
callq 0x3b5a6
movq 0x8(%r13), %rsi
movq 0x10(%rsp), %rcx
movq 0x8(%rcx), %rax
cmpq %rsi, %rax
movq %r12, %r15
movq %rbp, %r12
movq 0x40(%rsp), %rbp
je 0x1cf06
cmpq %rsi, 0x10(%rcx)
jne 0x1cf21
movq 0x10(%r13), %rsi
cmpq %rsi, %rax
je 0x1cf15
cmpq %rsi, 0x10(%rcx)
jne 0x1cf21
movq 0x18(%r13), %rsi
cmpq %rsi, 0x10(%rcx)
jne 0x1cf21
xorl %esi, %esi
addq $0x18, %rsi
movq %r15, %rdi
callq 0x3ca36
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4a10f
movq 0x10(%rsp), %rcx
movq 0x20(%r13), %r14
cmpq %rcx, %r14
je 0x1cfa2
movq 0x28(%r13), %rax
xorpd %xmm1, %xmm1
cmpq %rcx, %rax
je 0x1cfb2
ucomisd %xmm0, %xmm1
jbe 0x1cf5c
movq %rax, %r14
cmpq %rcx, 0x30(%r13)
movl $0x0, %eax
cmovneq %rax, %r14
jmp 0x1cfbc
testq %rbp, %rbp
je 0x1cf8d
movq %rbp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x3cd94
movsd 0x20(%rsp), %xmm2
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %rdx
testq %r14, %r14
je 0x1cf95
movq %rdx, (%r14)
xorl %ebx, %ebx
movsd %xmm2, 0x18(%rsp)
jmp 0x1d172
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x1cfb8
movq 0x28(%r13), %r14
jmp 0x1cfbc
ucomisd %xmm0, %xmm1
ja 0x1cfbc
movq 0x30(%r13), %r14
ucomisd 0x85224(%rip), %xmm0    # 0xa21e8
jne 0x1cfcc
jnp 0x1d073
movq 0x8(%r14), %rdx
movq 0x10(%r14), %rsi
addq $0x18, %rsi
addq $0x18, %rdx
movq %r12, %rdi
callq 0x3cc88
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3d756
ucomisd 0x851f1(%rip), %xmm0    # 0xa21e8
jne 0x1cffb
jnp 0x1d073
movq 0x8(%r14), %rsi
addq $0x18, %rsi
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
movsd %xmm0, 0x70(%rsp)
callq 0x3ca36
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x4a10f
xorpd 0x84ff4(%rip), %xmm0    # 0xa2020
divsd 0x70(%rsp), %xmm0
leaq 0x100(%rsp), %rdi
movq %r12, %rsi
callq 0x3de8e
movq %r12, %rbx
leaq 0x118(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
movq 0x40(%rsp), %rbp
leaq 0x100(%rsp), %rdx
callq 0x3cd71
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %r12
callq 0x3cd94
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x49efc
movsd 0x20(%rsp), %xmm2
addsd %xmm2, %xmm0
ucomisd 0x28(%rsp), %xmm0
movq %rbx, %rdi
setb %bl
jae 0x1d0d1
movq %r15, %rsi
movsd %xmm0, 0x10(%rsp)
callq 0x3cd94
leaq 0x78(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x3dc6a
movsd 0x10(%rsp), %xmm2
movq %r14, %rsi
movq %r13, %rdx
movq 0x38(%rsp), %r14
jmp 0x1d172
testq %rbp, %rbp
je 0x1d14f
movsd 0x28(%rsp), %xmm1
subsd %xmm2, %xmm1
subsd %xmm2, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movsd 0x84f42(%rip), %xmm0    # 0xa2038
subsd %xmm1, %xmm0
leaq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x3d470
movq %r12, %r14
leaq 0x118(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movsd 0x18(%rsp), %xmm0
callq 0x3d470
movq %r14, %rdi
leaq 0x90(%rsp), %rsi
movq %r12, %rdx
movq %r14, %r12
callq 0x3d42a
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3cd94
movsd 0x20(%rsp), %xmm2
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x1d15c
movq %r13, (%r14)
movsd 0x28(%rsp), %xmm1
movsd %xmm1, 0x18(%rsp)
movq 0x30(%rsp), %rdx
movq 0x10(%rsp), %rsi
testb %bl, %bl
jne 0x1cd5d
jmp 0x1d219
xorpd %xmm0, %xmm0
jmp 0x1d213
movq 0x40(%rsp), %r14
testq %r14, %r14
movq 0x38(%rsp), %rbx
je 0x1d205
movsd 0x28(%rsp), %xmm1
divsd %xmm2, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x84e89(%rip), %xmm0    # 0xa2038
subsd %xmm1, %xmm0
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3d470
leaq 0xc8(%rsp), %r13
leaq 0x50(%rsp), %rsi
movq %r13, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x3d470
leaq 0x1d8(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x3d42a
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3cd94
movq %r12, %rdx
testq %rbx, %rbx
je 0x1d20d
movq %rdx, (%rbx)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop