VinaLC: Parallel Molecular Docking Program

Biochemical and Biophysical Systems Group
VinaLC version: 1.1.2

quaternion.h
Go to the documentation of this file.
1 /*
2 
3  Copyright (c) 2006-2010, The Scripps Research Institute
4 
5  Licensed under the Apache License, Version 2.0 (the "License");
6  you may not use this file except in compliance with the License.
7  You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11  Unless required by applicable law or agreed to in writing, software
12  distributed under the License is distributed on an "AS IS" BASIS,
13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  See the License for the specific language governing permissions and
15  limitations under the License.
16 
17  Author: Dr. Oleg Trott <ot14@columbia.edu>,
18  The Olson Lab,
19  The Scripps Research Institute
20 
21 */
22 
23 #ifndef VINA_QUATERNION_H
24 #define VINA_QUATERNION_H
25 
26 #include <boost/math/quaternion.hpp>
27 #include <boost/serialization/split_free.hpp>
28 
29 #include "common.h"
30 #include "random.h"
31 
32 typedef boost::math::quaternion<fl> qt;
33 
34 bool quaternion_is_normalized(const qt& q);
35 
36 // non-intrusive free function split serialization
37 namespace boost {
38  namespace serialization {
39  template<class Archive>
40  void save(Archive& ar, const qt& q, const unsigned version) {
41  fl q1 = q.R_component_1();
42  fl q2 = q.R_component_2();
43  fl q3 = q.R_component_3();
44  fl q4 = q.R_component_4();
45 
46  ar & q1;
47  ar & q2;
48  ar & q3;
49  ar & q4;
50  }
51  template<typename Archive>
52  void load(Archive& ar, qt& q, const unsigned version) {
53  fl a, b, c, d;
54  ar & a;
55  ar & b;
56  ar & c;
57  ar & d;
58  q = qt(a, b, c, d);
59  }
60  }
61 }
62 BOOST_SERIALIZATION_SPLIT_FREE(qt)
63 
64 bool eq(const qt& a, const qt& b); // elementwise approximate equality - may return false for equivalent rotations
65 const qt qt_identity(1, 0, 0, 0);
66 qt angle_to_quaternion(const vec& axis, fl angle); // axis is assumed to be a unit vector
67 qt angle_to_quaternion(const vec& rotation); // rotation == angle * axis
68 vec quaternion_to_angle(const qt& q);
69 mat quaternion_to_r3(const qt& q);
70 
71 inline fl quaternion_norm_sqr(const qt& q) { // equivalent to sqr(boost::math::abs(const qt&))
72  return sqr(q.R_component_1()) + sqr(q.R_component_2()) + sqr(q.R_component_3()) + sqr(q.R_component_4());
73 }
74 
75 inline void quaternion_normalize(qt& q) {
76  const fl s = quaternion_norm_sqr(q);
77  assert(eq(s, sqr(boost::math::abs(q))));
78  const fl a = std::sqrt(s);
79  assert(a > epsilon_fl);
80  q *= 1/a;
81  assert(quaternion_is_normalized(q));
82 }
83 
84 inline void quaternion_normalize_approx(qt& q, const fl tolerance = 1e-6) {
85  const fl s = quaternion_norm_sqr(q);
86  assert(eq(s, sqr(boost::math::abs(q))));
87  if(std::abs(s - 1) < tolerance)
88  ; // most likely scenario
89  else {
90  const fl a = std::sqrt(s);
91  assert(a > epsilon_fl);
92  q *= 1/a;
93  assert(quaternion_is_normalized(q));
94  }
95 }
96 
97 qt random_orientation(rng& generator);
98 void quaternion_increment(qt& q, const vec& rotation);
99 vec quaternion_difference(const qt& b, const qt& a); // rotation that needs to be applied to convert a to b
100 void print(const qt& q, std::ostream& out = std::cout); // print as an angle
101 
102 #endif