23960a9550b0eeedb6902209c4f44b670d8ab643
[blender.git] / intern / iksolver / intern / IK_Solver.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 "../extern/IK_solver.h"
34
35 #include "IK_QJacobianSolver.h"
36 #include "IK_QSegment.h"
37 #include "IK_QTask.h"
38
39 #include <list>
40 using namespace std;
41
42 typedef struct {
43         IK_QJacobianSolver solver;
44         IK_QSegment *root;
45         std::list<IK_QTask*> tasks;
46 } IK_QSolver;
47
48 IK_QSegment *CreateSegment(int flag, bool translate)
49 {
50         int ndof = 0;
51         ndof += (flag & IK_XDOF)? 1: 0;
52         ndof += (flag & IK_YDOF)? 1: 0;
53         ndof += (flag & IK_ZDOF)? 1: 0;
54
55         IK_QSegment *seg;
56
57         if (ndof == 0)
58                 return NULL;
59         else if (ndof == 1) {
60                 int axis;
61
62                 if (flag & IK_XDOF) axis = 0;
63                 else if (flag & IK_YDOF) axis = 1;
64                 else axis = 2;
65
66                 if (translate)
67                         seg = new IK_QTranslateSegment(axis);
68                 else
69                         seg = new IK_QRevoluteSegment(axis);
70         }
71         else if (ndof == 2) {
72                 int axis1, axis2;
73
74                 if (flag & IK_XDOF) {
75                         axis1 = 0;
76                         axis2 = (flag & IK_YDOF)? 1: 2;
77                 }
78                 else {
79                         axis1 = 1;
80                         axis2 = 2;
81                 }
82
83                 if (translate)
84                         seg = new IK_QTranslateSegment(axis1, axis2);
85                 else {
86                         if (axis1 + axis2 == 2)
87                                 seg = new IK_QSwingSegment();
88                         else
89                                 seg = new IK_QElbowSegment((axis1 == 0)? 0: 2);
90                 }
91         }
92         else {
93                 if (translate)
94                         seg = new IK_QTranslateSegment();
95                 else
96                         seg = new IK_QSphericalSegment();
97         }
98
99         return seg;
100 }
101
102 IK_Segment *IK_CreateSegment(int flag)
103 {
104         IK_QSegment *rot = CreateSegment(flag, false);
105         IK_QSegment *trans = CreateSegment(flag >> 3, true);
106
107         IK_QSegment *seg;
108
109         if (rot == NULL && trans == NULL)
110                 seg = new IK_QNullSegment();
111         else if (rot == NULL)
112                 seg = trans;
113         else {
114                 seg = rot;
115
116                 // make it seem from the interface as if the rotation and translation
117                 // segment are one
118                 if (trans) {
119                         seg->SetComposite(trans);
120                         trans->SetParent(seg);
121                 }
122         }
123
124         return seg;
125 }
126
127 void IK_FreeSegment(IK_Segment *seg)
128 {
129         IK_QSegment *qseg = (IK_QSegment*)seg;
130
131         if (qseg->Composite())
132                 delete qseg->Composite();
133         delete qseg;
134 }
135
136 void IK_SetParent(IK_Segment *seg, IK_Segment *parent)
137 {
138         IK_QSegment *qseg = (IK_QSegment*)seg;
139         IK_QSegment *qparent = (IK_QSegment*)parent;
140
141         if (qparent && qparent->Composite())
142                 qseg->SetParent(qparent->Composite());
143         else
144                 qseg->SetParent(qparent);
145 }
146
147 void IK_SetTransform(IK_Segment *seg, float start[3], float rest[][3], float basis[][3], float length)
148 {
149         IK_QSegment *qseg = (IK_QSegment*)seg;
150
151         MT_Vector3 mstart(start);
152         // convert from blender column major to moto row major
153         MT_Matrix3x3 mbasis(basis[0][0], basis[1][0], basis[2][0],
154                             basis[0][1], basis[1][1], basis[2][1],
155                             basis[0][2], basis[1][2], basis[2][2]);
156         MT_Matrix3x3 mrest(rest[0][0], rest[1][0], rest[2][0],
157                            rest[0][1], rest[1][1], rest[2][1],
158                            rest[0][2], rest[1][2], rest[2][2]);
159         MT_Scalar mlength(length);
160
161         if (qseg->Composite()) {
162                 MT_Vector3 cstart(0, 0, 0);
163                 MT_Matrix3x3 cbasis;
164                 cbasis.setIdentity();
165                 
166                 qseg->SetTransform(mstart, mrest, mbasis, 0.0);
167                 qseg->Composite()->SetTransform(cstart, cbasis, cbasis, mlength);
168         }
169         else
170                 qseg->SetTransform(mstart, mrest, mbasis, mlength);
171 }
172
173 void IK_SetLimit(IK_Segment *seg, IK_SegmentAxis axis, float lmin, float lmax)
174 {
175         IK_QSegment *qseg = (IK_QSegment*)seg;
176
177         if (axis >= IK_TRANS_X) {
178                 if(!qseg->Translational())
179                         if(qseg->Composite() && qseg->Composite()->Translational())
180                                 qseg = qseg->Composite();
181                         else
182                                 return;
183
184                 if(axis == IK_TRANS_X) axis = IK_X;
185                 else if(axis == IK_TRANS_Y) axis = IK_Y;
186                 else axis = IK_Z;
187         }
188
189         qseg->SetLimit(axis, lmin, lmax);
190 }
191
192 void IK_SetStiffness(IK_Segment *seg, IK_SegmentAxis axis, float stiffness)
193 {
194         if (stiffness < 0.0)
195                 return;
196         
197         if (stiffness > 0.99)
198                 stiffness = 0.99;
199
200         IK_QSegment *qseg = (IK_QSegment*)seg;
201         MT_Scalar weight = 1.0-stiffness;
202
203
204         if (axis >= IK_TRANS_X) {
205                 if(!qseg->Translational())
206                         if(qseg->Composite() && qseg->Composite()->Translational())
207                                 qseg = qseg->Composite();
208                         else
209                                 return;
210
211                 if(axis == IK_TRANS_X) axis = IK_X;
212                 else if(axis == IK_TRANS_Y) axis = IK_Y;
213                 else axis = IK_Z;
214         }
215
216         qseg->SetWeight(axis, weight);
217 }
218
219 void IK_GetBasisChange(IK_Segment *seg, float basis_change[][3])
220 {
221         IK_QSegment *qseg = (IK_QSegment*)seg;
222         const MT_Matrix3x3& change = qseg->BasisChange();
223
224         if (qseg->Translational() && qseg->Composite())
225                 qseg = qseg->Composite();
226
227         // convert from moto row major to blender column major
228         basis_change[0][0] = (float)change[0][0];
229         basis_change[1][0] = (float)change[0][1];
230         basis_change[2][0] = (float)change[0][2];
231         basis_change[0][1] = (float)change[1][0];
232         basis_change[1][1] = (float)change[1][1];
233         basis_change[2][1] = (float)change[1][2];
234         basis_change[0][2] = (float)change[2][0];
235         basis_change[1][2] = (float)change[2][1];
236         basis_change[2][2] = (float)change[2][2];
237 }
238
239 void IK_GetTranslationChange(IK_Segment *seg, float *translation_change)
240 {
241         IK_QSegment *qseg = (IK_QSegment*)seg;
242
243         if (!qseg->Translational() && qseg->Composite())
244                 qseg = qseg->Composite();
245         
246         const MT_Vector3& change = qseg->TranslationChange();
247
248         translation_change[0] = (float)change[0];
249         translation_change[1] = (float)change[1];
250         translation_change[2] = (float)change[2];
251 }
252
253 IK_Solver *IK_CreateSolver(IK_Segment *root)
254 {
255         if (root == NULL)
256                 return NULL;
257         
258         IK_QSolver *solver = new IK_QSolver();
259         solver->root = (IK_QSegment*)root;
260
261         return (IK_Solver*)solver;
262 }
263
264 void IK_FreeSolver(IK_Solver *solver)
265 {
266         if (solver == NULL)
267                 return;
268
269         IK_QSolver *qsolver = (IK_QSolver*)solver;
270         std::list<IK_QTask*>& tasks = qsolver->tasks;
271         std::list<IK_QTask*>::iterator task;
272
273         for (task = tasks.begin(); task != tasks.end(); task++)
274                 delete (*task);
275         
276         delete qsolver;
277 }
278
279 void IK_SolverAddGoal(IK_Solver *solver, IK_Segment *tip, float goal[3], float weight)
280 {
281         if (solver == NULL || tip == NULL)
282                 return;
283
284         IK_QSolver *qsolver = (IK_QSolver*)solver;
285         IK_QSegment *qtip = (IK_QSegment*)tip;
286
287         if (qtip->Composite())
288                 qtip = qtip->Composite();
289
290         MT_Vector3 pos(goal);
291
292         IK_QTask *ee = new IK_QPositionTask(true, qtip, pos);
293         ee->SetWeight(weight);
294         qsolver->tasks.push_back(ee);
295 }
296
297 void IK_SolverAddGoalOrientation(IK_Solver *solver, IK_Segment *tip, float goal[][3], float weight)
298 {
299         if (solver == NULL || tip == NULL)
300                 return;
301
302         IK_QSolver *qsolver = (IK_QSolver*)solver;
303         IK_QSegment *qtip = (IK_QSegment*)tip;
304
305         if (qtip->Composite())
306                 qtip = qtip->Composite();
307
308         // convert from blender column major to moto row major
309         MT_Matrix3x3 rot(goal[0][0], goal[1][0], goal[2][0],
310                          goal[0][1], goal[1][1], goal[2][1],
311                          goal[0][2], goal[1][2], goal[2][2]);
312
313         IK_QTask *orient = new IK_QOrientationTask(true, qtip, rot);
314         orient->SetWeight(weight);
315         qsolver->tasks.push_back(orient);
316 }
317
318 void IK_SolverAddCenterOfMass(IK_Solver *solver, IK_Segment *root, float goal[3], float weight)
319 {
320         if (solver == NULL || root == NULL)
321                 return;
322
323         IK_QSolver *qsolver = (IK_QSolver*)solver;
324         IK_QSegment *qroot = (IK_QSegment*)root;
325
326         // convert from blender column major to moto row major
327         MT_Vector3 center(goal);
328
329         IK_QTask *com = new IK_QCenterOfMassTask(true, qroot, center);
330         com->SetWeight(weight);
331         qsolver->tasks.push_back(com);
332 }
333
334 int IK_Solve(IK_Solver *solver, float tolerance, int max_iterations)
335 {
336         if (solver == NULL)
337                 return 0;
338
339         IK_QSolver *qsolver = (IK_QSolver*)solver;
340
341         IK_QSegment *root = qsolver->root;
342         IK_QJacobianSolver& jacobian = qsolver->solver;
343         std::list<IK_QTask*>& tasks = qsolver->tasks;
344         MT_Scalar tol = tolerance;
345
346         bool result = jacobian.Solve(root, tasks, tol, max_iterations);
347
348         return ((result)? 1: 0);
349 }
350