93dbbfc280fd0b6e09a6ca63bc64432f8fe3a3b8
[blender.git] / intern / iksolver / intern / IK_QTask.h
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #ifndef NAN_INCLUDED_IK_QTask_h
31 #define NAN_INCLUDED_IK_QTask_h
32
33 #include "MT_Vector3.h"
34 #include "MT_Transform.h"
35 #include "MT_Matrix4x4.h"
36 #include "IK_QJacobian.h"
37 #include "IK_QSegment.h"
38
39 class IK_QTask
40 {
41 public:
42         IK_QTask(
43                 int size,
44                 bool primary,
45                 bool active,
46                 const IK_QSegment *segment
47         );
48         virtual ~IK_QTask() {};
49
50         int Id() const
51         { return m_size; }
52
53         void SetId(int id)
54         { m_id = id; }
55
56         int Size() const
57         { return m_size; }
58
59         bool Primary() const
60         { return m_primary; }
61
62         bool Active() const
63         { return m_active; }
64
65         MT_Scalar Weight() const
66         { return m_weight*m_weight; }
67
68         void SetWeight(MT_Scalar weight)
69         { m_weight = sqrt(weight); }
70
71         virtual void ComputeJacobian(IK_QJacobian& jacobian)=0;
72
73         virtual MT_Scalar Distance() const=0;
74
75         virtual bool PositionTask() const { return false; }
76
77         virtual void Scale(float) {}
78
79 protected:
80         int m_id;
81         int m_size;
82         bool m_primary;
83         bool m_active;
84         const IK_QSegment *m_segment;
85         MT_Scalar m_weight;
86 };
87
88 class IK_QPositionTask : public IK_QTask
89 {
90 public:
91         IK_QPositionTask(
92                 bool primary,
93                 const IK_QSegment *segment,
94                 const MT_Vector3& goal
95         );
96
97         void ComputeJacobian(IK_QJacobian& jacobian);
98
99         MT_Scalar Distance() const;
100
101         bool PositionTask() const { return true; }
102         void Scale(float scale) { m_goal *= scale; m_clamp_length *= scale; }
103
104 private:
105         MT_Vector3 m_goal;
106         MT_Scalar m_clamp_length;
107 };
108
109 class IK_QOrientationTask : public IK_QTask
110 {
111 public:
112         IK_QOrientationTask(
113                 bool primary,
114                 const IK_QSegment *segment,
115                 const MT_Matrix3x3& goal
116         );
117
118         MT_Scalar Distance() const { return m_distance; };
119         void ComputeJacobian(IK_QJacobian& jacobian);
120
121 private:
122         MT_Matrix3x3 m_goal;
123         MT_Scalar m_distance;
124 };
125
126
127 class IK_QCenterOfMassTask : public IK_QTask
128 {
129 public:
130         IK_QCenterOfMassTask(
131                 bool primary,
132                 const IK_QSegment *segment,
133                 const MT_Vector3& center
134         );
135
136         void ComputeJacobian(IK_QJacobian& jacobian);
137
138         MT_Scalar Distance() const;
139
140         void Scale(float scale) { m_goal_center *= scale; m_distance *= scale; }
141
142 private:
143         MT_Scalar ComputeTotalMass(const IK_QSegment *segment);
144         MT_Vector3 ComputeCenter(const IK_QSegment *segment);
145         void JacobianSegment(IK_QJacobian& jacobian, MT_Vector3& center, const IK_QSegment *segment);
146
147         MT_Vector3 m_goal_center;
148         MT_Scalar m_total_mass_inv;
149         MT_Scalar m_distance;
150 };
151
152 #endif
153