-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
85 lines (75 loc) · 2.43 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include "matrix.hpp"
#include "quaternion.hpp"
#include "axisAngle.hpp"
#include <cmath>
#include "test.hpp"
#include <string>
#include <fstream>
#include "testCompatibility.hpp"
#include <optional>
typedef std::array<double,3> point;
class Points{
private:
std::vector<point> data;
public:
Points():data{}{};
Points(const std::vector<point> &d): data{d}{}; //construct from data mtx
Points(const std::string & filename) { //construct from file
std::ifstream myfile(filename);
data.reserve(1000);
double x, y, z;
while (myfile >> x >> y >> z) { //Read a row of the file
point read {x, y, z};
data.push_back(read);
}
}
Points rotate(const std::optional<quaternion<double>> &quaternion) const {
std::vector<point> rotated;
if(quaternion){
rotated.reserve(data.size());
for(auto e : data){
auto result = rotateByQuaternion(quaternion.value(), e);
if(result){ // rotate each point, push back to result vector if succesful.
rotated.push_back(result.value());
}
}
}
return Points(rotated);
}
Points rotate(const std::optional<Matrix3<double>> &M) const {
std::vector<point> rotated;
if(M){
rotated.reserve(data.size());
for(auto e : data){
auto result = M.value()*e;
if(result){ // rotate each point, push back to result vector if succesful.
rotated.push_back(result.value());
}
}
}
return Points(rotated);
}
void writeToFile(const std::string & filename) const {
std::ofstream output;
output.open(filename);
for(auto e : data){
output << e[0] << " " << e[1] << " " << e[2] << "\n";
}
}
};
int main() {
//Basic Test cases:
TestQuaternion();
TestMatrix();
TestAxisAngle();
TestCompatibility();
//
//Rotating an ellipse :
//defining the quaternions, rotate around y axis by 45 degrees
axisAngle<double> rot({1./std::sqrt(2), 1./std::sqrt(2), 0.}, 45.);
Points ellipse("ellipse.dat");
Points rotated = ellipse.rotate(rot.convertToQuaternion());
rotated.writeToFile("quat_ellipse.dat");
return 0;
}