// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Ray const&, gaps::R3Cone const&, gaps::R3Point*, gaps::R3Vector*, double*)
RNClassID R3Intersects(const R3Ray& ray, const R3Cone& cone, 
    R3Point *hit_point1, R3Vector *hit_normal1, RNScalar *hit_t1)
{
    // Check if sphere contains ray start point
    RNBoolean start_inside = R3Contains(cone, ray.Start());
    if (start_inside) {
      if (hit_point1) *hit_point1 = ray.Start();
      if (hit_normal1) *hit_normal1 = -ray.Vector();
      if (hit_t1) *hit_t1 = 0;
      return R3_SPAN_CLASS_ID;
    }

    // Transform ray into canonical coordinate system with
    // axis end point at origin and axis going along negative z axis
    R3Ray ray1(ray);
    R3Affine xform(R4identity_matrix);
    xform.Translate(- (cone.Axis().End().Vector()) );
    xform.Rotate(cone.Axis().Vector(), R3negz_vector);
    ray1.Transform(xform);

    // Compute coefficients for quadratic formula
    RNScalar r = cone.Radius();
    const R3Point& p = ray1.Start();
    const R3Vector& v = ray1.Vector();
    RNScalar A = v.X()*v.X() + v.Y()*v.Y() - r*r*v.Z()*v.Z();
    RNScalar B = 2.0*p.X()*v.X() + 2.0*p.Y()*v.Y() - 2.0*r*r*p.Z()*v.Z();
    RNScalar C = p.X()*p.X() + p.Y()*p.Y() - r*r*p.Z()*p.Z();

    // Solve for t1 and t2 on sides
    RNScalar ray_t;
    if (RNIsZero(A)) {
        if (RNIsZero(B)) {
	    // No intersection
	    return RN_NULL_CLASS_ID;
	}
	else {
	    // ???
	    ray_t = -C/B;
	}
    }
    else {
        RNScalar sqr = B*B - 4.0*A*C;
	if (RNIsNegative(sqr)) {
	    // No intersection
	    return RN_NULL_CLASS_ID;
	}
	else if (RNIsPositive(sqr)) {
	    // Intersection at two points
	    RNScalar s = sqrt(sqr);
	    RNScalar ray_t1 = 0.5 * (-B - s) / A;
	    RNScalar ray_t2 = 0.5 * (-B + s) / A;
	    ray_t = (ray_t1 < ray_t2) ? ray_t1 : ray_t2;
	}
	else {
	    // Intersection at one point
	    ray_t = -0.5 * B / A;
	}
    }

    // Check whether intersection point is on right side of ray
    if (RNIsNegative(ray_t)) return RN_NULL_CLASS_ID;

    // Check whether intersection point is on right side of double cone or beyond cap
    R3Point p1 = ray1.Point(ray_t);
    if (RNIsNegative(p1.Z())) return RN_NULL_CLASS_ID;
    if (RNIsGreater(p1.Z(), cone.Axis().Length())) return RN_NULL_CLASS_ID;

    // Check for intersection with base
    if (RNIsGreater(ray1.Start().Z(), cone.Axis().Length())) {
        // ???
    }

    // This is not complete ???

    // Transform intersection back to world coordinates
    R3Point intersection = ray.Point(ray_t);
    intersection.InverseTransform(xform);

    // Ray hit cone
    if (hit_t1) *hit_t1 = ray_t;
    if (hit_point1) { *hit_point1 = intersection; }
    if (hit_normal1) { *hit_normal1 = R3zero_vector; }
    return R3_POINT_CLASS_ID;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %r12
movq %rsi, %rdi
movq %r12, %rsi
callq 0x42e82
testl %eax, %eax
je 0x3f096
testq %r15, %r15
je 0x3f05a
movq %r15, %rdi
movq %r12, %rsi
callq 0x37e36
testq %rbx, %rbx
je 0x3f081
addq $0x18, %r12
leaq 0x88(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
callq 0x38c72
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3887c
pushq $0x4
popq %rbp
testq %r14, %r14
je 0x3f377
andq $0x0, (%r14)
jmp 0x3f377
leaq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x37758
leaq 0x54d46(%rip), %rsi     # 0x93df0
leaq 0x88(%rsp), %rdi
xorl %edx, %edx
callq 0x276a0
leaq 0x38(%r13), %rsi
leaq 0x10(%rsp), %rdi
callq 0x212f8
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x38c72
leaq 0x88(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x212e2
leaq 0x20(%r13), %rsi
leaq 0x5a645(%rip), %rdx     # 0x99738
leaq 0x88(%rsp), %rdi
callq 0x21da2
leaq 0x28(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x37812
movsd 0x78(%r13), %xmm0
movsd 0x40(%rsp), %xmm3
movsd 0x48(%rsp), %xmm2
movsd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm4
addsd %xmm1, %xmm4
mulsd %xmm2, %xmm4
movapd %xmm2, %xmm5
mulsd %xmm2, %xmm5
movsd 0x28(%rsp), %xmm2
movapd %xmm2, %xmm6
addsd %xmm2, %xmm6
mulsd %xmm3, %xmm6
mulsd %xmm3, %xmm3
addsd %xmm5, %xmm3
movsd 0x50(%rsp), %xmm5
movapd 0x53ec9(%rip), %xmm8    # 0x93030
movapd %xmm0, %xmm7
xorpd %xmm8, %xmm7
mulsd %xmm0, %xmm7
movapd %xmm7, %xmm9
mulsd %xmm5, %xmm9
mulsd %xmm5, %xmm9
addsd %xmm3, %xmm9
addsd %xmm4, %xmm6
movsd 0x38(%rsp), %xmm3
movsd 0x54066(%rip), %xmm4    # 0x93200
mulsd %xmm0, %xmm4
mulsd %xmm0, %xmm4
mulsd %xmm3, %xmm4
mulsd %xmm5, %xmm4
addsd %xmm6, %xmm4
mulsd %xmm1, %xmm1
mulsd %xmm2, %xmm2
addsd %xmm1, %xmm2
mulsd %xmm3, %xmm7
mulsd %xmm3, %xmm7
addsd %xmm2, %xmm7
leaq 0x9b9db(%rip), %rbp     # 0xdaba8
movsd (%rbp), %xmm2
movapd %xmm2, %xmm1
xorpd %xmm8, %xmm1
ucomisd %xmm1, %xmm9
jb 0x3f20a
ucomisd %xmm9, %xmm2
jb 0x3f20a
ucomisd %xmm1, %xmm4
jb 0x3f1f9
ucomisd %xmm4, %xmm2
jae 0x3f368
xorpd 0x53e2f(%rip), %xmm7    # 0x93030
divsd %xmm4, %xmm7
jmp 0x3f2bd
movsd 0x5acee(%rip), %xmm3    # 0x99f00
mulsd %xmm9, %xmm3
mulsd %xmm7, %xmm3
movapd %xmm4, %xmm0
mulsd %xmm4, %xmm0
addsd %xmm3, %xmm0
ucomisd %xmm0, %xmm1
ja 0x3f368
ucomisd %xmm2, %xmm0
ja 0x3f24a
mulsd 0x54021(%rip), %xmm4    # 0x93260
divsd %xmm9, %xmm4
movapd %xmm4, %xmm7
jmp 0x3f2bd
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x3f25a
sqrtsd %xmm0, %xmm0
jmp 0x3f280
movapd %xmm9, (%rsp)
movapd %xmm4, 0x70(%rsp)
callq 0xe350
movapd 0x70(%rsp), %xmm4
movapd (%rsp), %xmm9
movapd 0x53db0(%rip), %xmm8    # 0x93030
movapd %xmm4, %xmm7
xorpd %xmm8, %xmm7
unpcklpd %xmm0, %xmm7            # xmm7 = xmm7[0],xmm0[0]
unpcklpd %xmm4, %xmm0            # xmm0 = xmm0[0],xmm4[0]
subpd %xmm0, %xmm7
mulpd 0x53da3(%rip), %xmm7    # 0x93040
unpcklpd %xmm9, %xmm9            # xmm9 = xmm9[0,0]
divpd %xmm9, %xmm7
movapd %xmm7, %xmm0
unpckhpd %xmm7, %xmm0            # xmm0 = xmm0[1],xmm7[1]
minsd %xmm0, %xmm7
movsd (%rbp), %xmm1
xorpd %xmm8, %xmm1
ucomisd %xmm7, %xmm1
ja 0x3f368
leaq 0x58(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movapd %xmm7, (%rsp)
movapd %xmm7, %xmm0
callq 0x37770
movsd 0x68(%rsp), %xmm0
movsd (%rbp), %xmm1
movapd 0x53d3e(%rip), %xmm2    # 0x93030
xorpd %xmm1, %xmm2
xorl %ebp, %ebp
ucomisd %xmm0, %xmm2
ja 0x3f36a
subsd 0x50(%r13), %xmm0
ucomisd %xmm1, %xmm0
ja 0x3f36a
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movapd (%rsp), %xmm0
callq 0x37770
leaq 0x10(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x381a4
testq %r14, %r14
je 0x3f33d
movapd (%rsp), %xmm0
movsd %xmm0, (%r14)
testq %r15, %r15
je 0x3f34f
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x37e36
testq %rbx, %rbx
je 0x3f363
leaq 0x5a335(%rip), %rsi     # 0x99690
movq %rbx, %rdi
callq 0x3887c
pushq $0x1
popq %rbp
jmp 0x3f36a
xorl %ebp, %ebp
leaq 0x88(%rsp), %rdi
callq 0x276ee
movl %ebp, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3f391
jmp 0x3f391
jmp 0x3f391
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x276ee
movq %rbx, %rdi
callq 0xe810