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

#include "KeyFrameMotionFactory.h"

#include <Tools/Math/Common.h>
#include <PlatformInterface/Platform.h>

#include "Tools/MotionNetParser.h"

#include <cstdio>
#include <fstream>
#include <iostream>

using namespace naoth;
using namespace std;

KeyFrameMotionFactory::KeyFrameMotionFactory()
{
  /*REGISTER_DEBUG_COMMAND("reload:motion_net", 
    "reloads a particular motion net. Usage: reload:motion_net file=turn_right", this);
  REGISTER_DEBUG_COMMAND("motion:load_editor_motionnet", 
      "load the temporary editor's motion net.", this);*/
  
  // load motion nets in different folders
  const std::string& dirlocation = Platform::getInstance().theConfigDirectory;
  //const std::string& scheme = Platform::getInstance().theScheme;
  const std::string& id = Platform::getInstance().theHardwareIdentity;
  const std::string& platform = Platform::getInstance().thePlatform;
  const std::string motionnet = "motionnet/";
  loadAvailableMotionNets(dirlocation + "general/" + motionnet);
  loadAvailableMotionNets(dirlocation + "platform/" + platform + "/" + motionnet);
  loadAvailableMotionNets(dirlocation + "robots/" + id + "/" + motionnet);
  loadAvailableMotionNets(dirlocation + "private/" + motionnet);

  //
  keyFrameMotionCreator = registerModule<KeyFrameMotion>("KeyFrameMotion");
}

KeyFrameMotionFactory::~KeyFrameMotionFactory()
{
}

void KeyFrameMotionFactory::loadAvailableMotionNets(std::string dirlocation)
{
  if (!g_str_has_suffix(dirlocation.c_str(), "/")) {
    dirlocation = dirlocation + "/";
  }

  GDir* dir = g_dir_open(dirlocation.c_str(), 0, NULL);
  if (dir != NULL)
  {
    const gchar* name;
    while ((name = g_dir_read_name(dir)) != NULL)
    {
      if (g_str_has_suffix(name, ".mef"))
      {
        gchar* group = g_strndup(name, strlen(name) - strlen(".cfg"));
        
        std::string completeFileName = dirlocation + name;
        loadMotionNet(completeFileName, std::string(group));

        g_free(group);
      }
    }// end while

    g_dir_close(dir);
  }
}//end loadAvailableMotionNets


void KeyFrameMotionFactory::loadMotionNetFromFile(const std::string& fileName, MotionNet& motionNet) const
{
  ifstream inputFileStream ( fileName.c_str() , ifstream::in );

  if(inputFileStream.fail()) {
    throw std::string("Could not open MotionNet file.");
  }

  Scanner scanner(inputFileStream);
  MotionNetParser parser(scanner);

  motionNet = parser.parse();

  inputFileStream.close();

  if(motionNet.isEmpty()) {
    throw std::string("MotionNet file is empty.");
  }
}//end loadMotionNetFromFile


bool KeyFrameMotionFactory::loadMotionNet(const std::string& fileName, const std::string& motionNetName)
{
  try
  {
    MotionNet motionNet;

    cout << "KeyFrameMotionFactory: load " << fileName;
    loadMotionNetFromFile(fileName, motionNet);
    cout << "\tok" << endl;

    // loading successfull: copy to the map
    getMotionNets()[motionNetName] = motionNet;
    return true;
  }
  catch(std::string e)
  {
    // loading failed
    cout << "\tfail" << endl; 
    cout << e << endl;
    return false;
  }
}//end loadMotionNet


Module* KeyFrameMotionFactory::createMotion(const MotionRequest& motionRequest)
{
  keyFrameMotionCreator->setEnabled(false);

  // special case: use by the motion editor
  if(motionRequest.id == motion::play_editor_motionnet) {
    try
    {
      MotionNet motionNet;

      cout << "KeyFrameMotionFactory: load Config/play_editor_motionnet.mef";
      loadMotionNetFromFile("Config/play_editor_motionnet.mef", motionNet);
      cout << "\tok" << endl;

      keyFrameMotionCreator->setEnabled(true);
      KeyFrameMotion* motion = keyFrameMotionCreator->getModuleT();
      motion->set(motionNet, motionRequest.id);

      return keyFrameMotionCreator->getModule();
    }
    catch(std::string e)
    {
      // loading failed
      cout << "\tfail" << endl; 
      cout << e << endl;
      return NULL;
    }
  }


  std::string motionName = motion::getName(motionRequest.id);
  const MotionNet* net = getMotionNets().get(motionName);

  if(net != NULL)
  {
    keyFrameMotionCreator->setEnabled(true);
    KeyFrameMotion* motion = keyFrameMotionCreator->getModuleT();
    motion->set(*net, motionRequest.id);

    return keyFrameMotionCreator->getModule();
  }//end if

  return NULL;
}//end createMotion

/*
 * TODO
void KeyFrameMotionFactory::executeDebugCommand(
    const std::string& command, const std::map<std::string,std::string>& arguments,
    std::stringstream &outstream)
{  
  if(command == "reload:motion_net")
  { 
    if(arguments.find("file") != arguments.end())
    {
      string fileName = arguments.find("file")->second;

      outstream << "load " << fileName;

      string filePath = Platform::getInstance().theConfigPathInfo.motionnet + fileName + ".mef";
      if(loadMotionNet(filePath, fileName))
        outstream << " ok\n" << endl;
      else
        outstream << " fail\n" << endl;
    }
    else
      outstream << "Command fail: Usage: reload:motion_net file=turn_right" << endl;
  }
  else if(command == "motion:load_editor_motionnet")
  {
    if(arguments.find("condition") != arguments.end())
    {
      cout << command << endl;
    }
    string filePath = Platform::getInstance().theConfigPathInfo.motionnet + "../" + "play_editor_motionnet.mef";
    outstream << "load motionnet editor file: " << filePath;
    if(loadMotionNet(filePath, string("play_editor_motionnet")))
    {
        outstream << " ok\n" << endl;
    }
    else
        outstream << " fail\n" << endl;
  }//end if
}//end executeDebugCommand
*/