forked from alliepiper/XtalComp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxctransform.h
More file actions
116 lines (93 loc) · 5.6 KB
/
Copy pathxctransform.h
File metadata and controls
116 lines (93 loc) · 5.6 KB
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
/**********************************************************************
XcTransform - Transformation class for transforming XcVectors
WARNING: This is not your typical transform class -- it has been
specialized for XtalComp. It stores the rotation and translation
separately, and applies the translation followed by the rotation
when multiplied by a vector. It may not work for you needs.
Copyright (C) 2011 by David C. Lonie
This source code is released under the New BSD License, (the "License").
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***********************************************************************/
#ifndef XCTRANSFORM_H
#define XCTRANSFORM_H
#include "xcvector.h"
#include "xcmatrix.h"
#include <stdio.h>
class XcTransform
{
public:
XcTransform() {};
XcTransform(const XcTransform &other) : rot(other.rot), trans(other.trans) {}
XcTransform & setIdentity()
{
this->rot.fillFromScalar(1.0);
this->trans.fill(0.0);
return *this;
}
static XcVector ZeroVector;
static XcMatrix IdentityMatrix;
XcTransform & rotate(const XcMatrix &mat) {return this->multiplyByTransform(mat, ZeroVector);}
XcTransform & prerotate(const XcMatrix &mat) {return this->premultiplyByTransform(mat, ZeroVector);}
XcTransform & translate(const XcVector &vec) {return this->multiplyByTransform(IdentityMatrix, vec);}
XcTransform & pretranslate(const XcVector &vec) {return this->premultiplyByTransform(IdentityMatrix, vec);}
XcTransform & multiplyByTransform(const XcMatrix & otherRot, const XcVector & otherTrans)
{
XcMatrix newRot;
XcVector newTrans;
newRot[0][0] = this->rot[0][0]*otherRot[0][0] + this->rot[0][1]*otherRot[1][0] + this->rot[0][2]*otherRot[2][0];
newRot[0][1] = this->rot[0][0]*otherRot[0][1] + this->rot[0][1]*otherRot[1][1] + this->rot[0][2]*otherRot[2][1];
newRot[0][2] = this->rot[0][0]*otherRot[0][2] + this->rot[0][1]*otherRot[1][2] + this->rot[0][2]*otherRot[2][2];
newTrans[0] = this->rot[0][0]*otherTrans[0] + this->rot[0][1]*otherTrans[1] + this->rot[0][2]*otherTrans[2] + this->trans[0];
newRot[1][0] = this->rot[1][0]*otherRot[0][0] + this->rot[1][1]*otherRot[1][0] + this->rot[1][2]*otherRot[2][0];
newRot[1][1] = this->rot[1][0]*otherRot[0][1] + this->rot[1][1]*otherRot[1][1] + this->rot[1][2]*otherRot[2][1];
newRot[1][2] = this->rot[1][0]*otherRot[0][2] + this->rot[1][1]*otherRot[1][2] + this->rot[1][2]*otherRot[2][2];
newTrans[1] = this->rot[1][0]*otherTrans[0] + this->rot[1][1]*otherTrans[1] + this->rot[1][2]*otherTrans[2] + this->trans[1];
newRot[2][0] = this->rot[2][0]*otherRot[0][0] + this->rot[2][1]*otherRot[1][0] + this->rot[2][2]*otherRot[2][0];
newRot[2][1] = this->rot[2][0]*otherRot[0][1] + this->rot[2][1]*otherRot[1][1] + this->rot[2][2]*otherRot[2][1];
newRot[2][2] = this->rot[2][0]*otherRot[0][2] + this->rot[2][1]*otherRot[1][2] + this->rot[2][2]*otherRot[2][2];
newTrans[2] = this->rot[2][0]*otherTrans[0] + this->rot[2][1]*otherTrans[1] + this->rot[2][2]*otherTrans[2] + this->trans[2];
this->rot = newRot;
this->trans = newTrans;
return *this;
}
XcTransform & premultiplyByTransform(const XcMatrix & otherRot, const XcVector & otherTrans)
{
XcMatrix newRot;
XcVector newTrans;
newRot[0][0] = otherRot[0][0]*this->rot[0][0] + otherRot[0][1]*this->rot[1][0] + otherRot[0][2]*this->rot[2][0];
newRot[0][1] = otherRot[0][0]*this->rot[0][1] + otherRot[0][1]*this->rot[1][1] + otherRot[0][2]*this->rot[2][1];
newRot[0][2] = otherRot[0][0]*this->rot[0][2] + otherRot[0][1]*this->rot[1][2] + otherRot[0][2]*this->rot[2][2];
newTrans[0] = otherRot[0][0]*this->trans[0] + otherRot[0][1]*this->trans[1] + otherRot[0][2]*this->trans[2] + otherTrans[0];
newRot[1][0] = otherRot[1][0]*this->rot[0][0] + otherRot[1][1]*this->rot[1][0] + otherRot[1][2]*this->rot[2][0];
newRot[1][1] = otherRot[1][0]*this->rot[0][1] + otherRot[1][1]*this->rot[1][1] + otherRot[1][2]*this->rot[2][1];
newRot[1][2] = otherRot[1][0]*this->rot[0][2] + otherRot[1][1]*this->rot[1][2] + otherRot[1][2]*this->rot[2][2];
newTrans[1] = otherRot[1][0]*this->trans[0] + otherRot[1][1]*this->trans[1] + otherRot[1][2]*this->trans[2] + otherTrans[1];
newRot[2][0] = otherRot[2][0]*this->rot[0][0] + otherRot[2][1]*this->rot[1][0] + otherRot[2][2]*this->rot[2][0];
newRot[2][1] = otherRot[2][0]*this->rot[0][1] + otherRot[2][1]*this->rot[1][1] + otherRot[2][2]*this->rot[2][1];
newRot[2][2] = otherRot[2][0]*this->rot[0][2] + otherRot[2][1]*this->rot[1][2] + otherRot[2][2]*this->rot[2][2];
newTrans[2] = otherRot[2][0]*this->trans[0] + otherRot[2][1]*this->trans[1] + otherRot[2][2]*this->trans[2] + otherTrans[2];
this->rot = newRot;
this->trans = newTrans;
return *this;
}
XcVector & translation() {return this->trans;}
const XcVector & translation() const {return this->trans;}
XcMatrix & rotation() {return this->rot;}
const XcMatrix & rotation() const {return this->rot;}
XcVector operator*(const XcVector & vec) const
{
XcVector ret;
ret[0] = this->rot[0][0]*vec[0] + this->rot[0][1]*vec[1] + this->rot[0][2]*vec[2] + trans[0];
ret[1] = this->rot[1][0]*vec[0] + this->rot[1][1]*vec[1] + this->rot[1][2]*vec[2] + trans[1];
ret[2] = this->rot[2][0]*vec[0] + this->rot[2][1]*vec[1] + this->rot[2][2]*vec[2] + trans[2];
return ret;
}
private:
XcMatrix rot;
XcVector trans;
};
#endif