#include "SateOrbit.h"
//#define EIGEN_USE_MKL_ALL
//#define EIGEN_VECTORIZE_SSE4_2
//#include <mkl.h>
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <time.h>
//#include <mkl.h>

using namespace std;
using namespace Eigen;



OrbitPoly::OrbitPoly()
{
}

OrbitPoly::OrbitPoly(int polynum, Eigen::MatrixX<double> polySatellitePara, double SatelliteModelStartTime)
{
	if (polySatellitePara.rows() != polynum||polySatellitePara.cols()!=6) {
		throw exception("轨道模型参数形状不一致");
	}
	this->polySatellitePara = polySatellitePara;
	this->SatelliteModelStartTime = SatelliteModelStartTime;
	this->polynum = polynum;
}

OrbitPoly::~OrbitPoly()
{
}

Eigen::MatrixX<double> OrbitPoly::SatelliteSpacePoint(Eigen::MatrixX<double> satellitetime)
{
	Eigen::MatrixX<double> result= SatelliteSpacePoints(satellitetime, this->SatelliteModelStartTime, this->polySatellitePara, this->polynum);
	return result;
}

Eigen::MatrixX<double> OrbitPoly::SatelliteSpacePoint(long double satellitetime) {

	if (this->polySatellitePara.rows() != polynum || this->polySatellitePara.cols() != 6) {
		throw exception("the size of satellitetime has error!! row: p1,p2,p3,p4 col: x,y,z,vx,vy,vz ");
	}
	// 更新轨道时间
	double satellitetime2 =double( satellitetime - this->SatelliteModelStartTime);
	Eigen::MatrixX<double> satetime(1, polynum);
	for (int i = 0; i < polynum; i++) {
		satetime(0, i) = pow(satellitetime2, i);
	}

	// 计算
	Eigen::MatrixX<double> satellitePoints(1, 6);
	satellitePoints = satetime * polySatellitePara;
	return satellitePoints;
}

/// <summary>
/// 获取指定时刻的卫星轨道坐标
/// </summary>
/// <param name="satellitetime">卫星时刻</param>
/// <param name="SatelliteModelStartTime">模型起算时间</param>
/// <param name="polySatellitePara">模型参数[x1,y1,z1,vx1,vy1,vz1;  x2,y2,z2,vx2,vy2,vz2; .....  ]</param>
/// <param name="polynum">模型参数数量</param>
/// <returns>卫星坐标</returns>
Eigen::MatrixX<double> SatelliteSpacePoints(Eigen::MatrixX<double>& satellitetime, double SatelliteModelStartTime, Eigen::MatrixX<double>& polySatellitePara, int polynum)
{
	if (satellitetime.cols() != 1) {
		throw exception("the size of satellitetime has error!!");
	}
	if (polySatellitePara.rows() != polynum || polySatellitePara.cols() != 6) {
		throw exception("the size of satellitetime has error!! row: p1,p2,p3,p4 col: x,y,z,vx,vy,vz ");
	}
	// 更新轨道时间
	int satellitetime_num = satellitetime.rows();
	satellitetime = satellitetime.array() - SatelliteModelStartTime;
	Eigen::MatrixX<double> satelliteTime(satellitetime_num, polynum);
	for (int i = 0; i < polynum; i++) {
		satelliteTime.col(i) = satellitetime.array().pow(i);
	}

	// 计算
	Eigen::MatrixX<double> satellitePoints(satellitetime_num, 6);
	satellitePoints = satelliteTime * polySatellitePara;
	return satellitePoints;
}
