std::string settingToString(Setting& setting, uint32_t maxElements) { Setting::Type t = setting.getType(); switch (t) { case Setting::Type::TypeInt: { return stringFormat("%i", (int)setting); } case Setting::Type::TypeInt64: { return stringFormat("%ll", (long long)setting); } case Setting::Type::TypeString: { return stringFormat("\"%s\"", (const char*)setting); } case Setting::Type::TypeBoolean: { return stringFormat("%i", (bool)setting); } case Setting::Type::TypeArray: case Setting::Type::TypeList: { std::ostringstream value; value << ((t == Setting::Type::TypeArray) ? "[" : "("); if (setting.getLength() > 0) { int i = 0; while (true) { value << settingToString(setting[i++]); if (i == setting.getLength()) { break; } if (i == maxElements) { value << ",..."; break; } else { value << ","; } } } else { value << "empty"; } value << ((t == Setting::Type::TypeArray) ? "]" : ")"); return value.str(); } default: { return std::string("<unknown-type>"); } } }
int readShapes(Setting &shapeConfig, shapeMap &shapes, bodyMap &limbs) { //Shapes // name = "foot"; // body = "shin"; // position = { x = 0.0; y = -3.0; }; int nShapes = shapeConfig.getLength(); for (int i = 0; i<nShapes; ++i) { try { Setting &curShape = shapeConfig[i]; string name = curShape["name"], body = curShape["body"]; float x = curShape["position"]["x"], y = curShape["position"]["y"]; shapePos s; s.localPos.Set(x,y); s.b = limbs[body]; shapes.insert(make_pair(name, s)); } catch (...) { cerr<<"Creature::readShapes problem processing shape " <<i<<endl; return 0; } } return 1; }
int readJoints(Setting &jointConfig, jointMap &joints, bodyMap &limbs, World *w) { //Joints: // name = "knee"; // type, // XXX so far only revolute // obj1 // obj2 // position.{x,y} // lowerAngle // upperAngle int nJoints = jointConfig.getLength(); for (int i = 0; i<nJoints; ++i) { try { Setting &curJoint = jointConfig[i]; string name = curJoint["name"]; string type = curJoint["type"]; if (type == "revolute") { string obj1 = curJoint["obj1"]; string obj2 = curJoint["obj2"]; float x = curJoint["position"]["x"], y = curJoint["position"]["y"]; b2RevoluteJointDef jointDef; jointDef.Initialize(limbs[obj1].get(), limbs[obj2].get(), Vec2(x, y)); jointDef.lowerAngle = curJoint["lowerAngle"]; jointDef.upperAngle = curJoint["upperAngle"]; //XXX default to enableLimit true jointDef.enableLimit = true; RevoluteJointP joint = boost::dynamic_pointer_cast<RevoluteJoint,Joint> (w->createJoint(&jointDef)); joints.insert(make_pair(name, joint)); } else { cerr<<"Creature::readJoints joint "<<i<<":"<<name <<", unknown type "<<type<<endl; return 0; } } catch (SettingTypeException te) { cerr<<"Creature::readJoints problem processing joint "<<i<<endl; cerr<<" SettingTypeException: "<<((SettingException)te).what() <<", "<<te.getPath()<<endl; return 0; } catch (SettingNotFoundException te) { cerr<<"Creature::readJoints problem processing joint "<<i<<endl; cerr<<" SettingNotFoundException: " <<((SettingException)te).what()<<", "<<te.getPath()<<endl; return 0; } catch (...) { cerr<<"Creature::readJoints problem processing joint "<<i<<endl; return 0; } } return 1; }
void ConfigFactory::createTimeSeriesList(Setting& timeSeriesGroup) { int tsCount = timeSeriesGroup.getLength(); // loop through the time series and create them. for (int iSeries = 0; iSeries < tsCount; ++iSeries) { Setting& series = timeSeriesGroup[iSeries]; string seriesName = series["name"]; TimeSeries::sharedPointer theTimeSeries = createTimeSeriesOfType(series); if (theTimeSeries != NULL) { _timeSeriesList[seriesName] = theTimeSeries; } } // connect single sources (ModularTimeSeries subclasses) typedef std::map<string, string> stringMap_t; BOOST_FOREACH(const stringMap_t::value_type& stringPair, _timeSeriesSourceList) { string tsName = stringPair.first; string sourceName = stringPair.second; if (_timeSeriesList.find(tsName) == _timeSeriesList.end()) { std::cerr << "cannot locate Timeseries " << tsName << std::endl; continue; } if (_timeSeriesList.find(sourceName) == _timeSeriesList.end()) { std::cerr << "cannot locate specified source Timeseries " << sourceName << std::endl; std::cerr << "-- (specified by Timeseries " << tsName << ")" << std::endl; continue; } ModularTimeSeries::sharedPointer ts = boost::static_pointer_cast<ModularTimeSeries>(_timeSeriesList[tsName]); TimeSeries::sharedPointer source = _timeSeriesList[sourceName]; ts->setSource(source); }
int readSensors (Setting &sensorConfig, sensorList &sensors, bodyMap &limbs, jointMap &joints, shapeMap &shapes) { //Sensors: // type = {JointSensor, HeightSensor, BodyAngleSensor // target // for HeightSensor: minH, maxH // for BodyAngleSensor: minA, maxA int nSensors = sensorConfig.getLength(); for (int i = 0; i<nSensors; ++i) { try { Setting &curSensor = sensorConfig[i]; string type = curSensor["type"]; string target = curSensor["target"]; if (type == "JointSensor") { SensorP s(new JointSensor(joints[target])); sensors.push_back(s); } else if (type == "HeightSensor") { double minH = curSensor["minH"], maxH = curSensor["maxH"]; SensorP s(new HeightSensor(shapes[target], minH, maxH)); sensors.push_back(s); } else if (type == "BodyAngleSensor") { double minA = curSensor["minA"], maxA = curSensor["maxA"]; SensorP s(new BodyAngleSensor(limbs[target], minA, maxA)); sensors.push_back(s); } else { cerr<<"Creature::readSensors sensor "<<i <<", unknown type: "<<type<<endl; return 0; } } catch (SettingTypeException te) { cerr<<"Creature::readSensors problem processing sensor " <<i<<endl; cerr<<" SettingTypeException: "<<((SettingException)te).what() <<", "<<te.getPath()<<endl; return 0; } catch (SettingNotFoundException te) { cerr<<"Creature::readSensors problem processing sensor " <<i<<endl; cerr<<" SettingNotFoundException: " <<((SettingException)te).what()<<", "<<te.getPath()<<endl; return 0; } catch (...) { cerr<<"Creature::readSensors problem processing limb " <<i<<endl; return 0; } } return 1; }
// keeping the clock creation simple for now - e.g., no function pointers. void ConfigFactory::createClocks(Setting& clockGroup) { int clockCount = clockGroup.getLength(); for (int iClock = 0; iClock < clockCount; ++iClock) { Setting& clock = clockGroup[iClock]; string clockName = clock["name"]; int period = clock["period"]; Clock::sharedPointer aClock( new Clock(period) ); _clockList[clockName] = aClock; } return; }
void ServiceManager::setup(Setting& settings) { for(int i = 0; i < settings.getLength(); i++) { Setting& stSvc = settings[i]; String classname = stSvc.getName(); stSvc.lookupValue("class", classname); Service* svc = addService(classname); if(svc != NULL) { svc->doSetup(this, stSvc); } } }
int readMuscles (Setting &muscleConfig, muscleList &muscles, bodyMap &limbs) { //Muscles: // name = "hamstring"; // obj1 = "shin"; // pos1 = { x = -0.5; y = 1.5; }; // obj2 = "thigh"; // pos2 = { x = -0.5; y = 1.0; }; // minK = 3000.0; // maxK = 10000.0; // minEq = 1.5; // maxEq = 4.5; int nMuscles = muscleConfig.getLength(); for (int i = 0; i<nMuscles; ++i) { try { Setting &curMuscle = muscleConfig[i]; string name = curMuscle["name"]; string obj1 = curMuscle["obj1"], obj2 = curMuscle["obj2"]; float x1 = curMuscle["pos1"]["x"], y1 = curMuscle["pos1"]["y"], x2 = curMuscle["pos2"]["x"], y2 = curMuscle["pos2"]["y"]; MuscleP muscle(new Muscle(limbs[obj1], Vec2(x1, y1), limbs[obj2], Vec2(x2, y2), curMuscle["minK"], curMuscle["maxK"], curMuscle["minEq"], curMuscle["maxEq"], curMuscle["kd"])); muscles.push_back(muscle); } catch (SettingTypeException te) { cerr<<"Creature::readMuscles problem processing muscle "<<i<<endl; cerr<<" SettingTypeException: "<<((SettingException)te).what() <<", "<<te.getPath()<<endl; return 0; } catch (SettingNotFoundException te) { cerr<<"Creature::readMuscles problem processing muscle "<<i<<endl; cerr<<" SettingNotFoundException: " <<((SettingException)te).what()<<", "<<te.getPath()<<endl; return 0; } catch (...) { cerr<<"Creature::readMuscles problem processing muscle "<<i<<endl; return 0; } } return 1; }
void ConfigFactory::createPointRecords(Setting& records) { int recordCount = records.getLength(); // loop through the records and create them. for (int iRecord = 0; iRecord < recordCount; ++iRecord) { Setting& record = records[iRecord]; string recordName = record["name"]; PointRecord::sharedPointer pointRecord = createPointRecordOfType(record); if (pointRecord) { _pointRecordList[recordName] = pointRecord; } else { std::cerr << "could not load point record\n"; } } return; }
int readLimbs(Setting &limbConfig, bodyMap &limbs, bodyPosList &parts, World *w) { //Limbs specify: // name // position.{x,y} // angle // angularDamping // list of shapes: // each has type box or ball // for box: w, h // for ball: radius // density // optional position // optional friction int nLimbs = limbConfig.getLength(); for (int i = 0; i<nLimbs; ++i) { try { Setting &curLimb = limbConfig[i]; string name = curLimb["name"]; float x = curLimb["position"]["x"], y = curLimb["position"]["y"]; b2BodyDef bone; bone.type = b2_dynamicBody; bone.position.Set(x,y); curLimb.lookupValue("angle", bone.angle); curLimb.lookupValue("angularDamping", bone.angularDamping); BodyP limb(w->createBody(&bone)); BodyPos bp; bp.b = limb; bp.defaultPos.Set(x,y); bp.angle = bone.angle; Setting &shapes = curLimb["shapes"]; int nShapes = shapes.getLength(); for (int j = 0; j<nShapes; ++j) { Setting &curShape = shapes[j]; string type = curShape["type"]; b2FixtureDef fixtureDef; fixtureDef.density = curShape["density"]; curShape.lookupValue("friction", fixtureDef.friction); curShape.lookupValue("groupIndex", (int&)(fixtureDef.filter.groupIndex)); if (type == "box") { float w = curShape["w"], h = curShape["h"]; b2PolygonShape box; box.SetAsBox(w,h); fixtureDef.shape = &box; limb->CreateFixture(&fixtureDef); } else if (type == "ball") { b2CircleShape ball; ball.m_radius = curShape["radius"]; float x = curShape["position"]["x"], y = curShape["position"]["y"]; ball.m_p.Set(x,y); fixtureDef.shape = &ball; limb->CreateFixture(&fixtureDef); } else { cerr<<"Creature::initFromFile limb "<<name <<", shape "<<j<<" type "<<type<<" unknown."<<endl; return 0; } } parts.push_back(bp); limbs.insert(make_pair(name, limb)); } catch (SettingTypeException te) { cerr<<"Creature::readLimbs problem processing limb "<<i<<endl; cerr<<" SettingTypeException: "<<((SettingException)te).what() <<", "<<te.getPath()<<endl; return 0; } catch (SettingNotFoundException te) { cerr<<"Creature::readLimbs problem processing limb "<<i<<endl; cerr<<" SettingNotFoundException: " <<((SettingException)te).what()<<", "<<te.getPath()<<endl; return 0; } catch (...) { cerr<<"Creature::readLimbs problem processing limb "<<i<<endl; return 0; } } return 1; }
void _apply(Setting& root, Setting& source, LockList* lockList) { Setting* s = nullptr; if (!source.isRoot()) { const char* name = source.getName(); if (name != nullptr) { if (root.exists(name)) { s = &root[name]; if (_isLocked(*s, true, lockList)) { LogWarn("Ignored updating configuration '%s' to value " "'%s'. Setting locked to value '%s'.", s->getPath().c_str(), settingToString(source).c_str(), settingToString(*s).c_str()); return; } if (!s->isGroup()) { root.remove(s->getIndex()); s = &root.add(name, source.getType()); } } else { s = &root.add(name, source.getType()); if (_isLocked(*s, false, lockList)) { LogWarn("Ignored creating configuration '%s' with " "value '%s'. Setting locked.", s->getPath().c_str(), settingToString(*s).c_str()); root.remove(s->getIndex()); return; } } } else { // List or array elements s = &root.add(source.getType()); } } else { s = &root; } switch (source.getType()) { case Setting::Type::TypeInt: { *s = (int)source; break; } case Setting::Type::TypeInt64: { *s = (long long)source; break; } case Setting::Type::TypeString: { *s = (const char*)source; break; } case Setting::Type::TypeBoolean: { *s = (bool)source; break; } case Setting::Type::TypeArray: case Setting::Type::TypeList: case Setting::Type::TypeGroup: { for (int i = 0; i < source.getLength(); ++i) { _apply(*s, source[i], lockList); } break; } default: { assert(false); break; } } }