// File: FreeSlave[P]vhlt/hlrad/lerp.cpp
// Function: CalcWeight(localtriangulation_t const*, float const*, float*)
static bool CalcWeight (const localtriangulation_t *lt, const vec3_t spot, vec_t *weight_out)
	// It returns true when the point is inside the hull region (with boundary), even if weight = 0.
{
	vec3_t direction;
	const localtriangulation_t::HullPoint *hp1;
	const localtriangulation_t::HullPoint *hp2;
	bool istoofar;
	vec_t ratio;

	int i;
	int j;
	vec_t angle;
	std::vector< vec_t > angles;
	vec_t frac;
	vec_t len;
	vec_t dist;

	if (GetDirection (spot, lt->normal, direction) <= 2 * ON_EPSILON)
	{
		*weight_out = 1.0;
		return true;
	}

	if ((int)lt->sortedhullpoints.size () == 0)
	{
		*weight_out = 0.0;
		return false;
	}

	angles.resize ((int)lt->sortedhullpoints.size ());
	for (i = 0; i < (int)lt->sortedhullpoints.size (); i++)
	{
		angle = GetAngle (lt->sortedhullpoints[i].direction, direction, lt->normal);
		angles[i] = GetAngleDiff (angle, 0);
	}
	j = 0;
	for (i = 1; i < (int)lt->sortedhullpoints.size (); i++)
	{
		if (angles[i] < angles[j])
		{
			j = i;
		}
	}
	hp1 = &lt->sortedhullpoints[j];
	hp2 = &lt->sortedhullpoints[(j + 1) % (int)lt->sortedhullpoints.size ()];

	frac = GetFrac (hp1->spot, hp2->spot, direction, lt->normal);
	
	len = (1 - frac) * DotProduct (hp1->spot, direction) + frac * DotProduct (hp2->spot, direction);
	dist = DotProduct (spot, direction);
	if (len <= ON_EPSILON / 4 || dist > len + 2 * ON_EPSILON)
	{
		istoofar = true;
		ratio = 1.0;
	}
	else if (dist >= len - ON_EPSILON)
	{
		istoofar = false; // if we change this "false" to "true", we will see many places turned "green" in "-drawlerp" mode
		ratio = 1.0; // in order to prevent excessively small weight
	}
	else
	{
		istoofar = false;
		ratio = dist / len;
		ratio = qmax (0, qmin (ratio, 1));
	}

	*weight_out = 1 - ratio;
	return !istoofar;
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x1ec50
movq 0xb0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
addq $0x44, %rsi
leaq 0x9c(%rsp), %rdx
callq 0x18680
movss %xmm0, 0x34(%rsp)
jmp 0x14b22
movss 0x34(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x58cb4(%rip), %xmm0    # 0x6d7e8
ucomisd %xmm1, %xmm0
jb 0x14b80
movq 0xa8(%rsp), %rax
movss 0x55502(%rip), %xmm0    # 0x6a04c
movss %xmm0, (%rax)
movb $0x1, 0xc7(%rsp)
movl $0x1, 0x38(%rsp)
jmp 0x15073
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x1f200
jmp 0x1508e
movq 0xb8(%rsp), %rdi
addq $0x88, %rdi
callq 0x1f660
cmpl $0x0, %eax
jne 0x14bbd
movq 0xa8(%rsp), %rax
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
movb $0x0, 0xc7(%rsp)
movl $0x1, 0x38(%rsp)
jmp 0x15073
movq 0xb8(%rsp), %rdi
addq $0x88, %rdi
callq 0x1f660
movslq %eax, %rsi
leaq 0x58(%rsp), %rdi
callq 0x1eda0
jmp 0x14be0
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0xb8(%rsp), %rdi
addq $0x88, %rdi
callq 0x1f660
movq %rax, %rcx
movl 0x30(%rsp), %eax
cmpl %ecx, %eax
jge 0x14c9f
movq 0xb8(%rsp), %rdi
addq $0x88, %rdi
movslq 0x7c(%rsp), %rsi
callq 0x1f680
movq %rax, %rdi
addq $0xc, %rdi
movq 0xb8(%rsp), %rdx
addq $0x44, %rdx
leaq 0x9c(%rsp), %rsi
callq 0x187a0
movss %xmm0, 0x2c(%rsp)
jmp 0x14c54
movss 0x2c(%rsp), %xmm0
movss %xmm0, 0x74(%rsp)
movss 0x74(%rsp), %xmm0
xorps %xmm1, %xmm1
callq 0x18920
movss %xmm0, 0x28(%rsp)
jmp 0x14c76
movslq 0x7c(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x1f160
movss 0x28(%rsp), %xmm0
movss %xmm0, (%rax)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x14be8
movl $0x0, 0x78(%rsp)
movl $0x1, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0xb8(%rsp), %rdi
addq $0x88, %rdi
callq 0x1f660
movq %rax, %rcx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x14d24
movslq 0x7c(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x1f160
movss (%rax), %xmm0
movss %xmm0, 0x20(%rsp)
movslq 0x78(%rsp), %rsi
leaq 0x58(%rsp), %rdi
callq 0x1f160
movss 0x20(%rsp), %xmm1
movss (%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x14d15
movl 0x7c(%rsp), %eax
movl %eax, 0x78(%rsp)
jmp 0x14d17
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x14caf
movq 0xb8(%rsp), %rdi
addq $0x88, %rdi
movslq 0x78(%rsp), %rsi
callq 0x1f680
movq %rax, 0x90(%rsp)
movq 0xb8(%rsp), %rdi
addq $0x88, %rdi
movq %rdi, 0x10(%rsp)
movl 0x78(%rsp), %eax
incl %eax
movl %eax, 0x18(%rsp)
callq 0x1f660
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl 0x18(%rsp), %eax
cltd
idivl %ecx
movslq %edx, %rsi
callq 0x1f680
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0xb8(%rsp), %rcx
addq $0x44, %rcx
leaq 0x9c(%rsp), %rdx
callq 0x18970
movss %xmm0, 0x1c(%rsp)
jmp 0x14db8
movss 0x1c(%rsp), %xmm0
movss %xmm0, 0x54(%rsp)
movss 0x55280(%rip), %xmm0    # 0x6a04c
subss 0x54(%rsp), %xmm0
movq 0x90(%rsp), %rax
movss (%rax), %xmm1
movss 0x9c(%rsp), %xmm3
movq 0x90(%rsp), %rax
movss 0x4(%rax), %xmm2
mulss 0xa0(%rsp), %xmm2
mulss %xmm3, %xmm1
addss %xmm2, %xmm1
movq 0x90(%rsp), %rax
movss 0x8(%rax), %xmm2
movss 0xa4(%rsp), %xmm3
mulss %xmm3, %xmm2
addss %xmm1, %xmm2
movss 0x54(%rsp), %xmm1
movq 0x88(%rsp), %rax
movss (%rax), %xmm4
movss 0x9c(%rsp), %xmm5
movq 0x88(%rsp), %rax
movss 0x4(%rax), %xmm3
mulss 0xa0(%rsp), %xmm3
mulss %xmm5, %xmm4
addss %xmm3, %xmm4
movq 0x88(%rsp), %rax
movss 0x8(%rax), %xmm3
movss 0xa4(%rsp), %xmm5
mulss %xmm5, %xmm3
addss %xmm4, %xmm3
mulss %xmm3, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x50(%rsp)
movq 0xb0(%rsp), %rax
movss (%rax), %xmm1
movss 0x9c(%rsp), %xmm2
movq 0xb0(%rsp), %rax
movss 0x4(%rax), %xmm0
mulss 0xa0(%rsp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movq 0xb0(%rsp), %rax
movss 0x8(%rax), %xmm0
movss 0xa4(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x4c(%rsp)
movss 0x50(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x588e3(%rip), %xmm0    # 0x6d7d8
ucomisd %xmm1, %xmm0
jae 0x14f21
movss 0x4c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x50(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x588d1(%rip), %xmm2    # 0x6d7e8
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x14f3f
movb $0x1, 0x87(%rsp)
movss 0x5511b(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0x80(%rsp)
jmp 0x1503c
movss 0x4c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x50(%rsp), %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x58885(%rip), %xmm2    # 0x6d7e0
subsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x14f83
movb $0x0, 0x87(%rsp)
movss 0x550d7(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0x80(%rsp)
jmp 0x1503a
movb $0x0, 0x87(%rsp)
movss 0x4c(%rsp), %xmm0
divss 0x50(%rsp), %xmm0
movss %xmm0, 0x80(%rsp)
movss 0x550a4(%rip), %xmm0    # 0x6a04c
ucomiss 0x80(%rsp), %xmm0
jbe 0x14fc3
movss 0x80(%rsp), %xmm0
movss %xmm0, 0xc(%rsp)
jmp 0x14fd3
movss 0x55081(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0xc(%rsp)
jmp 0x14fd3
movss 0xc(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x14fec
xorps %xmm0, %xmm0
movss %xmm0, 0x8(%rsp)
jmp 0x1502b
movss 0x55058(%rip), %xmm0    # 0x6a04c
ucomiss 0x80(%rsp), %xmm0
jbe 0x1500f
movss 0x80(%rsp), %xmm0
movss %xmm0, 0x4(%rsp)
jmp 0x1501f
movss 0x55035(%rip), %xmm0    # 0x6a04c
movss %xmm0, 0x4(%rsp)
jmp 0x1501f
movss 0x4(%rsp), %xmm0
movss %xmm0, 0x8(%rsp)
movss 0x8(%rsp), %xmm0
movss %xmm0, 0x80(%rsp)
jmp 0x1503c
movss 0x55008(%rip), %xmm0    # 0x6a04c
subss 0x80(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movss %xmm0, (%rax)
movb 0x87(%rsp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0xc7(%rsp)
movl $0x1, 0x38(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x1f200
movb 0xc7(%rsp), %al
andb $0x1, %al
addq $0xc8, %rsp
retq
movq 0x40(%rsp), %rdi
callq 0x45c0
nopl (%rax,%rax)