// File: intheswim[P]FractalTubes/m3d.cpp
// Function: M3DDrawer::SetGlobals(ShapeParams*, DrawParams*)
long M3DDrawer::SetGlobals (struct ShapeParams *sp, struct DrawParams *dp)
{
    //double k = 1.414213562373095048801688724209698078;
	double k = sqrt (2.0) ;

	double incircle = (floor)(k * (2 * M_PI * sp->l) + 0.5);
	double cycles = k * hypot ((sp->r + sp->l) * sp->alpha_range, sp->y_range);
    
    exposed_y_range = sp->y_range / sp->r;
    
	//double cycles_in2pi = 2 * M_PI * cycles / sp->alpha_range;
    
    // Jan 2015 we need this for the mesh:
    m_points_in_cycle = (int)incircle;
    
    /*  if ((m_points_in_cycle & 1) != 0)
    {
        m_points_in_cycle++;
        incircle = m_points_in_cycle;
    } */
    
	sp->total_points = incircle * cycles;
    
	sp->delta_y = sp->y_range / sp->total_points;
	sp->delta_alpha = sp->alpha_range / sp->total_points;
	//sp->coef = hypot (2*PI * (sp->r + sp->l), sp->y_range * (2 * PI/sp->alpha_range)) ;
    
	//sp->coef = 2 * PI * (sp->l + sp->r);
	sp->coef = 2 * M_PI * cycles / sp->alpha_range;
	//sp->coef = cycles * PI * 2 * sp->l/ sp->alpha_range; //cycles_in2pi;
    
	if (dp->method == 0)
    {
		if (dp->max_radius < 0)
			dp->max_radius = dp->xRadius / (pow ((double)POWER_BASE, (double)(POWER_RANGE + POWER_ADD) * dp->stretchRatio));
		else //f (dp->stretchRatio == 0)
        {
			double t = log (dp->max_radius / dp->xRadius);
			dp->stretchRatio = (t / log (1/POWER_BASE)) / (POWER_RANGE + POWER_ADD);
        }
    }
    
	if (dp->method == 1) // two points
    {
		dp->steps1 = log (dp->max_radius / dp->xRadius) / log (1/POWER_BASE) - POWER_ADD;
		dp->steps2 = log (dp->max_radius / dp->xRadius2) / log (1/POWER_BASE) - POWER_ADD;
    }
    
    m_total_points = sp->total_points;
    
	return sp->total_points;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x98(%rbp)
movsd 0x44fe(%rip), %xmm0     # 0xe258
callq 0x32a0
movsd %xmm0, -0x20(%rbp)
movsd -0x20(%rbp), %xmm0
movq -0x10(%rbp), %rax
movsd 0x48(%rax), %xmm1
movsd 0x46fe(%rip), %xmm2     # 0xe478
movsd %xmm2, -0x90(%rbp)
mulsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd 0x44ce(%rip), %xmm1     # 0xe260
addsd %xmm1, %xmm0
callq 0x3680
movsd %xmm0, -0x28(%rbp)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x88(%rbp)
movq -0x10(%rbp), %rax
movsd 0x40(%rax), %xmm0
movsd 0x48(%rax), %xmm1
addsd %xmm1, %xmm0
movsd 0x10(%rax), %xmm1
movsd 0x18(%rax), %xmm2
mulsd %xmm2, %xmm0
callq 0x3110
movq -0x98(%rbp), %rax
movsd -0x90(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x88(%rbp), %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x10(%rbp), %rcx
movsd 0x10(%rcx), %xmm0
movsd 0x40(%rcx), %xmm2
divsd %xmm2, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x28(%rax)
cvttsd2si -0x28(%rbp), %ecx
movl %ecx, 0x60(%rax)
movsd -0x28(%rbp), %xmm0
movsd -0x30(%rbp), %xmm2
mulsd %xmm2, %xmm0
cvttsd2si %xmm0, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movsd 0x10(%rax), %xmm0
cvtsi2sdq (%rax), %xmm2
divsd %xmm2, %xmm0
movsd %xmm0, -0x40(%rbp)
fldl -0x40(%rbp)
fstpt 0x20(%rax)
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm0
cvtsi2sdq (%rax), %xmm2
divsd %xmm2, %xmm0
movsd %xmm0, -0x48(%rbp)
fldl -0x48(%rbp)
fstpt 0x30(%rax)
movsd -0x30(%rbp), %xmm0
mulsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
movsd 0x18(%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x50(%rbp)
fldl -0x50(%rbp)
fstpt 0x50(%rax)
movq -0x18(%rbp), %rax
cmpl $0x0, 0xb8(%rax)
jne 0x9f6e
movq -0x18(%rbp), %rax
xorps %xmm0, %xmm0
ucomisd 0xb0(%rax), %xmm0
jbe 0x9f04
movq -0x18(%rbp), %rax
fldt 0x70(%rax)
fstpt -0xa4(%rbp)
movss 0x30(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x45b3(%rip), %xmm0     # 0xe480
mulsd %xmm0, %xmm1
movsd 0x45b7(%rip), %xmm0     # 0xe490
callq 0x3690
fldt -0xa4(%rbp)
movsd %xmm0, -0x60(%rbp)
fldl -0x60(%rbp)
fdivrp %st, %st(1)
fstpl -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, 0xb0(%rax)
jmp 0x9f6c
movq -0x18(%rbp), %rax
fldl 0xb0(%rax)
fldt 0x70(%rax)
fdivrp %st, %st(1)
movq %rsp, %rax
fstpt (%rax)
callq 0xb1b0
fstpl -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd 0x4549(%rip), %xmm0     # 0xe488
callq 0x32d0
movaps %xmm0, %xmm1
movsd -0xb0(%rbp), %xmm0
divsd %xmm1, %xmm0
movsd 0x4525(%rip), %xmm1     # 0xe480
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x18(%rbp), %rax
movss %xmm0, 0x30(%rax)
jmp 0x9f6e
movq -0x18(%rbp), %rax
cmpl $0x1, 0xb8(%rax)
jne 0xa020
movq -0x18(%rbp), %rax
fldl 0xb0(%rax)
fldt 0x70(%rax)
fdivrp %st, %st(1)
movq %rsp, %rax
fstpt (%rax)
callq 0xb1b0
fstpt -0xd4(%rbp)
movsd 0x44e2(%rip), %xmm0     # 0xe488
movsd %xmm0, -0xc8(%rbp)
callq 0x32d0
fldt -0xd4(%rbp)
movsd %xmm0, -0x70(%rbp)
fldl -0x70(%rbp)
fdivrp %st, %st(1)
movq -0x18(%rbp), %rax
fstps 0xbc(%rax)
movq -0x18(%rbp), %rax
fldl 0xb0(%rax)
fldt 0xa0(%rax)
fdivrp %st, %st(1)
movq %rsp, %rax
fstpt (%rax)
callq 0xb1b0
movsd -0xc8(%rbp), %xmm0
fstpt -0xbc(%rbp)
callq 0x32d0
fldt -0xbc(%rbp)
movsd %xmm0, -0x78(%rbp)
fldl -0x78(%rbp)
fdivrp %st, %st(1)
fstps -0x7c(%rbp)
movss -0x7c(%rbp), %xmm0
movq -0x18(%rbp), %rax
movss %xmm0, 0xc0(%rax)
movq -0x98(%rbp), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
movq %rcx, 0x58(%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)