// File: /workspace/github_cpp_makefile_star2_O1/GUI-Graphics[P]cpuraytracing/sphere.cpp
// Function: gui::Sphere::intersect(gui::Ray const&, float, float, gui::Intersection&) const
bool Sphere::intersect(
		const Ray& ray,
		float tMin,
		float tMax,
		Intersection& intersection) const {
			
			++Renderer::nIntersects;

			Vector3 co = ray.origin - center;
			float a = ray.direction.lengthSquared();
			float b = ray.direction.dot(co);
			float c = co.lengthSquared() - radius * radius;
			float delta = b * b - a * c;

			if(delta < 0) return false;

			delta = sqrt(delta);
			float t = (-b - delta) / a;

			if(t > tMin && t < tMax) {
				intersection.t = t;
				intersection.position = ray.origin + ray.direction * t;
				intersection.normal = (intersection.position - center) / radius;
				intersection.material = material;
				return true;
			}

			t = (-b + delta) / a;
			if(t > tMin && t < tMax) {
				intersection.t = t;
				intersection.position = ray.origin + ray.direction * t;
				intersection.normal = (intersection.position - center) / radius;
				intersection.material = material;
				return true;
			}

			return false;
	}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x20, %rsp
leaq 0x5e6c(%rip), %rax      # 0x9868
incq (%rax)
movss (%rsi), %xmm2
movss 0x4(%rsi), %xmm3
subss 0x10(%rdi), %xmm2
subss 0x14(%rdi), %xmm3
movss 0x8(%rsi), %xmm7
subss 0x18(%rdi), %xmm7
movaps %xmm0, %xmm8
movss 0xc(%rsi), %xmm0
movss 0x10(%rsi), %xmm4
movaps %xmm4, %xmm5
mulss %xmm4, %xmm5
movaps %xmm0, %xmm6
mulss %xmm0, %xmm6
addss %xmm5, %xmm6
movss 0x14(%rsi), %xmm9
movaps %xmm9, %xmm5
mulss %xmm9, %xmm5
addss %xmm6, %xmm5
mulss %xmm3, %xmm4
mulss %xmm2, %xmm0
addss %xmm4, %xmm0
mulss %xmm7, %xmm9
addss %xmm0, %xmm9
mulss %xmm3, %xmm3
mulss %xmm2, %xmm2
addss %xmm3, %xmm2
mulss %xmm7, %xmm7
addss %xmm2, %xmm7
movss 0x1c(%rdi), %xmm0
mulss %xmm0, %xmm0
subss %xmm0, %xmm7
mulss %xmm5, %xmm7
movaps %xmm9, %xmm0
mulss %xmm9, %xmm0
subss %xmm7, %xmm0
xorps %xmm2, %xmm2
ucomiss %xmm0, %xmm2
jbe 0x3aa6
xorl %eax, %eax
jmp 0x3bb7
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
ucomiss %xmm2, %xmm0
jb 0x3aba
sqrtss %xmm0, %xmm0
jmp 0x3af1
movss %xmm1, 0xc(%rsp)
movss %xmm8, 0x8(%rsp)
movss %xmm5, 0x4(%rsp)
movaps %xmm9, 0x10(%rsp)
callq 0x2250
movaps 0x10(%rsp), %xmm9
movss 0x4(%rsp), %xmm5
movss 0x8(%rsp), %xmm8
movss 0xc(%rsp), %xmm1
movaps 0x1628(%rip), %xmm4     # 0x5120
xorps %xmm9, %xmm4
subss %xmm0, %xmm4
divss %xmm5, %xmm4
ucomiss %xmm8, %xmm4
jbe 0x3b0f
ucomiss %xmm4, %xmm1
ja 0x3b30
subss %xmm9, %xmm0
movaps %xmm0, %xmm4
divss %xmm5, %xmm4
xorl %eax, %eax
ucomiss %xmm8, %xmm4
jbe 0x3bb7
ucomiss %xmm4, %xmm1
jbe 0x3bb7
movss %xmm4, (%rbx)
movss 0xc(%r15), %xmm0
mulss %xmm4, %xmm0
movss 0x10(%r15), %xmm2
mulss %xmm4, %xmm2
mulss 0x14(%r15), %xmm4
addss (%r15), %xmm0
addss 0x4(%r15), %xmm2
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
addss 0x8(%r15), %xmm4
movlps %xmm0, 0x4(%rbx)
movss %xmm4, 0xc(%rbx)
movss 0x4(%rbx), %xmm0
movss 0x8(%rbx), %xmm2
subss 0x10(%r14), %xmm0
subss 0x14(%r14), %xmm2
subss 0x18(%r14), %xmm4
movss 0x1481(%rip), %xmm3     # 0x5010
divss 0x1c(%r14), %xmm3
mulss %xmm3, %xmm0
mulss %xmm3, %xmm2
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
mulss %xmm4, %xmm3
movlps %xmm0, 0x10(%rbx)
movss %xmm3, 0x18(%rbx)
movq 0x8(%r14), %rax
movq %rax, 0x20(%rbx)
movb $0x1, %al
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
nop