30 WorldBase::WorldBase()
38 sensorsAreEnabled =
false;
39 numRegisteredLinkPairs = 0;
43 WorldBase::~WorldBase()
49 int WorldBase::bodyIndex(
const std::string&
name)
51 NameToIndexMap::iterator p = nameToBodyIndexMap.find(
name);
52 return (p != nameToBodyIndexMap.end()) ? p->second : -1;
58 if(index < 0 || (
int)bodyInfoArray.size() <= index)
61 return bodyInfoArray[index].body;
67 int idx = bodyIndex(
name);
68 if(idx < 0 || (
int)bodyInfoArray.size() <= idx)
71 return bodyInfoArray[idx].body;
75 void WorldBase::setTimeStep(
double ts)
81 void WorldBase::setCurrentTime(
double time)
87 void WorldBase::setGravityAcceleration(
const Vector3& g)
93 void WorldBase::enableSensors(
bool on)
95 sensorsAreEnabled = on;
99 void WorldBase::initialize()
101 const int n = bodyInfoArray.size();
103 for(
int i=0;
i <
n; ++
i){
108 bool hasHighGainModeJoints =
false;
109 int nL = body->numLinks();
110 for(
int j=0; j < nL; ++j){
111 if(body->link(j)->isHighGainMode){
112 hasHighGainModeJoints =
true;
117 if(hasHighGainModeJoints){
123 info.forwardDynamics->setEulerMethod();
125 info.forwardDynamics->setRungeKuttaMethod();
127 info.forwardDynamics->setGravityAcceleration(g);
128 info.forwardDynamics->setTimeStep(timeStep_);
129 info.forwardDynamics->enableSensors(sensorsAreEnabled);
130 info.forwardDynamics->initialize();
135 void WorldBase::calcNextState()
138 cout <<
"World current time = " << currentTime_ << endl;
140 const int n = bodyInfoArray.size();
143 #pragma omp parallel for num_threads(3) schedule(dynamic)
144 for(
int i=0;
i <
n; ++
i){
146 info.forwardDynamics->calcNextState();
148 if (sensorsAreEnabled) updateRangeSensors();
149 currentTime_ += timeStep_;
155 if(!body->name().empty()){
156 nameToBodyIndexMap[body->name()] = bodyInfoArray.size();
160 bodyInfoArray.push_back(
info);
162 return bodyInfoArray.size() - 1;
166 void WorldBase::clearBodies()
168 nameToBodyIndexMap.clear();
169 bodyInfoArray.clear();
173 void WorldBase::clearCollisionPairs()
175 linkPairKeyToIndexMap.clear();
176 numRegisteredLinkPairs = 0;
180 void WorldBase::setEulerMethod()
182 isEulerMethod =
true;
186 void WorldBase::setRungeKuttaMethod()
188 isEulerMethod =
false;
192 std::pair<int,bool> WorldBase::getIndexOfLinkPairs(
Link* link1,
Link* link2)
195 int isRegistered =
false;
201 linkPair.
link1 = link1;
202 linkPair.
link2 = link2;
204 linkPair.
link1 = link2;
205 linkPair.
link2 = link1;
208 LinkPairKeyToIndexMap::iterator p = linkPairKeyToIndexMap.find(linkPair);
210 if(p != linkPairKeyToIndexMap.end()){
214 index = numRegisteredLinkPairs++;
215 linkPairKeyToIndexMap[linkPair] = index;
219 return std::make_pair(index, isRegistered);
223 bool WorldBase::LinkPairKey::operator<(
const LinkPairKey& pair2)
const
225 if(link1 < pair2.
link1){
227 }
else if(link1 == pair2.
link1){
228 return (link2 < pair2.
link2);
234 void WorldBase::updateRangeSensors()
236 for (
unsigned int bodyIndex = 0; bodyIndex<numBodies(); bodyIndex++){
237 BodyPtr bodyptr = body(bodyIndex);
238 int n = bodyptr->numSensors(Sensor::RANGE);
239 for(
int i=0;
i <
n; ++
i){
242 updateRangeSensor(s);
258 for (
int i = -scan_half;
i<= scan_half;
i++){
260 v[0] = -sin(th); v[2] = -cos(th);
263 for (
unsigned int bodyIndex=0; bodyIndex<numBodies(); bodyIndex++){
264 BodyPtr bodyptr = body(bodyIndex);
265 for (
unsigned int linkIndex=0; linkIndex<bodyptr->numLinks(); linkIndex++){
266 Link *link = bodyptr->link(linkIndex);
268 D = link->
coldetModel->computeDistanceWithRay(p.data(), dir.data());
269 if ((minD==0&&D>0)||(minD>0&&D>0&&minD>D)) minD = D;