Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/gprim/transform3d.hpp
3206 views
1
#ifndef FILE_TRANSFORM3D
2
#define FILE_TRANSFORM3D
3
4
/* *************************************************************************/
5
/* File: transform3d.hh */
6
/* Author: Joachim Schoeberl */
7
/* Date: 22. Mar. 98 */
8
/* *************************************************************************/
9
10
/*
11
Affine - Linear mapping in 3D space
12
*/
13
14
class Transformation3d;
15
ostream & operator<< (ostream & ost, Transformation3d & trans);
16
17
class Transformation3d
18
{
19
double lin[3][3];
20
double offset[3];
21
public:
22
///
23
Transformation3d ();
24
/// Unit tet is mapped to tet descibed by pp
25
Transformation3d (const Point3d ** pp);
26
/// Unit tet is mapped to tet descibed by pp
27
Transformation3d (const Point3d pp[]);
28
/// translation
29
Transformation3d (const Vec3d & translate);
30
/// rotation with ...
31
Transformation3d (const Point3d & c, double alpha, double beta, double gamma);
32
///
33
void CalcInverse (Transformation3d & inv) const;
34
/// this = ta x tb
35
void Combine (const Transformation3d & ta, const Transformation3d & tb);
36
/// dir = 1..3 (== x..z)
37
void SetAxisRotation (int dir, double alpha);
38
///
39
void Transform (const Point3d & from, Point3d & to) const
40
{
41
for (int i = 1; i <= 3; i++)
42
{
43
to.X(i) = offset[i-1] + lin[i-1][0] * from.X(1) +
44
lin[i-1][1] * from.X(2) + lin[i-1][2] * from.X(3);
45
}
46
}
47
48
///
49
void Transform (Point3d & p) const
50
{
51
Point3d hp;
52
Transform (p, hp);
53
p = hp;
54
}
55
56
/// transform vector, apply only linear part, not offset
57
void Transform (const Vec3d & from, Vec3d & to) const
58
{
59
for (int i = 1; i <= 3; i++)
60
{
61
to.X(i) = lin[i-1][0] * from.X(1) +
62
lin[i-1][1] * from.X(2) + lin[i-1][2] * from.X(3);
63
}
64
}
65
friend ostream & operator<< (ostream & ost, Transformation3d & trans);
66
};
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
template <int D>
82
class Transformation
83
{
84
Mat<D> m;
85
Vec<D> v;
86
public:
87
///
88
Transformation () { m = 0; v = 0; }
89
90
/// Unit tet is mapped to tet descibed by pp
91
Transformation (const Point<D> * pp);
92
93
/// translation
94
Transformation (const Vec<D> & translate)
95
{
96
v = translate;
97
m = 0;
98
for (int i = 0; i < D; i++)
99
m(i,i) = 1;
100
}
101
102
// rotation with ...
103
Transformation (const Point<D> & c, double alpha, double beta, double gamma)
104
{
105
// total = T_c x Rot_0 x T_c^{-1}
106
// Use Euler angles, see many books from tech mech, e.g.
107
// Shabana "multibody systems"
108
109
Vec<D> vc(c);
110
Transformation<D> tc(vc);
111
Transformation<D> tcinv(-vc);
112
// tc.CalcInverse (tcinv);
113
114
Transformation<D> r1, r2, r3, ht, ht2;
115
r1.SetAxisRotation (3, alpha);
116
r2.SetAxisRotation (1, beta);
117
r3.SetAxisRotation (3, gamma);
118
119
ht.Combine (tc, r3);
120
ht2.Combine (ht, r2);
121
ht.Combine (ht2, r1);
122
Combine (ht, tcinv);
123
124
// cout << "Rotation - Transformation:" << (*this) << endl;
125
// (*testout) << "Rotation - Transformation:" << (*this) << endl;
126
}
127
128
///
129
void CalcInverse (Transformation & inv) const;
130
131
/// this = ta x tb
132
void Combine (const Transformation & ta, const Transformation & tb)
133
{
134
v = ta.v + ta.m * tb.v;
135
m = ta.m * tb.m;
136
}
137
138
139
140
/// dir = 1..3 (== x..z)
141
void SetAxisRotation (int dir, double alpha)
142
{
143
double co = cos(alpha);
144
double si = sin(alpha);
145
dir--;
146
int pos1 = (dir+1) % 3;
147
int pos2 = (dir+2) % 3;
148
149
int i, j;
150
for (i = 0; i <= 2; i++)
151
{
152
v(i) = 0;
153
for (j = 0; j <= 2; j++)
154
m(i,j) = 0;
155
}
156
157
m(dir,dir) = 1;
158
m(pos1, pos1) = co;
159
m(pos2, pos2) = co;
160
m(pos1, pos2) = si;
161
m(pos2, pos1) = -si;
162
}
163
164
///
165
void Transform (const Point<D> & from, Point<D> & to) const
166
{
167
to = Point<D> (v + m * Vec<D>(from));
168
}
169
170
void Transform (Point<D> & p) const
171
{
172
p = Point<D> (v + m * Vec<D>(p));
173
}
174
175
176
177
/// transform vector, apply only linear part, not offset
178
void Transform (const Vec<D> & from, Vec<D> & to) const
179
{
180
to = m * from;
181
}
182
};
183
184
template <int D>
185
ostream & operator<< (ostream & ost, Transformation<D> & trans);
186
187
188
189
190
#endif
191
192