Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75deb2c

Browse files
committed
add quaternion normalization to CartesianPose constructors
1 parent 6bf5a30 commit 75deb2c

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ target_link_libraries(test_utilities Utilities)
3030
# headers
3131

3232
install(DIRECTORY include/
33-
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}
33+
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/
3434
)
3535

3636
# binaries

src/test.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ int main() {
3434
// std::cout << "Pose 2: " << pose2.poseString() << std::endl;
3535
// pose2.print();
3636

37+
// Eigen::Quaterniond q = Eigen::Quaterniond(0.1, 0.9, 0.9, 0.1);
38+
// MatrixXd R = q.toRotationMatrix();
39+
// std::cout << "q: " << q.w() << ", " << q.x() << ", " << q.y() << ", " << q.z() << std::endl;
40+
// std::cout << "R:\n" << R << std::endl;
41+
// std::cout << "R'*R:\n" << R.transpose()*R << std::endl;
42+
// std::cout << "R norm: " << R.norm() << std::endl;
43+
44+
std::vector<double> pose_vec1 = {0,0,0,1,0,0,0};
45+
std::vector<double> pose_vec2 = {0.405014, -0.348191, 0.330936, -0.017058, 0.998968, -0.019972, 0.037051};
46+
CartesianPose pose_WGs = CartesianPose(pose_vec1);
47+
CartesianPose pose_WT = CartesianPose(pose_vec2);
48+
CartesianPose pose_TGs = pose_WT.inv()*pose_WGs;
49+
std::cout << "pose_WGs: " << pose_WGs.poseString() << std::endl;
50+
std::cout << "pose_WT: " << pose_WT.poseString() << std::endl;
51+
std::cout << "pose_WT.inv(): " << pose_WT.inv().poseString() << std::endl;
52+
std::cout << "pose_TGs: " << pose_TGs.poseString() << std::endl;
53+
3754
// Eigen::MatrixXd A(2,5);
3855
// A.setRandom();
3956
// // A << -4.3102, 14.3674, -3.3332,
@@ -63,9 +80,9 @@ int main() {
6380
// std::cout << sample() << std::endl;
6481

6582
/* test wedgeright*/
66-
Eigen::VectorXd v = Eigen::VectorXd::Random(6);
67-
Eigen::VectorXd p = Eigen::VectorXd::Random(4);
68-
std::cout << "wedge6(v)*p:\n" << wedge6(v)*p << std::endl;
69-
std::cout << "wedgeRight6(p) * v:\n" << wedgeRight6(p) * v << std::endl;
83+
// Eigen::VectorXd v = Eigen::VectorXd::Random(6);
84+
// Eigen::VectorXd p = Eigen::VectorXd::Random(4);
85+
// std::cout << "wedge6(v)*p:\n" << wedge6(v)*p << std::endl;
86+
// std::cout << "wedgeRight6(p) * v:\n" << wedgeRight6(p) * v << std::endl;
7087
return 0;
7188
}

src/utilities.cpp

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,27 @@ void so32quat(const Vector3d &so3, double *q) {
541541
}
542542
}
543543
void SO32quat(const Matrix3d &SO3, double *q) {
544-
Quaterniond q_eigen(SO3);
545-
q_eigen.normalize();
546-
q[0] = q_eigen.w();
547-
q[1] = q_eigen.x();
548-
q[2] = q_eigen.y();
549-
q[3] = q_eigen.z();
544+
// Eigen implementation assumes input matrix is strictly orthogonal and is not
545+
// numerically stable sometimes.
546+
Quaterniond q_eigen(SO3);
547+
q_eigen.normalize();
548+
q[0] = q_eigen.w();
549+
q[1] = q_eigen.x();
550+
q[2] = q_eigen.y();
551+
q[3] = q_eigen.z();
552+
// double r11 = SO3(0,0);
553+
// double r12 = SO3(0,1);
554+
// double r13 = SO3(0,2);
555+
// double r21 = SO3(1,0);
556+
// double r22 = SO3(1,1);
557+
// double r23 = SO3(1,2);
558+
// double r31 = SO3(2,0);
559+
// double r32 = SO3(2,1);
560+
// double r33 = SO3(2,2);
561+
// q1_sq = 0.25*(1.0+r11+r22+r33);
562+
// q2_sq = 0.25*(1.0+r11-r22-r33);
563+
// q3_sq = 0.25*(1.0-r11+r22-r33);
564+
// q4_sq = 0.25*(1.0-r11-r22+r33);
550565
}
551566

552567
Matrix4d pose2SE3(const double *pose) {
@@ -881,17 +896,13 @@ CartesianPose::CartesianPose(std::vector<double> pose) {
881896
(*p_)[0] = pose[0];
882897
(*p_)[1] = pose[1];
883898
(*p_)[2] = pose[2];
884-
qw_ = pose[3];
885-
qx_ = pose[4];
886-
qy_ = pose[5];
887-
qz_ = pose[6];
888899
double norm = sqrt(pose[3]*pose[3] + pose[4]*pose[4] + pose[5]*pose[5]
889900
+ pose[6]*pose[6]);
890901
assert(abs(norm - 1.0) < 0.1);
891-
// q_->w() = pose[3]/norm;
892-
// q_->x() = pose[4]/norm;
893-
// q_->y() = pose[5]/norm;
894-
// q_->z() = pose[6]/norm;
902+
qw_ = pose[3]/norm;
903+
qx_ = pose[4]/norm;
904+
qy_ = pose[5]/norm;
905+
qz_ = pose[6]/norm;
895906
Eigen::Quaterniond q = Eigen::Quaterniond(qw_, qx_, qy_, qz_);
896907
*R_ = q.toRotationMatrix();
897908
}
@@ -922,6 +933,11 @@ CartesianPose::CartesianPose(const Eigen::MatrixXd &T) {
922933
*p_ = T.block<3,1>(0,3);
923934
*R_ = T.block<3,3>(0,0);
924935
Eigen::Quaterniond q = Eigen::Quaterniond(*R_);
936+
if (fabs(q.norm() - 1) > 0.1) {
937+
std::cout << "[cpplibrary] Error: input matrix is not orthonormal." << std::endl;
938+
std::cout << "R:\n" << *R_ << std::endl;
939+
std::cout << "R'*R:\n" << R_->transpose()*(*R_) << std::endl;
940+
}
925941
qw_ = q.w();
926942
qx_ = q.x();
927943
qy_ = q.y();

0 commit comments

Comments
 (0)