// File: /workspace/github_cpp_makefile_star2_O1/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %rbx
movq 0x58(%rdi), %rax
movq 0x60(%rdi), %r15
subq %rax, %r15
shrq $0x2, %r15
testl %r15d, %r15d
jle 0xedac
movl %r15d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %r12d, %r12d
cmpl %edx, (%rax,%r12,4)
je 0xedaf
incq %r12
cmpq %r12, %rcx
jne 0xed99
movl %r15d, %r12d
jmp 0xedaf
xorl %r12d, %r12d
cmpl %r15d, %r12d
je 0xef1d
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, 0xc(%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, 0x10(%rsp)
movaps %xmm4, %xmm0
subss %xmm1, %xmm0
movss %xmm0, 0x8(%rbx)
leaq 0x3c(%rsp), %r14
movl %edx, %edi
movq %r14, %rdx
callq 0x1a89c
movss (%r14), %xmm2
movss 0x4(%r14), %xmm3
movsd (%rbx), %xmm0
movaps %xmm0, %xmm8
movaps %xmm0, %xmm7
shufps $0x55, %xmm0, %xmm7     # xmm7 = xmm7[1,1],xmm0[1,1]
movaps %xmm7, %xmm1
mulss %xmm3, %xmm1
movaps %xmm0, %xmm6
mulss %xmm2, %xmm6
addss %xmm1, %xmm6
movss 0x8(%rbx), %xmm1
movss 0x8(%r14), %xmm4
movaps %xmm1, %xmm5
mulss %xmm4, %xmm5
addss %xmm6, %xmm5
movaps 0x2c4a6(%rip), %xmm6    # 0x3b340
andps %xmm5, %xmm6
cvtss2sd %xmm6, %xmm6
xorps %xmm9, %xmm9
ucomisd 0x2c79b(%rip), %xmm6    # 0x3b648
jbe 0xeeda
mulss 0x8(%rsp), %xmm3
mulss 0xc(%rsp), %xmm2
addss %xmm3, %xmm2
mulss 0x10(%rsp), %xmm4
addss %xmm2, %xmm4
divss %xmm5, %xmm4
minss 0x29263(%rip), %xmm4    # 0x38138
maxss %xmm4, %xmm9
mulss %xmm9, %xmm8
mulss %xmm9, %xmm7
mulss %xmm1, %xmm9
mulps %xmm0, %xmm0
cvtps2pd %xmm0, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
mulss %xmm1, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
addsd %xmm2, %xmm0
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm0
jb 0xef30
xorps %xmm5, %xmm5
sqrtsd %xmm0, %xmm5
jmp 0xef63
movl $0x0, 0x8(%rbx)
movq $0x0, (%rbx)
jmp 0xf0a8
movaps %xmm7, 0x20(%rsp)
movss %xmm8, 0x1c(%rsp)
movss %xmm9, 0x18(%rsp)
callq 0x4240
xorpd %xmm6, %xmm6
movss 0x18(%rsp), %xmm9
movss 0x1c(%rsp), %xmm8
movaps 0x20(%rsp), %xmm7
movapd %xmm0, %xmm5
movss 0x8(%rsp), %xmm2
movss 0x10(%rsp), %xmm3
movss 0xc(%rsp), %xmm4
subss %xmm8, %xmm4
subss %xmm7, %xmm2
subss %xmm9, %xmm3
mulss %xmm8, %xmm8
xorps %xmm0, %xmm0
cvtss2sd %xmm8, %xmm0
mulss %xmm7, %xmm7
xorps %xmm1, %xmm1
cvtss2sd %xmm7, %xmm1
addsd %xmm0, %xmm1
mulss %xmm9, %xmm9
xorps %xmm0, %xmm0
cvtss2sd %xmm9, %xmm0
addsd %xmm1, %xmm0
ucomisd %xmm6, %xmm0
jb 0xefbf
xorps %xmm6, %xmm6
sqrtsd %xmm0, %xmm6
jmp 0xeff8
movss %xmm2, 0x8(%rsp)
movss %xmm3, 0x10(%rsp)
movss %xmm4, 0xc(%rsp)
movsd %xmm5, 0x20(%rsp)
callq 0x4240
movsd 0x20(%rsp), %xmm5
movss 0xc(%rsp), %xmm4
movss 0x10(%rsp), %xmm3
movss 0x8(%rsp), %xmm2
movapd %xmm0, %xmm6
cvtsd2ss %xmm5, %xmm5
mulss %xmm4, %xmm4
xorps %xmm0, %xmm0
cvtss2sd %xmm4, %xmm0
mulss %xmm2, %xmm2
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
addsd %xmm0, %xmm1
mulss %xmm3, %xmm3
xorps %xmm0, %xmm0
cvtss2sd %xmm3, %xmm0
addsd %xmm1, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xf035
sqrtsd %xmm0, %xmm0
jmp 0xf052
movss %xmm5, 0x8(%rsp)
movsd %xmm6, 0x10(%rsp)
callq 0x4240
movsd 0x10(%rsp), %xmm6
movss 0x8(%rsp), %xmm5
xorps %xmm1, %xmm1
cvtss2sd %xmm5, %xmm1
ucomisd 0x2c5e7(%rip), %xmm1    # 0x3b648
jbe 0xf0a8
addsd %xmm0, %xmm6
xorps %xmm0, %xmm0
cvtsd2ss %xmm6, %xmm0
movaps %xmm0, %xmm1
subss %xmm5, %xmm1
andps 0x2c2c4(%rip), %xmm1    # 0x3b340
cvtss2sd %xmm1, %xmm1
ucomisd 0x2c5c0(%rip), %xmm1    # 0x3b648
jbe 0xf0a8
divss %xmm5, %xmm0
movsd (%rbx), %xmm1
movaps %xmm0, %xmm2
unpcklps %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0],xmm2[1],xmm0[1]
mulps %xmm1, %xmm2
movlps %xmm2, (%rbx)
mulss 0x8(%rbx), %xmm0
movss %xmm0, 0x8(%rbx)
cmpl %r15d, %r12d
setne %al
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq