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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)
#include "Wm5GraphicsPCH.h"
#include "Wm5IKController.h"
using namespace Wm5;
WM5_IMPLEMENT_RTTI(Wm5, Controller, IKController);
WM5_IMPLEMENT_STREAM(IKController);
WM5_IMPLEMENT_FACTORY(IKController);
//----------------------------------------------------------------------------
IKController::IKController (int numJoints, IKJointPtr* joints, int numGoals,
IKGoalPtr* goals)
:
Iterations(128),
OrderEndToRoot(true),
mNumJoints(numJoints),
mJoints(joints),
mNumGoals(numGoals),
mGoals(goals)
{
}
//----------------------------------------------------------------------------
IKController::~IKController ()
{
delete1(mJoints);
delete1(mGoals);
}
//----------------------------------------------------------------------------
bool IKController::Update (double applicationTime)
{
if (!Controller::Update(applicationTime))
{
return false;
}
// Make sure effectors are all current in world space. It is assumed
// that the joints form a chain, so the world transforms of joint I
// are the parent transforms for the joint I+1.
int k;
for (k = 0; k < mNumJoints; ++k)
{
mJoints[k]->UpdateWorldSRT();
}
// Update joints one-at-a-time to meet goals. As each joint is updated,
// the nodes occurring in the chain after that joint must be made current
// in world space.
int iter, i, j;
IKJoint* joint;
if (OrderEndToRoot)
{
for (iter = 0; iter < Iterations; ++iter)
{
for (k = 0; k < mNumJoints; ++k)
{
int r = mNumJoints - 1 - k;
joint = mJoints[r];
for (i = 0; i < 3; ++i)
{
if (joint->AllowTranslation[i])
{
if (joint->UpdateLocalT(i))
{
for (j = r; j < mNumJoints; ++j)
{
mJoints[j]->UpdateWorldRT();
}
}
}
}
for (i = 0; i < 3; ++i)
{
if (joint->AllowRotation[i])
{
if (joint->UpdateLocalR(i))
{
for (j = r; j < mNumJoints; ++j)
{
mJoints[j]->UpdateWorldRT();
}
}
}
}
}
}
}
else // order root to end
{
for (iter = 0; iter < Iterations; ++iter)
{
for (k = 0; k < mNumJoints; ++k)
{
joint = mJoints[k];
for (i = 0; i < 3; ++i)
{
if (joint->AllowTranslation[i])
{
if (joint->UpdateLocalT(i))
{
for (j = k; j < mNumJoints; ++j)
{
mJoints[j]->UpdateWorldRT();
}
}
}
}
for (i = 0; i < 3; ++i)
{
if (joint->AllowRotation[i])
{
if (joint->UpdateLocalR(i))
{
for (j = k; j < mNumJoints; ++j)
{
mJoints[j]->UpdateWorldRT();
}
}
}
}
}
}
}
return true;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Name support.
//----------------------------------------------------------------------------
Object* IKController::GetObjectByName (const std::string& name)
{
Object* found = Controller::GetObjectByName(name);
if (found)
{
return found;
}
int i;
for (i = 0; i < mNumJoints; ++i)
{
WM5_GET_OBJECT_BY_NAME(mJoints[i], name, found);
}
for (i = 0; i < mNumGoals; ++i)
{
WM5_GET_OBJECT_BY_NAME(mGoals[i], name, found);
}
return 0;
}
//----------------------------------------------------------------------------
void IKController::GetAllObjectsByName (const std::string& name,
std::vector<Object*>& objects)
{
Controller::GetAllObjectsByName(name,objects);
int i;
for (i = 0; i < mNumJoints; ++i)
{
WM5_GET_ALL_OBJECTS_BY_NAME(mJoints[i], name, objects);
}
for (i = 0; i < mNumGoals; ++i)
{
WM5_GET_ALL_OBJECTS_BY_NAME(mGoals[i], name, objects);
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Streaming support.
//----------------------------------------------------------------------------
IKController::IKController (LoadConstructor value)
:
Controller(value),
Iterations(0),
OrderEndToRoot(false),
mNumJoints(0),
mNumGoals(0),
mGoals(0)
{
}
//----------------------------------------------------------------------------
void IKController::Load (InStream& source)
{
WM5_BEGIN_DEBUG_STREAM_LOAD(source);
Controller::Load(source);
source.Read(Iterations);
source.ReadBool(OrderEndToRoot);
source.ReadPointerRR(mNumJoints, mJoints);
source.ReadPointerRR(mNumGoals, mGoals);
WM5_END_DEBUG_STREAM_LOAD(IKController, source);
}
//----------------------------------------------------------------------------
void IKController::Link (InStream& source)
{
Controller::Link(source);
source.ResolveLink(mNumJoints, mJoints);
source.ResolveLink(mNumGoals, mGoals);
}
//----------------------------------------------------------------------------
void IKController::PostLink ()
{
Controller::PostLink();
}
//----------------------------------------------------------------------------
bool IKController::Register (OutStream& target) const
{
if (Controller::Register(target))
{
target.Register(mNumJoints, mJoints);
target.Register(mNumGoals, mGoals);
return true;
}
return false;
}
//----------------------------------------------------------------------------
void IKController::Save (OutStream& target) const
{
WM5_BEGIN_DEBUG_STREAM_SAVE(target);
Controller::Save(target);
target.Write(Iterations);
target.WriteBool(OrderEndToRoot);
target.WritePointerW(mNumJoints, mJoints);
target.WritePointerW(mNumGoals, mGoals);
WM5_END_DEBUG_STREAM_SAVE(IKController, target);
}
//----------------------------------------------------------------------------
int IKController::GetStreamingSize () const
{
int size = Controller::GetStreamingSize();
size += sizeof(Iterations);
size += WM5_BOOLSIZE(OrderEndToRoot);
size += sizeof(mNumJoints);
size += mNumJoints*WM5_POINTERSIZE(mJoints[0]);
size += sizeof(mNumGoals);
size += mNumGoals*WM5_POINTERSIZE(mGoals[0]);
return size;
}
//----------------------------------------------------------------------------
|