Declare loop variables inside for loop in C++ files.

PiperOrigin-RevId: 518852339
Change-Id: I0174d873d89e48f11b816d337728f213c2798248
This commit is contained in:
Nimrod Gileadi
2023-03-23 07:21:46 -07:00
committed by Copybara-Service
parent 145fe7f354
commit 0e7e299bf6
11 changed files with 375 additions and 447 deletions
+18 -20
View File
@@ -730,8 +730,6 @@ void mjXReader::PrintSchema(std::stringstream& str, bool html, bool pad) {
// main entry point for XML parser
// mjCModel is allocated here; caller is responsible for deallocation
void mjXReader::Parse(XMLElement* root) {
XMLElement *section;
// check schema
if (!schema.GetError().empty()) {
throw mjXError(0, "XML Schema Construction Error: %s\n",
@@ -757,86 +755,86 @@ void mjXReader::Parse(XMLElement* root) {
//------------------- parse MuJoCo sections embedded in all XML formats
for (section = root->FirstChildElement("compiler"); section;
for (XMLElement* section = root->FirstChildElement("compiler"); section;
section = section->NextSiblingElement("compiler")) {
Compiler(section, model);
}
for (section = root->FirstChildElement("option"); section;
for (XMLElement* section = root->FirstChildElement("option"); section;
section = section->NextSiblingElement("option")) {
Option(section, &model->option);
}
for (section = root->FirstChildElement("size"); section;
for (XMLElement* section = root->FirstChildElement("size"); section;
section = section->NextSiblingElement("size")) {
Size(section, model);
}
//------------------ parse MJCF-specific sections
for (section = root->FirstChildElement("visual"); section;
for (XMLElement* section = root->FirstChildElement("visual"); section;
section = section->NextSiblingElement("visual")) {
Visual(section);
}
for (section = root->FirstChildElement("statistic"); section;
for (XMLElement* section = root->FirstChildElement("statistic"); section;
section = section->NextSiblingElement("statistic")) {
Statistic(section);
}
readingdefaults = true;
for (section = root->FirstChildElement("default"); section;
for (XMLElement* section = root->FirstChildElement("default"); section;
section = section->NextSiblingElement("default")) {
Default(section, -1);
}
readingdefaults = false;
for (section = root->FirstChildElement("extension"); section;
for (XMLElement* section = root->FirstChildElement("extension"); section;
section = section->NextSiblingElement("extension")) {
Extension(section);
}
for (section = root->FirstChildElement("custom"); section;
for (XMLElement* section = root->FirstChildElement("custom"); section;
section = section->NextSiblingElement("custom")) {
Custom(section);
}
for (section = root->FirstChildElement("asset"); section;
for (XMLElement* section = root->FirstChildElement("asset"); section;
section = section->NextSiblingElement("asset")) {
Asset(section);
}
for (section = root->FirstChildElement("worldbody"); section;
for (XMLElement* section = root->FirstChildElement("worldbody"); section;
section = section->NextSiblingElement("worldbody")) {
Body(section, model->GetWorld());
}
for (section = root->FirstChildElement("contact"); section;
for (XMLElement* section = root->FirstChildElement("contact"); section;
section = section->NextSiblingElement("contact")) {
Contact(section);
}
for (section = root->FirstChildElement("equality"); section;
for (XMLElement* section = root->FirstChildElement("equality"); section;
section = section->NextSiblingElement("equality")) {
Equality(section);
}
for (section = root->FirstChildElement("tendon"); section;
for (XMLElement* section = root->FirstChildElement("tendon"); section;
section = section->NextSiblingElement("tendon")) {
Tendon(section);
}
for (section = root->FirstChildElement("actuator"); section;
for (XMLElement* section = root->FirstChildElement("actuator"); section;
section = section->NextSiblingElement("actuator")) {
Actuator(section);
}
for (section = root->FirstChildElement("sensor"); section;
for (XMLElement* section = root->FirstChildElement("sensor"); section;
section = section->NextSiblingElement("sensor")) {
Sensor(section);
}
for (section = root->FirstChildElement("keyframe"); section;
for (XMLElement* section = root->FirstChildElement("keyframe"); section;
section = section->NextSiblingElement("keyframe")) {
Keyframe(section);
}
@@ -1605,7 +1603,6 @@ void mjXReader::OneTendon(XMLElement* elem, mjCTendon* pten) {
// actuator element parser
void mjXReader::OneActuator(XMLElement* elem, mjCActuator* pact) {
int n;
string text, type;
double diameter;
@@ -1672,6 +1669,7 @@ void mjXReader::OneActuator(XMLElement* elem, mjCActuator* pact) {
// explicit attributes
if (type=="general") {
// explicit attributes
int n;
if (MapValue(elem, "dyntype", &n, dyn_map, dyn_sz)) {
pact->dyntype = (mjtDyn)n;
}
@@ -1811,7 +1809,7 @@ void mjXReader::OneActuator(XMLElement* elem, mjCActuator* pact) {
ReadAttr(elem, "fvmax", 1, pact->gainprm+8, text);
// biasprm = gainprm
for (n=0; n<9; n++) {
for (int n=0; n<9; n++) {
pact->biasprm[n] = pact->gainprm[n];
}
+17 -21
View File
@@ -1135,7 +1135,6 @@ void mjXWriter::Extension(XMLElement* root) {
// custom section
void mjXWriter::Custom(XMLElement* root) {
XMLElement* elem;
int i, j;
// get sizes, skip section if empty
int nnum = model->NumObjects(mjOBJ_NUMERIC);
@@ -1151,7 +1150,7 @@ void mjXWriter::Custom(XMLElement* root) {
XMLElement* section = InsertEnd(root, "custom");
// write all numerics
for (i=0; i<nnum; i++) {
for (int i=0; i<nnum; i++) {
mjCNumeric* ptr = (mjCNumeric*)model->GetObject(mjOBJ_NUMERIC, i);
elem = InsertEnd(section, "numeric");
WriteAttrTxt(elem, "name", ptr->name);
@@ -1160,7 +1159,7 @@ void mjXWriter::Custom(XMLElement* root) {
}
// write all texts
for (i=0; i<ntxt; i++) {
for (int i=0; i<ntxt; i++) {
mjCText* ptr = (mjCText*)model->GetObject(mjOBJ_TEXT, i);
elem = InsertEnd(section, "text");
WriteAttrTxt(elem, "name", ptr->name);
@@ -1168,13 +1167,13 @@ void mjXWriter::Custom(XMLElement* root) {
}
// write all tuples
for (i=0; i<ntup; i++) {
for (int i=0; i<ntup; i++) {
mjCTuple* ptr = (mjCTuple*)model->GetObject(mjOBJ_TUPLE, i);
elem = InsertEnd(section, "tuple");
WriteAttrTxt(elem, "name", ptr->name);
// write objects in tuple
for (j=0; j<(int)ptr->objtype.size(); j++) {
for (int j=0; j<(int)ptr->objtype.size(); j++) {
XMLElement* obj = InsertEnd(elem, "element");
WriteAttrTxt(obj, "objtype", mju_type2Str((int)ptr->objtype[j]));
WriteAttrTxt(obj, "objname", ptr->objname[j].c_str());
@@ -1191,7 +1190,6 @@ void mjXWriter::Custom(XMLElement* root) {
// asset section
void mjXWriter::Asset(XMLElement* root) {
XMLElement* elem;
int i;
// get sizes
int ntex = model->NumObjects(mjOBJ_TEXTURE);
@@ -1210,7 +1208,7 @@ void mjXWriter::Asset(XMLElement* root) {
// write textures
mjCTexture deftex(0);
for (i=0; i<ntex; i++) {
for (int i=0; i<ntex; i++) {
// create element
mjCTexture* ptex = (mjCTexture*)model->GetObject(mjOBJ_TEXTURE, i);
elem = InsertEnd(section, "texture");
@@ -1260,7 +1258,7 @@ void mjXWriter::Asset(XMLElement* root) {
}
// write materials
for (i=0; i<nmat; i++) {
for (int i=0; i<nmat; i++) {
// create element and write
mjCMaterial* pmat = (mjCMaterial*)model->GetObject(mjOBJ_MATERIAL, i);
elem = InsertEnd(section, "material");
@@ -1268,7 +1266,7 @@ void mjXWriter::Asset(XMLElement* root) {
}
// write meshes
for (i=0; i<nmesh; i++) {
for (int i=0; i<nmesh; i++) {
// create element and write
mjCMesh* pmesh = (mjCMesh*)model->GetObject(mjOBJ_MESH, i);
elem = InsertEnd(section, "mesh");
@@ -1276,7 +1274,7 @@ void mjXWriter::Asset(XMLElement* root) {
}
// write skins
for (i=0; i<nskin; i++) {
for (int i=0; i<nskin; i++) {
// create element and write
mjCSkin* pskin = (mjCSkin*)model->GetObject(mjOBJ_SKIN, i);
elem = InsertEnd(section, "skin");
@@ -1284,7 +1282,7 @@ void mjXWriter::Asset(XMLElement* root) {
}
// write hfields
for (i=0; i<nhfield; i++) {
for (int i=0; i<nhfield; i++) {
// create element
mjCHField* phf = (mjCHField*)model->GetObject(mjOBJ_HFIELD, i);
elem = InsertEnd(section, "hfield");
@@ -1306,7 +1304,6 @@ void mjXWriter::Asset(XMLElement* root) {
// recursive body writer
void mjXWriter::Body(XMLElement* elem, mjCBody* body) {
double unitq[4] = {1, 0, 0, 0};
unsigned int i;
if (!body) {
throw mjXError(0, "missing body in XML write"); // SHOULD NOT OCCUR
@@ -1340,27 +1337,27 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body) {
}
// write joints
for (i=0; i<body->joints.size(); i++) {
for (int i=0; i<body->joints.size(); i++) {
OneJoint(InsertEnd(elem, "joint"), body->joints[i], body->joints[i]->def);
}
// write geoms
for (i=0; i<body->geoms.size(); i++) {
for (int i=0; i<body->geoms.size(); i++) {
OneGeom(InsertEnd(elem, "geom"), body->geoms[i], body->geoms[i]->def);
}
// write sites
for (i=0; i<body->sites.size(); i++) {
for (int i=0; i<body->sites.size(); i++) {
OneSite(InsertEnd(elem, "site"), body->sites[i], body->sites[i]->def);
}
// write cameras
for (i=0; i<body->cameras.size(); i++) {
for (int i=0; i<body->cameras.size(); i++) {
OneCamera(InsertEnd(elem, "camera"), body->cameras[i], body->cameras[i]->def);
}
// write lights
for (i=0; i<body->lights.size(); i++) {
for (int i=0; i<body->lights.size(); i++) {
OneLight(InsertEnd(elem, "light"), body->lights[i], body->lights[i]->def);
}
@@ -1387,7 +1384,7 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body) {
}
// write child bodies recursively
for (i=0; i<body->bodies.size(); i++) {
for (int i=0; i<body->bodies.size(); i++) {
Body(InsertEnd(elem, "body"), body->bodies[i]);
}
}
@@ -1397,7 +1394,6 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body) {
// collision section
void mjXWriter::Contact(XMLElement* root) {
XMLElement* elem;
int i;
// get number of pairs of each type
int npair = model->NumObjects(mjOBJ_PAIR);
@@ -1412,7 +1408,7 @@ void mjXWriter::Contact(XMLElement* root) {
XMLElement* section = InsertEnd(root, "contact");
// write all geom pairs
for (i=0; i<npair; i++) {
for (int i=0; i<npair; i++) {
// create element and write
mjCPair* ppair = (mjCPair*)model->GetObject(mjOBJ_PAIR, i);
elem = InsertEnd(section, "pair");
@@ -1420,7 +1416,7 @@ void mjXWriter::Contact(XMLElement* root) {
}
// write all exclude pairs
for (i=0; i<nexclude; i++) {
for (int i=0; i<nexclude; i++) {
// create element
mjCBodyPair* pexclude = (mjCBodyPair*)model->GetObject(mjOBJ_EXCLUDE, i);
elem = InsertEnd(section, "exclude");
+2 -2
View File
@@ -74,7 +74,7 @@ void mjXURDF::Clear(void) {
void mjXURDF::Parse(XMLElement* root) {
std::string name, text;
XMLElement *elem, *temp;
int id_parent, id_child, i;
int id_parent, id_child;
// set compiler defaults suitable for URDF
model->strippath = true;
@@ -158,7 +158,7 @@ void mjXURDF::Parse(XMLElement* root) {
}
// find all top-level bodies, call recursive tree constructor
for (i=0; i<(int)urName.size(); i++) {
for (int i=0; i<(int)urName.size(); i++) {
if (urParent[i] < 0) {
AddToTree(i);
}
+8 -13
View File
@@ -282,8 +282,6 @@ static int _max(int a, int b) {
// print schema as text
void mjXSchema::Print(std::stringstream& str, int level) {
int i;
// replace body with (world)body
string name1 = (name=="body" ? "(world)body" : name);
@@ -297,7 +295,7 @@ void mjXSchema::Print(std::stringstream& str, int level) {
// attributes
int cnt = _max(baselen, 30);
for (i=0; i<(int)attr.size(); i++) {
for (int i=0; i<(int)attr.size(); i++) {
if (cnt>60) {
str << "\n";
printspace(str, (cnt = _max(30, baselen)), " ");
@@ -310,7 +308,7 @@ void mjXSchema::Print(std::stringstream& str, int level) {
str << "\n";
// children
for (i=0; i<(int)child.size(); i++) {
for (int i=0; i<(int)child.size(); i++) {
child[i]->Print(str, level+1);
}
}
@@ -319,8 +317,6 @@ void mjXSchema::Print(std::stringstream& str, int level) {
// print schema as HTML table
void mjXSchema::PrintHTML(std::stringstream& str, int level, bool pad) {
int i;
// replace body with (world)body
string name1 = (name=="body" ? "(world)body" : name);
@@ -350,7 +346,7 @@ void mjXSchema::PrintHTML(std::stringstream& str, int level, bool pad) {
// attributes
str << "\t<td class=\"at\">";
if (!attr.empty()) {
for (i=0; i<(int)attr.size(); i++) {
for (int i=0; i<(int)attr.size(); i++) {
str << attr[i] << " ";
}
} else {
@@ -359,7 +355,7 @@ void mjXSchema::PrintHTML(std::stringstream& str, int level, bool pad) {
str << "</td>\n</tr>\n";
// children
for (i=0; i<(int)child.size(); i++) {
for (int i=0; i<(int)child.size(); i++) {
child[i]->PrintHTML(str, level+1, pad);
}
@@ -394,7 +390,6 @@ bool mjXSchema::NameMatch(XMLElement* elem, int level) {
// validator
XMLElement* mjXSchema::Check(XMLElement* elem, int level) {
int i;
bool missing;
char msg[100];
XMLElement *bad, *sub;
@@ -414,7 +409,7 @@ XMLElement* mjXSchema::Check(XMLElement* elem, int level) {
const XMLAttribute* attribute = elem->FirstAttribute();
while (attribute) {
missing = true;
for (i=0; i<(int)attr.size(); i++) {
for (int i=0; i<(int)attr.size(); i++) {
if (attr[i]==attribute->Name()) {
missing = false;
break;
@@ -445,7 +440,7 @@ XMLElement* mjXSchema::Check(XMLElement* elem, int level) {
}
// clear reference counts
for (i=0; i<(int)child.size(); i++) {
for (int i=0; i<(int)child.size(); i++) {
child[i]->refcnt = 0;
}
@@ -454,7 +449,7 @@ XMLElement* mjXSchema::Check(XMLElement* elem, int level) {
while (sub) {
// find in child array, update refcnt
missing = true;
for (i=0; i<(int)child.size(); i++) {
for (int i=0; i<(int)child.size(); i++) {
if (child[i]->NameMatch(sub, level+1)) {
// check sub-tree
if ((bad = child[i]->Check(sub, level+1))) {
@@ -481,7 +476,7 @@ XMLElement* mjXSchema::Check(XMLElement* elem, int level) {
// enforce sub-element types
msg[0] = 0;
for (i=0; i<(int)child.size(); i++) {
for (int i=0; i<(int)child.size(); i++) {
switch (child[i]->type) {
case '!':
if (child[i]->refcnt != 1)