// File: /workspace/github_cpp_makefile_star2_O3/istellartech[P]OpenTsiolkovsky/src/coordinate_transform.cpp
// Function: posLLH_IIP(Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>)
Vector3d posLLH_IIP(Vector3d posECEF_, Vector3d vel_ECEF_ECEFframe_){

    int n_iter = 5;
    double Ra = 6378137.0;
    double f = 1.0 / 298.257223563;
    double Rb = Ra * (1.0 - f);
    double e2 = (Ra * Ra - Rb *Rb) / Ra / Ra;
    double mu = 3.986004418e14;
    double omega = 7.2921151467e-5;
    Vector3d omegaVec_;
    omegaVec_ << 0.0, 0.0, omega;

    // (v)-(A): The distance frome the center of the Earth ellipsoid to the launch point (the initial approximation of r_k1, k=1)
    double r_k1 = Rb;
   
    // (v)-(B): The radial distance from the geocenter to the launch vehicle position
    Vector3d posECI_init_ = posECEF_;
    double r0 = posECI_init_.norm();
    if (r0 < r_k1) // then tha launch vehicle position is below the Earth's surface and an impact point cannot be computed
        return Vector3d::Zero();      // no solution
    
    // (v)-(C): The inertial velocity compoents
    Vector3d velECI_init_ = vel_ECEF_ECEFframe_ + omegaVec_.cross(posECEF_);
    // (v)-(D): The magnitude of the inertial velocity vector
    double v0 = velECI_init_.norm();
    
    // (v)-(E): The eccentricity of the trajectory ellipse multiplied by the cosine of the eccentric anomaly at epoch
    double eps_cos = (r0 * v0 * v0 / mu) - 1.0;
    
    if (eps_cos >= 1.0) // then the trajectory orbit is not elliptical, but is hyperbolic or parabolic, and an impact point cannot be computed
        return Vector3d::Zero();      // no solution

    // (v)-(F): The semi-major axis of the trajectory ellipse
    double a_t = r0 / (1 - eps_cos);
    
    // (v)-(G): The eccentricity of the trajectory ellipse multiplied by the sine of the eccentric anomaly at epoch
    double eps_sin = posECI_init_.dot(velECI_init_) / sqrt(mu * a_t);
    
    // (v)-(H): The eccentricity of the trajectory ellipse squared 
    double eps2 = eps_cos * eps_cos + eps_sin * eps_sin;
    if (sqrt(eps2) <= 1.0 && a_t * (1 - sqrt(eps2)) - Ra >= 0.0) // then the trajectory perigee height is positive and an impact point cannot be computed
        return Vector3d::Zero();      // no solution
    
    double eps_k_cos, eps_k_sin, delta_eps_k_cos, delta_eps_k_sin;
    double fseries_2, gseries_2, Ek, Fk, Gk, r_k2, r_k1_tmp;
    
    for (int i=0; i<n_iter; i++){
        // (v)-(I): The eccentricity of the trajectory ellipse multiplied by the cosine of the eccentric anomaly at impact
        eps_k_cos = (a_t - r_k1) / a_t;
        
        // (v)-(J): The eccentricity of the trajectory ellipse multiplied by the sine of the eccentric anomaly at impact
        if ((eps2 - eps_k_cos * eps_k_cos) < 0) // then the trajectory orbit does not intersect the Earth's surface and an impact point cannot be computed
            return Vector3d::Zero();      // no solution
        eps_k_sin = -sqrt(eps2 - eps_k_cos * eps_k_cos);
        
        // (v)-(K): The cosine of the difference between the eccentric anomaly at impact and epoch
        delta_eps_k_cos = (eps_k_cos*eps_cos + eps_k_sin*eps_sin) / eps2;
        
        // (v)-(L): The sine of the difference between the eccentric anomaly at impact and epoch
        delta_eps_k_sin = (eps_k_sin*eps_cos - eps_k_cos*eps_sin) / eps2;
        
        // (v)-(M): The f-series expansion of Kepler's equations
        fseries_2 = (delta_eps_k_cos - eps_cos) / (1 - eps_cos);
        // (v)-(N): The g-series expansion of Kepler's equations
        gseries_2 = (delta_eps_k_sin + eps_sin - eps_k_sin) * sqrt(a_t*a_t*a_t / mu);
        
        // (v)-(O): The E,F,G coordinates at impact
        Ek = fseries_2*posECI_init_[0] + gseries_2*velECI_init_[0];
        Fk = fseries_2*posECI_init_[1] + gseries_2*velECI_init_[1];
        Gk = fseries_2*posECI_init_[2] + gseries_2*velECI_init_[2];
        
        // (v)-(P): The approximated distance from the geocenter to the launch vehicle position at impact
        r_k2 = Ra / sqrt((e2/(1 - e2))*(Gk/r_k1)*(Gk/r_k1) + 1);
        
        // (v)-(Q): Substituting and repeating
        r_k1_tmp = r_k1;
        r_k1 = r_k2;
    }

    // (v)-(Q): check convergence
    if (abs(r_k1_tmp - r_k2) > 1.0) // then the iterative solution does not converge and an impact point does not meet the accuracy tolerance
        return Vector3d::Zero();      // no solution
    
    // (v)-(R): The difference between the eccentric anomaly at impact and epoch
    double delta_eps = atan2(delta_eps_k_sin, delta_eps_k_cos);
    
    // (v)-(S): The time of flight from epoch to impact
    double time_sec = (delta_eps + eps_sin - eps_k_sin) * sqrt(a_t*a_t*a_t / mu);
    
    // (v)-(T): The geocentric latitude at impact
    double phi_impact_tmp = asin(Gk / r_k2);
    // (v)-(U): The geodetic latitude at impact
    double phi_impact = atan2(tan(phi_impact_tmp), 1.0 - e2);
    // (v)-(V): The East longitude at impact
    double lambda_impact = atan2(Fk, Ek) - omega*time_sec;
    
    // finish: convert to posECEF_IIP_
    // posECEF_IIP_ = coord_utils.CoordUtils([rad2deg(phi_impact), rad2deg(lammda_impact), 0]).ecef_origin
    
    Vector3d posIIP(phi_impact, lambda_impact, 0.0);
    return posIIP * 180.0 / M_PI;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movabsq $0x3f131da7d7cb8d5b, %rax # imm = 0x3F131DA7D7CB8D5B
movq %rax, 0x48(%rsp)
leaq 0x130(%rsp), %rax
leaq 0x90(%rsp), %rdi
movq %rax, (%rdi)
xorl %ecx, %ecx
movq %rcx, 0x8(%rdi)
movl $0x1, %edx
movq %rdx, 0x10(%rdi)
movq %rdx, 0x18(%rdi)
movq %rcx, (%rax)
leaq 0x128(%rsp), %rsi
movq %rcx, (%rsi)
callq 0x15520
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0x15520
movq 0xa8(%rsp), %rax
addq 0x98(%rsp), %rax
cmpq $0x3, %rax
jne 0x24a9f
cmpq $0x1, 0xa0(%rsp)
jne 0x24a9f
movupd (%r15), %xmm3
movsd 0x10(%r15), %xmm5
movapd %xmm3, %xmm0
mulpd %xmm3, %xmm0
movapd %xmm5, %xmm1
mulsd %xmm5, %xmm1
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm1, %xmm2
addsd %xmm0, %xmm2
sqrtsd %xmm2, %xmm6
movsd 0x3c4e(%rip), %xmm8     # 0x281e8
ucomisd %xmm6, %xmm8
ja 0x24987
movsd 0x130(%rsp), %xmm7
movupd 0x138(%rsp), %xmm0
movapd %xmm0, %xmm1
shufpd $0x1, %xmm7, %xmm1      # xmm1 = xmm1[1],xmm7[0]
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
movapd %xmm3, %xmm2
shufpd $0x1, %xmm5, %xmm2      # xmm2 = xmm2[1],xmm5[0]
mulpd %xmm1, %xmm2
movapd %xmm5, %xmm1
unpcklpd %xmm3, %xmm1            # xmm1 = xmm1[0],xmm3[0]
mulpd %xmm0, %xmm1
subpd %xmm2, %xmm1
mulsd %xmm3, %xmm0
movapd %xmm4, 0x60(%rsp)
mulsd %xmm4, %xmm7
subsd %xmm0, %xmm7
movupd (%r14), %xmm4
addpd %xmm1, %xmm4
addsd 0x10(%r14), %xmm7
movapd %xmm4, %xmm0
mulpd %xmm4, %xmm0
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm1, %xmm2
addsd %xmm0, %xmm2
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
movapd %xmm6, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
divsd 0x3bd7(%rip), %xmm1     # 0x28218
addsd 0x17d7(%rip), %xmm1     # 0x25e20
ucomisd 0xa1f(%rip), %xmm1      # 0x25070
jae 0x24987
movsd 0xa11(%rip), %xmm0      # 0x25070
movsd %xmm1, 0x40(%rsp)
subsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
divsd %xmm0, %xmm6
movapd %xmm3, %xmm0
mulpd %xmm4, %xmm0
movapd %xmm5, %xmm1
mulsd %xmm7, %xmm1
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movapd %xmm2, 0x30(%rsp)
movsd 0x3b74(%rip), %xmm0     # 0x28218
mulsd %xmm6, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movapd %xmm3, 0x50(%rsp)
movapd %xmm5, 0x70(%rsp)
movsd %xmm6, 0x20(%rsp)
movapd %xmm4, 0xb0(%rsp)
movapd %xmm7, 0x110(%rsp)
jb 0x246dc
sqrtsd %xmm0, %xmm0
jmp 0x246f0
callq 0x52e0
movsd 0x3afe(%rip), %xmm8     # 0x281e8
movsd 0x20(%rsp), %xmm6
movapd 0x30(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, 0x30(%rsp)
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movsd 0x40(%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
xorps %xmm0, %xmm0
sqrtsd %xmm1, %xmm0
movsd 0x945(%rip), %xmm1      # 0x25070
ucomisd %xmm0, %xmm1
jb 0x2474f
subsd %xmm0, %xmm1
mulsd %xmm6, %xmm1
addsd 0x3c77(%rip), %xmm1     # 0x283b8
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jae 0x24987
movapd %xmm6, %xmm5
mulsd %xmm6, %xmm5
mulsd %xmm6, %xmm5
divsd 0x3ab5(%rip), %xmm5     # 0x28218
movl $0x5, %ebp
xorpd %xmm9, %xmm9
xorps %xmm0, %xmm0
sqrtsd %xmm5, %xmm0
movsd %xmm0, 0x80(%rsp)
movsd %xmm5, (%rsp)
movapd %xmm6, %xmm1
movapd %xmm8, 0xc0(%rsp)
subsd %xmm8, %xmm1
divsd %xmm6, %xmm1
movapd %xmm1, %xmm2
mulsd %xmm1, %xmm2
movsd 0x8(%rsp), %xmm0
subsd %xmm2, %xmm0
ucomisd %xmm0, %xmm9
ja 0x24987
ucomisd %xmm9, %xmm0
movapd %xmm1, 0x10(%rsp)
jb 0x247c9
sqrtsd %xmm0, %xmm0
jmp 0x247de
callq 0x52e0
xorpd %xmm9, %xmm9
movapd 0x10(%rsp), %xmm1
movsd (%rsp), %xmm5
movsd %xmm0, 0x28(%rsp)
movapd 0x70(%rsp), %xmm2
ucomisd %xmm9, %xmm5
movsd 0x80(%rsp), %xmm0
jae 0x24819
movapd %xmm5, %xmm0
callq 0x52e0
xorpd %xmm9, %xmm9
movapd 0x10(%rsp), %xmm1
movsd (%rsp), %xmm5
movapd 0x70(%rsp), %xmm2
movapd %xmm1, %xmm10
xorpd 0x1729(%rip), %xmm10    # 0x25f50
movapd 0x30(%rsp), %xmm3
movapd %xmm3, %xmm6
movsd 0x28(%rsp), %xmm8
mulsd %xmm8, %xmm6
movsd 0x40(%rsp), %xmm4
mulsd %xmm4, %xmm1
subsd %xmm6, %xmm1
movsd 0x8(%rsp), %xmm6
divsd %xmm6, %xmm1
mulsd %xmm3, %xmm10
movapd %xmm8, %xmm7
mulsd %xmm4, %xmm7
subsd %xmm7, %xmm10
divsd %xmm6, %xmm10
movapd %xmm1, %xmm7
subsd %xmm4, %xmm7
divsd 0x88(%rsp), %xmm7
movapd %xmm3, %xmm11
addsd %xmm10, %xmm11
addsd %xmm8, %xmm11
mulsd %xmm0, %xmm11
movapd 0x110(%rsp), %xmm0
mulsd %xmm11, %xmm0
movapd %xmm7, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm0, %xmm3
movapd %xmm3, 0x100(%rsp)
movapd %xmm3, %xmm2
divsd 0xc0(%rsp), %xmm2
movapd %xmm2, %xmm0
mulsd 0x3b22(%rip), %xmm0     # 0x283f0
mulsd %xmm2, %xmm0
addsd 0x796(%rip), %xmm0      # 0x25070
ucomisd %xmm9, %xmm0
jb 0x248e7
sqrtsd %xmm0, %xmm0
jmp 0x2493c
movapd %xmm1, 0x10(%rsp)
movapd %xmm10, 0xf0(%rsp)
movapd %xmm7, 0xe0(%rsp)
movapd %xmm11, 0xd0(%rsp)
callq 0x52e0
movapd 0xd0(%rsp), %xmm11
movapd 0xe0(%rsp), %xmm7
movapd 0xf0(%rsp), %xmm10
xorpd %xmm9, %xmm9
movapd 0x10(%rsp), %xmm1
movsd (%rsp), %xmm5
movapd 0x50(%rsp), %xmm4
movsd 0x20(%rsp), %xmm6
movapd 0xb0(%rsp), %xmm3
movsd 0x38ae(%rip), %xmm8     # 0x28208
divsd %xmm0, %xmm8
decl %ebp
jne 0x24782
movapd 0xc0(%rsp), %xmm0
subsd %xmm8, %xmm0
andpd 0x6b3(%rip), %xmm0      # 0x25030
ucomisd 0x6eb(%rip), %xmm0      # 0x25070
jbe 0x249a8
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq %rbx, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movsd %xmm8, 0x10(%rsp)
movapd %xmm3, %xmm0
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
mulsd %xmm11, %xmm0
movapd 0x60(%rsp), %xmm2
mulsd %xmm7, %xmm2
addsd %xmm0, %xmm2
movapd %xmm2, 0x60(%rsp)
mulsd %xmm11, %xmm3
mulsd %xmm7, %xmm4
addsd %xmm3, %xmm4
movapd %xmm4, 0x50(%rsp)
movapd %xmm10, %xmm0
callq 0x50a0
movsd (%rsp), %xmm2
movapd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
addsd 0x28(%rsp), %xmm1
movapd %xmm1, 0x30(%rsp)
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jb 0x24a1b
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0x24a24
movapd %xmm2, %xmm0
callq 0x52e0
mulsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movapd 0x100(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x55d0
callq 0x5240
movsd 0x3997(%rip), %xmm1     # 0x283e8
callq 0x50a0
movapd %xmm0, 0x10(%rsp)
movapd 0x60(%rsp), %xmm0
movaps 0x50(%rsp), %xmm1
callq 0x50a0
movsd 0x8(%rsp), %xmm1
mulsd 0x48(%rsp), %xmm1
subsd %xmm1, %xmm0
movapd 0x10(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd 0x1512(%rip), %xmm1     # 0x25fa0
divpd 0x151a(%rip), %xmm1     # 0x25fb0
movupd %xmm1, (%rbx)
jmp 0x2498f
leaq 0x282e(%rip), %rdi      # 0x272d4
leaq 0x28ae(%rip), %rsi      # 0x2735b
leaq 0x28d4(%rip), %rcx      # 0x27388
movl $0x6a, %edx
callq 0x5250
movq 0xa8(%rsp), %rcx
addq 0x98(%rsp), %rcx
cmpq $0x3, %rcx
jne 0x24ae7
cmpq $0x1, 0xa0(%rsp)
jne 0x24ae7
movq %rax, %rdi
callq 0x5650
callq 0x589d