Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MT_ExpMap.cpp « intern « iksolver « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea005a42096fd7ba46da48113b166a50eac4b400 (plain)
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
/**
 * $Id$
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

/**

 * $Id$
 * Copyright (C) 2001 NaN Technologies B.V.
 *
 * @author Laurence
 */

#include "MT_ExpMap.h"

/** 
 * Set the exponential map from a quaternion. The quaternion must be non-zero.
 */

	void
MT_ExpMap::
setRotation(
	const MT_Quaternion &q
) {
	// ok first normailize the quaternion
	// then compute theta the axis-angle and the normalized axis v
	// scale v by theta and that's it hopefully!

	MT_Quaternion qt = q.normalized();

	MT_Vector3 axis(qt.x(),qt.y(),qt.z());
	MT_Scalar cosp = qt.w();
	MT_Scalar sinp = axis.length();
	axis /= sinp;

	MT_Scalar theta = atan2(double(sinp),double(cosp));

	axis *= theta;	
	m_v = axis;
}
 	
/** 
 * Convert from an exponential map to a quaternion
 * representation
 */	

	MT_Quaternion 
MT_ExpMap::
getRotation(
) const {
    bool  rep=0;
    MT_Scalar  cosp, sinp, theta;

	MT_Quaternion q;

	theta = m_v.length();
    
    cosp = MT_Scalar(cos(.5*theta));
    sinp = MT_Scalar(sin(.5*theta));

    q.w() = cosp;

    if (theta < MT_EXPMAP_MINANGLE) {

		MT_Vector3 temp = m_v * MT_Scalar(MT_Scalar(.5) - theta*theta/MT_Scalar(48.0)); /* Taylor Series for sinc */
		q.x() = temp.x();
		q.y() = temp.y();
		q.z() = temp.z();
	} else {
		MT_Vector3 temp = m_v * (sinp/theta); /* Taylor Series for sinc */
		q.x() = temp.x();
		q.y() = temp.y();
		q.z() = temp.z();
	}

    return q;
}
	
/** 
 * Convert the exponential map to a 3x3 matrix
 */

	MT_Matrix3x3
MT_ExpMap::
getMatrix(
) const {

	MT_Quaternion q = getRotation();
	return MT_Matrix3x3(q);
}




/** 
 * Force a reparameterization of the exponential
 * map.
 */

	bool
MT_ExpMap::
reParameterize(
	MT_Scalar &theta
){
    bool rep(false);
    theta = m_v.length();

    if (theta > MT_PI){
		MT_Scalar scl = theta;
		if (theta > MT_2_PI){	/* first get theta into range 0..2PI */
			theta = MT_Scalar(fmod(theta, MT_2_PI));
			scl = theta/scl;
			m_v *= scl;
			rep = true;
		}
		if (theta > MT_PI){
			scl = theta;
			theta = MT_2_PI - theta;
			scl = MT_Scalar(1.0) - MT_2_PI/scl;
			m_v *= scl;
			rep = true;
		}
    }
	return rep;

}

/**
 * Compute the partial derivatives of the exponential
 * map  (dR/de - where R is a 4x4 rotation matrix formed 
 * from the map) and return them as a 4x4 matrix
 */

	MT_Matrix4x4
MT_ExpMap::
partialDerivatives(
	const int i
) const {
	
	MT_Quaternion q = getRotation();
	MT_Quaternion dQdx;

	MT_Matrix4x4 output;

	compute_dQdVi(i,dQdx);
	compute_dRdVi(q,dQdx,output);

	return output;
}

	void
MT_ExpMap::
compute_dRdVi(
	const MT_Quaternion &q,
	const MT_Quaternion &dQdvi,
	MT_Matrix4x4 & dRdvi
) const {

    MT_Scalar  prod[9];
    
    /* This efficient formulation is arrived at by writing out the
     * entire chain rule product dRdq * dqdv in terms of 'q' and 
     * noticing that all the entries are formed from sums of just
     * nine products of 'q' and 'dqdv' */

    prod[0] = -MT_Scalar(4)*q.x()*dQdvi.x();
    prod[1] = -MT_Scalar(4)*q.y()*dQdvi.y();
    prod[2] = -MT_Scalar(4)*q.z()*dQdvi.z();
    prod[3] = MT_Scalar(2)*(q.y()*dQdvi.x() + q.x()*dQdvi.y());
    prod[4] = MT_Scalar(2)*(q.w()*dQdvi.z() + q.z()*dQdvi.w());
    prod[5] = MT_Scalar(2)*(q.z()*dQdvi.x() + q.x()*dQdvi.z());
    prod[6] = MT_Scalar(2)*(q.w()*dQdvi.y() + q.y()*dQdvi.w());
    prod[7] = MT_Scalar(2)*(q.z()*dQdvi.y() + q.y()*dQdvi.z());
    prod[8] = MT_Scalar(2)*(q.w()*dQdvi.x() + q.x()*dQdvi.w());

    /* first row, followed by second and third */
    dRdvi[0][0] = prod[1] + prod[2];
    dRdvi[0][1] = prod[3] - prod[4];
    dRdvi[0][2] = prod[5] + prod[6];

    dRdvi[1][0] = prod[3] + prod[4];
    dRdvi[1][1] = prod[0] + prod[2];
    dRdvi[1][2] = prod[7] - prod[8];

    dRdvi[2][0] = prod[5] - prod[6];
    dRdvi[2][1] = prod[7] + prod[8];
    dRdvi[2][2] = prod[0] + prod[1];

    /* the 4th row and column are all zero */
    int       i;

    for (i=0; i<3; i++)	
      dRdvi[3][i] = dRdvi[i][3] = MT_Scalar(0);
    dRdvi[3][3] = 0;
}

// compute partial derivatives dQ/dVi

	void
MT_ExpMap::
compute_dQdVi(
	const int i,
	MT_Quaternion & dQdX
) const {

    MT_Scalar   theta = m_v.length();
    MT_Scalar   cosp(cos(MT_Scalar(.5)*theta)), sinp(sin(MT_Scalar(.5)*theta));
    
    MT_assert(i>=0 && i<3);

    /* This is an efficient implementation of the derivatives given
     * in Appendix A of the paper with common subexpressions factored out */
    if (theta < MT_EXPMAP_MINANGLE){
		const int i2 = (i+1)%3, i3 = (i+2)%3;
		MT_Scalar Tsinc = MT_Scalar(0.5) - theta*theta/MT_Scalar(48.0);
		MT_Scalar vTerm = m_v[i] * (theta*theta/MT_Scalar(40.0) - MT_Scalar(1.0)) / MT_Scalar(24.0);
		
		dQdX.w() = -.5*m_v[i]*Tsinc;
		dQdX[i]  = m_v[i]* vTerm + Tsinc;
		dQdX[i2] = m_v[i2]*vTerm;
		dQdX[i3] = m_v[i3]*vTerm;
    } else {
		const int i2 = (i+1)%3, i3 = (i+2)%3;
		const MT_Scalar  ang = 1.0/theta, ang2 = ang*ang*m_v[i], sang = sinp*ang;
		const MT_Scalar  cterm = ang2*(.5*cosp - sang);
		
		dQdX[i]  = cterm*m_v[i] + sang;
		dQdX[i2] = cterm*m_v[i2];
		dQdX[i3] = cterm*m_v[i3];
		dQdX.w() = MT_Scalar(-.5)*m_v[i]*sang;
    }
}