Update SConscript.
[blender-staging.git] / intern / iksolver / intern / IK_QTask.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Original Author: Laurence
28  * Contributor(s): Brecht
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include "IK_QTask.h"
34
35 // IK_QTask
36
37 IK_QTask::IK_QTask(
38         int size,
39         bool primary,
40         bool active,
41         const IK_QSegment *segment
42 ) :
43         m_size(size), m_primary(primary), m_active(active), m_segment(segment),
44         m_weight(1.0)
45 {
46 }
47
48 // IK_QPositionTask
49
50 IK_QPositionTask::IK_QPositionTask(
51         bool primary,
52         const IK_QSegment *segment,
53         const MT_Vector3& goal
54 ) :
55         IK_QTask(3, primary, true, segment), m_goal(goal)
56 {
57         // computing clamping length
58         int num;
59         const IK_QSegment *seg;
60
61         m_clamp_length = 0.0;
62         num = 0;
63
64         for (seg = m_segment; seg; seg = seg->Parent()) {
65                 m_clamp_length += seg->MaxExtension();
66                 num++;
67         }
68
69         m_clamp_length /= 2*num;
70 }
71
72 void IK_QPositionTask::ComputeJacobian(IK_QJacobian& jacobian)
73 {
74         // compute beta
75         const MT_Vector3& pos = m_segment->GlobalEnd();
76
77         MT_Vector3 d_pos = m_goal - pos;
78         MT_Scalar length = d_pos.length();
79
80         if (length > m_clamp_length)
81                 d_pos = (m_clamp_length/length)*d_pos;
82         
83         jacobian.SetBetas(m_id, m_size, m_weight*d_pos);
84
85
86         // compute derivatives
87         int i;
88         const IK_QSegment *seg;
89
90         for (seg = m_segment; seg; seg = seg->Parent()) {
91                 MT_Vector3 p = seg->GlobalStart() - pos;
92
93                 for (i = 0; i < seg->NumberOfDoF(); i++) {
94                         MT_Vector3 axis = seg->Axis(i)*m_weight;
95                         
96                         if (seg->Translational())
97                                 jacobian.SetDerivatives(m_id, seg->DoFId()+i, axis);
98                         else {
99                                 MT_Vector3 pa = p.cross(axis);
100                                 jacobian.SetDerivatives(m_id, seg->DoFId()+i, pa);
101                         }
102                 }
103         }
104 }
105
106 MT_Scalar IK_QPositionTask::Distance() const
107 {
108         const MT_Vector3& pos = m_segment->GlobalEnd();
109         MT_Vector3 d_pos = m_goal - pos;
110         return d_pos.length();
111 }
112
113 // IK_QOrientationTask
114
115 IK_QOrientationTask::IK_QOrientationTask(
116         bool primary,
117         const IK_QSegment *segment,
118         const MT_Matrix3x3& goal
119 ) :
120         IK_QTask(3, primary, true, segment), m_goal(goal), m_distance(0.0)
121 {
122 }
123
124 void IK_QOrientationTask::ComputeJacobian(IK_QJacobian& jacobian)
125 {
126         // compute betas
127         const MT_Matrix3x3& rot = m_segment->GlobalTransform().getBasis();
128
129         MT_Matrix3x3 d_rotm = m_goal*rot.transposed();
130         d_rotm.transpose();
131
132         MT_Vector3 d_rot;
133         d_rot = -0.5*MT_Vector3(d_rotm[2][1] - d_rotm[1][2],
134                                 d_rotm[0][2] - d_rotm[2][0],
135                                 d_rotm[1][0] - d_rotm[0][1]);
136
137         m_distance = d_rot.length();
138
139         jacobian.SetBetas(m_id, m_size, m_weight*d_rot);
140
141         // compute derivatives
142         int i;
143         const IK_QSegment *seg;
144
145         for (seg = m_segment; seg; seg = seg->Parent())
146                 for (i = 0; i < seg->NumberOfDoF(); i++) {
147
148                         if (seg->Translational())
149                                 jacobian.SetDerivatives(m_id, seg->DoFId()+i, MT_Vector3(0, 0, 0));
150                         else {
151                                 MT_Vector3 axis = seg->Axis(i)*m_weight;
152                                 jacobian.SetDerivatives(m_id, seg->DoFId()+i, axis);
153                         }
154                 }
155 }
156
157 // IK_QCenterOfMassTask
158 // Note: implementation not finished!
159
160 IK_QCenterOfMassTask::IK_QCenterOfMassTask(
161         bool primary,
162         const IK_QSegment *segment,
163         const MT_Vector3& goal_center
164 ) :
165         IK_QTask(3, primary, true, segment), m_goal_center(goal_center)
166 {
167         m_total_mass_inv = ComputeTotalMass(m_segment);
168         if (!MT_fuzzyZero(m_total_mass_inv))
169                 m_total_mass_inv = 1.0/m_total_mass_inv;
170 }
171
172 MT_Scalar IK_QCenterOfMassTask::ComputeTotalMass(const IK_QSegment *segment)
173 {
174         MT_Scalar mass = /*seg->Mass()*/ 1.0;
175
176         const IK_QSegment *seg;
177         for (seg = segment->Child(); seg; seg = seg->Sibling())
178                 mass += ComputeTotalMass(seg);
179         
180         return mass;
181 }
182
183 MT_Vector3 IK_QCenterOfMassTask::ComputeCenter(const IK_QSegment *segment)
184 {
185         MT_Vector3 center = /*seg->Mass()**/segment->GlobalStart();
186
187         const IK_QSegment *seg;
188         for (seg = segment->Child(); seg; seg = seg->Sibling())
189                 center += ComputeCenter(seg);
190         
191         return center;
192 }
193
194 void IK_QCenterOfMassTask::JacobianSegment(IK_QJacobian& jacobian, MT_Vector3& center, const IK_QSegment *segment)
195 {
196         int i;
197         MT_Vector3 p = center - segment->GlobalStart();
198
199         for (i = 0; i < segment->NumberOfDoF(); i++) {
200                 MT_Vector3 axis = segment->Axis(i)*m_weight;
201                 axis *= /*segment->Mass()**/m_total_mass_inv;
202                 
203                 if (segment->Translational())
204                         jacobian.SetDerivatives(m_id, segment->DoFId()+i, axis);
205                 else {
206                         MT_Vector3 pa = axis.cross(p);
207                         jacobian.SetDerivatives(m_id, segment->DoFId()+i, pa);
208                 }
209         }
210         
211         const IK_QSegment *seg;
212         for (seg = segment->Child(); seg; seg = seg->Sibling())
213                 JacobianSegment(jacobian, center, seg);
214 }
215
216 void IK_QCenterOfMassTask::ComputeJacobian(IK_QJacobian& jacobian)
217 {
218         MT_Vector3 center = ComputeCenter(m_segment)*m_total_mass_inv;
219
220         // compute beta
221         MT_Vector3 d_pos = m_goal_center - center;
222
223         m_distance = d_pos.length();
224
225 #if 0
226         if (m_distance > m_clamp_length)
227                 d_pos = (m_clamp_length/m_distance)*d_pos;
228 #endif
229         
230         jacobian.SetBetas(m_id, m_size, m_weight*d_pos);
231
232         // compute derivatives
233         JacobianSegment(jacobian, center, m_segment);
234 }
235
236 MT_Scalar IK_QCenterOfMassTask::Distance() const
237 {
238         return m_distance;
239 }
240