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

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


#include "IK_QTask.h"

// IK_QTask

IK_QTask::IK_QTask(
	int size,
	bool primary,
	bool active,
	const IK_QSegment *segment
) :
	m_size(size), m_primary(primary), m_active(active), m_segment(segment),
	m_weight(1.0)
{
}

// IK_QPositionTask

IK_QPositionTask::IK_QPositionTask(
	bool primary,
	const IK_QSegment *segment,
	const MT_Vector3& goal
) :
	IK_QTask(3, primary, true, segment), m_goal(goal)
{
	// computing clamping length
	int num;
	const IK_QSegment *seg;

	m_clamp_length = 0.0;
	num = 0;

	for (seg = m_segment; seg; seg = seg->Parent()) {
		m_clamp_length += seg->MaxExtension();
		num++;
	}

	m_clamp_length /= 2*num;
}

void IK_QPositionTask::ComputeJacobian(IK_QJacobian& jacobian)
{
	// compute beta
	const MT_Vector3& pos = m_segment->GlobalEnd();

	MT_Vector3 d_pos = m_goal - pos;
	MT_Scalar length = d_pos.length();

	if (length > m_clamp_length)
		d_pos = (m_clamp_length/length)*d_pos;
	
	jacobian.SetBetas(m_id, m_size, m_weight*d_pos);

	// compute derivatives
	int i;
	const IK_QSegment *seg;

	for (seg = m_segment; seg; seg = seg->Parent()) {
		MT_Vector3 p = seg->GlobalStart() - pos;

		for (i = 0; i < seg->NumberOfDoF(); i++) {
			MT_Vector3 axis = seg->Axis(i)*m_weight;

			if (seg->Translational())
				jacobian.SetDerivatives(m_id, seg->DoFId()+i, axis);
			else {
				MT_Vector3 pa = p.cross(axis);
				jacobian.SetDerivatives(m_id, seg->DoFId()+i, pa);
			}
		}
	}
}

MT_Scalar IK_QPositionTask::Distance() const
{
	const MT_Vector3& pos = m_segment->GlobalEnd();
	MT_Vector3 d_pos = m_goal - pos;
	return d_pos.length();
}

// IK_QOrientationTask

IK_QOrientationTask::IK_QOrientationTask(
	bool primary,
	const IK_QSegment *segment,
	const MT_Matrix3x3& goal
) :
	IK_QTask(3, primary, true, segment), m_goal(goal), m_distance(0.0)
{
}

void IK_QOrientationTask::ComputeJacobian(IK_QJacobian& jacobian)
{
	// compute betas
	const MT_Matrix3x3& rot = m_segment->GlobalTransform().getBasis();

	MT_Matrix3x3 d_rotm = m_goal*rot.transposed();
	d_rotm.transpose();

	MT_Vector3 d_rot;
	d_rot = -0.5*MT_Vector3(d_rotm[2][1] - d_rotm[1][2],
	                        d_rotm[0][2] - d_rotm[2][0],
	                        d_rotm[1][0] - d_rotm[0][1]);

	m_distance = d_rot.length();

	jacobian.SetBetas(m_id, m_size, m_weight*d_rot);

	// compute derivatives
	int i;
	const IK_QSegment *seg;

	for (seg = m_segment; seg; seg = seg->Parent())
		for (i = 0; i < seg->NumberOfDoF(); i++) {

			if (seg->Translational())
				jacobian.SetDerivatives(m_id, seg->DoFId()+i, MT_Vector3(0, 0, 0));
			else {
				MT_Vector3 axis = seg->Axis(i)*m_weight;
				jacobian.SetDerivatives(m_id, seg->DoFId()+i, axis);
			}
		}
}

// IK_QCenterOfMassTask
// Note: implementation not finished!

IK_QCenterOfMassTask::IK_QCenterOfMassTask(
	bool primary,
	const IK_QSegment *segment,
	const MT_Vector3& goal_center
) :
	IK_QTask(3, primary, true, segment), m_goal_center(goal_center)
{
	m_total_mass_inv = ComputeTotalMass(m_segment);
	if (!MT_fuzzyZero(m_total_mass_inv))
		m_total_mass_inv = 1.0/m_total_mass_inv;
}

MT_Scalar IK_QCenterOfMassTask::ComputeTotalMass(const IK_QSegment *segment)
{
	MT_Scalar mass = /*seg->Mass()*/ 1.0;

	const IK_QSegment *seg;
	for (seg = segment->Child(); seg; seg = seg->Sibling())
		mass += ComputeTotalMass(seg);
	
	return mass;
}

MT_Vector3 IK_QCenterOfMassTask::ComputeCenter(const IK_QSegment *segment)
{
	MT_Vector3 center = /*seg->Mass()**/segment->GlobalStart();

	const IK_QSegment *seg;
	for (seg = segment->Child(); seg; seg = seg->Sibling())
		center += ComputeCenter(seg);
	
	return center;
}

void IK_QCenterOfMassTask::JacobianSegment(IK_QJacobian& jacobian, MT_Vector3& center, const IK_QSegment *segment)
{
	int i;
	MT_Vector3 p = center - segment->GlobalStart();

	for (i = 0; i < segment->NumberOfDoF(); i++) {
		MT_Vector3 axis = segment->Axis(i)*m_weight;
		axis *= /*segment->Mass()**/m_total_mass_inv;
		
		if (segment->Translational())
			jacobian.SetDerivatives(m_id, segment->DoFId()+i, axis);
		else {
			MT_Vector3 pa = axis.cross(p);
			jacobian.SetDerivatives(m_id, segment->DoFId()+i, pa);
		}
	}
	
	const IK_QSegment *seg;
	for (seg = segment->Child(); seg; seg = seg->Sibling())
		JacobianSegment(jacobian, center, seg);
}

void IK_QCenterOfMassTask::ComputeJacobian(IK_QJacobian& jacobian)
{
	MT_Vector3 center = ComputeCenter(m_segment)*m_total_mass_inv;

	// compute beta
	MT_Vector3 d_pos = m_goal_center - center;

	m_distance = d_pos.length();

#if 0
	if (m_distance > m_clamp_length)
		d_pos = (m_clamp_length/m_distance)*d_pos;
#endif
	
	jacobian.SetBetas(m_id, m_size, m_weight*d_pos);

	// compute derivatives
	JacobianSegment(jacobian, center, m_segment);
}

MT_Scalar IK_QCenterOfMassTask::Distance() const
{
	return m_distance;
}