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
|
// $Id$
//
// Copyright (C) 2004-2008 Greg Landrum and Rational Discovery LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include "AlignPoints.h"
#include <RDGeneral/Invariant.h>
#include <Geometry/point.h>
#include <Geometry/Transform3D.h>
#include <Numerics/Vector.h>
#define TOLERANCE 1.e-6
namespace RDNumeric {
namespace Alignments {
RDGeom::Point3D _weightedSumOfPoints(const RDGeom::Point3DConstPtrVect &points,
const DoubleVector &weights) {
PRECONDITION(points.size() == weights.size(), "");
RDGeom::Point3DConstPtrVect_CI pti;
RDGeom::Point3D tmpPt, res;
const double *wData = weights.getData();
unsigned int i = 0;
for (pti = points.begin(); pti != points.end(); pti++) {
tmpPt = (*(*pti));
tmpPt *= wData[i];
res += tmpPt;
i++;
}
return res;
}
double _weightedSumOfLenSq(const RDGeom::Point3DConstPtrVect &points,
const DoubleVector &weights) {
PRECONDITION(points.size() == weights.size(), "");
double res = 0.0;
RDGeom::Point3DConstPtrVect_CI pti;
const double *wData = weights.getData();
unsigned int i = 0;
for (pti = points.begin(); pti != points.end(); pti++) {
res += (wData[i] * ((*pti)->lengthSq()));
i++;
}
return res;
}
double _sumOfWeights(const DoubleVector &weights) {
const double *wData = weights.getData();
double res = 0.0;
for (unsigned int i = 0; i < weights.size(); i++) {
CHECK_INVARIANT(wData[i] > 0.0, "Negative weight specified for a point");
res += wData[i];
}
return res;
}
void _computeCovarianceMat(const RDGeom::Point3DConstPtrVect &refPoints,
const RDGeom::Point3DConstPtrVect &probePoints,
const DoubleVector &weights, double covMat[3][3]) {
unsigned int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
covMat[i][j] = 0.0;
}
}
unsigned int npt = refPoints.size();
CHECK_INVARIANT(npt == probePoints.size(), "Number of points mismatch");
CHECK_INVARIANT(npt == weights.size(),
"Number of points and number of weights do not match");
const double *wData = weights.getData();
const RDGeom::Point3D *rpt, *ppt;
double w;
for (i = 0; i < npt; i++) {
rpt = refPoints[i];
ppt = probePoints[i];
w = wData[i];
covMat[0][0] += w * (ppt->x) * (rpt->x);
covMat[0][1] += w * (ppt->x) * (rpt->y);
covMat[0][2] += w * (ppt->x) * (rpt->z);
covMat[1][0] += w * (ppt->y) * (rpt->x);
covMat[1][1] += w * (ppt->y) * (rpt->y);
covMat[1][2] += w * (ppt->y) * (rpt->z);
covMat[2][0] += w * (ppt->z) * (rpt->x);
covMat[2][1] += w * (ppt->z) * (rpt->y);
covMat[2][2] += w * (ppt->z) * (rpt->z);
}
}
void _covertCovMatToQuad(const double covMat[3][3],
const RDGeom::Point3D &rptSum,
const RDGeom::Point3D &pptSum, double wtsSum,
double quad[4][4]) {
double PxRx, PxRy, PxRz;
double PyRx, PyRy, PyRz;
double PzRx, PzRy, PzRz;
double temp;
temp = pptSum.x / wtsSum;
PxRx = covMat[0][0] - temp * rptSum.x;
PxRy = covMat[0][1] - temp * rptSum.y;
PxRz = covMat[0][2] - temp * rptSum.z;
temp = pptSum.y / wtsSum;
PyRx = covMat[1][0] - temp * rptSum.x;
PyRy = covMat[1][1] - temp * rptSum.y;
PyRz = covMat[1][2] - temp * rptSum.z;
temp = pptSum.z / wtsSum;
PzRx = covMat[2][0] - temp * rptSum.x;
PzRy = covMat[2][1] - temp * rptSum.y;
PzRz = covMat[2][2] - temp * rptSum.z;
quad[0][0] = -2.0 * (PxRx + PyRy + PzRz);
quad[1][1] = -2.0 * (PxRx - PyRy - PzRz);
quad[2][2] = -2.0 * (PyRy - PzRz - PxRx);
quad[3][3] = -2.0 * (PzRz - PxRx - PyRy);
quad[0][1] = quad[1][0] = 2.0 * (PyRz - PzRy);
quad[0][2] = quad[2][0] = 2.0 * (PzRx - PxRz);
quad[0][3] = quad[3][0] = 2.0 * (PxRy - PyRx);
quad[1][2] = quad[2][1] = -2.0 * (PxRy + PyRx);
quad[1][3] = quad[3][1] = -2.0 * (PzRx + PxRz);
quad[2][3] = quad[3][2] = -2.0 * (PyRz + PzRy);
}
//! Obtain the eigen vectors and eigen values
/*!
\param quad 4x4 matrix of interest
\param eigenVals storage for eigen values
\param eigenVecs storage for eigen vectors
\param maxIter max number of iterations
<b>Reference:<\b>
This is essentailly a copy of the jacobi routine taken from the program
quatfit.c available from the Computational Chemistry Archives.
http://www.ccl.net/cca/software/SOURCES/C/quaternion-mol-fit/index.shtml
E-mail jkl@osc.edu for details.
It was written by:
David J. Heisterberg
The Ohio Supercomputer Center
1224 Kinnear Rd.
Columbus, OH 43212-1163
(614)292-6036
djh@osc.edu djh@ohstpy.bitnet ohstpy::djh
Copyright: Ohio Supercomputer Center, David J. Heisterberg, 1990.
The program can be copied and distributed freely, provided that
this copyright in not removed. You may acknowledge the use of the
program in published material as:
David J. Heisterberg, 1990, unpublished results.
Also see page 463 in Numerical Recipes in C (second edition)
*/
unsigned int jacobi(double quad[4][4], double eigenVals[4],
double eigenVecs[4][4], unsigned int maxIter) {
double offDiagNorm, diagNorm;
double b, dma, q, t, c, s;
double atemp, vtemp, dtemp;
int i, j, k;
unsigned int l;
// initialize the eigen vector to Identity
for (j = 0; j <= 3; j++) {
for (i = 0; i <= 3; i++) eigenVecs[i][j] = 0.0;
eigenVecs[j][j] = 1.0;
eigenVals[j] = quad[j][j];
}
for (l = 0; l < maxIter; l++) {
diagNorm = 0.0;
offDiagNorm = 0.0;
for (j = 0; j <= 3; j++) {
diagNorm += fabs(eigenVals[j]);
for (i = 0; i <= j - 1; i++) {
offDiagNorm += fabs(quad[i][j]);
}
}
if ((offDiagNorm / diagNorm) <= TOLERANCE) goto Exit_now;
for (j = 1; j <= 3; j++) {
for (i = 0; i <= j - 1; i++) {
b = quad[i][j];
if (fabs(b) > 0.0) {
dma = eigenVals[j] - eigenVals[i];
if ((fabs(dma) + fabs(b)) <= fabs(dma)) {
t = b / dma;
} else {
q = 0.5 * dma / b;
t = 1.0 / (fabs(q) + sqrt(1.0 + q * q));
if (q < 0.0) {
t = -t;
}
}
c = 1.0 / sqrt(t * t + 1.0);
s = t * c;
quad[i][j] = 0.0;
for (k = 0; k <= i - 1; k++) {
atemp = c * quad[k][i] - s * quad[k][j];
quad[k][j] = s * quad[k][i] + c * quad[k][j];
quad[k][i] = atemp;
}
for (k = i + 1; k <= j - 1; k++) {
atemp = c * quad[i][k] - s * quad[k][j];
quad[k][j] = s * quad[i][k] + c * quad[k][j];
quad[i][k] = atemp;
}
for (k = j + 1; k <= 3; k++) {
atemp = c * quad[i][k] - s * quad[j][k];
quad[j][k] = s * quad[i][k] + c * quad[j][k];
quad[i][k] = atemp;
}
for (k = 0; k <= 3; k++) {
vtemp = c * eigenVecs[k][i] - s * eigenVecs[k][j];
eigenVecs[k][j] = s * eigenVecs[k][i] + c * eigenVecs[k][j];
eigenVecs[k][i] = vtemp;
}
dtemp = c * c * eigenVals[i] + s * s * eigenVals[j] - 2.0 * c * s * b;
eigenVals[j] =
s * s * eigenVals[i] + c * c * eigenVals[j] + 2.0 * c * s * b;
eigenVals[i] = dtemp;
} /* end if */
} /* end for i */
} /* end for j */
} /* end for l */
Exit_now:
for (j = 0; j <= 2; j++) {
k = j;
dtemp = eigenVals[k];
for (i = j + 1; i <= 3; i++) {
if (eigenVals[i] < dtemp) {
k = i;
dtemp = eigenVals[k];
}
}
if (k > j) {
eigenVals[k] = eigenVals[j];
eigenVals[j] = dtemp;
for (i = 0; i <= 3; i++) {
dtemp = eigenVecs[i][k];
eigenVecs[i][k] = eigenVecs[i][j];
eigenVecs[i][j] = dtemp;
}
}
}
return l + 1;
}
void reflectCovMat(double covMat[3][3]) {
unsigned int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
covMat[i][j] = -covMat[i][j];
}
}
}
double AlignPoints(const RDGeom::Point3DConstPtrVect &refPoints,
const RDGeom::Point3DConstPtrVect &probePoints,
RDGeom::Transform3D &trans, const DoubleVector *weights,
bool reflect, unsigned int maxIterations) {
unsigned int npt = refPoints.size();
PRECONDITION(npt == probePoints.size(), "Mismatch in number of points");
trans.setToIdentity();
const DoubleVector *wts;
double wtsSum;
bool ownWts;
if (weights) {
PRECONDITION(npt == weights->size(), "Mismatch in number of points");
wts = weights;
wtsSum = _sumOfWeights(*wts);
ownWts = false;
} else {
wts = new DoubleVector(npt, 1.0);
wtsSum = static_cast<double>(npt);
ownWts = true;
}
RDGeom::Point3D rptSum = _weightedSumOfPoints(refPoints, *wts);
RDGeom::Point3D pptSum = _weightedSumOfPoints(probePoints, *wts);
double rptSumLenSq = _weightedSumOfLenSq(refPoints, *wts);
double pptSumLenSq = _weightedSumOfLenSq(probePoints, *wts);
double covMat[3][3];
// compute the co-variance matrix
_computeCovarianceMat(refPoints, probePoints, *wts, covMat);
if (ownWts) {
delete wts;
wts = nullptr;
}
if (reflect) {
rptSum *= -1.0;
reflectCovMat(covMat);
}
// convert the covariance matrix to a 4x4 matrix that needs to be diagonalized
double quad[4][4];
_covertCovMatToQuad(covMat, rptSum, pptSum, wtsSum, quad);
// get the eigenVecs and eigenVals for the matrix
double eigenVecs[4][4], eigenVals[4];
jacobi(quad, eigenVals, eigenVecs, maxIterations);
// get the quaternion
double quater[4];
quater[0] = eigenVecs[0][0];
quater[1] = eigenVecs[1][0];
quater[2] = eigenVecs[2][0];
quater[3] = eigenVecs[3][0];
trans.SetRotationFromQuaternion(quater);
if (reflect) {
// put the flip in the rotation matrix
trans.Reflect();
}
// compute the SSR value
double ssr = eigenVals[0] - (pptSum.lengthSq() + rptSum.lengthSq()) / wtsSum +
rptSumLenSq + pptSumLenSq;
if ((ssr < 0.0) && (fabs(ssr) < TOLERANCE)) {
ssr = 0.0;
}
if (reflect) {
rptSum *= -1.0;
}
// set the translation
trans.TransformPoint(pptSum);
RDGeom::Point3D move = rptSum;
move -= pptSum;
move /= wtsSum;
trans.SetTranslation(move);
return ssr;
}
}
}
|