-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvektorr3.h
334 lines (264 loc) · 9.14 KB
/
vektorr3.h
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
*/
#ifndef _VektorR3_h
#define _VektorR3_h
#include <iostream>
#include <math.h>
using namespace std;
class VektorR3
{
private:
double c[3]; // Drei Komponenten des dreidim. Vektors
public:
VektorR3() {}
VektorR3(double x, double y, double z) {c[0] = x; c[1] = y; c[2] = z;}
VektorR3(VektorR3& Vektor) {c[0] = Vektor.c[0]; c[1] = Vektor.c[1]; c[2] = Vektor.c[2];}
VektorR3(VektorR3 const &Vektor) {c[0] = Vektor.c[0]; c[1] = Vektor.c[1]; c[2] = Vektor.c[2];}
double& operator [] (const int);
double operator [] (const int) const;
const bool operator == (const VektorR3&) const;
const VektorR3 operator + (const VektorR3&) const;
const VektorR3& operator += (const VektorR3&);
const VektorR3 operator - (const VektorR3&) const;
const VektorR3& operator -= (const VektorR3&);
const double operator * (const VektorR3&) const;
friend const VektorR3 operator * (const double, const VektorR3&);
const VektorR3 operator * (const double);
const VektorR3& operator *= (const double);
const VektorR3 operator / (const double) const;
const VektorR3& operator /= (const double);
static const VektorR3 CrossProduct(const VektorR3&, const VektorR3&);
void print() const {cout << c[0] << "," << c[1] << "," << c[2];};
double Norm2() const;
const double Normalize();
const VektorR3 A(VektorR3*) const;
static void SetRotMatrix(VektorR3*, double, double, double);
static void SetRotMatrix(VektorR3*, VektorR3, VektorR3);
static void MatrixMult(VektorR3*, VektorR3*, VektorR3*);
// static void CalcInverse(const VektorR3 const*, VektorR3*);
void LoadVektorR3(istream&);
void SaveVektorR3(ostream&);
}; // class VektorR3
// Inline-Elementfunktionen von VektorR3
inline double& VektorR3::operator [] (const int c_index)
{
return(c[c_index]);
} // VektorR3::operator []
inline double VektorR3::operator [] (const int c_index) const
{
return(c[c_index]);
} // VektorR3::operator []
inline const bool VektorR3::operator == (const VektorR3& Vektor) const
{
return((c[0] == Vektor.c[0]) && (c[1] == Vektor.c[1]) && (c[2] == Vektor.c[2]));
} // VektorR3::operator ==
inline const VektorR3 VektorR3::operator + (const VektorR3& Vektor) const
{
return VektorR3(c[0] + Vektor.c[0], c[1] + Vektor.c[1], c[2] + Vektor.c[2]);
} // VektorR3::operator+
inline const VektorR3& VektorR3::operator += (const VektorR3& Vektor)
{
c[0] += Vektor.c[0];
c[1] += Vektor.c[1];
c[2] += Vektor.c[2];
return *this;
} // VektorR3::operator +=
inline const VektorR3 VektorR3::operator - (const VektorR3& Vektor) const
{
return VektorR3(c[0] - Vektor.c[0], c[1] - Vektor.c[1], c[2] - Vektor.c[2]);
} // VektorR3::operator -
inline const VektorR3& VektorR3::operator -= (const VektorR3& Vektor)
{
c[0] -= Vektor.c[0];
c[1] -= Vektor.c[1];
c[2] -= Vektor.c[2];
return *this;
} // VektorR3::operator -=
inline const double VektorR3::operator * (const VektorR3& Vektor) const
{
return (c[0]*Vektor.c[0] + c[1]*Vektor.c[1] + c[2]*Vektor.c[2]);
} // VektorR3::operator *
inline const VektorR3 operator * (const double s, const VektorR3& Vektor)
{
return VektorR3(s*Vektor.c[0], s*Vektor.c[1], s*Vektor.c[2]);
} // friend operator * (const double, const VektorR3&);
inline const VektorR3 VektorR3::operator * (const double s)
{
return VektorR3(s*c[0], s*c[1], s*c[2]);
} // VektorR3::operator * (const double, const VektorR3&);
inline const VektorR3& VektorR3::operator *= (const double s)
{
c[0] *= s;
c[1] *= s;
c[2] *= s;
return *this;
} // VektorR3::opertor *= (const double&)
inline const VektorR3 VektorR3::operator / (const double s) const
{
return VektorR3(c[0]/s, c[1]/s, c[2]/s);
} // VektorR3::operator /
inline const VektorR3& VektorR3::operator /= (const double s)
{
c[0] /= s;
c[1] /= s;
c[2] /= s;
return *this;
} // VektorR3::operator /=
inline double VektorR3::Norm2() const
{
return ((double)(sqrt((double)(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]))));
} // VektorR3::Norm2
inline const double VektorR3::Normalize()
{
double length;
if((length = Norm2()) != 0.0)
{
*this /= length;
return length;
} // if
return -1.0;
} // VektorR3::Normalize
// Globale Operatoren
/*
inline ostream& operator << (ostream &OutStrm, VektorR3 &v)
{
v.Save(OutStrm);
return(OutStrm);
} // operator <<
inline istream& operator >> (istream &InStrm, VektorR3 &v)
{
v.Load(InStrm);
return(InStrm);
} // operator >>
*/
inline const VektorR3 VektorR3::CrossProduct(const VektorR3 &a, const VektorR3 &b)
{
VektorR3 r;
r[0] = a[1]*b[2] - a[2]*b[1];
r[1] = a[2]*b[0] - a[0]*b[2];
r[2] = a[0]*b[1] - a[1]*b[0];
return r;
} // VektorR3::CrossProduct
inline const VektorR3 VektorR3::A(VektorR3 *A) const
{
VektorR3 b;
b[0] = A[0][0]*c[0] + A[1][0]*c[1] + A[2][0]*c[2];
b[1] = A[0][1]*c[0] + A[1][1]*c[1] + A[2][1]*c[2];
b[2] = A[0][2]*c[0] + A[1][2]*c[1] + A[2][2]*c[2];
return(b);
} // VektorR3::A
inline void VektorR3::SetRotMatrix(VektorR3 *Matrix, double Angle_x, double Angle_y, double Angle_z)
{
double a_x = (double)Angle_x*(M_PI/180.0);
double a_y = (double)Angle_y*(M_PI/180.0);
double a_z = (double)Angle_z*(M_PI/180.0);
Matrix[0][0] = (double)(cos(a_y)*cos(a_z));
Matrix[0][1] = (double)(sin(a_x)*sin(a_y)*cos(a_z)+cos(a_x)*sin(a_z));
Matrix[0][2] = (double)(cos(a_x)*sin(a_y)*cos(a_z)-sin(a_x)*sin(a_z));
Matrix[1][0] = (double)(-cos(a_y)*sin(a_z));
Matrix[1][1] = (double)(-sin(a_x)*sin(a_y)*sin(a_z)+cos(a_x)*cos(a_z));
Matrix[1][2] = (double)(-cos(a_x)*sin(a_y)*sin(a_z)-sin(a_x)*cos(a_z));
Matrix[2][0] = (double)-sin(a_y);
Matrix[2][1] = (double)(sin(a_x)*cos(a_y));
Matrix[2][2] = (double)(cos(a_x)*cos(a_y));
} // VektorR3::SetRotMatrix
inline void VektorR3::SetRotMatrix(VektorR3 *Matrix, VektorR3 v1, VektorR3 v2)
{
VektorR3 SpinCS[3], SpinCSInv[3], RotMat[3], TmpMat[3], test;
double Angle, sign1, sign2, tf, MinDist = 100000.0;
int iteration = 0;
v1 /= v1.Norm2(); v2 /= v2.Norm2();
if(v1 == v2)
{
Matrix[0] = VektorR3(1.0, 0.0, 0.0);
Matrix[1] = VektorR3(0.0, 1.0, 0.0);
Matrix[2] = VektorR3(0.0, 0.0, 1.0);
return;
} // if
do
{
switch(iteration)
{
case 0:
sign1 = 1.0; sign2 = 1.0;
break;
case 1:
sign1 = -1.0; sign2 = 1.0;
break;
case 2:
sign1 = 1.0; sign2 = -1.0;
break;
case 3:
sign1 = -1.0; sign2 = -1.0;
break;
} // switch
iteration++;
// Lokales (Dreh-) KS berechnen, in dem um die Z-Achse gedreht wird
SpinCS[0] = v1;
SpinCS[2] = CrossProduct(v1, v2); SpinCS[2] /= sign1*SpinCS[2].Norm2();
SpinCS[1] = CrossProduct(v1, SpinCS[2]); SpinCS[1] /= sign2*SpinCS[1].Norm2();
// Inverses DrehKS berechnen
// CalcInverse(SpinCS, SpinCSInv);
// MatrixMult(TmpMat, SpinCS, SpinCSInv);
tf = v1*v2;
Angle = ((double)acos((double)tf))*((double)(180.0/M_PI));
SetRotMatrix(RotMat, 0.0, 0.0, Angle);
MatrixMult(TmpMat, RotMat, SpinCSInv);
MatrixMult(Matrix, SpinCS, TmpMat);
test = v1.A(Matrix);
tf = (v2-test).Norm2();
if(tf < MinDist) MinDist = tf;
} while((tf > 0.001) && (iteration < 4));
/*
if(iteration == 4)
MessageBox(NULL, "Die Rotationsmatrix konnte nicht berechnet werden.", "VektorR3::SetRotMatrix", MB_OK|MB_ICONQUESTION);
*/
} // VektorR3::SetRotMatrix
inline void VektorR3::MatrixMult(VektorR3 *pParaE, VektorR3 *A, VektorR3 *B)
{
VektorR3 E[3];
// Matrixmultiplikation: E = A*B
for(int zeile = 0; zeile < 3; zeile++)
{
for(int spalte = 0; spalte < 3; spalte++)
{
E[spalte][zeile] = 0.0;
for(int i = 0; i < 3; i++)
E[spalte][zeile] += A[i][zeile] * B[spalte][i];
} // for
} // for
for(int i = 0; i < 3; i++)
pParaE[i] = E[i];
} // VektorR3::MatrixMult
/*
void VektorR3::CalcInverse(const VektorR3 const *SourceMat, VektorR3 *DestMat)
{
Gauss LGS(9);
// Besetzen des 9x9 LGS mit den Elementen der Quellmatrix
LGS.A(1, 1, SourceMat[0][0]); LGS.A(1, 2, SourceMat[0][1]); LGS.A(1, 3, SourceMat[0][2]);
LGS.A(2, 1, SourceMat[1][0]); LGS.A(2, 2, SourceMat[1][1]); LGS.A(2, 3, SourceMat[1][2]);
LGS.A(3, 1, SourceMat[2][0]); LGS.A(3, 2, SourceMat[2][1]); LGS.A(3, 3, SourceMat[2][2]);
LGS.A(4, 4, SourceMat[0][0]); LGS.A(4, 5, SourceMat[0][1]); LGS.A(4, 6, SourceMat[0][2]);
LGS.A(5, 4, SourceMat[1][0]); LGS.A(5, 5, SourceMat[1][1]); LGS.A(5, 6, SourceMat[1][2]);
LGS.A(6, 4, SourceMat[2][0]); LGS.A(6, 5, SourceMat[2][1]); LGS.A(6, 6, SourceMat[2][2]);
LGS.A(7, 7, SourceMat[0][0]); LGS.A(7, 8, SourceMat[0][1]); LGS.A(7, 9, SourceMat[0][2]);
LGS.A(8, 7, SourceMat[1][0]); LGS.A(8, 8, SourceMat[1][1]); LGS.A(8, 9, SourceMat[1][2]);
LGS.A(9, 7, SourceMat[2][0]); LGS.A(9, 8, SourceMat[2][1]); LGS.A(9, 9, SourceMat[2][2]);
// Ergebnis-Vektor definieren
LGS.b(1, 1.0); LGS.b(5, 1.0); LGS.b(9, 1.0);
LGS.Calc(NULL); // Keine Speicherung im Stream
// Zielmatrix besetzen
DestMat[0][0] = LGS.x(1); DestMat[1][0] = LGS.x(2); DestMat[2][0] = LGS.x(3);
DestMat[0][1] = LGS.x(4); DestMat[1][1] = LGS.x(5); DestMat[2][1] = LGS.x(6);
DestMat[0][2] = LGS.x(7); DestMat[1][2] = LGS.x(8); DestMat[2][2] = LGS.x(9);
} // VektorR3::CalcInverse
*/
inline void VektorR3::SaveVektorR3(ostream &strm)
{
strm.write((char*)c, sizeof(VektorR3));
} // VektorR3::Save
inline void VektorR3::LoadVektorR3(istream &strm)
{
strm.read((char*)c, sizeof(VektorR3));
} // VektorR3::Save
#endif