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

IK_QJacobianSolver.cpp « intern « iksolver « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 32a703cb94d46da67f18733ef0a88237a5046038 (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
/**
 * $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 *****
 */

#include "IK_QJacobianSolver.h"

#include "TNT/svd.h"

using namespace std;

	IK_QJacobianSolver *
IK_QJacobianSolver::
New(
){
	return new IK_QJacobianSolver();
}

	bool
IK_QJacobianSolver::
Solve(
	IK_QChain &chain,
	const MT_Vector3 &g_position,
	const MT_Vector3 &g_pose,
	const MT_Scalar tolerance,
	const int max_iterations,
	const MT_Scalar max_angle_change
){

	const vector<IK_QSegment> & segs = chain.Segments();
	if (segs.size() == 0) return false;
	
	// compute the goal direction from the base of the chain
	// in global coordinates

	MT_Vector3 goal_dir = g_position - segs[0].GlobalSegmentStart();
	

	const MT_Scalar chain_max_extension = chain.MaxExtension();

	bool do_parallel_check(false);

	if (chain_max_extension < goal_dir.length()) {
		do_parallel_check = true;
	}

	goal_dir.normalize();


	ArmMatrices(chain.DoF());

	for (int iterations = 0; iterations < max_iterations; iterations++) {
		
		// check to see if the chain is pointing in the right direction

		if (iterations%32 && do_parallel_check && ParallelCheck(chain,goal_dir)) {

			return false;
		}
		
		MT_Vector3 end_effector = chain.EndEffector();
		MT_Vector3 d_pos = g_position - end_effector;
		const MT_Scalar x_length = d_pos.length();
		
		if (x_length < tolerance) {
			return true;
		}	

		chain.ComputeJacobian();

        try {
		    ComputeInverseJacobian(chain,x_length,max_angle_change);
        }
        catch(...) {
            return false;
        }
		
		ComputeBetas(chain,d_pos);
		UpdateChain(chain);
		chain.UpdateGlobalTransformations();
	}


	return false;
};

IK_QJacobianSolver::
~IK_QJacobianSolver(
){
	// nothing to do
}


IK_QJacobianSolver::
IK_QJacobianSolver(
){
	// nothing to do
};
 
	void
IK_QJacobianSolver::
ComputeBetas(
	IK_QChain &chain,
	const MT_Vector3 d_pos
){	

	m_beta = 0;

	m_beta[0] = d_pos.x();
	m_beta[1] = d_pos.y();
	m_beta[2] = d_pos.z();
	
	TNT::matmult(m_d_theta,m_svd_inverse,m_beta);

};	


	int
IK_QJacobianSolver::
ComputeInverseJacobian(
	IK_QChain &chain,
	const MT_Scalar x_length,
	const MT_Scalar max_angle_change
) {

	int dimension = 0;

	m_svd_u = MT_Scalar(0);

	// copy first 3 rows of jacobian into m_svd_u

	int row, column;

	for (row = 0; row < 3; row ++) {
		for (column = 0; column < chain.Jacobian().num_cols(); column ++) {
			m_svd_u[row][column] = chain.Jacobian()[row][column];
		}
	}

	m_svd_w = MT_Scalar(0);
	m_svd_v = MT_Scalar(0);

    m_svd_work_space = MT_Scalar(0);

	TNT::SVD(m_svd_u,m_svd_w,m_svd_v,m_svd_work_space);

	// invert the SVD and compute inverse

	TNT::transpose(m_svd_v,m_svd_v_t);
	TNT::transpose(m_svd_u,m_svd_u_t);

	// 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 m_svd_w value

	int i = 0;

	MT_Scalar w_min = MT_INFINITY;

	// anything below epsilon is treated as zero

	MT_Scalar epsilon = MT_Scalar(1e-10);

	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];
		}
	}

	MT_Scalar lambda(0);

	MT_Scalar d = x_length/max_angle_change;

	if (w_min <= d/2) {
		lambda = d/2;
	} else
	if (w_min < d) {
		lambda = sqrt(w_min*(d - w_min));
	} else {
		lambda = MT_Scalar(0);
	}


	lambda *= lambda;

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


	TNT::matmultdiag(m_svd_temp1,m_svd_w,m_svd_u_t);
	TNT::matmult(m_svd_inverse,m_svd_v,m_svd_temp1);

	return dimension;

}

	void
IK_QJacobianSolver::
UpdateChain(
	IK_QChain &chain
){

	// iterate through the set of angles and 
	// update their values from the d_thetas

	vector<IK_QSegment> &segs = chain.Segments(); 
	
	int seg_ind = 0;
	for (seg_ind = 0;seg_ind < segs.size(); seg_ind++) {
	
		MT_Vector3 dq;
		dq[0] = m_d_theta[3*seg_ind];
		dq[1] = m_d_theta[3*seg_ind + 1];
		dq[2] = m_d_theta[3*seg_ind + 2];
		segs[seg_ind].IncrementAngle(dq);	
	}

};
	
	void
IK_QJacobianSolver::
ArmMatrices(
	int dof
){

	m_beta.newsize(dof);
	m_d_theta.newsize(dof);

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

    m_svd_work_space.newsize(dof);

	m_svd_u = MT_Scalar(0);
	m_svd_v = MT_Scalar(0);
	m_svd_w = MT_Scalar(0);

	m_svd_u_t.newsize(dof,dof);
	m_svd_v_t.newsize(dof,dof);
	m_svd_w_diag.newsize(dof,dof);
	m_svd_inverse.newsize(dof,dof);
	m_svd_temp1.newsize(dof,dof);

};

	bool
IK_QJacobianSolver::
ParallelCheck(
	const IK_QChain &chain,
	const MT_Vector3 goal_dir
) const {
	
	// compute the start of the chain in global coordinates
	const vector<IK_QSegment> &segs = chain.Segments(); 

	int num_segs = segs.size();
	
	if (num_segs == 0) {
		return false;
	}

	MT_Scalar crossp_sum = 0;

	int i;
	for (i = 0; i < num_segs; i++) {
		MT_Vector3 global_seg_direction = segs[i].GlobalSegmentEnd() - 
			segs[i].GlobalSegmentStart();

		global_seg_direction.normalize();	

		MT_Scalar crossp = (global_seg_direction.cross(goal_dir)).length();
		crossp_sum += MT_Scalar(fabs(crossp));
	}

	if (crossp_sum < MT_Scalar(0.01)) {
		return true;
	} else {
		return false;
	}
}