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
/**
* @file MotionNetParser.h
*
* @author <a href="mailto:mellmann@informatik.hu-berlin.de">Heinrich Mellmann</a>
* Implementation of class MotionNetParser
*/

#include "MotionNetParser.h"

using namespace naoth;
using namespace std;

void MotionNetParser::eat()
{
  scanner.getNextToken();
}//end eat


bool MotionNetParser::isToken(KeyWordID tokenId)
{
  return scanner.isToken(getKeyWord(tokenId));
}//end isToken

void MotionNetParser::isTokenAndEat(KeyWordID tokenId)
{
  if(isToken(tokenId))
    eat();
  else
    throw_exception(
      string("Syntaxerror: found '")+string(scanner.buffer)+
      string("' but ")+ getKeyWord(tokenId)+string("' expected."));
}//end isTokenAndEat


MotionNet MotionNetParser::parse()
{
  // create new Motion Net
  MotionNet motionNet;

  eat();
  if(!scanner.isToken("nao"))
    throw_exception(string("not a 'nao' motion net."));

  eat(); // "nao"
  parseMotionNet(motionNet);
  //isTokenAndEat(EOF);

  return motionNet;
}//end parse


void MotionNetParser::parseMotionNet(MotionNet& motionNet)
{
  parseJointIndex(motionNet);

  while( isToken(keyFrame) )
  {
    parseKeyFrame(motionNet);
    //motionNet->keyFrameArray[motionNet->numberOfKeyFrames-1].print();
  }//end while

  while( isToken(transition) )
  {
    parseTransition(motionNet);
    //motionNet->transitionArray[motionNet->numberOfTransitions-1].print();
  }//end while

}//end parseMotionNet



void MotionNetParser::parseJointIndex(MotionNet& motionNet)
{
  while(!isToken(transition) && !isToken(keyFrame))
  {
    JointData::JointID id = JointData::jointIDFromName(std::string(scanner.buffer));
    eat(); // eat the joint name
    isTokenAndEat(separator); // ";"

    if(id == JointData::numOfJoint)
    {
      cout << JointData::getJointName(id) << ": " << std::string(scanner.buffer) << endl;
    }
    else
    {
      if(motionNet.getNumOfJoints() < JointData::numOfJoint)
      {
        motionNet.add(id);
      }
      else
      {
        throw_exception(string("To many joint indices."));
      }
    }
  }//end while
}//end parseJointIndex


void MotionNetParser::parseTransition(MotionNet& motionNet)
{
  MotionNet::Transition result;

  // transition;id_from;id_to;duration;condition

  isTokenAndEat(transition); // keyword "transition"
  isTokenAndEat(separator); // ";"
  result.fromKeyFrame = parseInt(); // id from
  isTokenAndEat(separator); // ";"
  result.toKeyFrame = parseInt(); // id to
  isTokenAndEat(separator); // ";"
  result.duration = parseFloat(); // duration

  isTokenAndEat(separator); // ";"

  // get the condition and eat
  result.condition = std::string(scanner.buffer);
  eat();

  motionNet.add(result);
}//end parseTransition


void MotionNetParser::parseKeyFrame(MotionNet& motionNet)
{
  MotionNet::KeyFrame result;
  
  isTokenAndEat(keyFrame); // keyword "keyframe"
  isTokenAndEat(separator); // ";"
  result.id = parseInt(); // id

  // skip the coordinates
  isTokenAndEat(separator); // ";"
  parseInt(); // x
  isTokenAndEat(separator); // ";"
  parseInt(); // y

  // parse joint values
  for(int i = 0; i < motionNet.getNumOfJoints(); i++)
  {
    isTokenAndEat(separator); // ";"
    result.jointData[i] = Math::fromDegrees(parseFloat());
  }//end for
  
  motionNet.add(result);
}//end parseKeyFrame


// pars a integer (something like "- 20" is not allowed, correct would be "-20")
int MotionNetParser::parseInt()
{
  int result = atoi(scanner.buffer);
  eat();

  return result;
}//end parseInt


// pars a float
float MotionNetParser::parseFloat()
{
  float result = (float)atof(scanner.buffer);
  eat();

  return result;
}//end parseFloat


std::string MotionNetParser::getKeyWord(KeyWordID keyWordID)
{
  switch(keyWordID)
  {
    case endOfLine: return std::string("\n");
    case separator: return std::string(";");
    case keyFrame: return std::string("keyframe");
    case transition: return std::string("transition");
    case comment: return std::string("#");
    default: return std::string("unknown");
  }//end switch
}//end getKeyWord

void MotionNetParser::throw_exception(std::string message)
{
  std::stringstream out;
  out << "[MotionNetParser](" << scanner.getLineNumber() << ") ERROR : " << message;
  throw out.str();
}//end throw_exception