
#include "../Header/myPCA.h"
#include <string>

myPCA::myPCA()
{
	std::cout << "PCA obj created" << std::endl;
}


myPCA::~myPCA()
{
	std::cout << "PCA obj destructed" << std::endl;
}

int myPCA::readfile(std::string PCA_ref_filename, std::string PCA_deform_normal_filename, std::string PCA_eigenvec_filename) {
	float temp_value, temp_value2;
	std::string line;
	std::string temp_number;

	// read ref file and deform file
	std::ifstream infile0(PCA_ref_filename);
	std::ifstream infile1(PCA_deform_normal_filename);
	while (infile0 >> temp_value && infile1 >> temp_value2) {
		ref_shape_1d.push_back(temp_value);
		deform_norm_1d.push_back((double)temp_value2);
		ref_and_deform_1d.push_back((double)temp_value + (double)temp_value2);
	}
	ref_and_deform_1d_mat = cv::Mat(ref_and_deform_1d);
	infile0.close();
	infile1.close();
	std::cout << "PCA_ref_file read complete." << std::endl;
	std::cout << "PCA_deform_normal_file read complete." << std::endl;


	// read eigen vectors
	std::ifstream infile2(PCA_eigenvec_filename);
	num_featurepnt = 0;
	std::vector<std::vector<float> > eigenvect;
	while (std::getline(infile2, line)) {
		// Got a line
		std::stringstream linestream2(line);
		num_eigenvect = 0;
		std::vector<float> temp_row2;
		while (linestream2 >> temp_number) {
			temp_value = std::strtof((temp_number).c_str(), 0);
			temp_row2.push_back(temp_value);
			num_eigenvect++;
		}
		eigenvect.push_back(temp_row2);
		num_featurepnt++;
	}

	cv::Mat tempEigenVect(num_featurepnt, num_eigenvect, CV_64F);
	for (int row = 0; row < num_featurepnt; row++) {
		for (int col = 0; col < num_eigenvect; col++) {
			tempEigenVect.at<double>(row, col) = (double)eigenvect[row][col];
		}
	}

	eigenVectors = tempEigenVect.clone();
	num_featurepnt = num_featurepnt / 3;
	infile2.close();
	std::cout << "PCA_eigenvec_file read complete." << std::endl;

	return 0;
}

