Skip to content

Commit

Permalink
Add qcpik.
Browse files Browse the repository at this point in the history
Add some documentation.

Add validation for joint settings and implement rootmost bone check

Fix rootmost bone validation and adjust bone chain transformation logic

Refactor joint processing logic to improve clarity and efficiency

Fix comments for clarity and adjust bone translation logic in CCDIK3D

Refactor CCDIK3D joint processing by removing unused rotation calculations
  • Loading branch information
fire committed Jan 28, 2025
1 parent 435c782 commit f23f7f2
Show file tree
Hide file tree
Showing 7 changed files with 730 additions and 66 deletions.
261 changes: 261 additions & 0 deletions core/math/qcp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
/**************************************************************************/
/* qcp.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "qcp.h"

QuaternionCharacteristicPolynomial::QuaternionCharacteristicPolynomial(double p_evec_prec) {
eigenvector_precision = p_evec_prec;
}

void QuaternionCharacteristicPolynomial::set(PackedVector3Array &r_target, PackedVector3Array &r_moved) {
target = r_target;
moved = r_moved;
transformation_calculated = false;
inner_product_calculated = false;
}

Quaternion QuaternionCharacteristicPolynomial::_get_rotation() {
Quaternion result;
if (!transformation_calculated) {
if (!inner_product_calculated) {
inner_product(target, moved);
}
result = calculate_rotation();
transformation_calculated = true;
}
return result;
}

Quaternion QuaternionCharacteristicPolynomial::calculate_rotation() {
Quaternion result;

if (moved.size() == 1) {
Vector3 u = moved[0];
Vector3 v = target[0];
double norm_product = u.length() * v.length();

if (norm_product == 0.0) {
return Quaternion();
}

double dot = u.dot(v);

if (dot < ((2.0e-15 - 1.0) * norm_product)) {
Vector3 w = u.normalized();
result = Quaternion(w.x, w.y, w.z, 0.0f).normalized();
} else {
double q0 = Math::sqrt(0.5 * (1.0 + dot / norm_product));
double coeff = 1.0 / (2.0 * q0 * norm_product);
Vector3 q = v.cross(u).normalized();
result = Quaternion(coeff * q.x, coeff * q.y, coeff * q.z, q0).normalized();
}
} else {
double a13 = -sum_xz_minus_zx;
double a14 = sum_xy_minus_yx;
double a21 = sum_yz_minus_zy;
double a22 = sum_xx_minus_yy - sum_zz - max_eigenvalue;
double a23 = sum_xy_plus_yx;
double a24 = sum_xz_plus_zx;
double a31 = a13;
double a32 = a23;
double a33 = sum_yy - sum_xx - sum_zz - max_eigenvalue;
double a34 = sum_yz_plus_zy;
double a41 = a14;
double a42 = a24;
double a43 = a34;
double a44 = sum_zz - sum_xx_plus_yy - max_eigenvalue;

double a3344_4334 = a33 * a44 - a43 * a34;
double a3244_4234 = a32 * a44 - a42 * a34;
double a3243_4233 = a32 * a43 - a42 * a33;
double a3143_4133 = a31 * a43 - a41 * a33;
double a3144_4134 = a31 * a44 - a41 * a34;
double a3142_4132 = a31 * a42 - a41 * a32;

double quaternion_w = a22 * a3344_4334 - a23 * a3244_4234 + a24 * a3243_4233;
double quaternion_x = -a21 * a3344_4334 + a23 * a3144_4134 - a24 * a3143_4133;
double quaternion_y = a21 * a3244_4234 - a22 * a3144_4134 + a24 * a3142_4132;
double quaternion_z = -a21 * a3243_4233 + a22 * a3143_4133 - a23 * a3142_4132;
double qsqr = quaternion_w * quaternion_w + quaternion_x * quaternion_x + quaternion_y * quaternion_y + quaternion_z * quaternion_z;

if (qsqr < eigenvector_precision) {
result = Quaternion();
} else {
quaternion_x *= -1;
quaternion_y *= -1;
quaternion_z *= -1;
double min = quaternion_w;
min = quaternion_x < min ? quaternion_x : min;
min = quaternion_y < min ? quaternion_y : min;
min = quaternion_z < min ? quaternion_z : min;
quaternion_w /= min;
quaternion_x /= min;
quaternion_y /= min;
quaternion_z /= min;
result = Quaternion(quaternion_x, quaternion_y, quaternion_z, quaternion_w).normalized();
}
}

return result;
}

void QuaternionCharacteristicPolynomial::translate(Vector3 r_translate, PackedVector3Array &r_x) {
for (Vector3 &p : r_x) {
p += r_translate;
}
}

Vector3 QuaternionCharacteristicPolynomial::_get_translation() {
return target_center - moved_center;
}

Vector3 QuaternionCharacteristicPolynomial::move_to_weighted_center(PackedVector3Array &r_to_center, Vector<double> &r_weight) {
Vector3 center;
double total_weight = 0;
bool weight_is_empty = r_weight.is_empty();
int size = r_to_center.size();

for (int i = 0; i < size; i++) {
if (!weight_is_empty) {
total_weight += r_weight[i];
center += r_to_center[i] * r_weight[i];
} else {
center += r_to_center[i];
total_weight++;
}
}

if (total_weight > 0) {
center /= total_weight;
}

return center;
}

void QuaternionCharacteristicPolynomial::inner_product(PackedVector3Array &r_coords1, PackedVector3Array &r_coords2) {
Vector3 weighted_coord1, weighted_coord2;
double sum_of_squares1 = 0, sum_of_squares2 = 0;

sum_xx = 0;
sum_xy = 0;
sum_xz = 0;
sum_yx = 0;
sum_yy = 0;
sum_yz = 0;
sum_zx = 0;
sum_zy = 0;
sum_zz = 0;

bool weight_is_empty = weight.is_empty();
int size = r_coords1.size();

for (int i = 0; i < size; i++) {
if (!weight_is_empty) {
weighted_coord1 = weight[i] * r_coords1[i];
sum_of_squares1 += weighted_coord1.dot(r_coords1[i]);
} else {
weighted_coord1 = r_coords1[i];
sum_of_squares1 += weighted_coord1.dot(weighted_coord1);
}

weighted_coord2 = r_coords2[i];

sum_of_squares2 += weight_is_empty ? weighted_coord2.dot(weighted_coord2) : (weight[i] * weighted_coord2.dot(weighted_coord2));

sum_xx += (weighted_coord1.x * weighted_coord2.x);
sum_xy += (weighted_coord1.x * weighted_coord2.y);
sum_xz += (weighted_coord1.x * weighted_coord2.z);

sum_yx += (weighted_coord1.y * weighted_coord2.x);
sum_yy += (weighted_coord1.y * weighted_coord2.y);
sum_yz += (weighted_coord1.y * weighted_coord2.z);

sum_zx += (weighted_coord1.z * weighted_coord2.x);
sum_zy += (weighted_coord1.z * weighted_coord2.y);
sum_zz += (weighted_coord1.z * weighted_coord2.z);
}

double initial_eigenvalue = (sum_of_squares1 + sum_of_squares2) * 0.5;

sum_xz_plus_zx = sum_xz + sum_zx;
sum_yz_plus_zy = sum_yz + sum_zy;
sum_xy_plus_yx = sum_xy + sum_yx;
sum_yz_minus_zy = sum_yz - sum_zy;
sum_xz_minus_zx = sum_xz - sum_zx;
sum_xy_minus_yx = sum_xy - sum_yx;
sum_xx_plus_yy = sum_xx + sum_yy;
sum_xx_minus_yy = sum_xx - sum_yy;
max_eigenvalue = initial_eigenvalue;

inner_product_calculated = true;
}

Quaternion QuaternionCharacteristicPolynomial::_weighted_superpose(const PackedVector3Array &p_moved, const PackedVector3Array &p_target, const Vector<double> &p_weight, bool p_translate) {
set(p_moved, p_target, p_weight, p_translate);
return _get_rotation();
}

void QuaternionCharacteristicPolynomial::set(const PackedVector3Array &p_moved, const PackedVector3Array &p_target, const Vector<double> &p_weight, bool p_translate) {
transformation_calculated = false;
inner_product_calculated = false;

moved = p_moved;
target = p_target;
weight = p_weight;

if (p_translate) {
moved_center = move_to_weighted_center(moved, weight);
w_sum = 0; // set wsum to 0 so we don't double up.
target_center = move_to_weighted_center(target, weight);
translate(moved_center * -1, moved);
translate(target_center * -1, target);
} else {
if (!p_weight.is_empty()) {
for (int i = 0; i < p_weight.size(); i++) {
w_sum += p_weight[i];
}
} else {
w_sum = p_moved.size();
}
}
}

void QuaternionCharacteristicPolynomial::weighted_superpose(
const PackedVector3Array p_moved,
const PackedVector3Array p_target,
const Vector<double> p_weight,
bool p_translate,
Quaternion &r_rotation,
Vector3 &r_translation,
double p_precision) {
QuaternionCharacteristicPolynomial qcp(p_precision);
r_rotation = qcp._weighted_superpose(p_moved, p_target, p_weight, p_translate);
r_translation = qcp._get_translation();
}
111 changes: 111 additions & 0 deletions core/math/qcp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**************************************************************************/
/* qcp.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef QCP_H
#define QCP_H

#include "core/math/vector3.h"
#include "core/object/class_db.h"
#include "core/object/object.h"
#include "core/variant/variant.h"

/**
* SPDX short identifier: BSD-2-Clause
*
* Implementation of the Quaternion-Based Characteristic Polynomial algorithm
* for RMSD and Superposition calculations.
*
* Usage:
* 1. Create a QCP object with two Vector3 arrays of equal length as input.
* The input coordinates are not changed.
* 2. Optionally, provide weighting factors [0 - 1] for each point.
* 3. For maximum efficiency, create a QCP object once and reuse it.
*
* A. Calculate rmsd only: double rmsd = qcp.getRmsd();
* B. Calculate a 4x4 transformation (Quaternion and translation) matrix: Matrix4f trans = qcp.getTransformationMatrix();
* C. Get transformed points (y superposed onto the reference x): Vector3[] ySuperposed = qcp.getTransformedCoordinates();
*
* Citations:
* - Liu P, Agrafiotis DK, & Theobald DL (2011) Reply to comment on: "Fast determination of the optimal Quaternionation matrix for macromolecular superpositions." Journal of Computational Chemistry 32(1):185-186. [http://dx.doi.org/10.1002/jcc.21606]
* - Liu P, Agrafiotis DK, & Theobald DL (2010) "Fast determination of the optimal Quaternionation matrix for macromolecular superpositions." Journal of Computational Chemistry 31(7):1561-1563. [http://dx.doi.org/10.1002/jcc.21439]
* - Douglas L Theobald (2005) "Rapid calculation of RMSDs using a quaternion-based characteristic polynomial." Acta Crystallogr A 61(4):478-480. [http://dx.doi.org/10.1107/S0108767305015266]
*
* This is an adaptation of the original C code QCPQuaternion 1.4 (2012, October 10) to C++.
* The original C source code is available from http://theobald.brandeis.edu/qcp/ and was developed by:
* - Douglas L. Theobald, Department of Biochemistry, Brandeis University
* - Pu Liu, Johnson & Johnson Pharmaceutical Research and Development, L.L.C.
*
* @author Douglas L. Theobald (original C code)
* @author Pu Liu (original C code)
* @author Peter Rose (adapted to Java)
* @author Aleix Lafita (adapted to Java)
* @author Eron Gjoni (adapted to EWB IK)
* @author K. S. Ernest (iFire) Lee (adapted to ManyBoneIK)
*/

class QuaternionCharacteristicPolynomial : Object {
GDCLASS(QuaternionCharacteristicPolynomial, Object);
double eigenvector_precision = 1E-10;

PackedVector3Array target, moved;
Vector<double> weight;
double w_sum = 0;

Vector3 target_center, moved_center;

double sum_xy = 0, sum_xz = 0, sum_yx = 0, sum_yz = 0, sum_zx = 0, sum_zy = 0;
double sum_xx_plus_yy = 0, sum_zz = 0, max_eigenvalue = 0, sum_yz_minus_zy = 0, sum_xz_minus_zx = 0, sum_xy_minus_yx = 0;
double sum_xx_minus_yy = 0, sum_xy_plus_yx = 0, sum_xz_plus_zx = 0;
double sum_yy = 0, sum_xx = 0, sum_yz_plus_zy = 0;
bool transformation_calculated = false, inner_product_calculated = false;

void inner_product(PackedVector3Array &r_coords1, PackedVector3Array &r_coords2);
void set(PackedVector3Array &r_target, PackedVector3Array &r_moved);
Quaternion calculate_rotation();
void set(const PackedVector3Array &p_moved, const PackedVector3Array &p_target, const Vector<double> &p_weight, bool p_translate);
static void translate(Vector3 r_translate, PackedVector3Array &r_x);
Vector3 move_to_weighted_center(PackedVector3Array &r_to_center, Vector<double> &r_weight);
Quaternion _weighted_superpose(const PackedVector3Array &p_moved, const PackedVector3Array &p_target, const Vector<double> &p_weight, bool p_translate);
Quaternion _get_rotation();
Vector3 _get_translation();
QuaternionCharacteristicPolynomial(double p_evec_prec);

public:
static void weighted_superpose(
const PackedVector3Array p_moved,
const PackedVector3Array p_target,
const Vector<double> p_weight,
bool p_translate,
Quaternion &r_rotation,
Vector3 &r_translation,
double p_precision = 1E-10);
};

#endif // QCP_H
Loading

0 comments on commit f23f7f2

Please sign in to comment.