This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
coordsarray.cpp
242 lines (170 loc) · 5.46 KB
/
coordsarray.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
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
/****************************************************************************
* Copyright (C) 2008 Adrien Saladin *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#include<stdexcept>
#include "coordsarray.h"
#include "geometry.h"
namespace PTools{
CoordsArray::CoordsArray()
{
for (uint i=0; i<4; i++)
for (uint j=0; j<4; j++)
{
mat44[i][j]=0;
if (i==j) mat44[i][j]=1;
}
}
CoordsArray::CoordsArray(const CoordsArray & ca) //copy constructor
{
_refcoords = ca._refcoords;
_movedcoords = ca._movedcoords;
_modified();
//copy of the matrix:
//Note: very naive way. May be slower than memcpy()
for (uint i=0; i<4; i++)
for (uint j=0; j<4; j++)
this->mat44[i][j]=ca.mat44[i][j];
}
void CoordsArray::Translate(const Coord3D& tr)
{
//updates rotation/translation matrix:
this->mat44[0][3]+=tr.x;
this->mat44[1][3]+=tr.y;
this->mat44[2][3]+=tr.z;
_modified();
}
void CoordsArray::MatrixMultiply(const dbl mat[4][4])
{
mat44xmat44( mat, mat44, mat44 ); //multiply our internal matrix4x4 by mat
_modified(); // invalidates coordinates
}
void CoordsArray::ResetMatrix()
{
_modified();
for (uint i=0; i<4; i++)
for (uint j=0; j<4; j++)
{
if (i!=j) mat44[i][j]=0;
else mat44[i][j]=1;
}
}
std::string CoordsArray::PrintMatrix() const
{
std::string out;
out += "### MAT BEGIN\n";
for (uint i=0; i<4; i++)
{
out += "MAT ";
for (uint j=0; j<4; j++)
{
char tmp[20];
snprintf(tmp,19,"%14.7f ", real(this->mat44[i][j])) ;
out += tmp;
}
out +="\n";
}
out += "### MAT END\n";
return out;
}
Matrix CoordsArray::GetMatrix() const
{
Matrix matrix(4,4);
for(uint i=0; i<4; i++)
for(uint j=0; j<4; j++)
matrix(i,j)=mat44[i][j];
return matrix;
}
void CoordsArray::GetCoords(const uint i, Coord3D& co) const throw(std::out_of_range)
{
if (i>=Size())
{
std::string message = (std::string) "CoordsArray::GetCoords : out of range : ";
message += i ;
message += " is out of bounds (object size: ";
message += Size() ;
message += ")\n";
std::cerr << message ;
throw std::out_of_range (message);
}
(*this.*_getcoords)(i,co);
};
void CoordsArray::SetCoords(const uint k, const Coord3D& co)
{
//sets the coordinate [i] to be 'co' after rotation/translation
Coord3D co2 (co);
co2.x -= mat44[0][3];
co2.y -= mat44[1][3];
co2.z -= mat44[2][3];
dbl matinv[4][4]; //invert matrix
for(uint i=0; i<3; i++)
for(uint j=0; j<3;j++)
matinv[i][j]=mat44[j][i];
for(uint i=0; i<4; i++)
{
matinv[i][3]=0;
matinv[3][i]=0;
}
matinv[3][3]=1;
Coord3D final;
PTools::matrix44xVect(matinv,co2, final );
_refcoords[k] = final;
_modified();
}
/*! \brief this function makes an euler rotation with the Attract convention.
*
* Note that for this new implementation only the 4x4 rotational/translational
* matrix is updated. This may allow a big speedup (to be tested) and a
* higher flexibility ( rig.Translate(a); rig.Translate(minus(a)); may now be delayed
* until the coordinates are really needed.
* If coordinates are never asked (why?), then no costly calculation is performed !
*/
void CoordsArray::AttractEulerRotate(dbl phi, dbl ssi, dbl rot)
{
dbl cp, cs, ss, sp, cscp, sscp, sssp, crot, srot, cssp ;
cs=cos(ssi);
cp=cos(phi);
ss=sin(ssi);
sp=sin(phi);
cscp=cs*cp;
cssp=cs*sp;
sscp=ss*cp;
sssp=ss*sp;
crot=cos(rot);
srot=sin(rot);
dbl eulermat[4][4];
eulermat[0][0] = crot*cscp + srot*sp;
eulermat[0][1] = srot*cscp - crot*sp;
eulermat[0][2] = sscp;
eulermat[0][3] = 0.0;
eulermat[1][0] = crot*cssp-srot*cp ;
eulermat[1][1] = srot*cssp+crot*cp;
eulermat[1][2] = sssp;
eulermat[1][3] = 0.0;
eulermat[2][0] = -crot*ss;
eulermat[2][1] = -srot*ss;
eulermat[2][2] = cs;
eulermat[2][3] = 0.0;
eulermat[3][0] = 0.0;
eulermat[3][1] = 0.0;
eulermat[3][2] = 0.0;
eulermat[3][3] = 1.0;
//matrix multiplication
this->MatrixMultiply(eulermat);
// mat44xmat44( eulermat , this->mat44, this->mat44);
}
}//namespace PTools