-
Notifications
You must be signed in to change notification settings - Fork 2
/
superpose3d.hpp
446 lines (377 loc) · 15.2 KB
/
superpose3d.hpp
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/// @file superpose3d.hpp
/// @brief Calculate the optimal rotation, translation and scale needed to
/// optimally fit two different point clouds containing n points.
/// @author Andrew Jewett
/// @license MIT
#ifndef _SUPERPOSE3D_HPP
#define _SUPERPOSE3D_HPP
#include "matrix_alloc_jpd.hpp" // Defines "Alloc2D()" and "Dealloc2D()"
#include "peigencalc.hpp" // Calculates eigenvalues and eigenvectors
namespace superpose3d {
using namespace matrix_alloc_jpd;
// -----------------------------------------------------------
// ------------------------ INTERFACE ------------------------
// -----------------------------------------------------------
/// @brief Superpose3d is a class with only one important member function
/// Superpose(). It is useful for calculating the optimal
/// superposition (rotations, translations, and scale transformations)
/// between two point clouds of the same size.
template<typename Scalar,
typename ConstArrayOfCoords,
typename ConstArray=Scalar const*>
class Superpose3D {
private:
size_t N; //number of points in the point clouds
Scalar *aWeights; //weights applied to points when computing RMSD
PEigenCalculator<Scalar, Scalar*, Scalar const* const*>
eigen_calc; // calculates principal eigenvalues
Scalar **aaXf_shifted; //preallocated space for fixed point cloud (Nx3 array)
Scalar **aaXm_shifted; //preallocated space for mobile point cloud (Nx3 array)
public:
// The following data members store the rotation, translation and scale
// after optimal superposition
Scalar **R; //!< store optimal rotation here (this is a 3x3 array).
Scalar T[3]; //!< store optimal translation here
Scalar c; //!< store optimal scale (typically 1 unless requested by the user)
Scalar q[4]; //!< quaternion corresponding to the rotation stored in R.
// The first entry of q is cos(θ/2). The remaining 3 entries
// of q are the axis of rotation (with length sin(θ/2)).
// (Note: This is not the same as "p" from Diamond's 1988 paper.)
Superpose3D(size_t N = 0); //!< N=number of points in both point clouds
Superpose3D(size_t N, //!< N = number of points in both point clouds
ConstArray aWeights); //!< weight per point for computing RMSD
~Superpose3D();
/// @brief specify he number of points in both point clouds
void SetNumPoints(size_t N);
/// @brief return the number of points in both point clouds
size_t GetNumPoints() { return N; }
/// @brief specify the weight applied to each point when computing RMSD
void SetWeights(ConstArray aWeights);
/// @brief Use rigid-body transformations (rotations, translations, and
/// optionally scale transformations) to superimpose two point clouds.
///
/// @details
/// This function takes two lists of xyz coordinates (of the same length) and
/// attempts to superimpose them using rotations, translations, and
/// (optionally) scale transformations. These transformations are applied to
/// to the coordinates in the "aaXm_orig" array (the "mobile" point cloud)
/// in order to minimize the root-mean-squared-distance (RMSD) between the
/// corresponding points in each cloud, where RMSD is defined as:
///
/// @verbatim
/// sqrt((Σ_n w[n]*Σ_i |X[n][i] - (Σ_j c*R[i][j]*x[n][j]+T[i])|^2)/(Σ_n w[n]))
/// @endverbatim
///
/// In this formula, the "X_i" and "x_i" are coordinates of the ith fixed and
/// mobile point clouds (represented by "aaXf" and "aaXm" in the code below)
/// and "w_i" are optional weights (represented by "aWeights" in the code).
/// This function implements a more general variant of the method from:
/// @verbatim
/// R. Diamond, (1988) "A Note on the Rotational Superposition Problem",
/// Acta Cryst. A44, pp. 211-216
/// @endverbatim
///
/// @note:
/// This code has been augmented with a new feature. The version in the
/// original paper only considers rotation and translation and does not allow
/// coordinates of either cloud to be rescaled (multiplied by a scalar).
/// To enable the ability to rescale the coordinates, set allow_rescale=true.
/// (By default, this feature is disabled.)
///
/// @returns
/// The RMSD between the 2 pointclouds after optimal rotation, translation
/// (and scaling if requested) was applied to the "mobile" point cloud.
/// After this function is called, the optimal rotation, translation,
/// and scale (if requested) will be stored in the "R", "T", and "c"
/// public data members.
Scalar Superpose(ConstArrayOfCoords aaXf, //!< coords for the "frozen" object
ConstArrayOfCoords aaXm, //!< coords for the "mobile" object
bool allow_rescale=false //!< rescale mobile object? (c≠1?)
);
// memory management: copy and move constructor, swap, and assignment operator
Superpose3D(const Superpose3D<Scalar,ConstArrayOfCoords,ConstArray>& source);
Superpose3D(Superpose3D<Scalar,ConstArrayOfCoords,ConstArray>&& other);
void swap(Superpose3D<Scalar,ConstArrayOfCoords,ConstArray> &other);
Superpose3D<Scalar,ConstArrayOfCoords,ConstArray>& operator = (Superpose3D<Scalar,ConstArrayOfCoords,ConstArray> source);
private:
// memory management:
void Alloc(size_t N);
void Init();
void Dealloc();
}; // class Superpose3D
// -------------- IMPLEMENTATION --------------
template<typename Scalar>
static inline Scalar SQR(Scalar x) {return x*x;}
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
Scalar Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
Superpose(ConstArrayOfCoords aaXf, // coords for the "frozen" object
ConstArrayOfCoords aaXm, // coords for the "mobile" object
bool allow_rescale) // rescale mobile object? (c!=1?)
{
assert(aaXf && aaXm);
assert(aaXf_shifted && aaXm_shifted);
assert(aWeights);
assert(R && T);
// Find the center of mass of each object:
Scalar aCenter_f[3] = {0.0, 0.0, 0.0};
Scalar aCenter_m[3] = {0.0, 0.0, 0.0};
Scalar sum_weights = 0.0;
for (size_t n=0; n < N; n++) {
Scalar weight = aWeights[n];
for (int d=0; d < 3; d++) {
aCenter_f[d] += aaXf[n][d]*weight;
aCenter_m[d] += aaXm[n][d]*weight;
}
sum_weights += weight;
}
assert((sum_weights != 0.0) || (N==0));
for (int d=0; d < 3; d++) {
aCenter_f[d] /= sum_weights;
aCenter_m[d] /= sum_weights;
}
//Subtract the centers-of-mass from the original coordinates for each object
for (size_t n=0; n < N; n++) {
for (int d=0; d < 3; d++) {
// shift the coordinates so that the new center of mass is at the origin
aaXf_shifted[n][d] = aaXf[n][d] - aCenter_f[d];
aaXm_shifted[n][d] = aaXm[n][d] - aCenter_m[d];
}
}
// Calculate the "M" array from the Diamond paper (equation 16)
Scalar M[3][3];
for (int i=0; i < 3; i++)
for (int j=0; j < 3; j++)
M[i][j] = 0.0;
for (size_t n=0; n < N; n++) {
Scalar weight = aWeights[n];
for (int i=0; i < 3; i++) {
for (int j=0; j < 3; j++) {
M[i][j] += weight * aaXm_shifted[n][i] * aaXf_shifted[n][j];
}
}
}
// Calculate Q (equation 17)
Scalar traceM = 0.0;
for (int i=0; i < 3; i++)
traceM += M[i][i];
Scalar Q[3][3];
for (int i=0; i < 3; i++) {
for (int j=0; j < 3; j++) {
Q[i][j] = M[i][j] + M[j][i];
if (i==j)
Q[i][j] -= 2.0 * traceM;
}
}
// Calculate V (equation 18)
Scalar V[3];
V[0] = M[1][2] - M[2][1];
V[1] = M[2][0] - M[0][2];
V[2] = M[0][1] - M[1][0];
// Calculate "P" (equation 22)
// First we must allocate space for the P matrix. It's not safe to declare:
// Scalar P[4][4];
// ...because most matrix solvers expect arrays in pointer-to-pointer format.
// (a different format). Below I create a fixed size matrix P in this format.
Scalar _P[4*4]; // Contiguous 1D array for storing contents of the 2D P array
Scalar *P[4]; // This version of P has has ** (pointer-to-pointer) format.
for (int i=0; i < 4; i++)
P[i] = &(_P[4*i]); //P[i] points to data corresponding to i'th row from _P
// Now fill the P array
for (int i=0; i < 3; i++)
for (int j=0; j < 3; j++)
P[i][j] = Q[i][j];
P[0][3] = V[0];
P[3][0] = V[0];
P[1][3] = V[1];
P[3][1] = V[1];
P[2][3] = V[2];
P[3][2] = V[2];
P[3][3] = 0.0;
// The vector "p" contains the optimal rotation (backwards quaternion format)
Scalar p[4] = {0.0, 0.0, 0.0, 1.0}; // default value
Scalar pPp = 0.0; // = p^T * P * p (zero by default)
Scalar rmsd = 0.0; // default value
bool singular = N<2; // (it doesn't make sense to rotate a single point)
if (! singular) {
// The vector "p" will contain the optimal rotation (in quaternion format)
Scalar eval_max = eigen_calc.PrincipalEigen(P, p, true);
pPp = eval_max; // = the maximum eigenvalue of P
}
// Now normalize p
Scalar pnorm = 0.0;
for (int i=0; i < 4; i++)
pnorm += p[i]*p[i];
pnorm = sqrt(pnorm);
for (int i=0; i < 4; i++)
p[i] /= pnorm;
// Finally, calculate the rotation matrix corresponding to "p"
// (convert a quaternion into a 3x3 rotation matrix)
R[0][0] = (p[0]*p[0])-(p[1]*p[1])-(p[2]*p[2])+(p[3]*p[3]);
R[1][1] = -(p[0]*p[0])+(p[1]*p[1])-(p[2]*p[2])+(p[3]*p[3]);
R[2][2] = -(p[0]*p[0])-(p[1]*p[1])+(p[2]*p[2])+(p[3]*p[3]);
R[0][1] = 2*(p[0]*p[1] - p[2]*p[3]);
R[1][0] = 2*(p[0]*p[1] + p[2]*p[3]);
R[1][2] = 2*(p[1]*p[2] - p[0]*p[3]);
R[2][1] = 2*(p[1]*p[2] + p[0]*p[3]);
R[0][2] = 2*(p[0]*p[2] + p[1]*p[3]);
R[2][0] = 2*(p[0]*p[2] - p[1]*p[3]);
q[0] = p[3]; // Note: The "p" variable is not a quaternion in the
q[1] = p[0]; // conventional sense because its elements
q[2] = p[1]; // are in the wrong order. I correct for that here.
q[3] = p[2]; // "q" is the quaternion correspond to rotation R.
// Optional: Decide the scale factor, c
c = 1.0; // by default, don't rescale the coordinates
if ((allow_rescale) && (! singular)) {
Scalar Waxaixai = 0.0;
Scalar WaxaiXai = 0.0;
for (size_t a=0; a < N; a++) {
Scalar weight = aWeights[a];
for (int i=0; i < 3; i++) {
Waxaixai += weight * aaXm_shifted[a][i] * aaXm_shifted[a][i];
WaxaiXai += weight * aaXm_shifted[a][i] * aaXf_shifted[a][i];
}
}
c = (WaxaiXai + pPp) / Waxaixai;
} // if (allow_rescale)
// Finally compute the RMSD between the two coordinate sets:
// First compute E0 from equation 24 of the paper
Scalar E0 = 0.0;
for (size_t n=0; n < N; n++) {
Scalar weight = aWeights[n];
for (int d=0; d < 3; d++)
// (remember to include the scale factor "c" that we inserted)
E0 += weight * (SQR(aaXf_shifted[n][d] - c*aaXm_shifted[n][d]));
}
Scalar sum_sqr_dist = E0 - c*2.0*pPp;
if (sum_sqr_dist < 0.0) //(edge case due to rounding error)
sum_sqr_dist = 0.0;
if (! singular)
rmsd = sqrt(sum_sqr_dist/sum_weights);
// Lastly, calculate the translational offset.
// If c!=1, this is slightly more complicated than it seems. Recall that:
//RMSD=sqrt((Sum_i w_i * |X_i - Sum_j(c*R_ij*x_j + T_i))|^2) / (Sum_j w_j))
// =sqrt((Sum_i w_i * |X_i - x_i')|^2) / (Sum_j w_j))
// where
// x_i' = Sum_j(c*R_ij*x_j) + T_i
// = Xcm_i + c*R_ij*(x_j - xcm_j)
// and Xcm and xcm = center_of_mass for the frozen and mobile point clouds
//
// Hence:
// T_i = Xcm_i - Sum_j c*R_ij*xcm_j
// In the code, Xcm_i is represented by "aCenter_f[i]"
// and xcm_j is represented by "aCenter_m[j]"
for (int i=0; i < 3; i++) {
T[i] = aCenter_f[i];
for (int j=0; j < 3; j++) {
T[i] -= c*R[i][j]*aCenter_m[j];
}
}
return rmsd;
} //Superpose3D::Superpose(aaXf, aaXm, allow_rescale)
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
void Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
SetNumPoints(size_t N) {
Dealloc();
Alloc(N);
}
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
void Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
SetWeights(ConstArray aWeights) {
for (size_t i = 0; i < N; i++)
this->aWeights[i] = aWeights[i];
}
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::Superpose3D(size_t N)
:eigen_calc(4)
{
Init();
Alloc(N);
}
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
Superpose3D(size_t N, ConstArray aWeights)
:eigen_calc(4)
{
Init();
Alloc(N);
SetWeights(aWeights);
}
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::~Superpose3D() {
Dealloc();
}
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
void Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
Init() {
R = nullptr;
aWeights = nullptr;
aaXf_shifted = nullptr;
aaXm_shifted = nullptr;
}
// memory management:
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
void Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
Alloc(size_t N) {
this->N = N;
aWeights = new Scalar [N];
for (size_t i = 0; i < N; i++)
aWeights[i] = 1.0 / N;
Alloc2D(3, 3, &R);
Alloc2D(N, 3, &aaXf_shifted);
Alloc2D(N, 3, &aaXm_shifted);
}
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
void Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
Dealloc() {
if (R)
Dealloc2D(&R);
if (aWeights)
delete [] aWeights;
if (aaXf_shifted)
Dealloc2D(&aaXf_shifted);
if (aaXm_shifted)
Dealloc2D(&aaXm_shifted);
}
// memory management: copy and move constructor, swap, and assignment operator:
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
Superpose3D(const Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>& source)
:eigen_calc(4)
{
Init();
Alloc(source.N);
assert(N == source.N);
for (int i = 0; i < N; i++) {
std::copy(source.aaXf_shifted[i],
source.aaXf_shifted[i] + 3,
aaXf_shifted[i]);
std::copy(source.aaXm_shifted[i],
source.aaXm_shifted[i] + 3,
aaXm_shifted[i]);
}
}
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
void Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
swap(Superpose3D<Scalar, ConstArrayOfCoords, ConstArray> &other) {
std::swap(N, other.N);
std::swap(R, other.R);
std::swap(aaXf_shifted, other.aaXf_shifted);
std::swap(aaXm_shifted, other.aaXm_shifted);
}
// Move constructor (C++11)
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
Superpose3D(Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>&& other) {
Init();
swap(*this, other);
}
// Using the "copy-swap" idiom for the assignment operator
template<typename Scalar, typename ConstArrayOfCoords, typename ConstArray>
Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>&
Superpose3D<Scalar, ConstArrayOfCoords, ConstArray>::
operator = (Superpose3D<Scalar, ConstArrayOfCoords, ConstArray> source) {
this->swap(source);
return *this;
}
} //namespace superposed3d
#endif //#ifndef _SUPERPOSE3D_HPP