Last active
April 18, 2018 09:17
-
-
Save oofin008/3b3b06b6b4e1fd10cf025a37474fa52c to your computer and use it in GitHub Desktop.
3D Matrix Rotation using C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#ifndef _FILEIMPORT_H | |
#define _FILEIMPORT_H | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <sstream> | |
#include <string.h> | |
#include "matrixrotate.h" | |
using namespace std; | |
class myfile | |
{ | |
protected://Move every variable to here | |
vector<point> g_mydata; | |
string g_fname; | |
public: | |
void menu(void) | |
{ | |
openfile(); | |
getData2(); | |
if (g_mydata.empty()) cout << "No data in here" << endl; | |
else | |
{ | |
/*for (int count = 0; count < g_mydata.size(); count++) | |
{ | |
//when use struct point | |
cout << "X = " << g_mydata[count].x << ",Y = " << g_mydata[count].y << ",Z = " << g_mydata[count].z << endl; | |
//mydata[count].hello(); //when use class point | |
}*/ | |
testRotate(); | |
} | |
} | |
void set_fname(string a) { g_fname = a; } | |
void set_data(vector<point> a) { g_mydata = a; } | |
void openfile() | |
{ | |
string o_fname; | |
string input_fname; | |
ifstream myfile; | |
string line; | |
cout << "****** 3D matrix rotation ******" << endl << "**** By Santi L. ID 5811191 ****" << endl; | |
cout << "Enter file name correctly : "; | |
cin >> o_fname; | |
myfile.open(o_fname); | |
cout << "File name: " << o_fname << endl; | |
if (myfile.is_open()) | |
{ | |
cout << "Status : OPEN" << endl; | |
cout << "CONTENT OF FILE : \n"; | |
while (getline(myfile, line)) | |
{ | |
cout << line << endl; | |
} | |
myfile.close(); | |
set_fname(o_fname); | |
} | |
else | |
{ | |
cout << "Status : FAIL" << endl; | |
myfile.close(); | |
} | |
} | |
void getData2() | |
{ | |
ifstream myfile; | |
myfile.open(g_fname); | |
double i = 0, j = 0, k = 0; | |
string line; | |
vector<point> mydata; | |
if (myfile.is_open()) | |
{ | |
cout << "\nFetch data from file : \n"; | |
int count = 0; | |
while (getline(myfile, line)) | |
{ | |
if (!isdigit(line[0])) | |
{ | |
cout << "skip this line, it's not digit" << endl; | |
} | |
else | |
{ | |
stringstream ss(line); | |
string token; | |
mydata.push_back(point()); | |
ss >> i; | |
mydata[count].x = i; | |
getline(ss, token, ','); | |
ss >> j; | |
mydata[count].y = j; | |
getline(ss, token, ','); | |
ss >> k; | |
mydata[count].z = k; | |
count++; | |
cout << i << "," << j << "," << k << endl; | |
} | |
} | |
/*if (mydata.empty()) cout << "No data in here" << endl; | |
else | |
{ | |
for (int count = 0; count < mydata.size(); count++) | |
{ | |
cout << "X = " << mydata[count].x << ",Y = " << mydata[count].y << ",Z = " << mydata[count].z << endl; | |
} | |
}*/ | |
cout << endl; | |
} | |
set_data(mydata); | |
myfile.close(); | |
} | |
void testRotate()//HERE | |
{ | |
char myaxis; | |
int deg; | |
char yesno; | |
double rad; | |
int again = 1; //Make program continue rotate | |
//vector<point> result; | |
while (again = 1) { | |
//Get rotation axis | |
cout << "\n Pick axis for rotaion: x, y, z :"; | |
cin >> myaxis; | |
myaxis = toupper(myaxis); | |
while (myaxis != 'X' && myaxis != 'Y' && myaxis != 'Z') | |
{ | |
cout << "ERROR: Invalid Input ! " << endl; | |
cout << "Pick axis for rotaion: x, y, z :"; | |
cin >> myaxis; | |
myaxis = toupper(myaxis); | |
} | |
//Get rotation angle | |
cout << "Enter rotation angle: "; | |
cin >> deg; | |
while (cin.fail()) | |
{ | |
cout << "ERROR: Invalid Input ! " << endl; | |
cout << "Enter rotation angle: "; | |
cin.clear(); | |
cin.ignore(); | |
cin >> deg; | |
} | |
cout << "Rotate Axis: " << myaxis << " ,Rotate Degree: " << deg << endl; | |
//Convert Degree to Radian | |
rad = deg*PI / 180; | |
//Rotate X-axis | |
if (myaxis == 'X') | |
{ | |
rotate_x r_x; | |
abstract_rotate *p_rotate = &r_x; | |
p_rotate->set_rad(rad); | |
g_mydata = p_rotate->rotate_result(g_mydata); | |
} | |
//Rotate Y-axis | |
if (myaxis == 'Y') | |
{ | |
rotate_y r_y; | |
abstract_rotate *p_rotate = &r_y; | |
p_rotate->set_rad(rad); | |
g_mydata = p_rotate->rotate_result(g_mydata); | |
} | |
//Rotate Z-axis | |
if (myaxis == 'Z') | |
{ | |
rotate_z r_z; | |
abstract_rotate *p_rotate = &r_z; | |
p_rotate->set_rad(rad); | |
g_mydata = p_rotate->rotate_result(g_mydata); | |
} | |
//Show Result of Rotation | |
for (int count = 0; count < g_mydata.size(); count++) { | |
cout << "X = " << g_mydata[count].x << ",Y = " << g_mydata[count].y << ",Z = " << g_mydata[count].z << endl; | |
} | |
//Set all value back to 0 and ask if continue | |
deg = 0; | |
rad = 0; | |
cout << "\nDo you want to continue?(Y/N) : "; | |
cin >> yesno; | |
yesno = toupper(yesno); | |
while (yesno != 'Y' && yesno != 'N') | |
{ | |
cout << "ERROR: Invalid Input ! " << endl; | |
cout << "Do you want to continue?(Y/N) : "; | |
cin >> yesno; | |
yesno = toupper(yesno); | |
} | |
if (yesno == 'N') | |
{ | |
again = 0; | |
} | |
}//End while(again=1) loop Here | |
} | |
}; | |
// No use function , just test | |
/* | |
void newfile(string fname) | |
{ | |
ofstream myfile; | |
myfile.open(fname); | |
myfile << "THIS IS YOUR NEW FILE \n"; | |
myfile.close(); | |
} | |
void getData(string fname) | |
{ | |
ifstream myfile; | |
myfile.open(fname); | |
int i,j,k; | |
string line; | |
if (myfile.is_open()) | |
{ | |
int point = 0; | |
while (getline(myfile,line)) | |
{ | |
try | |
{ | |
myfile >> i; | |
myfile >> j; | |
myfile >> k; | |
myvector.push_back(i); | |
myvector.push_back(j); | |
myvector.push_back(k); | |
} | |
catch(...) | |
{ | |
i = 0; | |
j = 0; | |
k = 0; | |
} | |
cout << i << "," << j << "," << k; | |
} | |
cout << endl; | |
} | |
myfile.close(); | |
} | |
*/ | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "fileimport.h" | |
#include "matrixrotate.h" | |
int main() | |
{ | |
myfile test; | |
test.menu(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _MAXTRIXROTATE_H | |
#define _MAXTRIXROTATE_H | |
#include <iostream> | |
#include <string.h> | |
#include <vector> | |
#include <math.h> | |
#define PI 3.14159265 | |
using namespace std; | |
struct point { | |
double x; | |
double y; | |
double z; | |
}; | |
/*class point { | |
public: | |
float x; | |
float y; | |
float z; | |
void hello() | |
{ | |
cout << x << " " << y << " " << z << endl; | |
} | |
virtual float rotate() | |
};*/ | |
class abstract_rotate { | |
protected: | |
double rad; | |
public: | |
void set_rad(double r) { rad = r; } | |
virtual vector<point> rotate_result(vector<point> mydata) = 0; | |
}; | |
class rotate_x : public abstract_rotate { | |
public: | |
vector<point> rotate_result(vector<point> mydata) { | |
vector<point> myresult = mydata; | |
for (int count = 0; count < mydata.size(); count++) | |
{ | |
myresult[count].x = mydata[count].x; | |
myresult[count].y = (mydata[count].y * cos(rad)) - (mydata[count].z * sin(rad)) ; | |
myresult[count].z = (mydata[count].y * sin(rad)) + (mydata[count].z * cos(rad)) ; | |
} | |
return myresult; | |
} | |
}; | |
class rotate_y : public abstract_rotate { | |
public: | |
vector<point> rotate_result(vector<point> mydata) { | |
vector<point> myresult = mydata; | |
for (int count = 0; count < mydata.size(); count++) | |
{ | |
myresult[count].x = (mydata[count].x * cos(rad)) + (mydata[count].z * sin(rad)); | |
myresult[count].y = mydata[count].y; | |
myresult[count].z = (-mydata[count].x * sin(rad)) + (mydata[count].z * cos(rad)); | |
} | |
return myresult; | |
} | |
}; | |
class rotate_z : public abstract_rotate { | |
public: | |
vector<point> rotate_result(vector<point> mydata) { | |
vector<point> myresult = mydata; | |
for (int count = 0; count < mydata.size(); count++) | |
{ | |
myresult[count].x = (mydata[count].x * cos(rad)) - (mydata[count].y * sin(rad)); | |
myresult[count].y = (mydata[count].x * sin(rad)) + (mydata[count].y * cos(rad)); | |
myresult[count].z = mydata[count].z; | |
} | |
return myresult; | |
} | |
}; | |
//No use function, just for education | |
/*void testVector() | |
{ | |
vector<point> sub; | |
//Push back new point created with default constructor. | |
sub.push_back(point()); | |
//Vector now has 1 element @ index 0 , so modify it | |
sub[0].x = 1.1; | |
sub[0].y = 2.2; | |
sub[0].z = 3.3; | |
//Add new point | |
sub.push_back(point()); | |
//modify again | |
sub[1].x = 4.1; | |
sub[1].y = 5.2; | |
sub[1].z = 6.3; | |
//Access point and print out | |
if (sub.empty()) cout << "No data in here" << endl; | |
else | |
{ | |
for (int count = 0; count < sub.size(); count++) | |
{ | |
cout << "X = " << sub[count].x << ",Y = " <<sub[count].y<<",Z = "<<sub[count].z<<endl; | |
} | |
} | |
}*/ | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment