// File: /workspace/github_cpp_makefile_star2_O3/FreeSlave[P]vhlt/hlrad/lerp.cpp
// Function: CalcAdaptedSpot(localtriangulation_t const*, float const*, int, float*)
static bool CalcAdaptedSpot (const localtriangulation_t *lt, const vec3_t position, int surface, vec3_t spot)
	// If the surface formed by the face and its neighbor faces is not flat, the surface should be unfolded onto the face plane
	// CalcAdaptedSpot calculates the coordinate of the unfolded spot on the face plane from the original position on the surface
	// CalcAdaptedSpot(center) = {0,0,0}
	// CalcAdaptedSpot(position on the face plane) = position - center
	// Param position: must include g_face_offset
{
	int i;
	vec_t dot;
	vec3_t surfacespot;
	vec_t dist;
	vec_t dist2;
	vec3_t phongnormal;
	vec_t frac;
	vec3_t middle;
	vec3_t v;
	
	for (i = 0; i < (int)lt->neighborfaces.size (); i++)
	{
		if (lt->neighborfaces[i] == surface)
		{
			break;
		}
	}
	if (i == (int)lt->neighborfaces.size ())
	{
		VectorClear (spot);
		return false;
	}

	VectorSubtract (position, lt->center, surfacespot);
	dot = DotProduct (surfacespot, lt->normal);
	VectorMA (surfacespot, -dot, lt->normal, spot);

	// use phong normal instead of face normal, because phong normal is a continuous function
	GetPhongNormal (surface, position, phongnormal);
	dot = DotProduct (spot, phongnormal);
	if (fabs (dot) > ON_EPSILON)
	{
		frac = DotProduct (surfacespot, phongnormal) / dot;
		frac = qmax (0, qmin (frac, 1)); // to correct some extreme cases
	}
	else
	{
		frac = 0;
	}
	VectorScale (spot, frac, middle);

	dist = VectorLength (spot);
	VectorSubtract (surfacespot, middle, v);
	dist2 = VectorLength (middle) + VectorLength (v);

	if (dist > ON_EPSILON && fabs (dist2 - dist) > ON_EPSILON)
	{
		VectorScale (spot, dist2 / dist, spot);
	}
	return true;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rcx, %rbx
movq 0x58(%rdi), %rcx
movq 0x60(%rdi), %rax
subq %rcx, %rax
shrq $0x2, %rax
testl %eax, %eax
jle 0xe8cb
movl %eax, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorl %r8d, %r8d
cmpl %edx, (%rcx,%r8,4)
je 0xe8ce
incq %r8
cmpq %r8, %r9
jne 0xe8bb
jmp 0xe8d3
xorl %r8d, %r8d
cmpl %eax, %r8d
jne 0xe8e8
movl $0x0, 0x8(%rbx)
movq $0x0, (%rbx)
xorl %eax, %eax
jmp 0xeb0b
movss (%rsi), %xmm5
movss 0x4(%rsi), %xmm3
subss 0x38(%rdi), %xmm5
subss 0x3c(%rdi), %xmm3
movss 0x8(%rsi), %xmm4
subss 0x40(%rdi), %xmm4
movss 0x44(%rdi), %xmm0
movss 0x48(%rdi), %xmm1
mulss %xmm3, %xmm1
movaps %xmm5, %xmm2
mulss %xmm0, %xmm2
addss %xmm1, %xmm2
movss 0x4c(%rdi), %xmm1
mulss %xmm4, %xmm1
addss %xmm2, %xmm1
mulss %xmm1, %xmm0
movss %xmm5, (%rsp)
movaps %xmm5, %xmm2
subss %xmm0, %xmm2
movss %xmm2, (%rbx)
movss 0x48(%rdi), %xmm0
mulss %xmm1, %xmm0
movss %xmm3, 0x8(%rsp)
movaps %xmm3, %xmm2
subss %xmm0, %xmm2
movss %xmm2, 0x4(%rbx)
mulss 0x4c(%rdi), %xmm1
movss %xmm4, 0x4(%rsp)
movaps %xmm4, %xmm0
subss %xmm1, %xmm0
movss %xmm0, 0x8(%rbx)
leaq 0xc(%rsp), %r14
movl %edx, %edi
movq %r14, %rdx
callq 0x19ea1
movss (%r14), %xmm3
movss 0x4(%r14), %xmm5
movsd (%rbx), %xmm0
movaps %xmm0, %xmm2
shufps $0x55, %xmm0, %xmm2     # xmm2 = xmm2[1,1],xmm0[1,1]
movaps %xmm2, %xmm1
mulss %xmm5, %xmm1
movaps %xmm0, %xmm4
mulss %xmm3, %xmm4
addss %xmm1, %xmm4
movss 0x8(%rbx), %xmm1
movss 0x8(%r14), %xmm6
movaps %xmm1, %xmm7
mulss %xmm6, %xmm7
addss %xmm4, %xmm7
movaps 0x2b97b(%rip), %xmm4    # 0x3a340
andps %xmm7, %xmm4
cvtss2sd %xmm4, %xmm8
xorps %xmm4, %xmm4
ucomisd 0x2bc1f(%rip), %xmm8    # 0x3a5f8
jbe 0xea04
mulss 0x8(%rsp), %xmm5
mulss (%rsp), %xmm3
addss %xmm5, %xmm3
mulss 0x4(%rsp), %xmm6
addss %xmm3, %xmm6
divss %xmm7, %xmm6
minss 0x286a4(%rip), %xmm6    # 0x370a4
maxss %xmm6, %xmm4
movaps %xmm0, %xmm3
mulps %xmm0, %xmm3
cvtps2pd %xmm3, %xmm3
movapd %xmm3, %xmm5
unpckhpd %xmm3, %xmm5            # xmm5 = xmm5[1],xmm3[1]
addsd %xmm3, %xmm5
movaps %xmm1, %xmm3
mulss %xmm1, %xmm3
cvtss2sd %xmm3, %xmm3
addsd %xmm5, %xmm3
sqrtsd %xmm3, %xmm3
cvtsd2ss %xmm3, %xmm3
xorps %xmm5, %xmm5
cvtss2sd %xmm3, %xmm5
movb $0x1, %al
ucomisd 0x2bbb7(%rip), %xmm5    # 0x3a5f8
jbe 0xeb0b
movaps %xmm0, %xmm5
mulss %xmm4, %xmm5
mulss %xmm4, %xmm2
mulss %xmm1, %xmm4
movss (%rsp), %xmm8
subss %xmm5, %xmm8
movss 0x8(%rsp), %xmm6
subss %xmm2, %xmm6
movss 0x4(%rsp), %xmm7
subss %xmm4, %xmm7
mulss %xmm5, %xmm5
cvtss2sd %xmm5, %xmm5
mulss %xmm2, %xmm2
cvtss2sd %xmm2, %xmm2
addsd %xmm5, %xmm2
mulss %xmm4, %xmm4
cvtss2sd %xmm4, %xmm4
addsd %xmm2, %xmm4
xorps %xmm2, %xmm2
sqrtsd %xmm4, %xmm2
mulss %xmm8, %xmm8
xorps %xmm4, %xmm4
cvtss2sd %xmm8, %xmm4
mulss %xmm6, %xmm6
xorps %xmm5, %xmm5
cvtss2sd %xmm6, %xmm5
addsd %xmm4, %xmm5
mulss %xmm7, %xmm7
xorps %xmm4, %xmm4
cvtss2sd %xmm7, %xmm4
addsd %xmm5, %xmm4
sqrtsd %xmm4, %xmm4
addsd %xmm2, %xmm4
xorps %xmm2, %xmm2
cvtsd2ss %xmm4, %xmm2
movaps %xmm2, %xmm4
subss %xmm3, %xmm4
andps 0x2b85c(%rip), %xmm4    # 0x3a340
cvtss2sd %xmm4, %xmm4
ucomisd 0x2bb08(%rip), %xmm4    # 0x3a5f8
jbe 0xeb0b
divss %xmm3, %xmm2
movaps %xmm2, %xmm3
unpcklps %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
mulps %xmm0, %xmm3
movlps %xmm3, (%rbx)
mulss %xmm1, %xmm2
movss %xmm2, 0x8(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r14
retq