00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <string.h>
00013 #include <OPENR/OSyslog.h>
00014 #include "MTN.h"
00015
00016 MTN::MTN() : mtnfile(0), currentKeyFrame(0), currentFrame(0)
00017 {
00018 }
00019
00020 void
00021 MTN::Set(MTNFile* file)
00022 {
00023 mtnfile = file;
00024 First();
00025 }
00026
00027 char*
00028 MTN::GetName()
00029 {
00030 static char name[128];
00031
00032 char* ptr = mtnfile->GetName();
00033 if (ptr == 0) {
00034 name[0] = '\0';
00035 } else {
00036 strcpy(name, ptr);
00037 }
00038
00039 return name;
00040 }
00041
00042 char*
00043 MTN::GetRobotDesign()
00044 {
00045 static char robotDesign[32];
00046
00047 char* ptr = mtnfile->GetRobotDesign();
00048 if (ptr == 0) {
00049 robotDesign[0] = '\0';
00050 } else {
00051 strcpy(robotDesign, ptr);
00052 }
00053
00054 return robotDesign;
00055 }
00056
00057 void
00058 MTN::First()
00059 {
00060 currentKeyFrame = 0;
00061 currentFrame = 0;
00062 }
00063
00064 bool
00065 MTN::More()
00066 {
00067 if (mtnfile == 0) return false;
00068 return (currentKeyFrame >= mtnfile->GetNumKeyFrames() - 1) ? false : true;
00069 }
00070
00071 void
00072 MTN::Next(int numFrames)
00073 {
00074 if (mtnfile == 0) return;
00075
00076 currentFrame += numFrames;
00077 int interpolate = mtnfile->GetNumInterpolate8ms(currentKeyFrame);
00078
00079 while (currentFrame > interpolate) {
00080 currentFrame = currentFrame - interpolate - 1;
00081 currentKeyFrame++;
00082 interpolate = mtnfile->GetNumInterpolate8ms(currentKeyFrame);
00083 if (interpolate == -1) {
00084 currentFrame = 0;
00085 break;
00086 }
00087 }
00088
00089 OSYSDEBUG(("MTN::Next() %d %d %d\n",
00090 numFrames, currentKeyFrame, currentFrame));
00091 }
00092
00093 int
00094 MTN::InterpolateCommandVectorData(OCommandVectorData* commandVec,
00095 int maxNumFrames)
00096 {
00097 OSYSDEBUG(("MTN::InterpolateCommandVectorData() %d %d %d\n",
00098 currentKeyFrame, currentFrame,
00099 mtnfile->GetNumInterpolate8ms(currentKeyFrame)));
00100
00101 if (mtnfile == 0) return -1;
00102 if (More() != true) return -1;
00103
00104
00105
00106
00107
00108
00109
00110 int numFrames = 0;
00111 for (int i = 0; i < mtnfile->GetNumJoints(); i++) {
00112 OCommandInfo* info = commandVec->GetInfo(i);
00113 OCommandData* data = commandVec->GetData(i);
00114 numFrames = InterpolateCommandData(i, data, 0,
00115 currentKeyFrame,
00116 currentFrame, maxNumFrames);
00117 info->Set(odataJOINT_COMMAND2, info->primitiveID, numFrames);
00118 }
00119
00120 return numFrames;
00121 }
00122
00123 int
00124 MTN::InterpolateCommandData(int jointIndex,
00125 OCommandData* data, int valueIndex,
00126 int keyFrame, int frame, int maxNumFrames)
00127 {
00128 OSYSDEBUG(("MTN::InterpolateCommandData() %d %d %d %d %d\n",
00129 jointIndex, valueIndex, keyFrame, frame, maxNumFrames));
00130
00131 if (keyFrame >= mtnfile->GetNumKeyFrames() - 1) return 0;
00132
00133 MTNKeyFrame* keyFrame0 = mtnfile->GetKeyFrame(keyFrame);
00134 MTNKeyFrame* keyFrame1 = mtnfile->GetKeyFrame(keyFrame + 1);
00135 slongword value0 = keyFrame0->data[jointIndex];
00136 slongword value1 = keyFrame1->data[jointIndex];
00137 slongword numInterpolate = mtnfile->GetNumInterpolate8ms(keyFrame);
00138 slongword divide = numInterpolate + 1;
00139 slongword delta = (value1 - value0) / divide;
00140
00141 int numFrames = 0;
00142 OJointCommandValue2* jvalue = (OJointCommandValue2*)data->value;
00143 while (valueIndex < maxNumFrames) {
00144
00145 jvalue[valueIndex].value = value0 + frame * delta;
00146 valueIndex++;
00147 frame++;
00148 numFrames++;
00149
00150 if (frame > numInterpolate) {
00151 keyFrame++;
00152 int n = InterpolateCommandData(jointIndex,
00153 data, valueIndex,
00154 keyFrame, 0, maxNumFrames);
00155 return numFrames + n;
00156 }
00157 }
00158
00159 return numFrames;
00160 }