// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x180, %rsp            # imm = 0x180
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x168(%rsp), %rax
leaq 0x60(%rsp), %rdi
movq %rax, (%rdi)
andq $0x0, 0x8(%rdi)
movabsq $0x3f131da7d7cb8d5b, %rcx # imm = 0x3F131DA7D7CB8D5B
movq %rcx, 0x48(%rsp)
pushq $0x1
popq %rcx
movq %rcx, 0x10(%rdi)
movq %rcx, 0x18(%rdi)
andq $0x0, (%rax)
leaq 0xa0(%rsp), %rsi
andq $0x0, (%rsi)
callq 0xfd60
leaq 0x48(%rsp), %rsi
movq %rax, %rdi
callq 0xfd60
leaq 0x60(%rsp), %rdi
callq 0xfe08
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x129c6
movq %r14, %rdi
callq 0x10c94
movsd 0x4fee(%rip), %xmm1     # 0x22e78
ucomisd %xmm0, %xmm1
ja 0x1e281
leaq 0xe8(%rsp), %r14
leaq 0x168(%rsp), %rsi
movq %r14, %rdi
movq %r12, %rdx
movsd %xmm0, 0x10(%rsp)
callq 0x1c922
leaq 0x60(%rsp), %rsi
movq %r15, (%rsi)
movq %r14, 0x8(%rsi)
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
callq 0x15aec
movq %r15, %rdi
callq 0x10c94
movsd 0x10(%rsp), %xmm1
movapd %xmm1, %xmm2
mulsd %xmm0, %xmm2
mulsd %xmm0, %xmm2
divsd 0x4fb5(%rip), %xmm2     # 0x22ea8
addsd 0x2005(%rip), %xmm2     # 0x1ff00
ucomisd 0x115d(%rip), %xmm2     # 0x1f060
jae 0x1e281
movsd 0x114f(%rip), %xmm0     # 0x1f060
movapd %xmm2, 0x80(%rsp)
subsd %xmm2, %xmm0
movsd %xmm0, 0x98(%rsp)
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x1e89c
movsd 0x10(%rsp), %xmm2
movapd %xmm0, 0x20(%rsp)
movsd 0x4f4e(%rip), %xmm0     # 0x22ea8
mulsd %xmm2, %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1df6e
sqrtsd %xmm0, %xmm0
jmp 0x1df79
callq 0x52e0
movsd 0x10(%rsp), %xmm2
movsd 0x4ef7(%rip), %xmm3     # 0x22e78
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
movapd 0x80(%rsp), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0xc0(%rsp)
sqrtsd %xmm1, %xmm0
movsd 0x10a1(%rip), %xmm4     # 0x1f060
ucomisd %xmm0, %xmm4
jb 0x1dfe7
movapd %xmm4, %xmm1
subsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
addsd 0x5157(%rip), %xmm1     # 0x23130
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jae 0x1e281
movapd %xmm2, %xmm5
mulsd %xmm2, %xmm5
mulsd %xmm2, %xmm5
divsd 0x4ead(%rip), %xmm5     # 0x22ea8
movaps 0x80(%rsp), %xmm0
movlhps %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movaps %xmm0, 0x150(%rsp)
movaps 0x20(%rsp), %xmm0
movlhps %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movaps %xmm0, 0x140(%rsp)
movaps 0xc0(%rsp), %xmm0
movlhps %xmm0, %xmm0            # xmm0 = xmm0[0,0]
movaps %xmm0, 0x130(%rsp)
pushq $0x5
popq %rbp
xorpd %xmm6, %xmm6
sqrtsd %xmm5, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd %xmm5, 0x18(%rsp)
subl $0x1, %ebp
jb 0x1e26a
movapd %xmm2, %xmm7
subsd %xmm3, %xmm7
divsd %xmm2, %xmm7
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
movapd 0xc0(%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd %xmm0, %xmm6
ja 0x1e281
ucomisd %xmm6, %xmm0
movsd %xmm3, 0x8(%rsp)
movapd %xmm7, 0x30(%rsp)
jb 0x1e098
sqrtsd %xmm0, %xmm10
jmp 0x1e0c0
callq 0x52e0
movapd 0x30(%rsp), %xmm7
xorpd %xmm6, %xmm6
movsd 0x18(%rsp), %xmm5
movsd 0xfab(%rip), %xmm4      # 0x1f060
movsd 0x8(%rsp), %xmm3
movapd %xmm0, %xmm10
ucomisd %xmm6, %xmm5
movsd 0x90(%rsp), %xmm0
jae 0x1e104
movapd %xmm5, %xmm0
movapd %xmm10, 0x50(%rsp)
callq 0x52e0
movapd 0x50(%rsp), %xmm10
movapd 0x30(%rsp), %xmm7
xorpd %xmm6, %xmm6
movsd 0x18(%rsp), %xmm5
movsd 0xf62(%rip), %xmm4      # 0x1f060
movsd 0x8(%rsp), %xmm3
movapd %xmm7, %xmm1
movapd 0xf00(%rip), %xmm2      # 0x1f010
xorpd %xmm2, %xmm1
movapd %xmm10, %xmm8
xorpd %xmm2, %xmm8
unpcklpd %xmm8, %xmm1            # xmm1 = xmm1[0],xmm8[0]
mulpd 0x140(%rsp), %xmm1
movapd %xmm8, %xmm9
unpcklpd %xmm7, %xmm9            # xmm9 = xmm9[0],xmm7[0]
mulpd 0x150(%rsp), %xmm9
addpd %xmm1, %xmm9
divpd 0x130(%rsp), %xmm9
movapd %xmm9, %xmm2
unpckhpd %xmm9, %xmm2            # xmm2 = xmm2[1],xmm9[1]
subsd 0x80(%rsp), %xmm2
divsd 0x98(%rsp), %xmm2
movapd 0x20(%rsp), %xmm1
addsd %xmm9, %xmm1
addsd %xmm10, %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, %xmm10
unpcklpd %xmm1, %xmm10           # xmm10 = xmm10[0],xmm1[0]
mulpd 0xd0(%rsp), %xmm10
movapd %xmm2, %xmm7
unpcklpd %xmm2, %xmm7            # xmm7 = xmm7[0],xmm2[0]
mulpd 0xa0(%rsp), %xmm7
mulsd 0xe0(%rsp), %xmm1
mulsd 0xb0(%rsp), %xmm2
addsd %xmm1, %xmm2
movapd %xmm2, 0x50(%rsp)
movapd %xmm2, %xmm1
divsd %xmm3, %xmm1
movapd %xmm1, %xmm0
mulsd 0x4f94(%rip), %xmm0     # 0x23168
mulsd %xmm1, %xmm0
addsd %xmm4, %xmm0
ucomisd %xmm6, %xmm0
movapd %xmm8, 0x120(%rsp)
jb 0x1e1f2
sqrtsd %xmm0, %xmm0
jmp 0x1e243
movapd %xmm7, 0x30(%rsp)
movapd %xmm9, 0x110(%rsp)
movapd %xmm10, 0x100(%rsp)
callq 0x52e0
movapd 0x100(%rsp), %xmm10
movapd 0x110(%rsp), %xmm9
movapd 0x30(%rsp), %xmm7
xorpd %xmm6, %xmm6
movsd 0x18(%rsp), %xmm5
movsd 0xe23(%rip), %xmm4      # 0x1f060
movsd 0x8(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
movsd 0x4c46(%rip), %xmm8     # 0x22e98
divsd %xmm0, %xmm8
addpd %xmm10, %xmm7
movapd %xmm3, %xmm0
movapd %xmm8, %xmm3
jmp 0x1e04b
subsd %xmm8, %xmm0
andpd 0xdd9(%rip), %xmm0      # 0x1f050
ucomisd 0xde1(%rip), %xmm0      # 0x1f060
jbe 0x1e2ac
leaq 0x60(%rsp), %r14
movq %r14, %rdi
callq 0x1e72e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1e99c
movq %rbx, %rax
addq $0x180, %rsp            # imm = 0x180
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movsd %xmm8, 0x8(%rsp)
movapd %xmm7, 0x30(%rsp)
movapd %xmm9, %xmm1
unpckhpd %xmm9, %xmm1            # xmm1 = xmm1[1],xmm9[1]
movapd %xmm9, %xmm0
callq 0x5090
movsd 0x18(%rsp), %xmm2
movapd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
subsd 0x120(%rsp), %xmm1
movapd %xmm1, 0x20(%rsp)
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jb 0x1e2fc
sqrtsd %xmm2, %xmm1
jmp 0x1e309
movapd %xmm2, %xmm0
callq 0x52e0
movapd %xmm0, %xmm1
movapd 0x50(%rsp), %xmm0
mulsd 0x20(%rsp), %xmm1
movsd %xmm1, 0x18(%rsp)
divsd 0x8(%rsp), %xmm0
callq 0x55d0
callq 0x5230
movsd 0x4e2d(%rip), %xmm1     # 0x23160
callq 0x5090
movsd %xmm0, 0x8(%rsp)
movapd 0x30(%rsp), %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
callq 0x5090
movsd 0x18(%rsp), %xmm1
mulsd 0x48(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0x8(%rsp), %xmm1
movsd %xmm1, 0xe8(%rsp)
movsd %xmm0, 0xf0(%rsp)
andq $0x0, 0xf8(%rsp)
leaq 0x60(%rsp), %rsi
movq %r14, 0x8(%rsi)
movapd 0x4ded(%rip), %xmm0     # 0x23180
movupd %xmm0, 0x10(%rsi)
movq %rbx, %rdi
callq 0x1e9d8
jmp 0x1e299
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0xfe08
movq %rbx, %rdi
callq 0x5640