// File: /workspace/github_cpp_makefile_star2_O2/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 $0x248, %rsp            # imm = 0x248
movq %r9, %rbp
movq %r8, %r13
movq %rcx, %r15
movapd %xmm0, %xmm1
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
incl 0xbfc21(%rip)           # 0xda4c0
testq %rcx, %rcx
movsd %xmm0, 0x20(%rsp)
je 0x1a8bc
leaq 0x18(%r14), %rsi
movq %r15, %rdi
callq 0x37e36
movsd 0x20(%rsp), %xmm1
movq 0x280(%rsp), %rax
testq %r13, %r13
je 0x1a8ce
andq $0x0, (%r13)
testq %rax, %rax
je 0x1a8d6
andl $0x0, (%rax)
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jae 0x1af70
movq %rbp, 0x58(%rsp)
movq %r15, 0x38(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0x21234
leaq 0x1b0(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x3831c
movq %r12, %rbp
leaq 0x150(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x389be
movq %r12, %rdi
callq 0x38412
movapd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jne 0x1a941
jnp 0x1af70
movq %rbp, 0x18(%rsp)
movq %r13, 0x30(%rsp)
leaq 0x18(%r14), %r15
leaq 0x150(%rsp), %rbx
movq %rbx, %rdi
movapd %xmm1, %xmm0
callq 0x38c0a
leaq 0xa0(%rsp), %rdi
movq %r15, 0x28(%rsp)
movq %r15, %rsi
movq %rbx, %rdx
callq 0x367ac
leaq 0x4c10ee(%rip), %rsi    # 0x4dba70
leaq 0x40(%rsp), %rdi
callq 0x37b5c
xorl %r13d, %r13d
movslq 0x14(%r14), %rax
cmpq %rax, %r13
jge 0x1aa62
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %r12
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0x2134a
xorl %ecx, %ecx
cmpq %r14, 0x8(%r12)
setne %cl
movq 0x18(%r12,%rcx,8), %rbp
testq %rbp, %rbp
je 0x1aa5a
movq %rax, %r15
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x221a0
movq %rax, 0x10(%rsp)
movq 0x8(%r12), %rbx
cmpq %r14, %rbx
jne 0x1a9f5
movq 0x10(%r12), %rbx
addq $0x18, %rbx
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
callq 0x43420
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1aa15
jnp 0x1aa69
movsd %xmm0, 0x8(%rsp)
movq 0x8(%r15), %r12
cmpq %r14, %r12
jne 0x1aa28
movq 0x10(%r15), %r12
addq $0x18, %r12
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
callq 0x43420
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1aa48
jnp 0x1aa73
movsd 0x8(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x1aa5a
ucomisd %xmm0, %xmm1
ja 0x1aa7d
incq %r13
jmp 0x1a98f
xorl %r13d, %r13d
xorl %ebp, %ebp
jmp 0x1aae3
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
jmp 0x1aad9
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
jmp 0x1aad9
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, 0x8(%rsp)
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x37d29
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x38a02
leaq 0x1c8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x37e13
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x37e36
movq 0x10(%rsp), %r13
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x28(%rsp), %r15
movq %r15, %rsi
callq 0x4321a
movsd %xmm0, 0x8(%rsp)
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x37d29
movq %r14, %rdi
callq 0x3860e
testq %rbp, %rbp
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %r12
je 0x1ae16
movsd 0x20(%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
ucomisd %xmm2, %xmm1
jae 0x1ae1f
cmpq $0x0, 0x280(%rsp)
setne %al
cmpq $0x0, 0x58(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0x7(%rsp)
movl 0xbf95d(%rip), %eax     # 0xda4c0
movl %eax, 0x48(%r13)
movl %eax, 0xb8(%rbp)
cmpb $0x0, 0x7(%rsp)
movsd %xmm1, 0x8(%rsp)
je 0x1abd9
movq 0x280(%rsp), %rax
movq %r13, %r12
movq %rax, %r13
movslq (%rax), %rax
shlq $0x6, %rax
movq 0x58(%rsp), %r15
leaq (%r15,%rax), %rdi
addq $0x20, %rdi
movl $0x2, -0x20(%rdi)
andq $0x0, -0x18(%rdi)
movq %r12, -0x10(%rdi)
movq %rbp, -0x8(%rdi)
leaq 0x40(%rsp), %rsi
callq 0x37e36
movsd 0x8(%rsp), %xmm1
movslq (%r13), %rax
leal 0x1(%rax), %ecx
shlq $0x6, %rax
movsd %xmm1, 0x38(%r15,%rax)
movl %ecx, (%r13)
movq %r12, %r13
movq 0x18(%r13), %rbx
cmpq %rbp, %rbx
jne 0x1abe6
movq 0x20(%r13), %rbx
testq %rbx, %rbx
je 0x1aea8
movq %r13, 0x10(%rsp)
movq 0x18(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x10dd0
leaq 0x138(%rsp), %rbp
movq %rbp, %rdi
movq %rax, %rsi
callq 0x3831c
movq %r13, %rdi
movq %rbx, %rsi
callq 0x10dd0
leaq 0x198(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x3831c
leaq 0x1c8(%rsp), %r14
movq %r14, %rdi
leaq 0x791a9(%rip), %rsi     # 0x93df0
callq 0x2967c
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x2aa9a
leaq 0x120(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
leaq 0x68(%rsp), %rdx
callq 0x2b193
movq %rbp, %rdi
callq 0x3860e
leaq 0x180(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
leaq 0x80(%rsp), %r14
leaq 0x108(%rsp), %r15
movq %rbp, %rdx
callq 0x389be
movq %r12, %rdi
callq 0x3860e
movq %r14, %rdi
leaq 0x40(%rsp), %rbp
movq %rbp, %rsi
movq %r12, %rdx
callq 0x367ac
movq %r13, %rdi
movq %rbx, %rsi
movq 0x10(%rsp), %r12
movq %r12, %rdx
callq 0x2136e
leaq 0x18(%rax), %rsi
movq %r15, %rdi
callq 0x37b5c
movq %r14, %rdi
movq %r15, %rsi
callq 0x43420
movsd %xmm0, 0x28(%rsp)
xorl %ecx, %ecx
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
seta %cl
movq %r13, %rdi
movq %rbx, 0x60(%rsp)
movq %rbx, %rsi
movq %r12, %rdx
callq 0x21a5e
movq %rax, %r13
movsd 0x28(%rsp), %xmm0
ucomisd 0x784cc(%rip), %xmm0    # 0x931e8
jne 0x1ad24
jnp 0x1adbe
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x213a6
movq %r14, %rdi
movq %r12, %rsi
callq 0x38586
ucomisd 0x78499(%rip), %xmm0    # 0x931e8
jne 0x1ad53
jnp 0x1adbe
movq 0x8(%r13), %rsi
addq $0x18, %rsi
leaq 0xd8(%rsp), %rbx
movq %rbx, %rdi
movsd %xmm0, 0x10(%rsp)
callq 0x37b5c
movq %r14, %rdi
movq %rbx, %rsi
callq 0x43420
xorpd 0x782ac(%rip), %xmm0    # 0x93030
divsd 0x10(%rsp), %xmm0
leaq 0x168(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x38a02
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x37e13
movq %r15, %rdi
movq %r12, %rsi
callq 0x37e36
movq %rbp, %rdi
movq %r15, %rsi
callq 0x4321a
movsd 0x8(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0x20(%rsp), %xmm2
ucomisd %xmm2, %xmm0
jae 0x1aed8
movq %rbp, %rdi
movq %r15, %rsi
movsd %xmm0, 0x10(%rsp)
callq 0x37e36
leaq 0x68(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x3887c
movsd 0x10(%rsp), %xmm1
movq 0x60(%rsp), %rbp
jmp 0x1ab5d
xorpd %xmm2, %xmm2
jmp 0x1af6c
testq %r12, %r12
je 0x1ae97
divsd %xmm1, %xmm2
movsd %xmm2, 0x18(%rsp)
movsd 0x781d2(%rip), %xmm0    # 0x93008
subsd %xmm2, %xmm0
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq 0x28(%rsp), %rsi
movq %rax, %r13
callq 0x382c0
leaq 0x138(%rsp), %r14
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x382c0
leaq 0x1c8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3827a
movq %r12, %rdi
movq %r15, %rsi
callq 0x37e36
movq %r13, %rax
movsd 0x20(%rsp), %xmm2
testq %rax, %rax
je 0x1af6c
movq %rbp, (%rax)
jmp 0x1af6c
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x1aec2
leaq 0x40(%rsp), %rsi
callq 0x37e36
movsd 0x8(%rsp), %xmm1
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x1aecf
movq %rbp, (%rax)
movapd %xmm1, %xmm2
jmp 0x1af6c
movq 0x38(%rsp), %rbp
testq %rbp, %rbp
je 0x1af5a
subsd %xmm1, %xmm2
subsd %xmm1, %xmm0
divsd %xmm0, %xmm2
movsd %xmm2, 0x18(%rsp)
movsd 0x7810c(%rip), %xmm0    # 0x93008
subsd %xmm2, %xmm0
leaq 0xd8(%rsp), %rbx
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x382c0
leaq 0xc0(%rsp), %r14
leaq 0x108(%rsp), %rsi
movq %r14, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x382c0
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3827a
movq %rbp, %rdi
movq %r12, %rsi
callq 0x37e36
movsd 0x20(%rsp), %xmm2
movq 0x30(%rsp), %rax
testq %rax, %rax
je 0x1af6c
movq 0x60(%rsp), %rcx
movq %rcx, (%rax)
movapd %xmm2, %xmm0
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq