home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2003 March / VPR0303A.ISO / AIBO / MoNet / common / libMTN / MTN.cc < prev    next >
C/C++ Source or Header  |  2002-12-19  |  4KB  |  155 lines

  1. //
  2. // Copyright 2002 Sony Corporation 
  3. //
  4. // Permission to use, copy, modify, and redistribute this software for
  5. // non-commercial use is hereby granted.
  6. //
  7. // This software is provided "as is" without warranty of any kind,
  8. // either expressed or implied, including but not limited to the
  9. // implied warranties of fitness for a particular purpose.
  10. //
  11.  
  12. #include <string.h>
  13. #include <OPENR/OSyslog.h>
  14. #include "MTN.h"
  15.  
  16. MTN::MTN() : mtnfile(0), currentKeyFrame(0), currentFrame(0)
  17. {
  18. }
  19.  
  20. void
  21. MTN::Set(MTNFile* file)
  22. {
  23.     mtnfile = file;
  24.     First();
  25. }
  26.  
  27. char*
  28. MTN::GetName()
  29. {
  30.     static char name[128]; // magic number !! Improve later
  31.  
  32.     char* ptr = mtnfile->GetName();
  33.     if (ptr == 0) {
  34.         name[0] = '\0';
  35.     } else {
  36.         strcpy(name, ptr);
  37.     }
  38.  
  39.     return name;
  40. }
  41.  
  42. char*
  43. MTN::GetRobotDesign()
  44. {
  45.     static char robotDesign[32]; // magic number !! Improve later
  46.  
  47.     char* ptr = mtnfile->GetRobotDesign();
  48.     if (ptr == 0) {
  49.         robotDesign[0] = '\0';
  50.     } else {
  51.         strcpy(robotDesign, ptr);
  52.     }
  53.  
  54.     return robotDesign;
  55. }
  56.  
  57. void
  58. MTN::First()
  59. {
  60.     currentKeyFrame = 0;
  61.     currentFrame    = 0;
  62. }
  63.  
  64. bool
  65. MTN::More()
  66. {
  67.     if (mtnfile == 0) return false;
  68.     return (currentKeyFrame >= mtnfile->GetNumKeyFrames() - 1) ? false : true;
  69. }
  70.  
  71. void
  72. MTN::Next(int numFrames)
  73. {
  74.     if (mtnfile == 0) return; // do nothing
  75.  
  76.     currentFrame += numFrames;
  77.     int interpolate = mtnfile->GetNumInterpolate8ms(currentKeyFrame);
  78.  
  79.     while (currentFrame > interpolate) {
  80.         currentFrame = currentFrame - interpolate - 1;
  81.         currentKeyFrame++;
  82.         interpolate = mtnfile->GetNumInterpolate8ms(currentKeyFrame);
  83.         if (interpolate == -1) {
  84.             currentFrame = 0;
  85.             break;
  86.         }
  87.     }
  88.     
  89.     OSYSDEBUG(("MTN::Next() %d %d %d\n",
  90.                numFrames, currentKeyFrame, currentFrame));
  91. }
  92.  
  93. int
  94. MTN::InterpolateCommandVectorData(OCommandVectorData* commandVec,
  95.                                   int maxNumFrames)
  96. {
  97.     OSYSDEBUG(("MTN::InterpolateCommandVectorData() %d %d %d\n",
  98.                currentKeyFrame, currentFrame, 
  99.                mtnfile->GetNumInterpolate8ms(currentKeyFrame)));
  100.  
  101.     if (mtnfile == 0) return -1;
  102.     if (More() != true) return -1;
  103.  
  104.     int numFrames = 0;
  105.     for (int i = 0; i < mtnfile->GetNumJoints(); i++) {
  106.         OCommandInfo* info = commandVec->GetInfo(i);
  107.         OCommandData* data = commandVec->GetData(i);
  108.         numFrames = InterpolateCommandData(i, data, 0,
  109.                                            currentKeyFrame,
  110.                                            currentFrame, maxNumFrames);
  111.         info->Set(odataJOINT_COMMAND2, info->primitiveID, numFrames);
  112.     }
  113.  
  114.     return numFrames;
  115. }
  116.  
  117. int
  118. MTN::InterpolateCommandData(int jointIndex,
  119.                             OCommandData* data, int valueIndex,
  120.                             int keyFrame, int frame, int maxNumFrames)
  121. {
  122.     OSYSDEBUG(("MTN::InterpolateCommandData() %d %d %d %d %d\n",
  123.                jointIndex, valueIndex, keyFrame, frame, maxNumFrames));
  124.  
  125.     if (keyFrame >= mtnfile->GetNumKeyFrames() - 1) return 0;
  126.  
  127.     MTNKeyFrame* keyFrame0   = mtnfile->GetKeyFrame(keyFrame);
  128.     MTNKeyFrame* keyFrame1   = mtnfile->GetKeyFrame(keyFrame + 1);
  129.     slongword value0         = keyFrame0->data[jointIndex];
  130.     slongword value1         = keyFrame1->data[jointIndex];
  131.     slongword numInterpolate = mtnfile->GetNumInterpolate8ms(keyFrame);
  132.     slongword divide         = numInterpolate + 1;
  133.     slongword delta          = (value1 - value0) / divide;
  134.     
  135.     int numFrames = 0;
  136.     OJointCommandValue2* jvalue = (OJointCommandValue2*)data->value;
  137.     while (valueIndex < maxNumFrames) {
  138.  
  139.         jvalue[valueIndex].value = value0 + frame * delta;
  140.         valueIndex++;
  141.         frame++;
  142.         numFrames++; 
  143.  
  144.         if (frame > numInterpolate) {
  145.             keyFrame++;
  146.             int n = InterpolateCommandData(jointIndex,
  147.                                            data, valueIndex,
  148.                                            keyFrame, 0, maxNumFrames);
  149.             return numFrames + n;
  150.         }
  151.     }
  152.  
  153.     return numFrames;
  154. }
  155.