-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformation.cpp
171 lines (141 loc) · 4.55 KB
/
transformation.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "transformation.h"
#include <limits>
#define PI 3.14159265359
Transformation::Transformation(int transformationType, int transformCoordinates) :
transformationType(transformationType),
transformCoordinates(transformCoordinates),
matrix(new QMatrix4x4()),
spins(0.10),
radius(0.25),
maxZ(0.0),
minZ(0.0)
{
}
Transformation::~Transformation()
{
delete matrix;
}
int Transformation::getTransformCoordinates() const
{
return transformCoordinates;
}
void Transformation::setTransformCoordinates(int transformCoordinates)
{
Transformation::transformCoordinates = transformCoordinates;
}
int Transformation::getTransformationType() const
{
return transformationType;
}
void Transformation::setTransformationType(int transformationType)
{
switch (transformationType) {
case TRANSFORMATION_LINEAR:
case TRANSFORMATION_TWISTING:
case TRANSFORMATION_TAPPERING:
case TRANSFORMATION_BENDING:
Transformation::transformationType = transformationType;
break;
default:
qDebug() << "Unknown transformationType: " << transformationType << ". Ignoring.";
}
}
void Transformation::transform(Vertex *vertex)
{
switch(transformationType) {
case TRANSFORMATION_LINEAR:
vertex->setQVector((*matrix)*(vertex->toQVector()));
break;
case TRANSFORMATION_TWISTING: {
double f = spins*PI/(maxZ-minZ)*vertex->z();
QMatrix4x4 m(cos(f), -1*sin(f), 0, 0,
sin(f), cos(f), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
vertex->setQVector(m*(vertex->toQVector()));
}
break;
case TRANSFORMATION_TAPPERING: {
double f = maxZ - vertex->z();
QMatrix4x4 m(f, 0, 0, 0,
0, f, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
vertex->setQVector(m*(vertex->toQVector()));
}
break;
case TRANSFORMATION_BENDING: {
// Avoid division by zero
double safe_z = vertex->z();
if (safe_z == 0.0)
safe_z = std::numeric_limits<double>::min();
double a = radius - radius * cos(vertex->z()/radius)/safe_z;
double b = radius * sin(vertex->z()/radius)/safe_z;
QMatrix4x4 m(1, 0, a, 0,
0, 1, 0, 0,
0, 0, b, 0,
0, 0, 0, 1);
vertex->setQVector(m*(vertex->toQVector()));
}
break;
}
}
void Transformation::transform(QList<Vertex *> &vertexes)
{
minZ = maxZ = vertexes.at(1)->z();
for (int i = 0; i<vertexes.size(); i++) {
if (vertexes.at(i)->z() > maxZ)
maxZ = vertexes.at(i)->z();
if (vertexes.at(i)->z() < minZ)
minZ = vertexes.at(i)->z();
}
for (int i = 0; i<vertexes.size(); i++)
transform(vertexes.at(i));
}
QString Transformation::toString()
{
switch(transformationType) {
case TRANSFORMATION_LINEAR:
return QString("(%1, %2, %3, %4)\n(%5, %6, %7, %8)\n(%9, %10, %11, %12)\n(%13, %14, %15, %16)")
.arg(matrix->row(0).w()).arg(matrix->row(0).x()).arg(matrix->row(0).y()).arg(matrix->row(0).z())
.arg(matrix->row(1).w()).arg(matrix->row(1).x()).arg(matrix->row(1).y()).arg(matrix->row(1).z())
.arg(matrix->row(2).w()).arg(matrix->row(2).x()).arg(matrix->row(2).y()).arg(matrix->row(2).z())
.arg(matrix->row(3).w()).arg(matrix->row(3).x()).arg(matrix->row(3).y()).arg(matrix->row(3).z());
case TRANSFORMATION_TWISTING:
return QString("Twisting");
case TRANSFORMATION_TAPPERING:
return QString("Tappering");
case TRANSFORMATION_BENDING:
return QString("Bending");
default:
return QString("Unknown transformation type");
}
}
// Linear
QMatrix4x4 *Transformation::getMatrix()
{
return matrix;
}
void Transformation::setMatrix(QMatrix4x4 *matrix)
{
delete Transformation::matrix;
Transformation::matrix = matrix;
}
// Twisting
double Transformation::getSpins() const
{
return spins;
}
void Transformation::setSpins(double spins)
{
Transformation::spins = spins;
}
// Bending
double Transformation::getRadius() const
{
return radius;
}
void Transformation::setRadius(double radius)
{
Transformation::radius = radius;
}