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

IK_QJacobian.cpp « intern « iksolver « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6cc73e9c8085444182c39872e445a03d9a8f85d9 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * $Id$
 * ***** BEGIN GPL 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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Original Author: Laurence
 * Contributor(s): Brecht
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file iksolver/intern/IK_QJacobian.cpp
 *  \ingroup iksolver
 */


#include "IK_QJacobian.h"
#include "TNT/svd.h"

IK_QJacobian::IK_QJacobian()
: m_sdls(true), m_min_damp(1.0)
{
}

IK_QJacobian::~IK_QJacobian()
{
}

void IK_QJacobian::ArmMatrices(int dof, int task_size)
{
	m_dof = dof;
	m_task_size = task_size;

	m_jacobian.newsize(task_size, dof);
	m_jacobian = 0;

	m_alpha.newsize(dof);
	m_alpha = 0;

	m_null.newsize(dof, dof);

	m_d_theta.newsize(dof);
	m_d_theta_tmp.newsize(dof);

	m_norm.newsize(dof);
	m_norm = 0.0;

	m_beta.newsize(task_size);

	m_weight.newsize(dof);
	m_weight_sqrt.newsize(dof);
	m_weight = 1.0;
	m_weight_sqrt = 1.0;

	if (task_size >= dof) {
		m_transpose = false;

		m_jacobian_tmp.newsize(task_size, dof);

		m_svd_u.newsize(task_size, dof);
		m_svd_v.newsize(dof, dof);
		m_svd_w.newsize(dof);

		m_work1.newsize(task_size);
		m_work2.newsize(dof);

		m_svd_u_t.newsize(dof, task_size);
		m_svd_u_beta.newsize(dof);
	}
	else {
		// use the SVD of the transpose jacobian, it works just as well
		// as the original, and often allows using smaller matrices.
		m_transpose = true;

		m_jacobian_tmp.newsize(dof, task_size);

		m_svd_u.newsize(task_size, task_size);
		m_svd_v.newsize(dof, task_size);
		m_svd_w.newsize(task_size);

		m_work1.newsize(dof);
		m_work2.newsize(task_size);

		m_svd_u_t.newsize(task_size, task_size);
		m_svd_u_beta.newsize(task_size);
	}
}

void IK_QJacobian::SetBetas(int id, int, const MT_Vector3& v)
{
	m_beta[id] = v.x();
	m_beta[id+1] = v.y();
	m_beta[id+2] = v.z();
}

void IK_QJacobian::SetDerivatives(int id, int dof_id, const MT_Vector3& v)
{
	m_jacobian[id][dof_id] = v.x()*m_weight_sqrt[dof_id];
	m_jacobian[id+1][dof_id] = v.y()*m_weight_sqrt[dof_id];
	m_jacobian[id+2][dof_id] = v.z()*m_weight_sqrt[dof_id];
}

void IK_QJacobian::Invert()
{
	if (m_transpose) {
		// SVD will decompose Jt into V*W*Ut with U,V orthogonal and W diagonal,
		// so J = U*W*Vt and Jinv = V*Winv*Ut
		TNT::transpose(m_jacobian, m_jacobian_tmp);
		TNT::SVD(m_jacobian_tmp, m_svd_v, m_svd_w, m_svd_u, m_work1, m_work2);
	}
	else {
		// SVD will decompose J into U*W*Vt with U,V orthogonal and W diagonal,
		// so Jinv = V*Winv*Ut
		m_jacobian_tmp = m_jacobian;
		TNT::SVD(m_jacobian_tmp, m_svd_u, m_svd_w, m_svd_v, m_work1, m_work2);
	}

	if (m_sdls)
		InvertSDLS();
	else
		InvertDLS();
}

bool IK_QJacobian::ComputeNullProjection()
{
	MT_Scalar epsilon = 1e-10;
	
	// compute null space projection based on V
	int i, j, rank = 0;
	for (i = 0; i < m_svd_w.size(); i++)
		if (m_svd_w[i] > epsilon)
			rank++;

	if (rank < m_task_size)
		return false;

	TMatrix basis(m_svd_v.num_rows(), rank);
	TMatrix basis_t(rank, m_svd_v.num_rows());
	int b = 0;

	for (i = 0; i < m_svd_w.size(); i++)
		if (m_svd_w[i] > epsilon) {
			for (j = 0; j < m_svd_v.num_rows(); j++)
				basis[j][b] = m_svd_v[j][i];
			b++;
		}
	
	TNT::transpose(basis, basis_t);
	TNT::matmult(m_null, basis, basis_t);

	for (i = 0; i < m_null.num_rows(); i++)
		for (j = 0; j < m_null.num_cols(); j++)
			if (i == j)
				m_null[i][j] = 1.0 - m_null[i][j];
			else
				m_null[i][j] = -m_null[i][j];
	
	return true;
}

void IK_QJacobian::SubTask(IK_QJacobian& jacobian)
{
	if (!ComputeNullProjection())
		return;

	// restrict lower priority jacobian
	jacobian.Restrict(m_d_theta, m_null);

	// add angle update from lower priority
	jacobian.Invert();

	// note: now damps secondary angles with minimum damping value from
	// SDLS, to avoid shaking when the primary task is near singularities,
	// doesn't work well at all
	int i;
	for (i = 0; i < m_d_theta.size(); i++)
		m_d_theta[i] = m_d_theta[i] + /*m_min_damp**/jacobian.AngleUpdate(i);
}

void IK_QJacobian::Restrict(TVector& d_theta, TMatrix& null)
{
	// subtract part already moved by higher task from beta
	TVector beta_sub(m_beta.size());

	TNT::matmult(beta_sub, m_jacobian, d_theta);
	m_beta = m_beta - beta_sub;

	// note: should we be using the norm of the unrestricted jacobian for SDLS?
	
	// project jacobian on to null space of higher priority task
	TMatrix jacobian_copy(m_jacobian);
	TNT::matmult(m_jacobian, jacobian_copy, null);
}

void IK_QJacobian::InvertSDLS()
{
	// Compute the dampeds least squeares pseudo inverse of J.
	//
	// Since J is usually not invertible (most of the times it's not even
	// square), the psuedo inverse is used. This gives us a least squares
	// solution.
	//
	// This is fine when the J*Jt is of full rank. When J*Jt is near to
	// singular the least squares inverse tries to minimize |J(dtheta) - dX)|
	// and doesn't try to minimize  dTheta. This results in eratic changes in
	// angle. The damped least squares minimizes |dtheta| to try and reduce this
	// erratic behaviour.
	//
	// The selectively damped least squares (SDLS) is used here instead of the
	// DLS. The SDLS damps individual singular values, instead of using a single
	// damping term.

	MT_Scalar max_angle_change = MT_PI/4.0;
	MT_Scalar epsilon = 1e-10;
	int i, j;

	m_d_theta = 0;
	m_min_damp = 1.0;

	for (i = 0; i < m_dof; i++) {
		m_norm[i] = 0.0;
		for (j = 0; j < m_task_size; j+=3) {
			MT_Scalar n = 0.0;
			n += m_jacobian[j][i]*m_jacobian[j][i];
			n += m_jacobian[j+1][i]*m_jacobian[j+1][i];
			n += m_jacobian[j+2][i]*m_jacobian[j+2][i];
			m_norm[i] += sqrt(n);
		}
	}

	for (i = 0; i<m_svd_w.size(); i++) {
		if (m_svd_w[i] <= epsilon)
			continue;

		MT_Scalar wInv = 1.0/m_svd_w[i];
		MT_Scalar alpha = 0.0;
		MT_Scalar N = 0.0;

		// compute alpha and N
		for (j=0; j<m_svd_u.num_rows(); j+=3) {
			alpha += m_svd_u[j][i]*m_beta[j];
			alpha += m_svd_u[j+1][i]*m_beta[j+1];
			alpha += m_svd_u[j+2][i]*m_beta[j+2];

			// note: for 1 end effector, N will always be 1, since U is
			// orthogonal, .. so could be optimized
			MT_Scalar tmp;
			tmp = m_svd_u[j][i]*m_svd_u[j][i];
			tmp += m_svd_u[j+1][i]*m_svd_u[j+1][i];
			tmp += m_svd_u[j+2][i]*m_svd_u[j+2][i];
			N += sqrt(tmp);
		}
		alpha *= wInv;

		// compute M, dTheta and max_dtheta
		MT_Scalar M = 0.0;
		MT_Scalar max_dtheta = 0.0, abs_dtheta;

		for (j = 0; j < m_d_theta.size(); j++) {
			MT_Scalar v = m_svd_v[j][i];
			M += MT_abs(v)*m_norm[j];

			// compute tmporary dTheta's
			m_d_theta_tmp[j] = v*alpha;

			// find largest absolute dTheta
			// multiply with weight to prevent unnecessary damping
			abs_dtheta = MT_abs(m_d_theta_tmp[j])*m_weight_sqrt[j];
			if (abs_dtheta > max_dtheta)
				max_dtheta = abs_dtheta;
		}

		M *= wInv;

		// compute damping term and damp the dTheta's
		MT_Scalar gamma = max_angle_change;
		if (N < M)
			gamma *= N/M;

		MT_Scalar damp = (gamma < max_dtheta)? gamma/max_dtheta: 1.0;

		for (j = 0; j < m_d_theta.size(); j++) {
			// slight hack: we do 0.80*, so that if there is some oscillation,
			// the system can still converge (for joint limits). also, it's
			// better to go a little to slow than to far
			
			MT_Scalar dofdamp = damp/m_weight[j];
			if (dofdamp > 1.0) dofdamp = 1.0;
			
			m_d_theta[j] += 0.80*dofdamp*m_d_theta_tmp[j];
		}

		if (damp < m_min_damp)
			m_min_damp = damp;
	}

	// weight + prevent from doing angle updates with angles > max_angle_change
	MT_Scalar max_angle = 0.0, abs_angle;

	for (j = 0; j<m_dof; j++) {
		m_d_theta[j] *= m_weight[j];

		abs_angle = MT_abs(m_d_theta[j]);

		if (abs_angle > max_angle)
			max_angle = abs_angle;
	}
	
	if (max_angle > max_angle_change) {
		MT_Scalar damp = (max_angle_change)/(max_angle_change + max_angle);

		for (j = 0; j<m_dof; j++)
			m_d_theta[j] *= damp;
	}
}

void IK_QJacobian::InvertDLS()
{
	// Compute damped least squares inverse of pseudo inverse
	// Compute damping term lambda

	// Note when lambda is zero this is equivalent to the
	// least squares solution. This is fine when the m_jjt is
	// of full rank. When m_jjt is near to singular the least squares
	// inverse tries to minimize |J(dtheta) - dX)| and doesn't 
	// try to minimize  dTheta. This results in eratic changes in angle.
	// Damped least squares minimizes |dtheta| to try and reduce this
	// erratic behaviour.

	// We don't want to use the damped solution everywhere so we
	// only increase lamda from zero as we approach a singularity.

	// find the smallest non-zero W value, anything below epsilon is
	// treated as zero

	MT_Scalar epsilon = 1e-10;
	MT_Scalar max_angle_change = 0.1;
	MT_Scalar x_length = sqrt(TNT::dot_prod(m_beta, m_beta));

	int i, j;
	MT_Scalar w_min = MT_INFINITY;

	for (i = 0; i <m_svd_w.size() ; i++) {
		if (m_svd_w[i] > epsilon && m_svd_w[i] < w_min)
			w_min = m_svd_w[i];
	}
	
	// compute lambda damping term

	MT_Scalar d = x_length/max_angle_change;
	MT_Scalar lambda;

	if (w_min <= d/2)
		lambda = d/2;
	else if (w_min < d)
		lambda = sqrt(w_min*(d - w_min));
	else
		lambda = 0.0;

	lambda *= lambda;

	if (lambda > 10)
		lambda = 10;

	// immediately multiply with Beta, so we can do matrix*vector products
	// rather than matrix*matrix products

	// compute Ut*Beta
	TNT::transpose(m_svd_u, m_svd_u_t);
	TNT::matmult(m_svd_u_beta, m_svd_u_t, m_beta);

	m_d_theta = 0.0;

	for (i = 0; i < m_svd_w.size(); i++) {
		if (m_svd_w[i] > epsilon) {
			MT_Scalar wInv = m_svd_w[i]/(m_svd_w[i]*m_svd_w[i] + lambda);

			// compute V*Winv*Ut*Beta
			m_svd_u_beta[i] *= wInv;

			for (j = 0; j<m_d_theta.size(); j++)
				m_d_theta[j] += m_svd_v[j][i]*m_svd_u_beta[i];
		}
	}

	for (j = 0; j<m_d_theta.size(); j++)
		m_d_theta[j] *= m_weight[j];
}

void IK_QJacobian::Lock(int dof_id, MT_Scalar delta)
{
	int i;

	for (i = 0; i < m_task_size; i++) {
		m_beta[i] -= m_jacobian[i][dof_id]*delta;
		m_jacobian[i][dof_id] = 0.0;
	}

	m_norm[dof_id] = 0.0; // unneeded
	m_d_theta[dof_id] = 0.0;
}

MT_Scalar IK_QJacobian::AngleUpdate(int dof_id) const
{
	return m_d_theta[dof_id];
}

MT_Scalar IK_QJacobian::AngleUpdateNorm() const
{
	int i;
	MT_Scalar mx = 0.0, dtheta_abs;

	for (i = 0; i < m_d_theta.size(); i++) {
		dtheta_abs = MT_abs(m_d_theta[i]);
		if (dtheta_abs > mx)
			mx = dtheta_abs;
	}
	
	return mx;
}

void IK_QJacobian::SetDoFWeight(int dof, MT_Scalar weight)
{
	m_weight[dof] = weight;
	m_weight_sqrt[dof] = sqrt(weight);
}