Patch to change license to GPL only, from GSR.
[blender-staging.git] / intern / iksolver / intern / IK_Solver.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL 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.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Original Author: Laurence
25  * Contributor(s): Brecht
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "../extern/IK_solver.h"
31
32 #include "IK_QJacobianSolver.h"
33 #include "IK_QSegment.h"
34 #include "IK_QTask.h"
35
36 #include <list>
37 using namespace std;
38
39 class IK_QSolver {
40 public:
41         IK_QSolver() : root(NULL) {};
42
43         IK_QJacobianSolver solver;
44         IK_QSegment *root;
45         std::list<IK_QTask*> tasks;
46 };
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.999)
198                 stiffness = 0.999;
199
200         IK_QSegment *qseg = (IK_QSegment*)seg;
201         MT_Scalar weight = 1.0-stiffness;
202
203         if (axis >= IK_TRANS_X) {
204                 if(!qseg->Translational())
205                         if(qseg->Composite() && qseg->Composite()->Translational())
206                                 qseg = qseg->Composite();
207                         else
208                                 return;
209
210                 if(axis == IK_TRANS_X) axis = IK_X;
211                 else if(axis == IK_TRANS_Y) axis = IK_Y;
212                 else axis = IK_Z;
213         }
214
215         qseg->SetWeight(axis, weight);
216 }
217
218 void IK_GetBasisChange(IK_Segment *seg, float basis_change[][3])
219 {
220         IK_QSegment *qseg = (IK_QSegment*)seg;
221         const MT_Matrix3x3& change = qseg->BasisChange();
222
223         if (qseg->Translational() && qseg->Composite())
224                 qseg = qseg->Composite();
225
226         // convert from moto row major to blender column major
227         basis_change[0][0] = (float)change[0][0];
228         basis_change[1][0] = (float)change[0][1];
229         basis_change[2][0] = (float)change[0][2];
230         basis_change[0][1] = (float)change[1][0];
231         basis_change[1][1] = (float)change[1][1];
232         basis_change[2][1] = (float)change[1][2];
233         basis_change[0][2] = (float)change[2][0];
234         basis_change[1][2] = (float)change[2][1];
235         basis_change[2][2] = (float)change[2][2];
236 }
237
238 void IK_GetTranslationChange(IK_Segment *seg, float *translation_change)
239 {
240         IK_QSegment *qseg = (IK_QSegment*)seg;
241
242         if (!qseg->Translational() && qseg->Composite())
243                 qseg = qseg->Composite();
244         
245         const MT_Vector3& change = qseg->TranslationChange();
246
247         translation_change[0] = (float)change[0];
248         translation_change[1] = (float)change[1];
249         translation_change[2] = (float)change[2];
250 }
251
252 IK_Solver *IK_CreateSolver(IK_Segment *root)
253 {
254         if (root == NULL)
255                 return NULL;
256         
257         IK_QSolver *solver = new IK_QSolver();
258         solver->root = (IK_QSegment*)root;
259
260         return (IK_Solver*)solver;
261 }
262
263 void IK_FreeSolver(IK_Solver *solver)
264 {
265         if (solver == NULL)
266                 return;
267
268         IK_QSolver *qsolver = (IK_QSolver*)solver;
269         std::list<IK_QTask*>& tasks = qsolver->tasks;
270         std::list<IK_QTask*>::iterator task;
271
272         for (task = tasks.begin(); task != tasks.end(); task++)
273                 delete (*task);
274         
275         delete qsolver;
276 }
277
278 void IK_SolverAddGoal(IK_Solver *solver, IK_Segment *tip, float goal[3], float weight)
279 {
280         if (solver == NULL || tip == NULL)
281                 return;
282
283         IK_QSolver *qsolver = (IK_QSolver*)solver;
284         IK_QSegment *qtip = (IK_QSegment*)tip;
285
286         if (qtip->Composite())
287                 qtip = qtip->Composite();
288
289         MT_Vector3 pos(goal);
290
291         IK_QTask *ee = new IK_QPositionTask(true, qtip, pos);
292         ee->SetWeight(weight);
293         qsolver->tasks.push_back(ee);
294 }
295
296 void IK_SolverAddGoalOrientation(IK_Solver *solver, IK_Segment *tip, float goal[][3], float weight)
297 {
298         if (solver == NULL || tip == NULL)
299                 return;
300
301         IK_QSolver *qsolver = (IK_QSolver*)solver;
302         IK_QSegment *qtip = (IK_QSegment*)tip;
303
304         if (qtip->Composite())
305                 qtip = qtip->Composite();
306
307         // convert from blender column major to moto row major
308         MT_Matrix3x3 rot(goal[0][0], goal[1][0], goal[2][0],
309                          goal[0][1], goal[1][1], goal[2][1],
310                          goal[0][2], goal[1][2], goal[2][2]);
311
312         IK_QTask *orient = new IK_QOrientationTask(true, qtip, rot);
313         orient->SetWeight(weight);
314         qsolver->tasks.push_back(orient);
315 }
316
317 void IK_SolverSetPoleVectorConstraint(IK_Solver *solver, IK_Segment *tip, float goal[3], float polegoal[3], float poleangle, int getangle)
318 {
319         if (solver == NULL || tip == NULL)
320                 return;
321
322         IK_QSolver *qsolver = (IK_QSolver*)solver;
323         IK_QSegment *qtip = (IK_QSegment*)tip;
324
325         MT_Vector3 qgoal(goal);
326         MT_Vector3 qpolegoal(polegoal);
327
328         qsolver->solver.SetPoleVectorConstraint(
329                 qtip, qgoal, qpolegoal, poleangle, getangle);
330 }
331
332 float IK_SolverGetPoleAngle(IK_Solver *solver)
333 {
334         if (solver == NULL)
335                 return 0.0f;
336
337         IK_QSolver *qsolver = (IK_QSolver*)solver;
338
339         return qsolver->solver.GetPoleAngle();
340 }
341
342 void IK_SolverAddCenterOfMass(IK_Solver *solver, IK_Segment *root, float goal[3], float weight)
343 {
344         if (solver == NULL || root == NULL)
345                 return;
346
347         IK_QSolver *qsolver = (IK_QSolver*)solver;
348         IK_QSegment *qroot = (IK_QSegment*)root;
349
350         // convert from blender column major to moto row major
351         MT_Vector3 center(goal);
352
353         IK_QTask *com = new IK_QCenterOfMassTask(true, qroot, center);
354         com->SetWeight(weight);
355         qsolver->tasks.push_back(com);
356 }
357
358 int IK_Solve(IK_Solver *solver, float tolerance, int max_iterations)
359 {
360         if (solver == NULL)
361                 return 0;
362
363         IK_QSolver *qsolver = (IK_QSolver*)solver;
364
365         IK_QSegment *root = qsolver->root;
366         IK_QJacobianSolver& jacobian = qsolver->solver;
367         std::list<IK_QTask*>& tasks = qsolver->tasks;
368         MT_Scalar tol = tolerance;
369
370         if(!jacobian.Setup(root, tasks))
371                 return 0;
372
373         bool result = jacobian.Solve(root, tasks, tol, max_iterations);
374
375         return ((result)? 1: 0);
376 }
377