[2576] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
| 2 | /* |
---|
[3177] | 3 | * Copyright (C) 2004-2012 HUBzero Foundation, LLC |
---|
[2576] | 4 | * |
---|
| 5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
| 6 | */ |
---|
| 7 | |
---|
[3615] | 8 | #ifndef VTKVIS_MATH_H |
---|
| 9 | #define VTKVIS_MATH_H |
---|
[2576] | 10 | |
---|
| 11 | #include <cmath> |
---|
| 12 | #include <cstring> |
---|
| 13 | |
---|
| 14 | #include <vtkMath.h> |
---|
| 15 | #include <vtkMatrix4x4.h> |
---|
| 16 | |
---|
| 17 | namespace VtkVis { |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * \brief Convert a quaternion to an axis/angle rotation |
---|
| 21 | * |
---|
| 22 | * \param[in] quat Quaternion with scalar first: wxyz |
---|
| 23 | * \param[out] angleAxis axis/angle rotation with angle in degrees first |
---|
| 24 | */ |
---|
| 25 | inline void quatToAngleAxis(const double quat[4], double angleAxis[4]) |
---|
| 26 | { |
---|
| 27 | angleAxis[0] = vtkMath::DegreesFromRadians(2.0 * acos(quat[0])); |
---|
| 28 | if (angleAxis[0] < 1.0e-6) { |
---|
| 29 | angleAxis[1] = 1; |
---|
| 30 | angleAxis[2] = 0; |
---|
| 31 | angleAxis[3] = 0; |
---|
| 32 | } else { |
---|
| 33 | double denom = sqrt(1. - quat[0] * quat[0]); |
---|
| 34 | angleAxis[1] = quat[1] / denom; |
---|
| 35 | angleAxis[2] = quat[2] / denom; |
---|
| 36 | angleAxis[3] = quat[3] / denom; |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * \brief Convert an axis/angle rotation to a quaternion |
---|
| 42 | * |
---|
| 43 | * \param[in] angleAxis axis/angle rotation with angle in degrees first |
---|
| 44 | * \param[out] quat Quaternion with scalar first: wxyz |
---|
| 45 | */ |
---|
| 46 | inline void angleAxisToQuat(const double angleAxis[4], double quat[4]) |
---|
| 47 | { |
---|
| 48 | quat[0] = cos(vtkMath::RadiansFromDegrees(angleAxis[0]) / 2.0); |
---|
| 49 | double sinHalfAngle = sin(vtkMath::RadiansFromDegrees(angleAxis[0]) / 2.0); |
---|
| 50 | quat[1] = angleAxis[1] * sinHalfAngle; |
---|
| 51 | quat[2] = angleAxis[2] * sinHalfAngle; |
---|
| 52 | quat[3] = angleAxis[3] * sinHalfAngle; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * \brief Fill a vtkMatrix4x4 from a quaternion |
---|
| 57 | * |
---|
| 58 | * \param[in] quat Quaternion with scalar first: wxyz |
---|
| 59 | * \param[out] mat Matrix to be filled in |
---|
| 60 | */ |
---|
| 61 | inline void quaternionToMatrix4x4(const double quat[4], vtkMatrix4x4& mat) |
---|
| 62 | { |
---|
| 63 | double ww = quat[0]*quat[0]; |
---|
| 64 | double wx = quat[0]*quat[1]; |
---|
| 65 | double wy = quat[0]*quat[2]; |
---|
| 66 | double wz = quat[0]*quat[3]; |
---|
| 67 | |
---|
| 68 | double xx = quat[1]*quat[1]; |
---|
| 69 | double yy = quat[2]*quat[2]; |
---|
| 70 | double zz = quat[3]*quat[3]; |
---|
| 71 | |
---|
| 72 | double xy = quat[1]*quat[2]; |
---|
| 73 | double xz = quat[1]*quat[3]; |
---|
| 74 | double yz = quat[2]*quat[3]; |
---|
| 75 | |
---|
| 76 | double rr = xx + yy + zz; |
---|
| 77 | // normalization factor, just in case quaternion was not normalized |
---|
| 78 | double f = double(1)/double(sqrt(ww + rr)); |
---|
| 79 | double s = (ww - rr)*f; |
---|
| 80 | f *= 2; |
---|
| 81 | |
---|
[5966] | 82 | mat.SetElement(0, 0, xx*f + s); |
---|
| 83 | mat.SetElement(1, 0, (xy + wz)*f); |
---|
| 84 | mat.SetElement(2, 0, (xz - wy)*f); |
---|
[2576] | 85 | |
---|
[5966] | 86 | mat.SetElement(0, 1, (xy - wz)*f); |
---|
| 87 | mat.SetElement(1, 1, yy*f + s); |
---|
| 88 | mat.SetElement(2, 1, (yz + wx)*f); |
---|
[2576] | 89 | |
---|
[5966] | 90 | mat.SetElement(0, 2, (xz + wy)*f); |
---|
| 91 | mat.SetElement(1, 2, (yz - wx)*f); |
---|
| 92 | mat.SetElement(2, 2, zz*f + s); |
---|
[2576] | 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
| 96 | * \brief Fill a vtkMatrix4x4 from a quaternion, but with the matrix |
---|
| 97 | * transposed. |
---|
| 98 | * |
---|
| 99 | * \param[in] quat Quaternion with scalar first: wxyz |
---|
| 100 | * \param[out] mat Matrix to be filled in |
---|
| 101 | */ |
---|
| 102 | inline void quaternionToTransposeMatrix4x4(const double quat[4], vtkMatrix4x4& mat) |
---|
| 103 | { |
---|
| 104 | double ww = quat[0]*quat[0]; |
---|
| 105 | double wx = quat[0]*quat[1]; |
---|
| 106 | double wy = quat[0]*quat[2]; |
---|
| 107 | double wz = quat[0]*quat[3]; |
---|
| 108 | |
---|
| 109 | double xx = quat[1]*quat[1]; |
---|
| 110 | double yy = quat[2]*quat[2]; |
---|
| 111 | double zz = quat[3]*quat[3]; |
---|
| 112 | |
---|
| 113 | double xy = quat[1]*quat[2]; |
---|
| 114 | double xz = quat[1]*quat[3]; |
---|
| 115 | double yz = quat[2]*quat[3]; |
---|
| 116 | |
---|
| 117 | double rr = xx + yy + zz; |
---|
| 118 | // normalization factor, just in case quaternion was not normalized |
---|
| 119 | double f = double(1)/double(sqrt(ww + rr)); |
---|
| 120 | double s = (ww - rr)*f; |
---|
| 121 | f *= 2; |
---|
| 122 | |
---|
[5966] | 123 | mat.SetElement(0, 0, xx*f + s); |
---|
| 124 | mat.SetElement(0, 1, (xy + wz)*f); |
---|
| 125 | mat.SetElement(0, 2, (xz - wy)*f); |
---|
[2576] | 126 | |
---|
[5966] | 127 | mat.SetElement(1, 0, (xy - wz)*f); |
---|
| 128 | mat.SetElement(1, 1, yy*f + s); |
---|
| 129 | mat.SetElement(1, 2, (yz + wx)*f); |
---|
[2576] | 130 | |
---|
[5966] | 131 | mat.SetElement(2, 0, (xz + wy)*f); |
---|
| 132 | mat.SetElement(2, 1, (yz - wx)*f); |
---|
| 133 | mat.SetElement(2, 2, zz*f + s); |
---|
[2576] | 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * \brief Multiply two quaternions and return the result |
---|
| 138 | * |
---|
| 139 | * Note: result can be the same memory location as one of the |
---|
| 140 | * inputs |
---|
| 141 | */ |
---|
| 142 | inline double *quatMult(const double q1[4], const double q2[4], double result[4]) |
---|
| 143 | { |
---|
| 144 | double q1w = q1[0]; |
---|
| 145 | double q1x = q1[1]; |
---|
| 146 | double q1y = q1[2]; |
---|
| 147 | double q1z = q1[3]; |
---|
| 148 | double q2w = q2[0]; |
---|
| 149 | double q2x = q2[1]; |
---|
| 150 | double q2y = q2[2]; |
---|
| 151 | double q2z = q2[3]; |
---|
| 152 | result[0] = (q1w*q2w) - (q1x*q2x) - (q1y*q2y) - (q1z*q2z); |
---|
| 153 | result[1] = (q1w*q2x) + (q1x*q2w) + (q1y*q2z) - (q1z*q2y); |
---|
| 154 | result[2] = (q1w*q2y) + (q1y*q2w) + (q1z*q2x) - (q1x*q2z); |
---|
| 155 | result[3] = (q1w*q2z) + (q1z*q2w) + (q1x*q2y) - (q1y*q2x); |
---|
| 156 | return result; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * \brief Compute the conjugate of a quaternion |
---|
| 161 | * |
---|
| 162 | * Note: result can be the same memory location as the input |
---|
| 163 | */ |
---|
| 164 | inline double *quatConjugate(const double quat[4], double result[4]) |
---|
| 165 | { |
---|
| 166 | if (result != quat) |
---|
| 167 | result[0] = quat[0]; |
---|
| 168 | result[1] = -quat[1]; |
---|
| 169 | result[2] = -quat[2]; |
---|
| 170 | result[3] = -quat[3]; |
---|
| 171 | return result; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * \brief Compute the reciprocal of a quaternion |
---|
| 176 | * |
---|
| 177 | * Note: result can be the same memory location as the input |
---|
| 178 | */ |
---|
| 179 | inline double *quatReciprocal(const double quat[4], double result[4]) |
---|
| 180 | { |
---|
| 181 | double denom = |
---|
| 182 | quat[0]*quat[0] + |
---|
| 183 | quat[1]*quat[1] + |
---|
| 184 | quat[2]*quat[2] + |
---|
| 185 | quat[3]*quat[3]; |
---|
[3615] | 186 | VtkVis::quatConjugate(quat, result); |
---|
[2576] | 187 | for (int i = 0; i < 4; i++) { |
---|
| 188 | result[i] /= denom; |
---|
| 189 | } |
---|
| 190 | return result; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * \brief Compute the reciprocal of a quaternion in place |
---|
| 195 | */ |
---|
| 196 | inline double *quatReciprocal(double quat[4]) |
---|
| 197 | { |
---|
[3615] | 198 | return VtkVis::quatReciprocal(quat, quat); |
---|
[2576] | 199 | } |
---|
| 200 | |
---|
| 201 | /** |
---|
| 202 | * \brief Deep copy a quaternion |
---|
| 203 | */ |
---|
| 204 | inline void copyQuat(const double quat[4], double result[4]) |
---|
| 205 | { |
---|
| 206 | memcpy(result, quat, sizeof(double)*4); |
---|
| 207 | } |
---|
| 208 | |
---|
[2612] | 209 | inline double min2(double a, double b) |
---|
| 210 | { |
---|
| 211 | return ((a < b) ? a : b); |
---|
[2576] | 212 | } |
---|
[2612] | 213 | |
---|
| 214 | inline double max2(double a, double b) |
---|
| 215 | { |
---|
| 216 | return ((a > b) ? a : b); |
---|
[2576] | 217 | } |
---|
| 218 | |
---|
[3744] | 219 | inline double min3(double a, double b, double c) |
---|
| 220 | { |
---|
| 221 | return min2(min2(a, b), c); |
---|
[2612] | 222 | } |
---|
| 223 | |
---|
[3744] | 224 | inline double max3(double a, double b, double c) |
---|
| 225 | { |
---|
| 226 | return max2(max2(a, b), c); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | } |
---|
| 230 | |
---|
[2576] | 231 | #endif |
---|