// File: 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:
subq $0x88, %rsp
movq %rdi, 0x78(%rsp)
movq %rsi, 0x70(%rsp)
movl %edx, 0x6c(%rsp)
movq %rcx, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0x78(%rsp), %rdi
addq $0x58, %rdi
callq 0x1eee0
movq %rax, %rcx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x145cf
movq 0x78(%rsp), %rdi
addq $0x58, %rdi
movslq 0x5c(%rsp), %rsi
callq 0x1ef00
movl (%rax), %eax
cmpl 0x6c(%rsp), %eax
jne 0x145c0
jmp 0x145cf
jmp 0x145c2
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x14582
movl 0x5c(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x78(%rsp), %rdi
addq $0x58, %rdi
callq 0x1eee0
movq %rax, %rcx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jne 0x14623
movq 0x60(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rax)
movq 0x60(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x4(%rax)
movq 0x60(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movb $0x0, 0x87(%rsp)
jmp 0x14abd
movq 0x70(%rsp), %rax
movss (%rax), %xmm0
movq 0x78(%rsp), %rax
subss 0x38(%rax), %xmm0
movss %xmm0, 0x4c(%rsp)
movq 0x70(%rsp), %rax
movss 0x4(%rax), %xmm0
movq 0x78(%rsp), %rax
subss 0x3c(%rax), %xmm0
movss %xmm0, 0x50(%rsp)
movq 0x70(%rsp), %rax
movss 0x8(%rax), %xmm0
movq 0x78(%rsp), %rax
subss 0x40(%rax), %xmm0
movss %xmm0, 0x54(%rsp)
movss 0x4c(%rsp), %xmm1
movq 0x78(%rsp), %rax
movss 0x44(%rax), %xmm2
movss 0x50(%rsp), %xmm0
movq 0x78(%rsp), %rax
mulss 0x48(%rax), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x54(%rsp), %xmm0
movq 0x78(%rsp), %rax
movss 0x4c(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x58(%rsp)
movss 0x4c(%rsp), %xmm1
movss 0x58(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x78(%rsp), %rax
movss 0x44(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, (%rax)
movss 0x50(%rsp), %xmm1
movss 0x58(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x78(%rsp), %rax
movss 0x48(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0x4(%rax)
movss 0x54(%rsp), %xmm1
movss 0x58(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0x78(%rsp), %rax
movss 0x4c(%rax), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0x8(%rax)
movl 0x6c(%rsp), %edi
movq 0x70(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x329b0
movq 0x60(%rsp), %rax
movss (%rax), %xmm1
movss 0x38(%rsp), %xmm2
movq 0x60(%rsp), %rax
movss 0x4(%rax), %xmm0
mulss 0x3c(%rsp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0x60(%rsp), %rax
movss 0x8(%rax), %xmm0
movss 0x40(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x58(%rsp)
movss 0x58(%rsp), %xmm0
callq 0x11ce0
cvtss2sd %xmm0, %xmm0
movsd 0x5901d(%rip), %xmm1    # 0x6d7e0
ucomisd %xmm1, %xmm0
jbe 0x1489a
movss 0x4c(%rsp), %xmm1
movss 0x38(%rsp), %xmm2
movss 0x50(%rsp), %xmm0
mulss 0x3c(%rsp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss 0x54(%rsp), %xmm0
movss 0x40(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
divss 0x58(%rsp), %xmm0
movss %xmm0, 0x34(%rsp)
movss 0x55837(%rip), %xmm0    # 0x6a04c
ucomiss 0x34(%rsp), %xmm0
jbe 0x1482a
movss 0x34(%rsp), %xmm0
movss %xmm0, 0x10(%rsp)
jmp 0x1483a
movss 0x5581a(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0x10(%rsp)
jmp 0x1483a
movss 0x10(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x14853
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
jmp 0x1488c
movss 0x557f1(%rip), %xmm0    # 0x6a04c
ucomiss 0x34(%rsp), %xmm0
jbe 0x14870
movss 0x34(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
jmp 0x14880
movss 0x557d4(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0x8(%rsp)
jmp 0x14880
movss 0x8(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x34(%rsp)
jmp 0x148a3
xorps %xmm0, %xmm0
movss %xmm0, 0x34(%rsp)
movq 0x60(%rsp), %rax
movss (%rax), %xmm0
mulss 0x34(%rsp), %xmm0
movss %xmm0, 0x28(%rsp)
movq 0x60(%rsp), %rax
movss 0x4(%rax), %xmm0
mulss 0x34(%rsp), %xmm0
movss %xmm0, 0x2c(%rsp)
movq 0x60(%rsp), %rax
movss 0x8(%rax), %xmm0
mulss 0x34(%rsp), %xmm0
movss %xmm0, 0x30(%rsp)
movq 0x60(%rsp), %rax
movss (%rax), %xmm0
movq 0x60(%rsp), %rax
mulss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x60(%rsp), %rax
movss 0x4(%rax), %xmm1
movq 0x60(%rsp), %rax
mulss 0x4(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss 0x8(%rax), %xmm1
movq 0x60(%rsp), %rax
mulss 0x8(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x48(%rsp)
movss 0x4c(%rsp), %xmm0
subss 0x28(%rsp), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x50(%rsp), %xmm0
subss 0x2c(%rsp), %xmm0
movss %xmm0, 0x20(%rsp)
movss 0x54(%rsp), %xmm0
subss 0x30(%rsp), %xmm0
movss %xmm0, 0x24(%rsp)
movss 0x28(%rsp), %xmm0
mulss 0x28(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x2c(%rsp), %xmm1
mulss 0x2c(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movss 0x30(%rsp), %xmm1
mulss 0x30(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
movsd %xmm0, (%rsp)
movss 0x1c(%rsp), %xmm0
mulss 0x1c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x20(%rsp), %xmm1
mulss 0x20(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
movss 0x24(%rsp), %xmm1
mulss 0x24(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
addsd %xmm1, %xmm0
callq 0x4270
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x44(%rsp)
movss 0x48(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x58dc2(%rip), %xmm1    # 0x6d7e0
ucomisd %xmm1, %xmm0
jbe 0x14ab5
movss 0x44(%rsp), %xmm0
subss 0x48(%rsp), %xmm0
callq 0x11ce0
cvtss2sd %xmm0, %xmm0
movsd 0x58d9b(%rip), %xmm1    # 0x6d7e0
ucomisd %xmm1, %xmm0
jbe 0x14ab5
movq 0x60(%rsp), %rax
movss (%rax), %xmm0
movss 0x44(%rsp), %xmm1
divss 0x48(%rsp), %xmm1
mulss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, (%rax)
movq 0x60(%rsp), %rax
movss 0x4(%rax), %xmm0
movss 0x44(%rsp), %xmm1
divss 0x48(%rsp), %xmm1
mulss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0x4(%rax)
movq 0x60(%rsp), %rax
movss 0x8(%rax), %xmm0
movss 0x44(%rsp), %xmm1
divss 0x48(%rsp), %xmm1
mulss %xmm1, %xmm0
movq 0x60(%rsp), %rax
movss %xmm0, 0x8(%rax)
movb $0x1, 0x87(%rsp)
movb 0x87(%rsp), %al
andb $0x1, %al
addq $0x88, %rsp
retq
nop