diff --git a/src/user/user_api.cc b/src/user/user_api.cc index dba3a965..5ed5caf6 100644 --- a/src/user/user_api.cc +++ b/src/user/user_api.cc @@ -108,7 +108,7 @@ mjModel* mj_compile(mjSpec* s, const mjVFS* vfs) { if (d) { modelC->MakeData(m, &d); modelC->RestoreState(state_name, m->qpos0, m->body_pos, m->body_quat, d->qpos, d->qvel, - d->act, d->ctrl, d->mocap_pos, d->mocap_quat); + d->act, d->ctrl, d->mocap_pos, d->mocap_quat); d->time = time; } } catch (mjCError& e) { @@ -166,7 +166,7 @@ mjsFrame* mjs_attachFrame(mjsBody* parent, const mjsFrame* child, // attach child body to a parent site mjsBody* mjs_attachToSite(mjsSite* parent, const mjsBody* child, - const char* prefix, const char* suffix) { + const char* prefix, const char* suffix) { if (!parent) { mju_error("parent site is null"); return nullptr; @@ -1178,8 +1178,8 @@ const double* mjs_getDouble(const mjDoubleVec* source, int* size) { // set plugin attributes void mjs_setPluginAttributes(mjsPlugin* plugin, void* attributes) { mjCPlugin* pluginC = static_cast(plugin->element); - std::map>* config_attribs = - reinterpret_cast>*>(attributes); + std::map >* config_attribs = + reinterpret_cast >*>(attributes); pluginC->config_attribs = std::move(*config_attribs); } diff --git a/src/user/user_cache.cc b/src/user/user_cache.cc index 35d99382..715e8e6a 100644 --- a/src/user/user_cache.cc +++ b/src/user/user_cache.cc @@ -172,8 +172,8 @@ std::size_t mjCCache::MaxSize() const { std::size_t mjCCache::Size() const { - std::lock_guard lock(mutex_); - return size_; + std::lock_guard lock(mutex_); + return size_; } @@ -183,7 +183,7 @@ void mjCCache::DeleteAsset(const std::string& id) { std::lock_guard lock(mutex_); auto it = lookup_.find(id); if (it != lookup_.end()) { - Delete(&(it->second)); + Delete(&(it->second)); } } @@ -209,7 +209,7 @@ void mjCCache::Delete(mjCAsset* asset, const std::string& skip) { for (auto& reference : asset->References()) { if (reference != skip) { models_[reference].erase(asset); - } + } } lookup_.erase(asset->Id()); } diff --git a/src/user/user_composite.cc b/src/user/user_composite.cc index af7bef69..0c09164b 100644 --- a/src/user/user_composite.cc +++ b/src/user/user_composite.cc @@ -28,6 +28,7 @@ #include #include +#include #include "cc/array_safety.h" #include "engine/engine_io.h" #include "engine/engine_util_blas.h" @@ -82,7 +83,7 @@ mjCComposite::mjCComposite(void) { skingroup = 0; // clear add flags - for (int i=0; igroup = 3; def[i].spec.site->group = 3; def[i].spec.tendon->group = 3; @@ -122,8 +123,8 @@ void mjCComposite::SetDefault(void) { AddDefaultJoint(); // set default geom and tendon group to 0 if needed to be visible - if (!skin || type==mjCOMPTYPE_CABLE) { - for (int i=0; igroup = 0; def[i].spec.tendon->group = 0; } @@ -137,20 +138,20 @@ bool mjCComposite::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) mjCModel* model = (mjCModel*)spec->element; // check counts - for (int i=0; i<3; i++) { - if (count[i]<1) { + for (int i=0; i < 3; i++) { + if (count[i] < 1) { return comperr(error, "Positive counts expected in composite", error_sz); } } // check cable sizes are nonzero if vertices are not prescribed - if (mjuu_dot3(size, size)1) { + if (count[0] > 1) { return comperr(error, "Either vertex or count can be specified, not both", error_sz); } count[0] = uservert.size()/3; @@ -159,8 +160,8 @@ bool mjCComposite::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) // determine dimensionality, check singleton order bool first = false; - for (int i=0; i<3; i++) { - if (count[i]==1) { + for (int i=0; i < 3; i++) { + if (count[i] == 1) { first = true; } else { dim++; @@ -180,8 +181,8 @@ bool mjCComposite::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) vertweight.clear(); // require 3x3 for subgrid - if (skin && skinsubgrid>0 && type!=mjCOMPTYPE_CABLE) { - if (count[0]<3 || count[1]<3) { + if (skin && skinsubgrid > 0 && type != mjCOMPTYPE_CABLE) { + if (count[0] < 3 || count[1] < 3) { return comperr(error, "At least 3x3 required for skin subgrid", error_sz); } } @@ -205,41 +206,41 @@ bool mjCComposite::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) // dispatch switch (type) { - case mjCOMPTYPE_PARTICLE: - return comperr(error, - "The \"particle\" composite type is deprecated. Please use " - "\"replicate\" instead.", - error_sz); + case mjCOMPTYPE_PARTICLE: + return comperr(error, + "The \"particle\" composite type is deprecated. Please use " + "\"replicate\" instead.", + error_sz); - case mjCOMPTYPE_GRID: - return comperr(error, - "The \"grid\" composite type is deprecated. Please use " - "\"flex\" instead.", - error_sz); + case mjCOMPTYPE_GRID: + return comperr(error, + "The \"grid\" composite type is deprecated. Please use " + "\"flex\" instead.", + error_sz); - case mjCOMPTYPE_ROPE: - return comperr(error, - "The \"rope\" composite type is deprecated. Please use " - "\"cable\" instead.", - error_sz); + case mjCOMPTYPE_ROPE: + return comperr(error, + "The \"rope\" composite type is deprecated. Please use " + "\"cable\" instead.", + error_sz); - case mjCOMPTYPE_LOOP: - return comperr(error, - "The \"loop\" composite type is deprecated. Please use " - "\"flexcomp\" instead.", - error_sz); + case mjCOMPTYPE_LOOP: + return comperr(error, + "The \"loop\" composite type is deprecated. Please use " + "\"flexcomp\" instead.", + error_sz); - case mjCOMPTYPE_CABLE: - return MakeCable(model, body, error, error_sz); + case mjCOMPTYPE_CABLE: + return MakeCable(model, body, error, error_sz); - case mjCOMPTYPE_CLOTH: - return comperr(error, - "The \"cloth\" composite type is deprecated. Please use " - "\"shell\" instead.", - error_sz); + case mjCOMPTYPE_CLOTH: + return comperr(error, + "The \"cloth\" composite type is deprecated. Please use " + "\"shell\" instead.", + error_sz); - default: - return comperr(error, "Unknown shape in composite", error_sz); + default: + return comperr(error, "Unknown shape in composite", error_sz); } } @@ -247,14 +248,14 @@ bool mjCComposite::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) bool mjCComposite::MakeCable(mjCModel* model, mjsBody* body, char* error, int error_sz) { // check dim - if (dim!=1) { + if (dim != 1) { return comperr(error, "Cable must be one-dimensional", error_sz); } // check geom type - if (def[0].spec.geom->type!=mjGEOM_CYLINDER && - def[0].spec.geom->type!=mjGEOM_CAPSULE && - def[0].spec.geom->type!=mjGEOM_BOX) { + if (def[0].spec.geom->type != mjGEOM_CYLINDER && + def[0].spec.geom->type != mjGEOM_CAPSULE && + def[0].spec.geom->type != mjGEOM_BOX) { return comperr(error, "Cable geom type must be sphere, capsule or box", error_sz); } @@ -265,25 +266,25 @@ bool mjCComposite::MakeCable(mjCModel* model, mjsBody* body, char* error, int er // populate uservert if not specified if (uservert.empty()) { - for (int ix=0; ixtype==mjGEOM_BOX) { - if (skinsubgrid>0) { + if (def[0].spec.geom->type == mjGEOM_BOX) { + if (skinsubgrid > 0) { count[1]+=2; MakeSkin2Subgrid(model, 2*def[0].spec.geom->size[2]); count[1]-=2; @@ -324,9 +325,9 @@ mjsBody* mjCComposite::AddCableBody(mjCModel* model, mjsBody* body, int ix, // set flags int lastidx = count[0]-2; - bool first = ix==0; - bool last = ix==lastidx; - bool secondlast = ix==lastidx-1; + bool first = ix == 0; + bool last = ix == lastidx; + bool secondlast = ix == lastidx-1; // compute edge and tangent vectors double edge[3], tprev[3], tnext[3], length_prev = 0; @@ -391,11 +392,11 @@ mjsBody* mjCComposite::AddCableBody(mjCModel* model, mjsBody* body, int ix, mjsGeom* geom = mjs_addGeom(body, &def[0].spec); mjs_setDefault(geom->element, mjs_getDefault(body->element)); mjs_setString(geom->name, txt_geom); - if (def[0].spec.geom->type==mjGEOM_CYLINDER || - def[0].spec.geom->type==mjGEOM_CAPSULE) { + if (def[0].spec.geom->type == mjGEOM_CYLINDER || + def[0].spec.geom->type == mjGEOM_CAPSULE) { mjuu_zerovec(geom->fromto, 6); geom->fromto[3] = length; - } else if (def[0].spec.geom->type==mjGEOM_BOX) { + } else if (def[0].spec.geom->type == mjGEOM_BOX) { mjuu_zerovec(geom->pos, 3); geom->pos[0] = length/2; geom->size[0] = length/2; @@ -417,10 +418,10 @@ mjsBody* mjCComposite::AddCableBody(mjCModel* model, mjsBody* body, int ix, if (!first || strcmp(initial.c_str(), "none")) { mjsJoint* jnt = mjs_addJoint(body, &defjoint[mjCOMPKIND_JOINT][0].spec); mjs_setDefault(jnt->element, mjs_getDefault(body->element)); - jnt->type = (first && strcmp(initial.c_str(), "free")==0) ? mjJNT_FREE : mjJNT_BALL; - jnt->damping = jnt->type==mjJNT_FREE ? 0 : jnt->damping; - jnt->armature = jnt->type==mjJNT_FREE ? 0 : jnt->armature; - jnt->frictionloss = jnt->type==mjJNT_FREE ? 0 : jnt->frictionloss; + jnt->type = (first && strcmp(initial.c_str(), "free") == 0) ? mjJNT_FREE : mjJNT_BALL; + jnt->damping = jnt->type == mjJNT_FREE ? 0 : jnt->damping; + jnt->armature = jnt->type == mjJNT_FREE ? 0 : jnt->armature; + jnt->frictionloss = jnt->type == mjJNT_FREE ? 0 : jnt->frictionloss; mjs_setString(jnt->name, this_joint); } @@ -453,10 +454,10 @@ void mjCComposite::CopyIntoSkin(mjsSkin* skin) { mjs_setFloat(skin->bindquat, bindquat.data(), bindquat.size()); mjs_setFloat(skin->texcoord, texcoord.data(), texcoord.size()); - for (int i=0; ivertid, vertid[i].data(), vertid[i].size()); } - for (int i=0; i< vertweight.size(); i++) { + for (int i=0; i < vertweight.size(); i++) { mjs_appendFloatVec(skin->vertweight, vertweight[i].data(), vertweight[i].size()); } @@ -486,9 +487,9 @@ void mjCComposite::MakeSkin2(mjCModel* model, mjtNum inflate) { skin->group = skingroup; // populate mesh: two sides - for (int i=0; i<2; i++) { - for (int ix=0; ix=count[0]-2) { + } else if (ix >= count[0]-2) { mju::sprintf_arr(this_body, "%sB_last", prefix.c_str()); } else { mju::sprintf_arr(this_body, "%sB_%d", prefix.c_str(), ix); } // bind pose - if (iy==0) { + if (iy == 0) { mjs_appendString(skin->bodyname, this_body); - bindpos.push_back((ix==count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); + bindpos.push_back((ix == count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); bindpos.push_back(-def[0].spec.geom->size[1]); bindpos.push_back(0); bindquat.push_back(1); bindquat.push_back(0); bindquat.push_back(0); bindquat.push_back(0); } else { mjs_appendString(skin->bodyname, this_body); - bindpos.push_back((ix==count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); + bindpos.push_back((ix == count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); bindpos.push_back(def[0].spec.geom->size[1]); bindpos.push_back(0); bindquat.push_back(1); bindquat.push_back(0); @@ -612,30 +613,30 @@ void mjCComposite::MakeCableBones(mjCModel* model, mjsSkin* skin) { void mjCComposite::MakeCableBonesSubgrid(mjCModel* model, mjsSkin* skin) { // populate bones - for (int ix=0; ix=count[0]-2) { + } else if (ix >= count[0]-2) { mju::sprintf_arr(txt, "%sB_last", prefix.c_str()); } else { mju::sprintf_arr(txt, "%sB_%d", prefix.c_str(), ix); } // bind pose - if (iy==0) { - bindpos.push_back((ix==count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); + if (iy == 0) { + bindpos.push_back((ix == count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); bindpos.push_back(-def[0].Geom().spec.size[1]); bindpos.push_back(0); - } else if (iy==2) { - bindpos.push_back((ix==count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); + } else if (iy == 2) { + bindpos.push_back((ix == count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); bindpos.push_back(def[0].Geom().spec.size[1]); bindpos.push_back(0); } else { - bindpos.push_back((ix==count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); + bindpos.push_back((ix == count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); bindpos.push_back(0); bindpos.push_back(0); } @@ -903,8 +904,8 @@ void mjCComposite::MakeSkin2Subgrid(mjCModel* model, mjtNum inflate) { // XY matrix const mjtNum step = 1.0/(1+skinsubgrid); int rxy = 0; - for (int sx=0; sx<=1+skinsubgrid; sx++) { - for (int sy=0; sy<=1+skinsubgrid; sy++) { + for (int sx=0; sx <= 1+skinsubgrid; sx++) { + for (int sy=0; sy <= 1+skinsubgrid; sy++) { // compute x, y mjtNum x = sx*step; mjtNum y = sy*step; @@ -939,15 +940,15 @@ void mjCComposite::MakeSkin2Subgrid(mjCModel* model, mjtNum inflate) { mju_mulMatMat(XY_W, XY, subW, N, 16, 16); // Weight matrices - for (int dx=0; dx<3; dx++) { - for (int dy=0; dy<3; dy++) { + for (int dx=0; dx < 3; dx++) { + for (int dy=0; dy < 3; dy++) { // make dense D mju_zero(D, 16*16); int cnt = 0; int r = 0, c; - while (r<16) { + while (r < 16) { // scan row - while ((c = mju_round(Dp[dx][dy][cnt]))!=-1) { + while ((c = mju_round(Dp[dx][dy][cnt])) != -1) { D[r*16+c] = Dp[dx][dy][cnt+1]; cnt +=2; } @@ -977,9 +978,9 @@ void mjCComposite::MakeSkin2Subgrid(mjCModel* model, mjtNum inflate) { int C0 = count[0] + (count[0]-1)*skinsubgrid; int C1 = count[1] + (count[1]-1)*skinsubgrid; int NN = C0*C1; - for (int i=0; i<2; i++) { - for (int ix=0; ix #include +#include #include #include "cc/array_safety.h" #include "engine/engine_crossplatform.h" @@ -133,12 +134,12 @@ bool mjCFlexcomp::Make(mjsBody* body, char* error, int error_sz) { } // check scale - if (scale[0]dim==3) { + else if (dflex->dim == 3) { for (int ix=pingridrange[i]; ix <= pingridrange[i+3]; ix++) { for (int iy=pingridrange[i+1]; iy <= pingridrange[i+4]; iy++) { for (int iz=pingridrange[i+2]; iz <= pingridrange[i+5]; iz++) { @@ -779,14 +780,14 @@ void mjCFlexcomp::BoxProject(double* pos, int ix, int iy, int iz) { }; // box - if (type==mjFCOMPTYPE_BOX) { + if (type == mjFCOMPTYPE_BOX) { pos[0] *= size[0]; pos[1] *= size[1]; pos[2] *= size[2]; } // cylinder - else if (type==mjFCOMPTYPE_CYLINDER) { + else if (type == mjFCOMPTYPE_CYLINDER) { double L0 = std::max(std::abs(pos[0]), std::abs(pos[1])); mjuu_normvec(pos, 2); pos[0] *= size[0]*L0; @@ -795,7 +796,7 @@ void mjCFlexcomp::BoxProject(double* pos, int ix, int iy, int iz) { } // ellipsoid - else if (type==mjFCOMPTYPE_ELLIPSOID) { + else if (type == mjFCOMPTYPE_ELLIPSOID) { mjuu_normvec(pos, 3); pos[0] *= size[0]; pos[1] *= size[1]; @@ -816,7 +817,7 @@ bool mjCFlexcomp::MakeSquare(char* error, int error_sz) { } // do projection - if (type==mjFCOMPTYPE_DISC) { + if (type == mjFCOMPTYPE_DISC) { double size[2] = { 0.5*spacing[0]*(count[0]-1), 0.5*spacing[1]*(count[1]-1), @@ -892,7 +893,7 @@ bool mjCFlexcomp::MakeBox(char* error, int error_sz) { for (int ix=0; ix < count[0]; ix++) { for (int iz=0; iz < count[2]; iz++) { // add point - if (iz>0 && iz < count[2]-1) { + if (iz > 0 && iz < count[2]-1) { BoxProject(pos, ix, iy, iz); point.push_back(pos[0]); point.push_back(pos[1]); @@ -1137,7 +1138,7 @@ bool mjCFlexcomp::MakeGMSH(mjCModel* model, char* error, int error_sz) { // load GMSH format 4.1 void mjCFlexcomp::LoadGMSH41(char* buffer, int binary, int nodeend, - int nodebegin, int elemend, int elembegin){ + int nodebegin, int elemend, int elembegin){ // header size constexpr int kGmsh41HeaderSize = 52; // base for node tags, to be subtracted from element data @@ -1555,7 +1556,7 @@ void mjCFlexcomp::LoadGMSH22(char* buffer, int binary, int nodeend, numNodeTags = 4; } - if (numNodeTags < 1 || numNodeTags >4) { + if (numNodeTags < 1 || numNodeTags > 4) { throw mjCError(NULL, "Invalid number of node tags"); } @@ -1564,7 +1565,7 @@ void mjCFlexcomp::LoadGMSH22(char* buffer, int binary, int nodeend, // read elements, discard all tags element.reserve(numNodeTags*numElements); - for (size_t i=0; i> tag >> elementType >> numTags; @@ -1643,7 +1644,7 @@ void mjCFlexcomp::LoadGMSH22(char* buffer, int binary, int nodeend, numNodeTags = 4; } - if (numNodeTags < 1 || numNodeTags >4) { + if (numNodeTags < 1 || numNodeTags > 4) { throw mjCError(NULL, "Invalid number of node tags"); } @@ -1680,7 +1681,7 @@ void mjCFlexcomp::LoadGMSH22(char* buffer, int binary, int nodeend, } // read first element - for (int k =0; k numNodes || nodeTag < 1) { throw mjCError(NULL, "Invalid node tag"); @@ -1719,7 +1720,7 @@ void mjCFlexcomp::LoadGMSH22(char* buffer, int binary, int nodeend, } // read every other element - for (int i=0; i cached_data(mesh, +[](const void* data) { + std::shared_ptr cached_data(mesh, +[] (const void* data) { const mjCMesh* mesh = static_cast(data); delete mesh; }); @@ -445,8 +445,8 @@ struct VertexKey { std::size_t operator()(const VertexKey& vertex) const { // combine all three hash values into a single hash value return ((std::hash()(vertex.v[0]) - ^ (std::hash()(vertex.v[1]) << 1)) >> 1) - ^ (std::hash()(vertex.v[2]) << 1); + ^ (std::hash()(vertex.v[1]) << 1)) >> 1) + ^ (std::hash()(vertex.v[2]) << 1); } }; @@ -541,7 +541,7 @@ bool mjCMesh::IsMSH(std::string_view filename, std::string_view ct) { return asset_type == "model/vnd.mujoco.msh"; } -bool mjCMesh::IsObj() const{ +bool mjCMesh::IsObj() const { return content_type_ == "model/obj"; } @@ -641,7 +641,7 @@ void mjCMesh::TryCompile(const mjVFS* vfs) { } else if (normal_.empty()) { normal_ = spec_normal_; } - if (!texcoord_.empty() && !spec_texcoord_.empty()) { + if (!texcoord_.empty() && !spec_texcoord_.empty()) { throw mjCError(this, "repeated texcoord specification"); } else if (texcoord_.empty()) { texcoord_ = spec_texcoord_; @@ -835,29 +835,29 @@ void mjCMesh::FitGeom(mjCGeom* geom, double* meshpos) { // get inertia box type (shell or volume) double* boxsz = GetInertiaBoxPtr(); switch (geom->type) { - case mjGEOM_SPHERE: - geom->size[0] = (boxsz[0] + boxsz[1] + boxsz[2])/3; - break; + case mjGEOM_SPHERE: + geom->size[0] = (boxsz[0] + boxsz[1] + boxsz[2])/3; + break; - case mjGEOM_CAPSULE: - geom->size[0] = (boxsz[0] + boxsz[1])/2; - geom->size[1] = max(0.0, boxsz[2] - geom->size[0]/2); - break; + case mjGEOM_CAPSULE: + geom->size[0] = (boxsz[0] + boxsz[1])/2; + geom->size[1] = max(0.0, boxsz[2] - geom->size[0]/2); + break; - case mjGEOM_CYLINDER: - geom->size[0] = (boxsz[0] + boxsz[1])/2; - geom->size[1] = boxsz[2]; - break; + case mjGEOM_CYLINDER: + geom->size[0] = (boxsz[0] + boxsz[1])/2; + geom->size[1] = boxsz[2]; + break; - case mjGEOM_ELLIPSOID: - case mjGEOM_BOX: - geom->size[0] = boxsz[0]; - geom->size[1] = boxsz[1]; - geom->size[2] = boxsz[2]; - break; + case mjGEOM_ELLIPSOID: + case mjGEOM_BOX: + geom->size[0] = boxsz[0]; + geom->size[1] = boxsz[1]; + geom->size[2] = boxsz[2]; + break; - default: - throw mjCError(this, "invalid geom type in fitting mesh %s", name.c_str()); + default: + throw mjCError(this, "invalid geom type in fitting mesh %s", name.c_str()); } } @@ -873,58 +873,58 @@ void mjCMesh::FitGeom(mjCGeom* geom, double* meshpos) { // compute depending on type switch (geom->type) { - case mjGEOM_SPHERE: - // find maximum distance - geom->size[0] = 0; - for (int i=0; i < nvert(); i++) { - double v[3] = {vert_[3*i], vert_[3*i+1], vert_[3*i+2]}; - double dst = mjuu_dist3(v, cen); - geom->size[0] = max(geom->size[0], dst); - } - break; + case mjGEOM_SPHERE: + // find maximum distance + geom->size[0] = 0; + for (int i=0; i < nvert(); i++) { + double v[3] = {vert_[3*i], vert_[3*i+1], vert_[3*i+2]}; + double dst = mjuu_dist3(v, cen); + geom->size[0] = max(geom->size[0], dst); + } + break; - case mjGEOM_CAPSULE: - case mjGEOM_CYLINDER: - // find maximum distance in XY, separately in Z - geom->size[0] = 0; - geom->size[1] = 0; - for (int i=0; i < nvert(); i++) { - double v[3] = {vert_[3*i], vert_[3*i+1], vert_[3*i+2]}; - double dst = sqrt((v[0]-cen[0])*(v[0]-cen[0]) + - (v[1]-cen[1])*(v[1]-cen[1])); - geom->size[0] = max(geom->size[0], dst); - - // proceed with z: valid for cylinder - double dst2 = abs(v[2]-cen[2]); - geom->size[1] = max(geom->size[1], dst2); - } - - // special handling of capsule: consider curved cap - if (geom->type==mjGEOM_CAPSULE) { + case mjGEOM_CAPSULE: + case mjGEOM_CYLINDER: + // find maximum distance in XY, separately in Z + geom->size[0] = 0; geom->size[1] = 0; for (int i=0; i < nvert(); i++) { - // get distance in XY and Z double v[3] = {vert_[3*i], vert_[3*i+1], vert_[3*i+2]}; double dst = sqrt((v[0]-cen[0])*(v[0]-cen[0]) + (v[1]-cen[1])*(v[1]-cen[1])); + geom->size[0] = max(geom->size[0], dst); + + // proceed with z: valid for cylinder double dst2 = abs(v[2]-cen[2]); - - // get spherical elevation at horizontal distance dst - double h = geom->size[0] * sin(acos(dst/geom->size[0])); - geom->size[1] = max(geom->size[1], dst2-h); + geom->size[1] = max(geom->size[1], dst2); } - } - break; - case mjGEOM_ELLIPSOID: - case mjGEOM_BOX: - geom->size[0] = aamm_[3] - cen[0]; - geom->size[1] = aamm_[4] - cen[1]; - geom->size[2] = aamm_[5] - cen[2]; - break; + // special handling of capsule: consider curved cap + if (geom->type == mjGEOM_CAPSULE) { + geom->size[1] = 0; + for (int i=0; i < nvert(); i++) { + // get distance in XY and Z + double v[3] = {vert_[3*i], vert_[3*i+1], vert_[3*i+2]}; + double dst = sqrt((v[0]-cen[0])*(v[0]-cen[0]) + + (v[1]-cen[1])*(v[1]-cen[1])); + double dst2 = abs(v[2]-cen[2]); - default: - throw mjCError(this, "invalid fittype in mesh %s", name.c_str()); + // get spherical elevation at horizontal distance dst + double h = geom->size[0] * sin(acos(dst/geom->size[0])); + geom->size[1] = max(geom->size[1], dst2-h); + } + } + break; + + case mjGEOM_ELLIPSOID: + case mjGEOM_BOX: + geom->size[0] = aamm_[3] - cen[0]; + geom->size[1] = aamm_[4] - cen[1]; + geom->size[2] = aamm_[5] - cen[2]; + break; + + default: + throw mjCError(this, "invalid fittype in mesh %s", name.c_str()); } } @@ -1186,7 +1186,7 @@ void mjCMesh::LoadMSH(mjResource* resource, bool remove_repeated) { } // make sure header is present - if (buffer_sz<4*sizeof(int)) { + if (buffer_sz < 4*sizeof(int)) { throw mjCError(this, "missing header in MSH file '%s'", resource->name); } @@ -1626,13 +1626,13 @@ double mjCMesh::ComputeInertia(double inert[6], const double CoM[3]) const { int C = (inertia == mjMESH_INERTIA_SHELL) ? 12 : 20; for (int j = 0; j < 6; j++) { P[j] += volume / - C * ( - 2*(D[k[j][0]] * D[k[j][1]] + - E[k[j][0]] * E[k[j][1]] + - F[k[j][0]] * F[k[j][1]]) + - D[k[j][0]] * E[k[j][1]] + D[k[j][1]] * E[k[j][0]] + - D[k[j][0]] * F[k[j][1]] + D[k[j][1]] * F[k[j][0]] + - E[k[j][0]] * F[k[j][1]] + E[k[j][1]] * F[k[j][0]]); + C * ( + 2*(D[k[j][0]] * D[k[j][1]] + + E[k[j][0]] * E[k[j][1]] + + F[k[j][0]] * F[k[j][1]]) + + D[k[j][0]] * E[k[j][1]] + D[k[j][1]] * E[k[j][0]] + + D[k[j][0]] * F[k[j][1]] + D[k[j][1]] * F[k[j][0]] + + E[k[j][0]] * F[k[j][1]] + E[k[j][1]] * F[k[j][0]]); } } @@ -1669,7 +1669,7 @@ void mjCMesh::Rotate(double quat[4]) { const double nrm[3] = {normal_[3*i], normal_[3*i+1], normal_[3*i+2]}; double res[3]; mjuu_mulvecmat(res, nrm, mat); - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { normal_[3*i+j] = (float) res[j]; } } @@ -1784,7 +1784,7 @@ void mjCMesh::MakeGraph() { FORALLvertices { // point id of this vertex, check int pid = qh_pointid(qh, vertex->point); - if (pid<0 || pid>=nvert()) { + if (pid < 0 || pid >= nvert()) { ok = 0; break; } @@ -1802,29 +1802,29 @@ void mjCMesh::MakeGraph() { // point id of face vertex, check int pid1 = qh_pointid(qh, vertex1->point); - if (pid1<0 || pid1>=nvert()) { + if (pid1 < 0 || pid1 >= nvert()) { ok = 0; break; } // if different from vertex id, try to insert - if (pid!=pid1) { + if (pid != pid1) { // check for previous record int j; - for (j=start; j=adr) { + if (j >= adr) { edge_localid[adr++] = pid1; } } } // make sure we have triangle: SHOULD NOT OCCUR - if (cnt!=3) { + if (cnt != 3) { mju_error("Qhull did not return triangle"); } } @@ -1835,7 +1835,7 @@ void mjCMesh::MakeGraph() { } // size check: SHOULD NOT OCCUR - if (adr!=numvert+3*numface) { + if (adr != numvert+3*numface) { mju_error("Wrong size in convex hull graph"); } @@ -1852,7 +1852,7 @@ void mjCMesh::MakeGraph() { // copy triangle data FOREACHsetelement_(vertexT, facet->vertices, vertex1) { // make sure we have triangle: SHOULD NOT OCCUR - if (ii>=3) { + if (ii >= 3) { mju_error("Qhull did not return triangle"); } @@ -1877,18 +1877,18 @@ void mjCMesh::MakeGraph() { // replace global ids with local ids in edge data for (int i=0; i < numvert+3*numface; i++) { - if (edge_localid[i]>=0) { + if (edge_localid[i] >= 0) { // search vert_globalid for match int adr; - for (adr=0; adr=numvert) { + if (adr >= numvert) { mju_error("Vertex id not found in convex hull"); } } @@ -1952,13 +1952,13 @@ void mjCMesh::MakeNormal() { for (int i=0; i < nface(); i++) { // get vertex ids int vertid[3]; - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { vertid[j] = face_[3*i+j]; } // get triangle edges double vec01[3], vec02[3]; - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { vec01[j] = vert_[3*vertid[1]+j] - vert_[3*vertid[0]+j]; vec02[j] = vert_[3*vertid[2]+j] - vert_[3*vertid[0]+j]; } @@ -1969,8 +1969,8 @@ void mjCMesh::MakeNormal() { double area = mjuu_normvec(nrm, 3); // add normal to each vertex with weight = area - for (int j=0; j<3; j++) { - for (int k=0; k<3; k++) { + for (int j=0; j < 3; j++) { + for (int k=0; k < 3; k++) { normal_[3*vertid[j]+k] += nrm[k]*area; } facenormal_[3*i+j] = vertid[j]; @@ -1987,13 +1987,13 @@ void mjCMesh::MakeNormal() { for (int i=0; i < nface(); i++) { // get vertex ids int vertid[3]; - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { vertid[j] = face_[3*i+j]; } // get triangle edges double vec01[3], vec02[3]; - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { vec01[j] = vert_[3*vertid[1]+j] - vert_[3*vertid[0]+j]; vec02[j] = vert_[3*vertid[2]+j] - vert_[3*vertid[0]+j]; } @@ -2004,14 +2004,14 @@ void mjCMesh::MakeNormal() { double area = mjuu_normvec(nrm, 3); // compare to vertex normal, subtract contribution if dot product too small - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { // normalized vertex normal double vnrm[3] = {normal_[3*vertid[j]], normal_[3*vertid[j]+1], normal_[3*vertid[j]+2]}; mjuu_normvec(vnrm, 3); // dot too small: remove - if (mjuu_dot3(nrm, vnrm)<0.8) { - for (int k=0; k<3; k++) { + if (mjuu_dot3(nrm, vnrm) < 0.8) { + for (int k=0; k < 3; k++) { nremove[3*vertid[j]+k] += nrm[k]*area; } } @@ -2033,12 +2033,12 @@ void mjCMesh::MakeNormal() { normal_[3*i+2]*normal_[3*i+2]); // divide by length - if (len>mjMINVAL) - for (int j=0; j<3; j++) { + if (len > mjMINVAL) + for (int j=0; j < 3; j++) { normal_[3*i+j] /= len; - } else { - normal_[3*i] = normal_[3*i+1] = 0; - normal_[3*i+2] = 1; + }else { + normal_[3*i] = normal_[3*i+1] = 0; + normal_[3*i+2] = 1; } } } @@ -2061,7 +2061,7 @@ void mjCMesh::MakeCenter() { // get triangle edges double a[3], b[3]; - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { a[j] = vert_[3*vertid[0]+j] - vert_[3*vertid[2]+j]; b[j] = vert_[3*vertid[1]+j] - vert_[3*vertid[2]+j]; } @@ -2093,12 +2093,12 @@ void mjCMesh::MakeCenter() { // compute the normals of the polygons void mjCMesh::MakePolygonNormals() { for (int i = 0; i < polygons_.size(); ++i) { - double n[3]; - mjuu_makenormal(n, &vert_[3*polygons_[i][0]], &vert_[3*polygons_[i][1]], - &vert_[3*polygons_[i][2]]); - polygon_normals_[3*i + 0] = n[0]; - polygon_normals_[3*i + 1] = n[1]; - polygon_normals_[3*i + 2] = n[2]; + double n[3]; + mjuu_makenormal(n, &vert_[3*polygons_[i][0]], &vert_[3*polygons_[i][1]], + &vert_[3*polygons_[i][2]]); + polygon_normals_[3*i + 0] = n[0]; + polygon_normals_[3*i + 1] = n[1]; + polygon_normals_[3*i + 2] = n[2]; } } @@ -2265,8 +2265,8 @@ void MeshPolygon::InsertFace(int v1, int v2, int v3) { // return the traverse vertices of the polygon; there may be multiple paths if the polygon is // not connected -std::vector> MeshPolygon::Paths() const { - std::vector> paths; +std::vector > MeshPolygon::Paths() const { + std::vector > paths; // shortcut if polygon is just a triangular face if (edges_.size() == 3) { return {{edges_[0].first, edges_[1].first, edges_[2].first}}; @@ -2304,7 +2304,7 @@ std::vector> MeshPolygon::Paths() const { } path.push_back(next); break; - } + } } // back at start @@ -2357,7 +2357,7 @@ void mjCMesh::MakePolygons() { } for (const auto& polygon : polygons) { - std::vector> paths = polygon.Paths(); + std::vector > paths = polygon.Paths(); // separate the polygons if they were grouped together for (const auto& path : paths) { @@ -2595,7 +2595,7 @@ void mjCSkin::Compile(const mjVFS* vfs) { if (vert_.size()%3) { throw mjCError(this, "Vertex data must be multiple of 3"); } - if (!texcoord_.empty() && texcoord_.size()!=2*vert_.size()/3) { + if (!texcoord_.empty() && texcoord_.size() != 2*vert_.size()/3) { throw mjCError(this, "Vertex and texcoord data incompatible size"); } if (face_.size()%3) { @@ -2604,16 +2604,16 @@ void mjCSkin::Compile(const mjVFS* vfs) { // check bone sizes size_t nbone = bodyname_.size(); - if (bindpos_.size()!=3*nbone) { + if (bindpos_.size() != 3*nbone) { throw mjCError(this, "Unexpected bindpos size in skin"); } - if (bindquat_.size()!=4*nbone) { + if (bindquat_.size() != 4*nbone) { throw mjCError(this, "Unexpected bindquat size in skin"); } - if (vertid_.size()!=nbone) { + if (vertid_.size() != nbone) { throw mjCError(this, "Unexpected vertid size in skin"); } - if (vertweight_.size()!=nbone) { + if (vertweight_.size() != nbone) { throw mjCError(this, "Unexpected vertweight size in skin"); } @@ -2638,15 +2638,15 @@ void mjCSkin::Compile(const mjVFS* vfs) { for (int i=0; i < nbone; i++) { // make sure bone has vertices and sizes match size_t nbv = vertid_[i].size(); - if (vertweight_[i].size()!=nbv || nbv==0) { + if (vertweight_[i].size() != nbv || nbv == 0) { throw mjCError(this, "vertid and vertweight must have same non-zero size in skin"); } // accumulate weights in global array - for (int j=0; j=nvert) { + if (jj < 0 || jj >= nvert) { throw mjCError(this, "vertid %d out of range in skin", NULL, jj); } @@ -2657,14 +2657,14 @@ void mjCSkin::Compile(const mjVFS* vfs) { // check coverage for (int i=0; i < nvert; i++) { - if (vw[i]<=mjMINVAL) { + if (vw[i] <= mjMINVAL) { throw mjCError(this, "vertex %d must have positive total weight in skin", NULL, i); } } // normalize vertex weights for (int i=0; i < nbone; i++) { - for (int j=0; jname); } @@ -2711,7 +2711,7 @@ void mjCSkin::LoadSKN(mjResource* resource) { int nbone = ((int*)buffer)[3]; // negative sizes not allowed - if (nvert<0 || ntexcoord<0 || nface<0 || nbone<0) { + if (nvert < 0 || ntexcoord < 0 || nface < 0 || nbone < 0) { throw mjCError(this, "negative size in header of SKN file '%s'", resource->name); } @@ -2779,7 +2779,7 @@ void mjCSkin::LoadSKN(mjResource* resource) { cnt += 1; // check for negative - if (vcount<1) { + if (vcount < 1) { throw mjCError(this, "vertex count must be positive in SKN file '%s', bone %d", resource->name, i); } @@ -2814,10 +2814,10 @@ void mjCSkin::LoadSKN(mjResource* resource) { // hash function for std::pair struct PairHash { - template - std::size_t operator() (const std::pair& pair) const { - return std::hash()(pair.first) ^ std::hash()(pair.second); - } + template + std::size_t operator() (const std::pair& pair) const { + return std::hash()(pair.first) ^ std::hash()(pair.second); + } }; // simplex connectivity @@ -3122,7 +3122,7 @@ void inline ComputeLinearStiffness(std::vector& K, double invJ[3] = {1.0 / dx, 1.0 / dy, 1.0 / dz}; // compute stiffness matrix - std::vector> F(n); + std::vector > F(n); double la = E * nu / (1 + nu) / (1 - 2 * nu); double mu = E / (2 * (1 + nu)); @@ -3316,22 +3316,22 @@ void mjCFlex::Compile(const mjVFS* vfs) { interpolated = !nodebody_.empty(); // set nelem; check sizes - if (dim<1 || dim>3) { - throw mjCError(this, "dim must be 1, 2 or 3"); + if (dim < 1 || dim > 3) { + throw mjCError(this, "dim must be 1, 2 or 3"); } if (elem_.empty()) { - throw mjCError(this, "elem is empty"); + throw mjCError(this, "elem is empty"); } if (elem_.size() % (dim+1)) { - throw mjCError(this, "elem size must be multiple of (dim+1)"); + throw mjCError(this, "elem size must be multiple of (dim+1)"); } if (vertbody_.empty() && !interpolated) { - throw mjCError(this, "vertbody and nodebody are both empty"); + throw mjCError(this, "vertbody and nodebody are both empty"); } if (vert_.size() % 3) { - throw mjCError(this, "vert size must be a multiple of 3"); + throw mjCError(this, "vert size must be a multiple of 3"); } - if (edgestiffness>0 && dim>1) { + if (edgestiffness > 0 && dim > 1) { throw mjCError(this, "edge stiffness only available for dim=1, please use elasticity plugins"); } if (interpolated && selfcollide != mjFLEXSELF_NONE) { @@ -3349,29 +3349,29 @@ void mjCFlex::Compile(const mjVFS* vfs) { } else { nvert = (int)vert_.size()/3; - if (vertbody_.size()==1) { + if (vertbody_.size() == 1) { rigid = true; } } - if (nvert=nvert) { + if (elem < 0 || elem >= nvert) { throw mjCError(this, "elem vertex id out of range"); } } // check texcoord - if (!texcoord_.empty() && texcoord_.size()!=2*nvert && elemtexcoord_.empty()) { + if (!texcoord_.empty() && texcoord_.size() != 2*nvert && elemtexcoord_.empty()) { throw mjCError(this, "two texture coordinates per vertex expected"); } @@ -3393,15 +3393,15 @@ void mjCFlex::Compile(const mjVFS* vfs) { ResolveReferences(model); // process elements - for (int e=0; e<(int)elem_.size()/(dim+1); e++) { + for (int e=0; e < (int)elem_.size()/(dim+1); e++) { // make sorted copy of element std::vector el; el.assign(elem_.begin()+e*(dim+1), elem_.begin()+(e+1)*(dim+1)); std::sort(el.begin(), el.end()); // check for repeated vertices - for (int k=0; k0) { + if (mjuu_dot3(nrm, v03) > 0) { // flip orientation int tmp = elem_[e*(dim+1)+1]; elem_[e*(dim+1)+1] = elem_[e*(dim+1)+2]; @@ -3513,7 +3513,7 @@ void mjCFlex::Compile(const mjVFS* vfs) { auto pair = std::pair( min(v[eledge[dim-1][e][0]], v[eledge[dim-1][e][1]]), max(v[eledge[dim-1][e][0]], v[eledge[dim-1][e][1]]) - ); + ); // if edge is already present in the vector only store its index auto [it, inserted] = edge_indices.insert({pair, nedge}); @@ -3547,11 +3547,11 @@ void mjCFlex::Compile(const mjVFS* vfs) { if (interpolated) { continue; } - if (dim==2) { + if (dim == 2) { ComputeStiffness(stiffness, vertxpos, elem_.data() + (dim + 1) * t, t, young, poisson, thickness); - } else if (dim==3) { + } else if (dim == 3) { ComputeStiffness(stiffness, vertxpos, elem_.data() + (dim + 1) * t, t, young, poisson); @@ -3570,7 +3570,7 @@ void mjCFlex::Compile(const mjVFS* vfs) { } if (model->Bodies()[vbodyid]->plugin.element) { mjCPlugin* plugin_instance = - static_cast(model->Bodies()[vbodyid]->plugin.element); + static_cast(model->Bodies()[vbodyid]->plugin.element); if (damping > 0) { plugin_instance->config_attribs["damping"] = std::to_string(damping); } @@ -3613,11 +3613,11 @@ void mjCFlex::CreateBVH() { tree.AllocateBoundingVolumes(nelem); // construct element bounding boxes, add to hierarchy - for (int e=0; e=activelayers) { + if (dim == 3 && elemlayer[e] >= activelayers) { continue; } @@ -3626,7 +3626,7 @@ void mjCFlex::CreateBVH() { mjuu_copyvec(xmin, vertxpos.data() + 3*edata[0], 3); mjuu_copyvec(xmax, vertxpos.data() + 3*edata[0], 3); for (int i=1; i <= dim; i++) { - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { xmin[j] = std::min(xmin[j], vertxpos[3*edata[i]+j]); xmax[j] = std::max(xmax[j], vertxpos[3*edata[i]+j]); } @@ -3655,13 +3655,13 @@ void mjCFlex::CreateBVH() { // create shells and element-vertex collision pairs void mjCFlex::CreateShellPair(void) { - std::vector> fragspec(nelem*(dim+1)); // [sorted frag vertices, elem, original frag vertices] - std::vector> connectspec; // [elem1, elem2, common sorted frag vertices] + std::vector > fragspec(nelem*(dim+1)); // [sorted frag vertices, elem, original frag vertices] + std::vector > connectspec; // [elem1, elem2, common sorted frag vertices] std::vector border(nelem, false); // is element on the border std::vector borderfrag(nelem*(dim+1), false); // is fragment on the border // make fragspec - for (int e=0; e1) { - for (int n=0; n 1) { + for (int n=0; n < nelem*(dim+1); n++) { std::sort(fragspec[n].begin(), fragspec[n].begin()+dim); } } @@ -3748,13 +3748,13 @@ void mjCFlex::CreateShellPair(void) { // make border and connectspec, record borderfrag int cnt = 1; - for (int n=1; n previous = {fragspec[n-1].begin(), fragspec[n-1].begin()+dim}; std::vector current = {fragspec[n].begin(), fragspec[n].begin()+dim}; // same sequential fragments - if (previous==current) { + if (previous == current) { // found pair of elements connected by common fragment std::vector connect; connect.insert(connect.end(), fragspec[n-1][dim]); @@ -3769,7 +3769,7 @@ void mjCFlex::CreateShellPair(void) { // different sequential fragments else { // found border fragment - if (cnt==1) { + if (cnt == 1) { border[fragspec[n-1][dim]] = true; borderfrag[n-1] = true; } @@ -3780,7 +3780,7 @@ void mjCFlex::CreateShellPair(void) { } // last fragment is border - if (cnt==1) { + if (cnt == 1) { int n = nelem*(dim+1); border[fragspec[n-1][dim]] = true; borderfrag[n-1] = true; @@ -3795,12 +3795,12 @@ void mjCFlex::CreateShellPair(void) { } // compute elemlayer (distance from border) via value iteration in 3D - if (dim<3) { + if (dim < 3) { elemlayer = std::vector (nelem, 0); } else { elemlayer = std::vector (nelem, nelem+1); // init with greater than max value - for (int e=0; eelemlayer[e2]+1) { - elemlayer[e1] = elemlayer[e2]+1; // better value found for e1: update + if (elemlayer[e1] > elemlayer[e2]+1) { + elemlayer[e1] = elemlayer[e2]+1; // better value found for e1: update change = true; - } else if (elemlayer[e2]>elemlayer[e1]+1) { + } else if (elemlayer[e2] > elemlayer[e1]+1) { elemlayer[e2] = elemlayer[e1]+1; // better value found for e2: update change = true; } @@ -3826,7 +3826,7 @@ void mjCFlex::CreateShellPair(void) { } // create evpairs in 1D and 2D - if (dim<3) { + if (dim < 3) { // process connected element pairs containing a border element for (const auto& connect : connectspec) { if (border[connect[0]] || border[connect[1]]) { diff --git a/src/user/user_model.cc b/src/user/user_model.cc index 80f77556..76f2a41f 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -208,7 +208,7 @@ mjCModel& mjCModel::operator=(const mjCModel& other) { *this += *subtree; // copy name maps - for (int i=0; i& source, candidate->model = this; candidate->NameSpace(plugin->model); bool referenced = instances.find(candidate->name) != instances.end(); - auto same_name = [candidate](const mjCPlugin* dest) { return dest->name == candidate->name; }; + auto same_name = [candidate](const mjCPlugin* dest) { + return dest->name == candidate->name; + }; bool instance_exists = std::find_if(plugins_.begin(), plugins_.end(), same_name) != plugins_.end(); if (referenced && !instance_exists) { @@ -390,13 +392,13 @@ void mjCModel::CopyPlugin(const std::vector& source, // return true if the plugin is already in the list of active plugins static bool IsPluginActive( - const mjpPlugin* plugin, - const std::vector>& active_plugins) { + const mjpPlugin* plugin, + const std::vector >& active_plugins) { return std::find_if( - active_plugins.begin(), active_plugins.end(), - [&plugin](const std::pair& element) { - return element.first == plugin; - }) != active_plugins.end(); + active_plugins.begin(), active_plugins.end(), + [&plugin](const std::pair& element) { + return element.first == plugin; + }) != active_plugins.end(); } @@ -770,25 +772,25 @@ mjCModel::~mjCModel() { bodies_[0]->Release(); // delete objects allocated in mjCModel - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); // also deletes wraps - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); + for (int i=0; i < flexes_.size(); i++) flexes_[i]->Release(); + for (int i=0; i < meshes_.size(); i++) meshes_[i]->Release(); + for (int i=0; i < skins_.size(); i++) skins_[i]->Release(); + for (int i=0; i < hfields_.size(); i++) hfields_[i]->Release(); + for (int i=0; i < textures_.size(); i++) textures_[i]->Release(); + for (int i=0; i < materials_.size(); i++) materials_[i]->Release(); + for (int i=0; i < pairs_.size(); i++) pairs_[i]->Release(); + for (int i=0; i < excludes_.size(); i++) excludes_[i]->Release(); + for (int i=0; i < equalities_.size(); i++) equalities_[i]->Release(); + for (int i=0; i < tendons_.size(); i++) tendons_[i]->Release(); // also deletes wraps + for (int i=0; i < actuators_.size(); i++) actuators_[i]->Release(); + for (int i=0; i < sensors_.size(); i++) sensors_[i]->Release(); + for (int i=0; i < numerics_.size(); i++) numerics_[i]->Release(); + for (int i=0; i < texts_.size(); i++) texts_[i]->Release(); + for (int i=0; i < tuples_.size(); i++) tuples_[i]->Release(); + for (int i=0; i < keys_.size(); i++) keys_[i]->Release(); + for (int i=0; i < defaults_.size(); i++) delete defaults_[i]; + for (int i=0; i < specs_.size(); i++) mj_deleteSpec(specs_[i]); + for (int i=0; i < plugins_.size(); i++) plugins_[i]->Release(); // clear sizes and pointer lists created in Compile Clear(); @@ -1157,7 +1159,7 @@ mjCBody* mjCModel::GetWorld() { // find default class name in array mjCDef* mjCModel::FindDefault(string name) { - for (int i=0; i<(int)defaults_.size(); i++) { + for (int i=0; i < (int)defaults_.size(); i++) { if (defaults_[i]->name == name) { return defaults_[i]; } @@ -1171,8 +1173,8 @@ mjCDef* mjCModel::FindDefault(string name) { mjCDef* mjCModel::AddDefault(string name, mjCDef* parent) { // check for repeated name int thisid = (int)defaults_.size(); - for (int i=0; iname==name) { + for (int i=0; i < thisid; i++) { + if (defaults_[i]->name == name) { return 0; } } @@ -1183,7 +1185,7 @@ mjCDef* mjCModel::AddDefault(string name, mjCDef* parent) { def->id = thisid; // initialize contents - if (parent && parent->idid < thisid) { parent->CopyFromSpec(); def->CopyWithoutChildren(*parent); parent->child.push_back(def); @@ -1203,7 +1205,7 @@ template static T* findobject(std::string_view name, const vector& list, const mjKeyMap& ids) { // this can occur in the URDF parser if (ids.empty()) { - for (unsigned int i=0; iname == name) { return list[i]; } @@ -1390,7 +1392,7 @@ static void DeleteTexcoord(std::vector& list) { // returns a vector that stores the reference correction for each entry template static void DeleteElements(std::vector& elements, - const std::vector& discard) { + const std::vector& discard) { if (elements.empty()) { return; } @@ -1398,7 +1400,7 @@ static void DeleteElements(std::vector& elements, std::vector ndiscard(elements.size(), 0); int i = 0; - for (int j=0; j& elements, } // count cumulative discarded elements - for (int i=0; i(std::vector& elements, // update bodies for (mjCBody* body : bodies_) { body->geoms.erase( - std::remove_if(body->geoms.begin(), body->geoms.end(), - [&discard](mjCGeom* geom) { return discard[geom->id]; }), - body->geoms.end()); + std::remove_if(body->geoms.begin(), body->geoms.end(), + [&discard](mjCGeom* geom) { + return discard[geom->id]; + }), + body->geoms.end()); } // remove geoms from the main vector @@ -1527,7 +1531,7 @@ void mjCModel::SetNuser() { // index assets void mjCModel::IndexAssets(bool discard) { // assets referenced in geoms - for (int i=0; itextures_[j].empty()) { mjCBase* texture = FindObject(mjOBJ_TEXTURE, material->textures_[j]); if (texture) { @@ -1632,9 +1636,13 @@ void mjCModel::IndexAssets(bool discard) { std::vector discard_geom(geoms_.size(), false); std::transform(meshes_.begin(), meshes_.end(), discard_mesh.begin(), - [](const mjCMesh* mesh) { return mesh->IsVisual(); }); + [](const mjCMesh* mesh) { + return mesh->IsVisual(); + }); std::transform(geoms_.begin(), geoms_.end(), discard_geom.begin(), - [](const mjCGeom* geom) { return geom->IsVisual(); }); + [](const mjCGeom* geom) { + return geom->IsVisual(); + }); Delete(meshes_, discard_mesh); Delete(geoms_, discard_geom); @@ -1646,14 +1654,14 @@ void mjCModel::IndexAssets(bool discard) { // throw error if a name is missing void mjCModel::CheckEmptyNames(void) { // meshes - for (int i=0; iname.empty()) { throw mjCError(meshes_[i], "empty name in mesh"); } } // hfields - for (int i=0; iname.empty()) { throw mjCError(hfields_[i], "empty name in height field"); } @@ -1661,7 +1669,7 @@ void mjCModel::CheckEmptyNames(void) { // textures for (int i=0; i < textures_.size(); i++) { - if (textures_[i]->name.empty() && textures_[i]->type!=mjTEXTURE_SKYBOX) { + if (textures_[i]->name.empty() && textures_[i]->type != mjTEXTURE_SKYBOX) { throw mjCError(textures_[i], "empty name in texture"); } } @@ -1716,31 +1724,31 @@ void mjCModel::SetSizes() { nq = nv = nu = na = nmocap = 0; // nq, nv - for (int i=0; inq(); nv += joints_[i]->nv(); } // nu, na - for (int i=0; iactdim; } // nbvh, nbvhstatic, nbvhdynamic - for (int i=0; itree.Nbvh(); } - for (int i=0; itree().Nbvh(); } - for (int i=0; itree.Nbvh(); } nbvh = nbvhstatic + nbvhdynamic; // flex counts - for (int i=0; innode; nflexvert += flexes_[i]->nvert; nflexedge += flexes_[i]->nedge; @@ -1753,7 +1761,7 @@ void mjCModel::SetSizes() { } // mesh counts - for (int i=0; invert(); nmeshnormal += meshes_[i]->nnormal(); nmeshface += meshes_[i]->nface(); @@ -1765,65 +1773,65 @@ void mjCModel::SetSizes() { } // skin counts - for (int i=0; iget_vert().size()/3; nskintexvert += skins_[i]->get_texcoord().size()/2; nskinface += skins_[i]->get_face().size()/3; nskinbone += skins_[i]->bodyid.size(); - for (int j=0; jbodyid.size(); j++) { + for (int j=0; j < skins_[i]->bodyid.size(); j++) { nskinbonevert += skins_[i]->get_vertid()[j].size(); } } // nhfielddata - for (int i=0; inrow * hfields_[i]->ncol; + for (int i=0; i < nhfield; i++)nhfielddata += hfields_[i]->nrow * hfields_[i]->ncol; // ntexdata - for (int i=0; inchannel * textures_[i]->width * textures_[i]->height; + for (int i=0; i < ntex; i++)ntexdata += textures_[i]->nchannel * textures_[i]->width * textures_[i]->height; // nwrap - for (int i=0; ipath.size(); + for (int i=0; i < ntendon; i++)nwrap += (int)tendons_[i]->path.size(); // nsensordata - for (int i=0; idim; + for (int i=0; i < nsensor; i++)nsensordata += sensors_[i]->dim; // nnumericdata - for (int i=0; isize; + for (int i=0; i < nnumeric; i++)nnumericdata += numerics_[i]->size; // ntextdata - for (int i=0; idata_.size() + 1; + for (int i=0; i < ntext; i++)ntextdata += (int)texts_[i]->data_.size() + 1; // ntupledata - for (int i=0; iobjtype_.size(); + for (int i=0; i < ntuple; i++)ntupledata += (int)tuples_[i]->objtype_.size(); // npluginattr - for (int i=0; iflattened_attributes.size(); + for (int i=0; i < nplugin; i++)npluginattr += (int)plugins_[i]->flattened_attributes.size(); // nnames nnames = (int)modelname_.size() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; - for (int i=0; iname.length() + 1; + for (int i=0; i < nbody; i++) nnames += (int)bodies_[i]->name.length() + 1; + for (int i=0; i < njnt; i++) nnames += (int)joints_[i]->name.length() + 1; + for (int i=0; i < ngeom; i++) nnames += (int)geoms_[i]->name.length() + 1; + for (int i=0; i < nsite; i++) nnames += (int)sites_[i]->name.length() + 1; + for (int i=0; i < ncam; i++) nnames += (int)cameras_[i]->name.length() + 1; + for (int i=0; i < nlight; i++) nnames += (int)lights_[i]->name.length() + 1; + for (int i=0; i < nflex; i++) nnames += (int)flexes_[i]->name.length() + 1; + for (int i=0; i < nmesh; i++) nnames += (int)meshes_[i]->name.length() + 1; + for (int i=0; i < nskin; i++) nnames += (int)skins_[i]->name.length() + 1; + for (int i=0; i < nhfield; i++) nnames += (int)hfields_[i]->name.length() + 1; + for (int i=0; i < ntex; i++) nnames += (int)textures_[i]->name.length() + 1; + for (int i=0; i < nmat; i++) nnames += (int)materials_[i]->name.length() + 1; + for (int i=0; i < npair; i++) nnames += (int)pairs_[i]->name.length() + 1; + for (int i=0; i < nexclude; i++) nnames += (int)excludes_[i]->name.length() + 1; + for (int i=0; i < neq; i++) nnames += (int)equalities_[i]->name.length() + 1; + for (int i=0; i < ntendon; i++) nnames += (int)tendons_[i]->name.length() + 1; + for (int i=0; i < nu; i++) nnames += (int)actuators_[i]->name.length() + 1; + for (int i=0; i < nsensor; i++) nnames += (int)sensors_[i]->name.length() + 1; + for (int i=0; i < nnumeric; i++) nnames += (int)numerics_[i]->name.length() + 1; + for (int i=0; i < ntext; i++) nnames += (int)texts_[i]->name.length() + 1; + for (int i=0; i < ntuple; i++) nnames += (int)tuples_[i]->name.length() + 1; + for (int i=0; i < nkey; i++) nnames += (int)keys_[i]->name.length() + 1; + for (int i=0; i < nplugin; i++) nnames += (int)plugins_[i]->name.length() + 1; // npaths npaths = 0; @@ -1836,10 +1844,10 @@ void mjCModel::SetSizes() { } // nemax - for (int i=0; itype==mjEQ_CONNECT) { + for (int i=0; i < neq; i++) { + if (equalities_[i]->type == mjEQ_CONNECT) { nemax += 3; - } else if (equalities_[i]->type==mjEQ_WELD) { + } else if (equalities_[i]->type == mjEQ_WELD) { nemax += 7; } else { nemax += 1; @@ -1852,7 +1860,7 @@ void mjCModel::SetSizes() { // automatic stiffness and damping computation void mjCModel::AutoSpringDamper(mjModel* m) { // process all joints - for (int n=0; nnjnt; n++) { + for (int n=0; n < m->njnt; n++) { // get joint dof address and number of dimensions int adr = m->jnt_dofadr[n]; int ndim = mjCJoint::nv((mjtJoint)m->jnt_type[n]); @@ -1862,13 +1870,13 @@ void mjCModel::AutoSpringDamper(mjModel* m) { mjtNum dampratio = (mjtNum)joints_[n]->springdamper[1]; // skip joint if either parameter is non-positive - if (timeconst<=0 || dampratio<=0) { + if (timeconst <= 0 || dampratio <= 0) { continue; } // get average inertia (dof_invweight0 in free joint is different for tran and rot) mjtNum inertia = 0; - for (int i=0; idof_invweight0[adr+i]; } inertia = ((mjtNum)ndim) / std::max(mjMINVAL, inertia); @@ -1883,7 +1891,7 @@ void mjCModel::AutoSpringDamper(mjModel* m) { // assign m->jnt_stiffness[n] = stiffness; - for (int i=0; idof_damping[adr+i] = damping; } } @@ -1908,8 +1916,8 @@ typedef struct _LRThreadArg LRThreadArg; void* LRfunc(void* arg) { LRThreadArg* larg = (LRThreadArg*)arg; - for (int i=larg->start; istart+larg->num; i++) { - if (im->nu) { + for (int i=larg->start; i < larg->start+larg->num; i++) { + if (i < larg->m->nu) { if (!mj_setLengthRange(larg->m, larg->data, i, larg->LRopt, larg->error, larg->error_sz)) { return nullptr; } @@ -1926,7 +1934,7 @@ void mjCModel::LengthRange(mjModel* m, mjData* data) { mjOption saveopt = m->opt; m->opt.disableflags = mjDSBL_FRICTIONLOSS | mjDSBL_CONTACT | mjDSBL_PASSIVE | mjDSBL_GRAVITY | mjDSBL_ACTUATION; - if (compiler.LRopt.timestep>0) { + if (compiler.LRopt.timestep > 0) { m->opt.timestep = compiler.LRopt.timestep; } @@ -1936,15 +1944,15 @@ void mjCModel::LengthRange(mjModel* m, mjData* data) { // count actuators that need computation int cnt = 0; - for (int i=0; inu; i++) { + for (int i=0; i < m->nu; i++) { // skip depending on mode and type - int ismuscle = (m->actuator_gaintype[i]==mjGAIN_MUSCLE || - m->actuator_biastype[i]==mjBIAS_MUSCLE); - int isuser = (m->actuator_gaintype[i]==mjGAIN_USER || - m->actuator_biastype[i]==mjBIAS_USER); - if ((compiler.LRopt.mode==mjLRMODE_NONE) || - (compiler.LRopt.mode==mjLRMODE_MUSCLE && !ismuscle) || - (compiler.LRopt.mode==mjLRMODE_MUSCLEUSER && !ismuscle && !isuser)) { + int ismuscle = (m->actuator_gaintype[i] == mjGAIN_MUSCLE || + m->actuator_biastype[i] == mjBIAS_MUSCLE); + int isuser = (m->actuator_gaintype[i] == mjGAIN_USER || + m->actuator_biastype[i] == mjBIAS_USER); + if ((compiler.LRopt.mode == mjLRMODE_NONE) || + (compiler.LRopt.mode == mjLRMODE_MUSCLE && !ismuscle) || + (compiler.LRopt.mode == mjLRMODE_MUSCLEUSER && !ismuscle && !isuser)) { continue; } @@ -1959,9 +1967,9 @@ void mjCModel::LengthRange(mjModel* m, mjData* data) { } // single thread - if (!compiler.usethread || cnt<2 || nthread<2) { + if (!compiler.usethread || cnt < 2 || nthread < 2) { char err[200]; - for (int i=0; inu; i++) { + for (int i=0; i < m->nu; i++) { if (!mj_setLengthRange(m, data, i, &compiler.LRopt, err, 200)) { throw mjCError(0, "%s", err); } @@ -1973,7 +1981,7 @@ void mjCModel::LengthRange(mjModel* m, mjData* data) { // allocate mjData for each thread char err[kMaxCompilerThreads][200]; mjData* pdata[kMaxCompilerThreads] = {data}; - for (int i=1; i static int namelist(vector& list, int adr, int* name_adr, char* names, int* map) { // compute hash map addresses int map_size = mjLOAD_MULTIPLE*list.size(); - for (unsigned int i=0; iname.empty()) { continue; @@ -2053,11 +2061,11 @@ static int namelist(vector& list, int adr, int* name_adr, char* names, int* uint64_t j = mj_hashString(list[i]->name.c_str(), map_size); // find first empty slot using linear probing - for (; map[j]!=-1; j=(j+1) % map_size) {} + for (; map[j] != -1; j=(j+1) % map_size) {} map[j] = i; } - for (unsigned int i=0; iname, adr, &name_adr[i], names); } @@ -2182,7 +2190,7 @@ void mjCModel::CopyTree(mjModel* m) { int bvh_adr = 0; // main loop over bodies - for (int i=0; iparent; @@ -2217,7 +2225,7 @@ void mjCModel::CopyTree(mjModel* m) { memcpy(m->bvh_aabb + 6*bvh_adr, pb->tree.Bvh().data(), 6*pb->tree.Nbvh()*sizeof(mjtNum)); memcpy(m->bvh_child + 2*bvh_adr, pb->tree.Child().data(), 2*pb->tree.Nbvh()*sizeof(int)); memcpy(m->bvh_depth + bvh_adr, pb->tree.Level().data(), pb->tree.Nbvh()*sizeof(int)); - for (int i=0; itree.Nbvh(); i++) { + for (int i=0; i < pb->tree.Nbvh(); i++) { m->bvh_nodeid[i + bvh_adr] = pb->tree.Nodeidptr(i) ? *(pb->tree.Nodeidptr(i)) : -1; } } @@ -2225,12 +2233,12 @@ void mjCModel::CopyTree(mjModel* m) { // count free joints int cntfree = 0; - for (int j=0; j<(int)pb->joints.size(); j++) { + for (int j=0; j < (int)pb->joints.size(); j++) { cntfree += (pb->joints[j]->type == mjJNT_FREE); } // check validity of free joint - if (cntfree>1 || (cntfree==1 && pb->joints.size()>1)) { + if (cntfree > 1 || (cntfree == 1 && pb->joints.size() > 1)) { throw mjCError(pb, "free joint can only appear by itself"); } if (cntfree && par && par->name != "world") { @@ -2238,7 +2246,7 @@ void mjCModel::CopyTree(mjModel* m) { } // rootid: self if world or child of world, otherwise parent's rootid - if (i==0 || (par && par->name == "world")) { + if (i == 0 || (par && par->name == "world")) { m->body_rootid[i] = i; } else { m->body_rootid[i] = m->body_rootid[par->id]; @@ -2262,18 +2270,18 @@ void mjCModel::CopyTree(mjModel* m) { // init simple: sameframe, and (self-root, or parent is fixed child of world) int parentid = m->body_parentid[i]; m->body_simple[i] = (sameframe == mjSAMEFRAME_BODY && - (m->body_rootid[i]==i || - (m->body_parentid[parentid]==0 && - m->body_dofnum[parentid]==0))); + (m->body_rootid[i] == i || + (m->body_parentid[parentid] == 0 && + m->body_dofnum[parentid] == 0))); // a parent body is never simple (unless world) - if (m->body_parentid[i]>0) { + if (m->body_parentid[i] > 0) { m->body_simple[m->body_parentid[i]] = 0; } // loop over joints for this body int rotfound = 0; - for (int j=0; j<(int)pb->joints.size(); j++) { + for (int j=0; j < (int)pb->joints.size(); j++) { // get pointer and id mjCJoint* pj = pb->joints[j]; int jid = pj->id; @@ -2309,38 +2317,38 @@ void mjCModel::CopyTree(mjModel* m) { } // mark rotation - if (pj->type==mjJNT_BALL || pj->type==mjJNT_HINGE) { + if (pj->type == mjJNT_BALL || pj->type == mjJNT_HINGE) { rotfound = 1; } // set qpos0 and qpos_spring, check type switch (pj->type) { - case mjJNT_FREE: - mjuu_copyvec(m->qpos0+qposadr, pb->pos, 3); - mjuu_copyvec(m->qpos0+qposadr+3, pb->quat, 4); - mjuu_copyvec(m->qpos_spring+qposadr, m->qpos0+qposadr, 7); - break; + case mjJNT_FREE: + mjuu_copyvec(m->qpos0+qposadr, pb->pos, 3); + mjuu_copyvec(m->qpos0+qposadr+3, pb->quat, 4); + mjuu_copyvec(m->qpos_spring+qposadr, m->qpos0+qposadr, 7); + break; - case mjJNT_BALL: - m->qpos0[qposadr] = 1; - m->qpos0[qposadr+1] = 0; - m->qpos0[qposadr+2] = 0; - m->qpos0[qposadr+3] = 0; - mjuu_copyvec(m->qpos_spring+qposadr, m->qpos0+qposadr, 4); - break; + case mjJNT_BALL: + m->qpos0[qposadr] = 1; + m->qpos0[qposadr+1] = 0; + m->qpos0[qposadr+2] = 0; + m->qpos0[qposadr+3] = 0; + mjuu_copyvec(m->qpos_spring+qposadr, m->qpos0+qposadr, 4); + break; - case mjJNT_SLIDE: - case mjJNT_HINGE: - m->qpos0[qposadr] = (mjtNum)pj->ref; - m->qpos_spring[qposadr] = (mjtNum)pj->springref; - break; + case mjJNT_SLIDE: + case mjJNT_HINGE: + m->qpos0[qposadr] = (mjtNum)pj->ref; + m->qpos_spring[qposadr] = (mjtNum)pj->springref; + break; - default: - throw mjCError(pj, "unknown joint type"); + default: + throw mjCError(pj, "unknown joint type"); } // set dof fields for this joint - for (int j1=0; j1nv(); j1++) { + for (int j1=0; j1 < pj->nv(); j1++) { // set attributes m->dof_bodyid[dofadr] = pb->id; m->dof_jntid[dofadr] = jid; @@ -2366,8 +2374,8 @@ void mjCModel::CopyTree(mjModel* m) { // simple body with sliders and no rotational dofs: promote to simple level 2 if (m->body_simple[i] && m->body_dofnum[i]) { m->body_simple[i] = 2; - for (int j=0; j<(int)pb->joints.size(); j++) { - if (pb->joints[j]->type!=mjJNT_SLIDE) { + for (int j=0; j < (int)pb->joints.size(); j++) { + if (pb->joints[j]->type != mjJNT_SLIDE) { m->body_simple[i] = 1; break; } @@ -2375,7 +2383,7 @@ void mjCModel::CopyTree(mjModel* m) { } // loop over geoms for this body - for (int j=0; j<(int)pb->geoms.size(); j++) { + for (int j=0; j < (int)pb->geoms.size(); j++) { // get pointer and id mjCGeom* pg = pb->geoms[j]; int gid = pg->id; @@ -2430,7 +2438,7 @@ void mjCModel::CopyTree(mjModel* m) { } // loop over sites for this body - for (int j=0; j<(int)pb->sites.size(); j++) { + for (int j=0; j < (int)pb->sites.size(); j++) { // get pointer and id mjCSite* ps = pb->sites[j]; int sid = ps->id; @@ -2463,7 +2471,7 @@ void mjCModel::CopyTree(mjModel* m) { } // loop over cameras for this body - for (int j=0; j<(int)pb->cameras.size(); j++) { + for (int j=0; j < (int)pb->cameras.size(); j++) { // get pointer and id mjCCamera* pc = pb->cameras[j]; int cid = pc->id; @@ -2484,7 +2492,7 @@ void mjCModel::CopyTree(mjModel* m) { } // loop over lights for this body - for (int j=0; j<(int)pb->lights.size(); j++) { + for (int j=0; j < (int)pb->lights.size(); j++) { // get pointer and id mjCLight* pl = pb->lights[j]; int lid = pl->id; @@ -2509,7 +2517,7 @@ void mjCModel::CopyTree(mjModel* m) { } // check number of dof's constructed, SHOULD NOT OCCUR - if (nv!=dofadr) { + if (nv != dofadr) { throw mjCError(0, "unexpected number of DOFs"); } @@ -2535,20 +2543,20 @@ void mjCModel::CopyTree(mjModel* m) { // count bodies with gravity compensation, compute ngravcomp int ngravcomp = 0; - for (int i=0; ibody_gravcomp[i] > 0); } m->ngravcomp = ngravcomp; // compute nM and dof_Madr nM = 0; - for (int i=0; idof_Madr[i] = nM; // count ancestor dofs including self int j = i; - while (j>=0) { + while (j >= 0) { nM++; j = m->dof_parentid[j]; } @@ -2603,7 +2611,7 @@ void mjCModel::CopyTree(mjModel* m) { // compute nC int nOD = 0; // number of off-diagonal (non-simple) parent dofs - for (int i=0; idof_simplenum[i]) { int j = i; @@ -2634,7 +2642,7 @@ void mjCModel::CopyPlugins(mjModel* m) { // query and set plugin-related information { // set actuator_plugin to the plugin instance ID - std::vector> plugin_to_actuators(nplugin); + std::vector > plugin_to_actuators(nplugin); for (int i = 0; i < nu; ++i) { if (actuators_[i]->plugin.active) { int actuator_plugin = static_cast(actuators_[i]->plugin.element)->id; @@ -2661,7 +2669,7 @@ void mjCModel::CopyPlugins(mjModel* m) { } } - std::vector> plugin_to_sensors(nplugin); + std::vector > plugin_to_sensors(nplugin); for (int i = 0; i < nsensor; ++i) { if (sensors_[i]->type == mjSENS_PLUGIN) { int sensor_plugin = static_cast(sensors_[i]->plugin.element)->id; @@ -2692,7 +2700,7 @@ void mjCModel::CopyPlugins(mjModel* m) { int nsensordata = plugin->nsensordata(m, i, sensor_id); sensors_[sensor_id]->dim = nsensordata; sensors_[sensor_id]->needstage = - static_cast(plugin->needstage); + static_cast(plugin->needstage); this->nsensordata += nsensordata; } } @@ -2777,7 +2785,7 @@ void mjCModel::CopyObjects(mjModel* m) { // find bvh_adr after bodies bvh_adr = 0; - for (int i=0; ibody_bvhadr[i] + m->body_bvhnum[i]); } @@ -2790,7 +2798,7 @@ void mjCModel::CopyObjects(mjModel* m) { poly_adr = 0; polyvert_adr = 0; polymap_adr = 0; - for (int i=0; ibvh_aabb + 6*bvh_adr, pme->tree().Bvh().data(), 6*pme->tree().Nbvh()*sizeof(mjtNum)); memcpy(m->bvh_child + 2*bvh_adr, pme->tree().Child().data(), 2*pme->tree().Nbvh()*sizeof(int)); memcpy(m->bvh_depth + bvh_adr, pme->tree().Level().data(), pme->tree().Nbvh()*sizeof(int)); - for (int j=0; jtree().Nbvh(); j++) { + for (int j=0; j < pme->tree().Nbvh(); j++) { m->bvh_nodeid[j + bvh_adr] = pme->tree().Nodeid(j) > -1 ? pme->tree().Nodeid(j) : -1; } } @@ -2864,7 +2872,7 @@ void mjCModel::CopyObjects(mjModel* m) { shelldata_adr = 0; evpair_adr = 0; texcoord_adr = 0; - for (int i=0; iflex_texcoordadr[i] = texcoord_adr; memcpy(m->flex_texcoord + 2*texcoord_adr, - pfl->texcoord_.data(), pfl->texcoord_.size()*sizeof(float)); + pfl->texcoord_.data(), pfl->texcoord_.size()*sizeof(float)); memcpy(m->flex_elemtexcoord + elemdata_adr, pfl->elemtexcoord_.data(), pfl->elemtexcoord_.size()*sizeof(int)); } @@ -2943,8 +2951,8 @@ void mjCModel::CopyObjects(mjModel* m) { // find equality constraint referencing this flex m->flex_edgeequality[i] = 0; - for (int k=0; k<(int)equalities_.size(); k++) { - if (equalities_[k]->type==mjEQ_FLEX && equalities_[k]->name1_==pfl->name) { + for (int k=0; k < (int)equalities_.size(); k++) { + if (equalities_[k]->type == mjEQ_FLEX && equalities_[k]->name1_ == pfl->name) { m->flex_edgeequality[i] = 1; break; } @@ -2954,7 +2962,7 @@ void mjCModel::CopyObjects(mjModel* m) { if (pfl->tree.Nbvh()) { memcpy(m->bvh_child + 2*bvh_adr, pfl->tree.Child().data(), 2*pfl->tree.Nbvh()*sizeof(int)); memcpy(m->bvh_depth + bvh_adr, pfl->tree.Level().data(), pfl->tree.Nbvh()*sizeof(int)); - for (int i=0; itree.Nbvh(); i++) { + for (int i=0; i < pfl->tree.Nbvh(); i++) { m->bvh_nodeid[i+ bvh_adr] = pfl->tree.Nodeidptr(i) ? *(pfl->tree.Nodeidptr(i)) : -1; } } @@ -2983,7 +2991,7 @@ void mjCModel::CopyObjects(mjModel* m) { // copy or set vertbodyid if (pfl->rigid) { - for (int k=0; knvert; k++) { + for (int k=0; k < pfl->nvert; k++) { m->flex_vertbodyid[vert_adr + k] = pfl->vertbodyid[0]; } } @@ -2993,7 +3001,7 @@ void mjCModel::CopyObjects(mjModel* m) { // copy or set nodebodyid if (pfl->rigid) { - for (int k=0; knnode; k++) { + for (int k=0; k < pfl->nnode; k++) { m->flex_nodebodyid[node_adr + k] = pfl->nodebodyid[0]; } } else { @@ -3004,7 +3012,7 @@ void mjCModel::CopyObjects(mjModel* m) { m->flex_interp[i] = pfl->interpolated; // convert edge pairs to int array, set edge rigid - for (int k=0; knedge; k++) { + for (int k=0; k < pfl->nedge; k++) { m->flex_edge[2*(edge_adr+k)] = pfl->edge[k].first; m->flex_edge[2*(edge_adr+k)+1] = pfl->edge[k].second; @@ -3038,7 +3046,7 @@ void mjCModel::CopyObjects(mjModel* m) { texcoord_adr = 0; bone_adr = 0; bonevert_adr = 0; - for (int i=0; ibodyid.size()*sizeof(int)); // copy per-bone vertex data, advance vertex counter - for (int j=0; jskin_bonenum[i]; j++) { + for (int j=0; j < m->skin_bonenum[i]; j++) { // set fields m->skin_bonevertadr[bone_adr+j] = bonevert_adr; m->skin_bonevertnum[bone_adr+j] = (int)psk->get_vertid()[j].size(); @@ -3095,7 +3103,7 @@ void mjCModel::CopyObjects(mjModel* m) { // hfields data_adr = 0; - for (int i=0; imat_texid[mjNTEXROLE*i+j] = pmat->texid[j]; } m->mat_texuniform[i] = pmat->texuniform; @@ -3154,7 +3162,7 @@ void mjCModel::CopyObjects(mjModel* m) { } // geom pairs to include - for (int i=0; ipair_dim[i] = pairs_[i]->condim; m->pair_geom1[i] = pairs_[i]->geom1->id; m->pair_geom2[i] = pairs_[i]->geom2->id; @@ -3168,12 +3176,12 @@ void mjCModel::CopyObjects(mjModel* m) { } // body pairs to exclude - for (int i=0; iexclude_signature[i] = excludes_[i]->signature; } // equality constraints - for (int i=0; itendon_rgba+4*i, pte->rgba, 4); // set wraps - for (int j=0; j<(int)pte->path.size(); j++) { + for (int j=0; j < (int)pte->path.size(); j++) { m->wrap_type[adr+j] = pte->path[j]->type; m->wrap_objid[adr+j] = pte->path[j]->obj ? pte->path[j]->obj->id : -1; m->wrap_prm[adr+j] = (mjtNum)pte->path[j]->prm; - if (pte->path[j]->type==mjWRAP_SPHERE || pte->path[j]->type==mjWRAP_CYLINDER) { + if (pte->path[j]->type == mjWRAP_SPHERE || pte->path[j]->type == mjWRAP_CYLINDER) { m->wrap_prm[adr+j] = (mjtNum)pte->path[j]->sideid; } } @@ -3232,7 +3240,7 @@ void mjCModel::CopyObjects(mjModel* m) { // actuators adr = 0; - for (int i=0; inumeric_adr[i] = adr; m->numeric_size[i] = pcu->size; - for (int j=0; j<(int)pcu->data_.size(); j++) { + for (int j=0; j < (int)pcu->data_.size(); j++) { m->numeric_data[adr+j] = (mjtNum)pcu->data_[j]; } - for (int j=(int)pcu->data_.size(); j<(int)pcu->size; j++) { + for (int j=(int)pcu->data_.size(); j < (int)pcu->size; j++) { m->numeric_data[adr+j] = 0; } @@ -3311,7 +3319,7 @@ void mjCModel::CopyObjects(mjModel* m) { // text fields adr = 0; - for (int i=0; ituple_adr[i] = adr; m->tuple_size[i] = (int)ptu->objtype_.size(); - for (int j=0; jtuple_size[i]; j++) { + for (int j=0; j < m->tuple_size[i]; j++) { m->tuple_objtype[adr+j] = (int)ptu->objtype_[j]; m->tuple_objid[adr+j] = ptu->obj[j]->id; m->tuple_objprm[adr+j] = (mjtNum)ptu->objprm_[j]; @@ -3344,7 +3352,7 @@ void mjCModel::CopyObjects(mjModel* m) { } // copy keyframe data - for (int i=0; ikey_time[i] = (mjtNum)keys_[i]->time; mjuu_copyvec(m->key_qpos+i*nq, keys_[i]->qpos_.data(), nq); @@ -3358,14 +3366,14 @@ void mjCModel::CopyObjects(mjModel* m) { } // normalize quaternions in m->key_qpos - for (int j=0; jnjnt; j++) { - if (m->jnt_type[j]==mjJNT_BALL || m->jnt_type[j]==mjJNT_FREE) { - mjuu_normvec(m->key_qpos+i*nq+m->jnt_qposadr[j]+3*(m->jnt_type[j]==mjJNT_FREE), 4); + for (int j=0; j < m->njnt; j++) { + if (m->jnt_type[j] == mjJNT_BALL || m->jnt_type[j] == mjJNT_FREE) { + mjuu_normvec(m->key_qpos+i*nq+m->jnt_qposadr[j]+3*(m->jnt_type[j] == mjJNT_FREE), 4); } } // normalize quaternions in m->key_mquat - for (int j=0; jkey_mquat+i*4*nmocap+4*j, 4); } @@ -3399,7 +3407,7 @@ void mjCModel::SaveState(const std::string& state_name, const T* qpos, const T* } } - for (unsigned int i=0; iactadr_ != -1 && actuator->actdim_ != -1 && act) { actuator->act(state_name).assign(actuator->actdim_, 0); @@ -3456,7 +3464,7 @@ void mjCModel::RestoreState(const std::string& state_name, const mjtNum* pos0, } // restore act - for (unsigned int i=0; iact(state_name).empty() && mjuu_defined(actuator->act(state_name)[0]) && act) { mjuu_copyvec(act + actuator->actadr_, actuator->act(state_name).data(), actuator->actdim_); @@ -3466,7 +3474,7 @@ void mjCModel::RestoreState(const std::string& state_name, const mjtNum* pos0, } } - for (unsigned int i=0; ispec.mocap) { continue; @@ -3490,12 +3498,12 @@ void mjCModel::RestoreState(const std::string& state_name, const mjtNum* pos0, // force explicit instantiations template void mjCModel::SaveState( - const std::string& name, const mjtNum* qpos, const mjtNum* qvel, const mjtNum* act, - const mjtNum* ctrl, const mjtNum* mpos, const mjtNum* mquat); + const std::string& name, const mjtNum* qpos, const mjtNum* qvel, const mjtNum* act, + const mjtNum* ctrl, const mjtNum* mpos, const mjtNum* mquat); template void mjCModel::RestoreState( - const std::string& name, const mjtNum* qpos0, const mjtNum* mpos0, const mjtNum* mquat0, - mjtNum* qpos, mjtNum* qvel, mjtNum* act, mjtNum* ctrl, mjtNum* mpos, mjtNum* mquat); + const std::string& name, const mjtNum* qpos0, const mjtNum* mpos0, const mjtNum* mquat0, + mjtNum* qpos, mjtNum* qvel, mjtNum* act, mjtNum* ctrl, mjtNum* mpos, mjtNum* mquat); @@ -3505,8 +3513,8 @@ void mjCModel::StoreKeyframes(mjCModel* dest) { if (this != dest && !key_pending_.empty()) { mju_warning( - "Child model has pending keyframes. They will not be namespaced correctly. " - "To prevent this, compile the child model before attaching it again."); + "Child model has pending keyframes. They will not be namespaced correctly. " + "To prevent this, compile the child model before attaching it again."); } // create tree lists if they are empty, occurs if an uncompiled model is attached @@ -3578,7 +3586,7 @@ void mjCModel::StoreKeyframes(mjCModel* dest) { template static void makelistid(std::vector& dest, std::vector& source) { - for (int i=0; iid = (int)dest.size(); dest.push_back(source[i]); } @@ -3600,7 +3608,7 @@ static void changeframe(double childpos[3], double childquat[4], // reindex elements during fuse void mjCModel::FuseReindex(mjCBody* body) { // set parentid and weldid of children - for (int i=0; ibodies.size(); i++) { + for (int i=0; i < body->bodies.size(); i++) { body->bodies[i]->parent = body; body->bodies[i]->weldid = (!body->bodies[i]->joints.empty() ? body->bodies[i]->id : body->weldid); @@ -3611,7 +3619,7 @@ void mjCModel::FuseReindex(mjCBody* body) { makelistid(sites_, body->sites); // process children recursively - for (int i=0; ibodies.size(); i++) { + for (int i=0; i < body->bodies.size(); i++) { FuseReindex(body->bodies[i]); } } @@ -3635,7 +3643,7 @@ void mjCModel::FuseStatic(void) { } // process fusable bodies - for (int i=1; iparent; @@ -3647,7 +3655,7 @@ void mjCModel::FuseStatic(void) { //------------- add mass and inertia (if parent not world) - if (body->parent && body->parent->name != "world" && body->mass>=mjMINVAL) { + if (body->parent && body->parent->name != "world" && body->mass >= mjMINVAL) { // body_ipose = body_pose * body_ipose changeframe(body->ipos, body->iquat, body->pos, body->quat); @@ -3672,7 +3680,7 @@ void mjCModel::FuseStatic(void) { // compute total mass par->mass = 0; mjuu_setvec(par->ipos, 0, 0, 0); - for (int j=0; j<2; j++) { + for (int j=0; j < 2; j++) { par->mass += mass[j]; par->ipos[0] += mass[j]*ipos[j][0]; par->ipos[1] += mass[j]*ipos[j][1]; @@ -3680,7 +3688,7 @@ void mjCModel::FuseStatic(void) { } // small mass: allow for now, check for errors later - if (par->massmass < mjMINVAL) { par->mass = 0; mjuu_setvec(par->inertia, 0, 0, 0); mjuu_setvec(par->ipos, 0, 0, 0); @@ -3696,7 +3704,7 @@ void mjCModel::FuseStatic(void) { // add inertias double toti[6] = {0, 0, 0, 0, 0, 0}; - for (int j=0; j<2; j++) { + for (int j=0; j < 2; j++) { double inertA[6], inertB[6]; double dpos[3] = { ipos[j][0] - par->ipos[0], @@ -3706,7 +3714,7 @@ void mjCModel::FuseStatic(void) { mjuu_globalinertia(inertA, inertia[j], iquat[j]); mjuu_offcenter(inertB, mass[j], dpos); - for (int k=0; k<6; k++) { + for (int k=0; k < 6; k++) { toti[k] += inertA[k] + inertB[k]; } } @@ -3723,14 +3731,14 @@ void mjCModel::FuseStatic(void) { //------------- replace body with its children in parent body list // change frames of child bodies - for (int j=0; jbodies.size(); j++) + for (int j=0; j < body->bodies.size(); j++) changeframe(body->bodies[j]->pos, body->bodies[j]->quat, body->pos, body->quat); // find body in parent list, insert children before it bool found = false; - for (auto iter=par->bodies.begin(); iter!=par->bodies.end(); iter++) { - if (*iter==body) { + for (auto iter=par->bodies.begin(); iter != par->bodies.end(); iter++) { + if (*iter == body) { par->bodies.insert(iter, body->bodies.begin(), body->bodies.end()); found = true; break; @@ -3742,8 +3750,8 @@ void mjCModel::FuseStatic(void) { // find body in parent list, erase found = false; - for (auto iter=par->bodies.begin(); iter!=par->bodies.end(); iter++) { - if (*iter==body) { + for (auto iter=par->bodies.begin(); iter != par->bodies.end(); iter++) { + if (*iter == body) { par->bodies.erase(iter); found = true; break; @@ -3756,7 +3764,7 @@ void mjCModel::FuseStatic(void) { //------------- assign geoms and sites to parent, change frames // geoms - for (int j=0; jgeoms.size(); j++) { + for (int j=0; j < body->geoms.size(); j++) { // assign body->geoms[j]->body = par; par->geoms.push_back(body->geoms[j]); @@ -3766,7 +3774,7 @@ void mjCModel::FuseStatic(void) { } // sites - for (int j=0; jsites.size(); j++) { + for (int j=0; j < body->sites.size(); j++) { // assign body->sites[j]->body = par; par->sites.push_back(body->sites[j]); @@ -3779,8 +3787,8 @@ void mjCModel::FuseStatic(void) { // find in global and erase found = false; - for (auto iter=bodies_.begin(); iter!=bodies_.end(); iter++) { - if (*iter==body) { + for (auto iter=bodies_.begin(); iter != bodies_.end(); iter++) { + if (*iter == body) { bodies_.erase(iter); found = true; break; @@ -3797,7 +3805,7 @@ void mjCModel::FuseStatic(void) { //------------- re-index bodies, joints, geoms, sites // body ids - for (int j=0; jid = j; } @@ -3851,7 +3859,7 @@ static int compareBodyPair(mjCBodyPair* el1, mjCBodyPair* el2) { // reassign ids template static void reassignid(vector& list) { - for (int i=0; i<(int)list.size(); i++) { + for (int i=0; i < (int)list.size(); i++) { list[i]->id = i; } } @@ -3865,7 +3873,7 @@ static void processlist(mjListKeyMap& ids, vector& list, if (type < mjNOBJECT) { for (size_t i=0; i < list.size(); i++) { // check for incompatible id setting; SHOULD NOT OCCUR - if (list[i]->id!=-1 && list[i]->id!=i) { + if (list[i]->id != -1 && list[i]->id != i) { throw mjCError(list[i], "incompatible id in %s array, position %d", mju_type2Str(type), i); } @@ -3942,7 +3950,7 @@ static void warninghandler(const char* msg) { mjModel* mjCModel::Compile(const mjVFS* vfs, mjModel** m) { if (compiled) { // clear kinematic tree - for (int i=0; isubtreedofs = 0; } mjCBody* world = bodies_[0]; @@ -4108,22 +4116,22 @@ void mjCModel::ComputeReference() { mjuu_copyvec(body_quat0.data()+4*b, body->spec.quat, 4); for (auto joint : body->joints) { switch (joint->type) { - case mjJNT_FREE: - mjuu_copyvec(qpos0.data()+joint->qposadr_, body->spec.pos, 3); - mjuu_copyvec(qpos0.data()+joint->qposadr_+3, body->spec.quat, 4); - break; + case mjJNT_FREE: + mjuu_copyvec(qpos0.data()+joint->qposadr_, body->spec.pos, 3); + mjuu_copyvec(qpos0.data()+joint->qposadr_+3, body->spec.quat, 4); + break; - case mjJNT_BALL: - mjuu_setvec(qpos0.data()+joint->qposadr_, 1, 0, 0, 0); - break; + case mjJNT_BALL: + mjuu_setvec(qpos0.data()+joint->qposadr_, 1, 0, 0, 0); + break; - case mjJNT_SLIDE: - case mjJNT_HINGE: - qpos0[joint->qposadr_] = (mjtNum)joint->spec.ref; - break; + case mjJNT_SLIDE: + case mjJNT_HINGE: + qpos0[joint->qposadr_] = (mjtNum)joint->spec.ref; + break; - default: - throw mjCError(joint, "unknown joint type"); + default: + throw mjCError(joint, "unknown joint type"); } } b++; @@ -4138,7 +4146,7 @@ void mjCModel::ResizeKeyframe(mjCKey* key, const mjtNum* qpos0_, if (!key->spec_qpos_.empty()) { int nq0 = key->spec_qpos_.size(); key->spec_qpos_.resize(nq); - for (int i=nq0; ispec_qpos_[i] = (double)qpos0_[i]; } } @@ -4221,26 +4229,26 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { } // check for too many body+flex - if (bodies_.size()+flexes_.size()>=65534) { + if (bodies_.size()+flexes_.size() >= 65534) { throw mjCError(0, "number of bodies plus flexes must be less than 65534"); } // append directory separator if (!meshdir_.empty()) { int n = meshdir_.length(); - if (meshdir_[n-1]!='/' && meshdir_[n-1]!='\\') { + if (meshdir_[n-1] != '/' && meshdir_[n-1] != '\\') { meshdir_ += '/'; } } if (!texturedir_.empty()) { int n = texturedir_.length(); - if (texturedir_[n-1]!='/' && texturedir_[n-1]!='\\') { + if (texturedir_[n-1] != '/' && texturedir_[n-1] != '\\') { texturedir_ += '/'; } } // add missing keyframes - for (int i=keys_.size(); imesh && (geoms_[i]->spec.type == mjGEOM_MESH || geoms_[i]->spec.type == mjGEOM_SDF) && (geoms_[i]->spec.contype || geoms_[i]->spec.conaffinity || @@ -4299,7 +4307,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { } // compile objects in kinematic tree - for (int i=0; iCompile(); // also compiles joints, geoms, sites, cameras, lights, frames } @@ -4384,7 +4392,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { // keyframe compilation needs access to nq, nv, na, nmocap, qpos0 ResolveKeyframes(m); - for (int i=0; iCompile(m); } @@ -4395,7 +4403,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { m->nJmom = nJmom = CountNJmom(m); // scale mass - if (compiler.settotalmass>0) { + if (compiler.settotalmass > 0) { mj_setTotalmass(m, compiler.settotalmass); } @@ -4420,11 +4428,11 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { // add an arena space equal to memory footprint prior to the introduction of the arena const std::size_t arena_bytes = ( - nconmax * sizeof(mjContact) + - njmax * (8 * sizeof(int) + 14 * sizeof(mjtNum)) + - m->nv * (3 * sizeof(int)) + - njmax * m->nv * (2 * sizeof(int) + 2 * sizeof(mjtNum)) + - njmax * njmax * (sizeof(int) + sizeof(mjtNum))); + nconmax * sizeof(mjContact) + + njmax * (8 * sizeof(int) + 14 * sizeof(mjtNum)) + + m->nv * (3 * sizeof(int)) + + njmax * m->nv * (2 * sizeof(int) + 2 * sizeof(mjtNum)) + + njmax * njmax * (sizeof(int) + sizeof(mjtNum))); m->narena += arena_bytes; // round up to the nearest megabyte @@ -4445,7 +4453,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { mj_resetData(m, d); // normalize keyframe quaternions - for (int i=0; inkey; i++) { + for (int i=0; i < m->nkey; i++) { mj_normalizeQuat(m, m->key_qpos+i*m->nq); } @@ -4507,7 +4515,7 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) { bool mjCModel::CheckBodiesMassInertia(std::vector bodies) { // check mass and inertia of moving bodies - for (int i=0; ijoints.empty()) { if (!CheckBodyMassInertia(bodies[i])) { return false; @@ -4521,15 +4529,15 @@ bool mjCModel::CheckBodiesMassInertia(std::vector bodies) { bool mjCModel::CheckBodyMassInertia(mjCBody* body) { // check if body has valid mass and inertia - if (body->mass>=mjMINVAL && - body->inertia[0]>=mjMINVAL && - body->inertia[1]>=mjMINVAL && - body->inertia[2]>=mjMINVAL) { - return true; - } + if (body->mass >= mjMINVAL && + body->inertia[0] >= mjMINVAL && + body->inertia[1] >= mjMINVAL && + body->inertia[2] >= mjMINVAL) { + return true; + } // body is valid if we find a single static child with valid mass and inertia - for (int i=0; iBodies().size(); i++) { + for (int i=0; i < body->Bodies().size(); i++) { // if we find a child with a joint, time to move on to the next moving body if (!body->Bodies()[i]->joints.empty()) { continue; @@ -4562,16 +4570,17 @@ bool mjCModel::CopyBack(const mjModel* m) { } // make sure sizes match - if (nq!=m->nq || nv!=m->nv || nu!=m->nu || na!=m->na || - nbody!=m->nbody ||njnt!=m->njnt || ngeom!=m->ngeom || nsite!=m->nsite || - ncam!=m->ncam || nlight != m->nlight || nmesh!=m->nmesh || - nskin!=m->nskin || nhfield!=m->nhfield || - nmat != m->nmat || ntex != m->ntex || npair!=m->npair || nexclude!=m->nexclude || - neq!=m->neq || ntendon!=m->ntendon || nwrap!=m->nwrap || nsensor!=m->nsensor || - nnumeric!=m->nnumeric || nnumericdata!=m->nnumericdata || ntext!=m->ntext || - ntextdata!=m->ntextdata || nnames!=m->nnames || nM!=m->nM || nD!=m->nD || nC!=m->nC || - nB!=m->nB || nJmom!=m->nJmom ||nemax!=m->nemax || nconmax!=m->nconmax || njmax!=m->njmax || - npaths!=m->npaths) { + if (nq != m->nq || nv != m->nv || nu != m->nu || na != m->na || + nbody != m->nbody ||njnt != m->njnt || ngeom != m->ngeom || nsite != m->nsite || + ncam != m->ncam || nlight != m->nlight || nmesh != m->nmesh || + nskin != m->nskin || nhfield != m->nhfield || + nmat != m->nmat || ntex != m->ntex || npair != m->npair || nexclude != m->nexclude || + neq != m->neq || ntendon != m->ntendon || nwrap != m->nwrap || nsensor != m->nsensor || + nnumeric != m->nnumeric || nnumericdata != m->nnumericdata || ntext != m->ntext || + ntextdata != m->ntextdata || nnames != m->nnames || + nM != m->nM || nD != m->nD || nC != m->nC || nB != m->nB || nJmom != m->nJmom || + nemax != m->nemax || nconmax != m->nconmax || njmax != m->njmax || + npaths != m->npaths) { errInfo = mjCError(0, "incompatible models in CopyBack"); return false; } @@ -4592,29 +4601,29 @@ bool mjCModel::CopyBack(const mjModel* m) { } // qpos0, qpos_spring - for (int i=0; itype) { - case mjJNT_FREE: - mjuu_copyvec(bodies_[m->jnt_bodyid[i]]->pos, m->qpos0+m->jnt_qposadr[i], 3); - mjuu_copyvec(bodies_[m->jnt_bodyid[i]]->quat, m->qpos0+m->jnt_qposadr[i]+3, 4); - break; + case mjJNT_FREE: + mjuu_copyvec(bodies_[m->jnt_bodyid[i]]->pos, m->qpos0+m->jnt_qposadr[i], 3); + mjuu_copyvec(bodies_[m->jnt_bodyid[i]]->quat, m->qpos0+m->jnt_qposadr[i]+3, 4); + break; - case mjJNT_SLIDE: - case mjJNT_HINGE: - joints_[i]->ref = (double)m->qpos0[m->jnt_qposadr[i]]; - joints_[i]->springref = (double)m->qpos_spring[m->jnt_qposadr[i]]; - break; + case mjJNT_SLIDE: + case mjJNT_HINGE: + joints_[i]->ref = (double)m->qpos0[m->jnt_qposadr[i]]; + joints_[i]->springref = (double)m->qpos_spring[m->jnt_qposadr[i]]; + break; - case mjJNT_BALL: - // nothing to do, qpos = unit quaternion always - break; + case mjJNT_BALL: + // nothing to do, qpos = unit quaternion always + break; } } mjuu_copyvec(qpos0.data(), m->qpos0, m->nq); // body mjCBody* pb; - for (int i=0; ipos, m->body_pos+3*i, 3); @@ -4631,7 +4640,7 @@ bool mjCModel::CopyBack(const mjModel* m) { // joint and dof mjCJoint* pj; - for (int i=0; isize, m->geom_size+3*i, 3); @@ -4679,7 +4688,7 @@ bool mjCModel::CopyBack(const mjModel* m) { // mesh mjCMesh* pm; - for (int i=0; iGetPosPtr(), m->mesh_pos+3*i, 3); mjuu_copyvec(pm->GetQuatPtr(), m->mesh_quat+4*i, 4); @@ -4687,7 +4696,7 @@ bool mjCModel::CopyBack(const mjModel* m) { // heightfield mjCHField* phf; - for (int i=0; iget_userdata().size(); if (size) { @@ -4696,7 +4705,7 @@ bool mjCModel::CopyBack(const mjModel* m) { float* userdata = phf->get_userdata().data(); float* modeldata = m->hfield_data + m->hfield_adr[i]; // copy back in reverse row order - for (int j=0; jsize, m->site_size + 3 * i, 3); mjuu_copyvec(sites_[i]->pos, m->site_pos+3*i, 3); mjuu_copyvec(sites_[i]->quat, m->site_quat+4*i, 4); @@ -4716,7 +4725,7 @@ bool mjCModel::CopyBack(const mjModel* m) { } // cameras - for (int i=0; ipos, m->cam_pos+3*i, 3); mjuu_copyvec(cameras_[i]->quat, m->cam_quat+4*i, 4); cameras_[i]->fovy = (double)m->cam_fovy[i]; @@ -4730,7 +4739,7 @@ bool mjCModel::CopyBack(const mjModel* m) { } // lights - for (int i=0; ipos, m->light_pos+3*i, 3); mjuu_copyvec(lights_[i]->dir, m->light_dir+3*i, 3); mjuu_copyvec(lights_[i]->attenuation, m->light_attenuation+3*i, 3); @@ -4742,7 +4751,7 @@ bool mjCModel::CopyBack(const mjModel* m) { } // materials - for (int i=0; itexrepeat, m->mat_texrepeat+2*i, 2); materials_[i]->emission = m->mat_emission[i]; materials_[i]->specular = m->mat_specular[i]; @@ -4752,7 +4761,7 @@ bool mjCModel::CopyBack(const mjModel* m) { } // pairs - for (int i=0; isolref, m->pair_solref+mjNREF*i, mjNREF); mjuu_copyvec(pairs_[i]->solreffriction, m->pair_solreffriction+mjNREF*i, mjNREF); mjuu_copyvec(pairs_[i]->solimp, m->pair_solimp+mjNIMP*i, mjNIMP); @@ -4762,14 +4771,14 @@ bool mjCModel::CopyBack(const mjModel* m) { } // equality constraints - for (int i=0; idata, m->eq_data+mjNEQDATA*i, mjNEQDATA); mjuu_copyvec(equalities_[i]->solref, m->eq_solref+mjNREF*i, mjNREF); mjuu_copyvec(equalities_[i]->solimp, m->eq_solimp+mjNIMP*i, mjNIMP); } // tendons - for (int i=0; irange, m->tendon_range+2*i, 2); mjuu_copyvec(tendons_[i]->solref_limit, m->tendon_solref_lim+mjNREF*i, mjNREF); mjuu_copyvec(tendons_[i]->solimp_limit, m->tendon_solimp_lim+mjNIMP*i, mjNIMP); @@ -4789,7 +4798,7 @@ bool mjCModel::CopyBack(const mjModel* m) { // actuators mjCActuator* pa; - for (int i=0; idynprm, m->actuator_dynprm+i*mjNDYN, mjNDYN); @@ -4808,7 +4817,7 @@ bool mjCModel::CopyBack(const mjModel* m) { } // sensors - for (int i=0; icutoff = (double)m->sensor_cutoff[i]; sensors_[i]->noise = (double)m->sensor_noise[i]; @@ -4818,21 +4827,21 @@ bool mjCModel::CopyBack(const mjModel* m) { } // numeric data - for (int i=0; inumeric_size[i]; j++) { + for (int i=0; i < nnumeric; i++) { + for (int j=0; j < m->numeric_size[i]; j++) { numerics_[i]->data_[j] = (double)m->numeric_data[m->numeric_adr[i]+j]; } } // tuple data - for (int i=0; ituple_size[i]; j++) { + for (int i=0; i < ntuple; i++) { + for (int j=0; j < m->tuple_size[i]; j++) { tuples_[i]->objprm_[j] = (double)m->tuple_objprm[m->tuple_adr[i]+j]; } } // keyframes - for (int i=0; inkey; i++) { + for (int i=0; i < m->nkey; i++) { mjCKey* pk = keys_[i]; pk->time = (double)m->key_time[i]; @@ -4902,22 +4911,22 @@ void mjCModel::ResolvePlugin(mjCBase* obj, const std::string& plugin_name, // implicit plugin instance if (*plugin_instance && (*plugin_instance)->plugin_slot == -1) { - (*plugin_instance)->plugin_slot = plugin_slot; - (*plugin_instance)->parent = obj; + (*plugin_instance)->plugin_slot = plugin_slot; + (*plugin_instance)->parent = obj; } // explicit plugin instance, look up existing mjCPlugin by instance name else if (!*plugin_instance) { *plugin_instance = - static_cast(FindObject(mjOBJ_PLUGIN, plugin_instance_name)); + static_cast(FindObject(mjOBJ_PLUGIN, plugin_instance_name)); (*plugin_instance)->plugin_slot = plugin_slot; if (!*plugin_instance) { throw mjCError( - obj, "unrecognized name '%s' for plugin instance", plugin_instance_name.c_str()); + obj, "unrecognized name '%s' for plugin instance", plugin_instance_name.c_str()); } if (plugin_slot != -1 && plugin_slot != (*plugin_instance)->plugin_slot) { throw mjCError( - obj, "'plugin' attribute does not match that of the instance"); + obj, "'plugin' attribute does not match that of the instance"); } plugin_slot = (*plugin_instance)->plugin_slot; } diff --git a/src/user/user_objects.cc b/src/user/user_objects.cc index 68288655..59397955 100644 --- a/src/user/user_objects.cc +++ b/src/user/user_objects.cc @@ -77,11 +77,11 @@ PNGImage PNGImage::Load(const mjCBase* obj, mjResource* resource, // try loading from cache if (cache && cache->PopulateData(resource, [&image](const void* data) { - const PNGImage *cached_image = static_cast(data); - if (cached_image->color_type_ == image.color_type_) { - image = *cached_image; - } - })) { + const PNGImage *cached_image = static_cast(data); + if (cached_image->color_type_ == image.color_type_) { + image = *cached_image; + } + })) { if (!image.data_.empty()) return image; } @@ -122,9 +122,9 @@ PNGImage PNGImage::Load(const mjCBase* obj, mjResource* resource, if (cache) { PNGImage *cached_image = new PNGImage(image);; std::size_t size = image.Size(); - std::shared_ptr cached_data(cached_image, +[](const void* data) { - delete static_cast(data); - }); + std::shared_ptr cached_data(cached_image, +[] (const void* data) { + delete static_cast(data); + }); cache->Insert("", resource, cached_data, size); } @@ -136,9 +136,9 @@ template void MapFrame(std::vector& parent, std::vector& child, mjCFrame* frame, mjCBody* parent_body) { std::for_each(child.begin(), child.end(), [frame, parent_body](T* element) { - element->SetFrame(frame); - element->SetParent(parent_body); - }); + element->SetFrame(frame); + element->SetParent(parent_body); + }); parent.insert(parent.end(), child.begin(), child.end()); child.clear(); } @@ -149,16 +149,16 @@ void MapFrame(std::vector& parent, std::vector& child, // utiility function for checking size parameters static void checksize(double* size, mjtGeom type, mjCBase* object, const char* name, int id) { // plane: handle infinite - if (type==mjGEOM_PLANE) { - if (size[2]<=0) { + if (type == mjGEOM_PLANE) { + if (size[2] <= 0) { throw mjCError(object, "plane size(3) must be positive"); } } // regular geom else { - for (int i=0; i::iterator elements_begin, - std::vector::iterator elements_end, int lev) { + std::vector::iterator elements_begin, + std::vector::iterator elements_end, int lev) { int nelements = elements_end - elements_begin; if (nelements == 0) { return -1; @@ -597,7 +597,7 @@ mjCDef& mjCDef::operator=(const mjCDef& other) { mjCDef& mjCDef::operator+=(const mjCDef& other) { - for (unsigned int i=0; iparent = this; } @@ -846,7 +846,7 @@ mjCBody& mjCBody::operator=(const mjCBody& other) { mjCBody& mjCBody::operator+=(const mjCBody& other) { // map other frames to indices std::map fmap; - for (int i=0; iparent = this; bodies.back()->frame = - other.bodies[i]->frame ? frames[fmap[other.bodies[i]->frame]] : nullptr; + other.bodies[i]->frame ? frames[fmap[other.bodies[i]->frame]] : nullptr; } return *this; @@ -935,7 +935,7 @@ mjCBody& mjCBody::operator+=(const mjCFrame& other) { } int nbodies = (int)subtree->bodies.size(); - for (int i=0; ibodies[i]->frame)) { continue; } @@ -952,7 +952,7 @@ mjCBody& mjCBody::operator+=(const mjCFrame& other) { } bodies.back()->parent = this; bodies.back()->frame = - subtree->bodies[i]->frame ? frames[fmap[subtree->bodies[i]->frame]] : nullptr; + subtree->bodies[i]->frame ? frames[fmap[subtree->bodies[i]->frame]] : nullptr; } // attach referencing elements @@ -978,7 +978,7 @@ void mjCBody::CopyList(std::vector& dst, const std::vector& src, std::map& fmap, const mjCFrame* pframe) { int nsrc = (int)src.size(); int ndst = (int)dst.size(); - for (int i=0; iIsAncestor(src[i]->frame)) { continue; // skip if the element is not inside pframe } @@ -1016,7 +1016,7 @@ void mjCBody::CopyList(std::vector& dst, const std::vector& src, // find and remove subtree mjCBody& mjCBody::operator-=(const mjCBody& subtree) { - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); - for (int i=0; iRelease(); + for (int i=0; i < bodies.size(); i++) bodies[i]->Release(); + for (int i=0; i < geoms.size(); i++) geoms[i]->Release(); + for (int i=0; i < frames.size(); i++) frames[i]->Release(); + for (int i=0; i < joints.size(); i++) joints[i]->Release(); + for (int i=0; i < sites.size(); i++) sites[i]->Release(); + for (int i=0; i < cameras.size(); i++) cameras[i]->Release(); + for (int i=0; i < lights.size(); i++) lights[i]->Release(); } @@ -1333,45 +1333,45 @@ mjCFrame* mjCBody::ToFrame() { // get number of objects of specified type int mjCBody::NumObjects(mjtObj type) { switch (type) { - case mjOBJ_BODY: - case mjOBJ_XBODY: - return (int)bodies.size(); - case mjOBJ_JOINT: - return (int)joints.size(); - case mjOBJ_GEOM: - return (int)geoms.size(); - case mjOBJ_SITE: - return (int)sites.size(); - case mjOBJ_CAMERA: - return (int)cameras.size(); - case mjOBJ_LIGHT: - return (int)lights.size(); - default: - return 0; + case mjOBJ_BODY: + case mjOBJ_XBODY: + return (int)bodies.size(); + case mjOBJ_JOINT: + return (int)joints.size(); + case mjOBJ_GEOM: + return (int)geoms.size(); + case mjOBJ_SITE: + return (int)sites.size(); + case mjOBJ_CAMERA: + return (int)cameras.size(); + case mjOBJ_LIGHT: + return (int)lights.size(); + default: + return 0; } } // get poiner to specified object -mjCBase* mjCBody::GetObject(mjtObj type, int id) { - if (id>=0 && id= 0 && i < NumObjects(type)) { switch (type) { - case mjOBJ_BODY: - case mjOBJ_XBODY: - return bodies[id]; - case mjOBJ_JOINT: - return joints[id]; - case mjOBJ_GEOM: - return geoms[id]; - case mjOBJ_SITE: - return sites[id]; - case mjOBJ_CAMERA: - return cameras[id]; - case mjOBJ_LIGHT: - return lights[id]; - default: - return 0; + case mjOBJ_BODY: + case mjOBJ_XBODY: + return bodies[i]; + case mjOBJ_JOINT: + return joints[i]; + case mjOBJ_GEOM: + return geoms[i]; + case mjOBJ_SITE: + return sites[i]; + case mjOBJ_CAMERA: + return cameras[i]; + case mjOBJ_LIGHT: + return lights[i]; + default: + return 0; } } @@ -1383,7 +1383,7 @@ mjCBase* mjCBody::GetObject(mjtObj type, int id) { // find object by name in given list template static T* findobject(std::string name, std::vector& list) { - for (unsigned int i=0; iname == name) { return list[i]; } @@ -1404,17 +1404,17 @@ mjCBase* mjCBody::FindObject(mjtObj type, std::string _name, bool recursive) { } // search elements of this body - if (type==mjOBJ_BODY || type==mjOBJ_XBODY) { + if (type == mjOBJ_BODY || type == mjOBJ_XBODY) { res = findobject(_name, bodies); - } else if (type==mjOBJ_JOINT) { + } else if (type == mjOBJ_JOINT) { res = findobject(_name, joints); - } else if (type==mjOBJ_GEOM) { + } else if (type == mjOBJ_GEOM) { res = findobject(_name, geoms); - } else if (type==mjOBJ_SITE) { + } else if (type == mjOBJ_SITE) { res = findobject(_name, sites); - } else if (type==mjOBJ_CAMERA) { + } else if (type == mjOBJ_CAMERA) { res = findobject(_name, cameras); - } else if (type==mjOBJ_LIGHT) { + } else if (type == mjOBJ_LIGHT) { res = findobject(_name, lights); } @@ -1425,7 +1425,7 @@ mjCBase* mjCBody::FindObject(mjtObj type, std::string _name, bool recursive) { // search children if (recursive) { - for (int i=0; i<(int)bodies.size(); i++) { + for (int i=0; i < (int)bodies.size(); i++) { if ((res = bodies[i]->FindObject(type, _name, true))) { return res; } @@ -1517,7 +1517,7 @@ mjsElement* mjCBody::NextChild(const mjsElement* child, mjtObj type, bool recurs } if (!candidate && recursive) { - for (int i=0; i<(int)bodies.size(); i++) { + for (int i=0; i < (int)bodies.size(); i++) { candidate = bodies[i]->NextChild(*found ? nullptr : child, type, recursive, found); if (candidate) { return candidate; @@ -1539,16 +1539,16 @@ void mjCBody::InertiaFromGeom(void) { // select geoms based on group sel.clear(); - for (int i=0; igroup>=compiler->inertiagrouprange[0] && - geoms[i]->group<=compiler->inertiagrouprange[1]) { + for (int i=0; i < geoms.size(); i++) { + if (geoms[i]->group >= compiler->inertiagrouprange[0] && + geoms[i]->group <= compiler->inertiagrouprange[1]) { sel.push_back(geoms[i]); } } sz = sel.size(); // single geom: copy - if (sz==1) { + if (sz == 1) { mjuu_copyvec(ipos, sel[0]->pos, 3); mjuu_copyvec(iquat, sel[0]->quat, 4); mass = sel[0]->mass_; @@ -1556,10 +1556,10 @@ void mjCBody::InertiaFromGeom(void) { } // multiple geoms - else if (sz>1) { + else if (sz > 1) { // compute total mass and center of mass mass = 0; - for (int i=0; imass_; com[0] += sel[i]->mass_ * sel[i]->pos[0]; com[1] += sel[i]->mass_ * sel[i]->pos[1]; @@ -1567,7 +1567,7 @@ void mjCBody::InertiaFromGeom(void) { } // check for small mass - if (masspos[0] - ipos[0], @@ -1587,7 +1587,7 @@ void mjCBody::InertiaFromGeom(void) { mjuu_globalinertia(inert0, sel[i]->inertia, sel[i]->quat); mjuu_offcenter(inert1, sel[i]->mass_, dpos); - for (int j=0; j<6; j++) { + for (int j=0; j < 6; j++) { toti[j] = toti[j] + inert0[j] + inert1[j]; } } @@ -1665,7 +1665,7 @@ void mjCBody::Compile(void) { CopyFromSpec(); // compile all frames - for (int i=0; iCompile(); } @@ -1680,7 +1680,7 @@ void mjCBody::Compile(void) { mjuu_normvec(iquat, 4); // set parentid and weldid of children - for (int i=0; iweldid = (!bodies[i]->joints.empty() ? bodies[i]->id : weldid); } @@ -1716,8 +1716,8 @@ void mjCBody::Compile(void) { } // compile all geoms - for (int i=0; iinferinertia = id>0 && + for (int i=0; i < geoms.size(); i++) { + geoms[i]->inferinertia = id > 0 && (!explicitinertial || compiler->inertiafromgeom == mjINERTIAFROMGEOM_TRUE) && geoms[i]->spec.group >= compiler->inertiagrouprange[0] && geoms[i]->spec.group <= compiler->inertiagrouprange[1]; @@ -1725,8 +1725,8 @@ void mjCBody::Compile(void) { } // set inertial frame from geoms if necessary - if (id>0 && (compiler->inertiafromgeom==mjINERTIAFROMGEOM_TRUE || - (!mjuu_defined(ipos[0]) && compiler->inertiafromgeom==mjINERTIAFROMGEOM_AUTO))) { + if (id > 0 && (compiler->inertiafromgeom == mjINERTIAFROMGEOM_TRUE || + (!mjuu_defined(ipos[0]) && compiler->inertiafromgeom == mjINERTIAFROMGEOM_AUTO))) { InertiaFromGeom(); } @@ -1737,7 +1737,7 @@ void mjCBody::Compile(void) { } // check and correct mass and inertia - if (id>0) { + if (id > 0) { // fix minimum mass = std::max(mass, compiler->boundmass); inertia[0] = std::max(inertia[0], compiler->boundinertia); @@ -1745,7 +1745,7 @@ void mjCBody::Compile(void) { inertia[2] = std::max(inertia[2], compiler->boundinertia); // check for negative values - if (mass<0 || inertia[0]<0 || inertia[1]<0 ||inertia[2]<0) { + if (mass < 0 || inertia[0] < 0 || inertia[1] < 0 ||inertia[2] < 0) { throw mjCError(this, "mass and inertia cannot be negative"); } @@ -1769,7 +1769,7 @@ void mjCBody::Compile(void) { // accumulate rbound, contype, conaffinity over geoms contype = conaffinity = 0; margin = 0; - for (int i=0; icontype; conaffinity |= geoms[i]->conaffinity; margin = std::max(margin, geoms[i]->margin); @@ -1797,7 +1797,7 @@ void mjCBody::Compile(void) { mjuu_setvec(iquat, 1, 0, 0, 0); // apply inverse iframe transformation to all child geoms - for (int i=0; ipos, geoms[i]->quat); } } @@ -1807,22 +1807,22 @@ void mjCBody::Compile(void) { // compile all joints, count dofs dofnum = 0; - for (int i=0; iCompile(); } // check for excessive number of dofs - if (dofnum>6) { + if (dofnum > 6) { throw mjCError(this, "more than 6 dofs in body '%s'", name.c_str()); } // check for rotation dof after ball joint bool hasball = false; - for (int i=0; itype==mjJNT_BALL || joints[i]->type==mjJNT_HINGE) && hasball) { + for (int i=0; i < joints.size(); i++) { + if ((joints[i]->type == mjJNT_BALL || joints[i]->type == mjJNT_HINGE) && hasball) { throw mjCError(this, "ball followed by rotation in body '%s'", name.c_str()); } - if (joints[i]->type==mjJNT_BALL) { + if (joints[i]->type == mjJNT_BALL) { hasball = true; } } @@ -1833,27 +1833,27 @@ void mjCBody::Compile(void) { } // compute body global pose (no joint transformations in qpos0) - if (id>0) { + if (id > 0) { mjuu_rotVecQuat(xpos0, pos, parent->xquat0); mjuu_addtovec(xpos0, parent->xpos0, 3); mjuu_mulquat(xquat0, parent->xquat0, quat); } // compile all sites - for (int i=0; iCompile(); + for (int i=0; i < sites.size(); i++)sites[i]->Compile(); // compile all cameras - for (int i=0; iCompile(); + for (int i=0; i < cameras.size(); i++)cameras[i]->Compile(); // compile all lights - for (int i=0; iCompile(); + for (int i=0; i < lights.size(); i++)lights[i]->Compile(); // plugin if (plugin.active) { if (plugin_name.empty() && plugin_instance_name.empty()) { throw mjCError( - this, "neither 'plugin' nor 'instance' is specified for body '%s', (id = %d)", - name.c_str(), id); + this, "neither 'plugin' nor 'instance' is specified for body '%s', (id = %d)", + name.c_str(), id); } mjCPlugin* plugin_instance = static_cast(plugin.element); @@ -1867,7 +1867,7 @@ void mjCBody::Compile(void) { // if discarding visual geoms, use explicit inertias if (compiler->discardvisual) { - for (int j=0; jIsVisual()) { explicitinertial = true; break; @@ -1880,17 +1880,17 @@ void mjCBody::Compile(void) { // frames have already been compiled and applied to children // sites - for (int i=0; ipos, sites[i]->quat); } // cameras - for (int i=0; ipos, cameras[i]->quat); } // lights - for (int i=0; ipos, qunit); mjuu_rotVecQuat(lights[i]->dir, lights[i]->dir, iquat_inverse); @@ -2103,8 +2103,12 @@ mjCJoint& mjCJoint::operator=(const mjCJoint& other) { -bool mjCJoint::is_limited() const { return islimited(limited, range); } -bool mjCJoint::is_actfrclimited() const { return islimited(actfrclimited, actfrcrange); } +bool mjCJoint::is_limited() const { + return islimited(limited, range); +} +bool mjCJoint::is_actfrclimited() const { + return islimited(actfrclimited, actfrcrange); +} @@ -2185,33 +2189,33 @@ int mjCJoint::Compile(void) { // check springdamper if (springdamper[0] || springdamper[1]) { - if (springdamper[0]<=0 || springdamper[1]<=0) { + if (springdamper[0] <= 0 || springdamper[1] <= 0) { throw mjCError(this, "when defined, springdamper values must be positive in joint"); } } // free joints cannot be limited - if (type==mjJNT_FREE) { + if (type == mjJNT_FREE) { limited = mjLIMITED_FALSE; } // otherwise if limited is auto, check consistency wrt auto-limits else if (limited == mjLIMITED_AUTO) { - bool hasrange = !(range[0]==0 && range[1]==0); + bool hasrange = !(range[0] == 0 && range[1] == 0); checklimited(this, compiler->autolimits, "joint", "", limited, hasrange); } // resolve limits if (is_limited()) { // check data - if (range[0]>=range[1] && type!=mjJNT_BALL) { + if (range[0] >= range[1] && type != mjJNT_BALL) { throw mjCError(this, "range[0] should be smaller than range[1] in joint"); } - if (range[0] && type==mjJNT_BALL) { + if (range[0] && type == mjJNT_BALL) { throw mjCError(this, "range[0] should be 0 in ball joint"); } // convert limits to radians - if (compiler->degree && (type==mjJNT_HINGE || type==mjJNT_BALL)) { + if (compiler->degree && (type == mjJNT_HINGE || type == mjJNT_BALL)) { if (range[0]) { range[0] *= mjPI/180.0; } @@ -2222,25 +2226,25 @@ int mjCJoint::Compile(void) { } // actuator force range: none for free or ball joints - if (type==mjJNT_FREE || type==mjJNT_BALL) { + if (type == mjJNT_FREE || type == mjJNT_BALL) { actfrclimited = mjLIMITED_FALSE; } // otherwise if actfrclimited is auto, check consistency wrt auto-limits else if (actfrclimited == mjLIMITED_AUTO) { - bool hasrange = !(actfrcrange[0]==0 && actfrcrange[1]==0); + bool hasrange = !(actfrcrange[0] == 0 && actfrcrange[1] == 0); checklimited(this, compiler->autolimits, "joint", "", actfrclimited, hasrange); } // resolve actuator force range limits if (is_actfrclimited()) { // check data - if (actfrcrange[0]>=actfrcrange[1]) { + if (actfrcrange[0] >= actfrcrange[1]) { throw mjCError(this, "actfrcrange[0] should be smaller than actfrcrange[1] in joint"); } } // axis: FREE or BALL are fixed to (0,0,1) - if (type==mjJNT_FREE || type==mjJNT_BALL) { + if (type == mjJNT_FREE || type == mjJNT_BALL) { axis[0] = axis[1] = 0; axis[2] = 1; } @@ -2251,12 +2255,12 @@ int mjCJoint::Compile(void) { } // normalize axis, check norm - if (mjuu_normvec(axis, 3)degree) { + if (type == mjJNT_HINGE && compiler->degree) { ref *= mjPI/180.0; springref *= mjPI/180.0; } // return dofnum - if (type==mjJNT_FREE) { + if (type == mjJNT_FREE) { return 6; - } else if (type==mjJNT_BALL) { + } else if (type == mjJNT_BALL) { return 3; } else { return 1; @@ -2411,8 +2415,8 @@ void mjCGeom::NameSpace(const mjCModel* m) { // compute geom volume / surface area double mjCGeom::GetVolume() const { // get from mesh - if (type==mjGEOM_MESH || type==mjGEOM_SDF) { - if (mesh->id<0 || !((std::size_t) mesh->id <= model->Meshes().size())) { + if (type == mjGEOM_MESH || type == mjGEOM_SDF) { + if (mesh->id < 0 || !((std::size_t) mesh->id <= model->Meshes().size())) { throw mjCError(this, "invalid mesh id in mesh geom"); } @@ -2523,7 +2527,7 @@ void mjCGeom::SetInertia(void) { switch (typeinertia) { case mjINERTIA_VOLUME: { double sphere_mass = - mass_ * 4 * radius / (4 * radius + 3 * height); // mass*(sphere_vol/total_vol) + mass_ * 4 * radius / (4 * radius + 3 * height); // mass*(sphere_vol/total_vol) double cylinder_mass = mass_ - sphere_mass; // cylinder part @@ -2717,36 +2721,36 @@ double mjCGeom::GetRBound(void) { double haabb[3] = {0}; switch (type) { - case mjGEOM_HFIELD: - hsize = hfield->size; - return sqrt(hsize[0]*hsize[0] + hsize[1]*hsize[1] + - std::max(hsize[2]*hsize[2], hsize[3]*hsize[3])); + case mjGEOM_HFIELD: + hsize = hfield->size; + return sqrt(hsize[0]*hsize[0] + hsize[1]*hsize[1] + + std::max(hsize[2]*hsize[2], hsize[3]*hsize[3])); - case mjGEOM_SPHERE: - return size[0]; + case mjGEOM_SPHERE: + return size[0]; - case mjGEOM_CAPSULE: - return size[0]+size[1]; + case mjGEOM_CAPSULE: + return size[0]+size[1]; - case mjGEOM_CYLINDER: - return sqrt(size[0]*size[0]+size[1]*size[1]); + case mjGEOM_CYLINDER: + return sqrt(size[0]*size[0]+size[1]*size[1]); - case mjGEOM_ELLIPSOID: - return std::max(std::max(size[0], size[1]), size[2]); + case mjGEOM_ELLIPSOID: + return std::max(std::max(size[0], size[1]), size[2]); - case mjGEOM_BOX: - return sqrt(size[0]*size[0]+size[1]*size[1]+size[2]*size[2]); + case mjGEOM_BOX: + return sqrt(size[0]*size[0]+size[1]*size[1]+size[2]*size[2]); - case mjGEOM_MESH: - case mjGEOM_SDF: - aamm = mesh->aamm(); - haabb[0] = std::max(std::abs(aamm[0]), std::abs(aamm[3])); - haabb[1] = std::max(std::abs(aamm[1]), std::abs(aamm[4])); - haabb[2] = std::max(std::abs(aamm[2]), std::abs(aamm[5])); - return sqrt(haabb[0]*haabb[0] + haabb[1]*haabb[1] + haabb[2]*haabb[2]); + case mjGEOM_MESH: + case mjGEOM_SDF: + aamm = mesh->aamm(); + haabb[0] = std::max(std::abs(aamm[0]), std::abs(aamm[3])); + haabb[1] = std::max(std::abs(aamm[1]), std::abs(aamm[4])); + haabb[2] = std::max(std::abs(aamm[2]), std::abs(aamm[5])); + return sqrt(haabb[0]*haabb[0] + haabb[1]*haabb[1] + haabb[2]*haabb[2]); - default: - return 0; + default: + return 0; } } @@ -2870,47 +2874,47 @@ void mjCGeom::SetFluidCoefs(void) { void mjCGeom::ComputeAABB(void) { double aamm[6]; // axis-aligned bounding box in (min, max) format switch (type) { - case mjGEOM_HFIELD: - aamm[0] = -hfield->size[0]; - aamm[1] = -hfield->size[1]; - aamm[2] = -hfield->size[3]; - aamm[3] = hfield->size[0]; - aamm[4] = hfield->size[1]; - aamm[5] = hfield->size[2]; - break; + case mjGEOM_HFIELD: + aamm[0] = -hfield->size[0]; + aamm[1] = -hfield->size[1]; + aamm[2] = -hfield->size[3]; + aamm[3] = hfield->size[0]; + aamm[4] = hfield->size[1]; + aamm[5] = hfield->size[2]; + break; - case mjGEOM_SPHERE: - aamm[3] = aamm[4] = aamm[5] = size[0]; - mjuu_setvec(aamm, -aamm[3], -aamm[4], -aamm[5]); - break; + case mjGEOM_SPHERE: + aamm[3] = aamm[4] = aamm[5] = size[0]; + mjuu_setvec(aamm, -aamm[3], -aamm[4], -aamm[5]); + break; - case mjGEOM_CAPSULE: - aamm[3] = aamm[4] = size[0]; - aamm[5] = size[0] + size[1]; - mjuu_setvec(aamm, -aamm[3], -aamm[4], -aamm[5]); - break; + case mjGEOM_CAPSULE: + aamm[3] = aamm[4] = size[0]; + aamm[5] = size[0] + size[1]; + mjuu_setvec(aamm, -aamm[3], -aamm[4], -aamm[5]); + break; - case mjGEOM_CYLINDER: - aamm[3] = aamm[4] = size[0]; - aamm[5] = size[1]; - mjuu_setvec(aamm, -aamm[3], -aamm[4], -aamm[5]); - break; + case mjGEOM_CYLINDER: + aamm[3] = aamm[4] = size[0]; + aamm[5] = size[1]; + mjuu_setvec(aamm, -aamm[3], -aamm[4], -aamm[5]); + break; - case mjGEOM_MESH: - case mjGEOM_SDF: - mjuu_copyvec(aamm, mesh->aamm(), 6); - break; + case mjGEOM_MESH: + case mjGEOM_SDF: + mjuu_copyvec(aamm, mesh->aamm(), 6); + break; - case mjGEOM_PLANE: - aamm[0] = aamm[1] = aamm[2] = -mjMAXVAL; - aamm[3] = aamm[4] = mjMAXVAL; - aamm[5] = 0; - break; + case mjGEOM_PLANE: + aamm[0] = aamm[1] = aamm[2] = -mjMAXVAL; + aamm[3] = aamm[4] = mjMAXVAL; + aamm[5] = 0; + break; - default: - mjuu_copyvec(aamm+3, size, 3); - mjuu_setvec(aamm, -size[0], -size[1], -size[2]); - break; + default: + mjuu_copyvec(aamm+3, size, 3); + mjuu_setvec(aamm, -size[0], -size[1], -size[2]); + break; } // convert aamm to aabb (center, size) format @@ -2936,27 +2940,27 @@ void mjCGeom::Compile(void) { userdata_.resize(model->nuser_geom); // check type - if (type<0 || type>=mjNGEOMTYPES) { + if (type < 0 || type >= mjNGEOMTYPES) { throw mjCError(this, "invalid type in geom"); } // check condim - if (condim!=1 && condim!=3 && condim!=4 && condim!=6) { + if (condim != 1 && condim != 3 && condim != 4 && condim != 6) { throw mjCError(this, "invalid condim in geom"); } // check mesh - if ((type==mjGEOM_MESH || type==mjGEOM_SDF) && !mesh) { + if ((type == mjGEOM_MESH || type == mjGEOM_SDF) && !mesh) { throw mjCError(this, "mesh geom '%s' (id = %d) must have valid meshid", name.c_str(), id); } // check hfield - if ((type==mjGEOM_HFIELD && !hfield) || (type != mjGEOM_HFIELD && hfield)) { + if ((type == mjGEOM_HFIELD && !hfield) || (type != mjGEOM_HFIELD && hfield)) { throw mjCError(this, "hfield geom '%s' (id = %d) must have valid hfieldid", name.c_str(), id); } // plane only allowed in static bodies - if (type==mjGEOM_PLANE && body->weldid!=0) { + if (type == mjGEOM_PLANE && body->weldid != 0) { throw mjCError(this, "plane only allowed in static bodies"); } @@ -2969,10 +2973,10 @@ void mjCGeom::Compile(void) { // 'fromto': compute pos, quat, size if (mjuu_defined(fromto[0])) { // check type - if (type!=mjGEOM_CAPSULE && - type!=mjGEOM_CYLINDER && - type!=mjGEOM_ELLIPSOID && - type!=mjGEOM_BOX) { + if (type != mjGEOM_CAPSULE && + type != mjGEOM_CYLINDER && + type != mjGEOM_ELLIPSOID && + type != mjGEOM_BOX) { throw mjCError(this, "fromto requires capsule, cylinder, box or ellipsoid in geom"); } @@ -2988,12 +2992,12 @@ void mjCGeom::Compile(void) { fromto[2]-fromto[5] }; size[1] = mjuu_normvec(vec, 3)/2; - if (size[1]GetPosPtr(), meshpos, 3); } else if (typeinertia == mjINERTIA_SHELL) { - throw mjCError(this, "for mesh geoms, inertia should be specified in the mesh asset"); + throw mjCError(this, "for mesh geoms, inertia should be specified in the mesh asset"); } // apply geom pos/quat as offset @@ -3046,11 +3050,11 @@ void mjCGeom::Compile(void) { checksize(size, type, this, name.c_str(), id); // set hfield sizes in geom.size - if (type==mjGEOM_HFIELD) { + if (type == mjGEOM_HFIELD) { size[0] = hfield->size[0]; size[1] = hfield->size[1]; size[2] = 0.25 * hfield->size[2] + 0.5 * hfield->size[3]; - } else if (type==mjGEOM_MESH || type==mjGEOM_SDF) { + } else if (type == mjGEOM_MESH || type == mjGEOM_SDF) { const double* aamm = mesh->aamm(); size[0] = std::max(std::abs(aamm[0]), std::abs(aamm[3])); size[1] = std::max(std::abs(aamm[1]), std::abs(aamm[4])); @@ -3069,10 +3073,10 @@ void mjCGeom::Compile(void) { if (inferinertia) { // mass is defined if (mjuu_defined(mass)) { - if (mass==0) { + if (mass == 0) { mass_ = 0; density = 0; - } else if (GetVolume()>mjEPS) { + } else if (GetVolume() > mjEPS) { mass_ = mass; density = mass / GetVolume(); SetInertia(); @@ -3091,7 +3095,7 @@ void mjCGeom::Compile(void) { // check for negative values - if (mass_<0 || inertia[0]<0 || inertia[1]<0 || inertia[2]<0 || density<0) + if (mass_ < 0 || inertia[0] < 0 || inertia[1] < 0 || inertia[2] < 0 || density < 0) throw mjCError(this, "mass, inertia or density are negative in geom"); } @@ -3104,7 +3108,7 @@ void mjCGeom::Compile(void) { if (plugin.active) { if (plugin_name.empty() && plugin_instance_name.empty()) { throw mjCError( - this, "neither 'plugin' nor 'instance' is specified for geom"); + this, "neither 'plugin' nor 'instance' is specified for geom"); } mjCPlugin* plugin_instance = static_cast(plugin.element); @@ -3214,22 +3218,22 @@ void mjCSite::Compile(void) { userdata_.resize(model->nuser_site); // check type - if (type<0 || type>=mjNGEOMTYPES) { + if (type < 0 || type >= mjNGEOMTYPES) { throw mjCError(this, "invalid type in site"); } // do not allow meshes, hfields and planes - if (type==mjGEOM_MESH || type==mjGEOM_HFIELD || type==mjGEOM_PLANE) { + if (type == mjGEOM_MESH || type == mjGEOM_HFIELD || type == mjGEOM_PLANE) { throw mjCError(this, "meshes, hfields and planes not allowed in site"); } // 'fromto': compute pos, quat, size if (mjuu_defined(fromto[0])) { // check type - if (type!=mjGEOM_CAPSULE && - type!=mjGEOM_CYLINDER && - type!=mjGEOM_ELLIPSOID && - type!=mjGEOM_BOX) { + if (type != mjGEOM_CAPSULE && + type != mjGEOM_CYLINDER && + type != mjGEOM_ELLIPSOID && + type != mjGEOM_BOX) { throw mjCError(this, "fromto requires capsule, cylinder, box or ellipsoid in geom"); } @@ -3245,12 +3249,12 @@ void mjCSite::Compile(void) { fromto[2]-fromto[5] }; size[1] = mjuu_normvec(vec, 3)/2; - if (size[1]0 && sensor_size[1]>0) { + if (sensor_size[0] > 0 && sensor_size[1] > 0) { float pixel_density[2] = { (float)resolution[0] / sensor_size[0], (float)resolution[1] / sensor_size[1], @@ -3525,7 +3529,7 @@ void mjCLight::Compile(void) { } // normalize direction, make sure it is not zero - if (mjuu_normvec(dir, 3)name); } @@ -3726,8 +3730,8 @@ void mjCHField::Compile(const mjVFS* vfs) { } // check size parameters - for (int i=0; i<4; i++) - if (size[i]<=0) + for (int i=0; i < 4; i++) + if (size[i] <= 0) throw mjCError(this, "size parameter is not positive in hfield"); // remove path from file if necessary @@ -3778,22 +3782,22 @@ void mjCHField::Compile(const mjVFS* vfs) { } // make sure hfield was specified (from file or manually) - if (nrow<1 || ncol<1 || data.empty()) { + if (nrow < 1 || ncol < 1 || data.empty()) { throw mjCError(this, "hfield not specified"); } // set elevation data to [0-1] range float emin = 1E+10, emax = -1E+10; - for (int i = 0; iemax) { + if (emin > emax) { throw mjCError(this, "invalid data range in hfield '%s'", file_.c_str()); } - for (int i=0; imjEPS) { + if (emax-emin > mjEPS) { data[i] /= (emax - emin); } } @@ -3918,10 +3922,10 @@ static void randomdot(std::byte* rgb, const double* markrgb, std::uniform_real_distribution dist(0, 1); // sample - for (int r=0; r1) { + } else if (alpha > 1) { alpha = 1; } - for (int j=0; j<3; j++) { + for (int j=0; j < 3; j++) { rgb[j] = (std::byte)(255*(alpha*rgb1[j] + (1-alpha)*rgb2[j])); } } @@ -3951,23 +3955,23 @@ static void interp(std::byte* rgb, const double* rgb1, const double* rgb2, doubl // make checker pattern for one side static void checker(std::byte* rgb, const std::byte* RGB1, const std::byte* RGB2, int width, int height) { - for (int r=0; r0) { + else if (mark == mjMARK_RANDOM && random > 0) { randomdot(data_.data(), markrgb, width, height, random); } } @@ -4198,7 +4202,7 @@ void mjCTexture::LoadCustom(mjResource* resource, h = pint[1]; // check dimensions - if (w<1 || h<1) { + if (w < 1 || h < 1) { throw mjCError(this, "Non-PNG texture, assuming custom binary file format,\n" "non-positive texture dimensions in file '%s'", resource->name); } @@ -4249,10 +4253,10 @@ void mjCTexture::LoadFlip(std::string filename, const mjVFS* vfs, if (hflip) { if (nchannel != 3) { throw mjCError( - this, "currently only 3-channel textures support horizontal flip"); + this, "currently only 3-channel textures support horizontal flip"); } - for (int r=0; r12) { + if (gridsize[0] < 1 || gridsize[1] < 1 || gridsize[0]*gridsize[1] > 12) { throw mjCError(this, "gridsize must be non-zero and no more than 12 squares in texture"); } @@ -4348,7 +4352,7 @@ void mjCTexture::LoadCubeSingle(std::string filename, const mjVFS* vfs) { } // assign size: repeated or full - if (gridsize[0]==1 && gridsize[1]==1) { + if (gridsize[0] == 1 && gridsize[1] == 1) { width = height = w; } else { width = w/gridsize[1]; @@ -4372,7 +4376,7 @@ void mjCTexture::LoadCubeSingle(std::string filename, const mjVFS* vfs) { } // copy: repeated - if (gridsize[0]==1 && gridsize[1]==1) { + if (gridsize[0] == 1 && gridsize[1] == 1) { memcpy(data_.data(), image.data(), 3*width*width); } @@ -4382,30 +4386,30 @@ void mjCTexture::LoadCubeSingle(std::string filename, const mjVFS* vfs) { int loaded[6] = {0, 0, 0, 0, 0, 0}; // process grid - for (int k=0; k=0) { + if (i >= 0) { // extract sub-image int rstart = width*(k/gridsize[1]); int cstart = width*(k%gridsize[1]); - for (int j=0; jstrippath) { @@ -4496,11 +4500,11 @@ void mjCTexture::LoadCubeSeparate(const mjVFS* vfs) { } // set undefined faces to rgb1 - for (int i=0; i<6; i++) { + for (int i=0; i < 6; i++) { if (!loaded[i]) { - for (int k=0; kprefix + spec_textures_[i] + m->suffix; } @@ -4840,7 +4844,7 @@ void mjCPair::Compile(void) { CopyFromSpec(); // check condim - if (condim!=1 && condim!=3 && condim!=4 && condim!=6) { + if (condim != 1 && condim != 3 && condim != 4 && condim != 6) { throw mjCError(this, "invalid condim in contact pair"); } @@ -4863,10 +4867,10 @@ void mjCPair::Compile(void) { // set undefined condim, friction, solref, solimp: different priority if (geom1->priority != geom2->priority) { - mjCGeom* pgh = (geom1->priority>geom2->priority ? geom1 : geom2); + mjCGeom* pgh = (geom1->priority > geom2->priority ? geom1 : geom2); // condim - if (condim<0) { + if (condim < 0) { condim = pgh->condim; } @@ -4879,14 +4883,14 @@ void mjCPair::Compile(void) { // reference if (!mjuu_defined(solref[0])) { - for (int i=0; isolref[i]; } } // impedance if (!mjuu_defined(solimp[0])) { - for (int i=0; isolimp[i]; } } @@ -4895,7 +4899,7 @@ void mjCPair::Compile(void) { // set undefined condim, friction, solref, solimp: same priority else { // condim: max - if (condim<0) { + if (condim < 0) { condim = std::max(geom1->condim, geom2->condim); } @@ -4908,11 +4912,11 @@ void mjCPair::Compile(void) { // solver mix factor double mix; - if (geom1->solmix>=mjEPS && geom2->solmix>=mjEPS) { + if (geom1->solmix >= mjEPS && geom2->solmix >= mjEPS) { mix = geom1->solmix / (geom1->solmix + geom2->solmix); - } else if (geom1->solmixsolmixsolmix < mjEPS && geom2->solmix < mjEPS) { mix = 0.5; - } else if (geom1->solmixsolmix < mjEPS) { mix = 0.0; } else { mix = 1.0; @@ -4921,15 +4925,15 @@ void mjCPair::Compile(void) { // reference if (!mjuu_defined(solref[0])) { // standard: mix - if (solref[0]>0) { - for (int i=0; i 0) { + for (int i=0; i < mjNREF; i++) { solref[i] = mix*geom1->solref[i] + (1-mix)*geom2->solref[i]; } } // direct: min else { - for (int i=0; isolref[i], geom2->solref[i]); } } @@ -4937,7 +4941,7 @@ void mjCPair::Compile(void) { // impedance if (!mjuu_defined(solimp[0])) { - for (int i=0; isolimp[i] + (1-mix)*geom2->solimp[i]; } } @@ -5159,21 +5163,21 @@ void mjCEquality::ResolveReferences(const mjCModel* m) { mjtJoint jt1, jt2; // determine object type - if (type==mjEQ_WELD) { + if (type == mjEQ_WELD) { if (objtype != mjOBJ_SITE && objtype != mjOBJ_BODY) { throw mjCError(this, "weld constraint supports only sites and bodies"); } object_type = objtype; - } else if (type==mjEQ_CONNECT) { + } else if (type == mjEQ_CONNECT) { if (objtype != mjOBJ_SITE && objtype != mjOBJ_BODY) { throw mjCError(this, "connect constraint supports only sites and bodies"); } object_type = objtype; - } else if (type==mjEQ_JOINT) { + } else if (type == mjEQ_JOINT) { object_type = mjOBJ_JOINT; - } else if (type==mjEQ_TENDON) { + } else if (type == mjEQ_TENDON) { object_type = mjOBJ_TENDON; - } else if (type==mjEQ_FLEX) { + } else if (type == mjEQ_FLEX) { object_type = mjOBJ_FLEX; } else { throw mjCError(this, "invalid type in equality constraint"); @@ -5230,7 +5234,7 @@ void mjCEquality::Compile(void) { ResolveReferences(model); // make sure flex is not rigid - if (type==mjEQ_FLEX && model->Flexes()[obj1id]->rigid) { + if (type == mjEQ_FLEX && model->Flexes()[obj1id]->rigid) { throw mjCError(this, "rigid flex '%s' in equality constraint %d", name1_.c_str(), id); } } @@ -5280,7 +5284,7 @@ mjCTendon& mjCTendon::operator=(const mjCTendon& other) { this->spec = other.spec; *static_cast(this) = static_cast(other); *static_cast(this) = static_cast(other); - for (int i=0; itendon = this; } @@ -5291,7 +5295,9 @@ mjCTendon& mjCTendon::operator=(const mjCTendon& other) { -bool mjCTendon::is_limited() const { return islimited(limited, range); } +bool mjCTendon::is_limited() const { + return islimited(limited, range); +} void mjCTendon::PointToLocal() { @@ -5320,8 +5326,8 @@ void mjCTendon::CopyFromSpec() { userdata_ = spec_userdata_; // clear precompiled - for (int i=0; itype==mjWRAP_CYLINDER) { + for (int i=0; i < path.size(); i++) { + if (path[i]->type == mjWRAP_CYLINDER) { path[i]->type = mjWRAP_SPHERE; } } @@ -5332,7 +5338,7 @@ void mjCTendon::CopyFromSpec() { // desctructor mjCTendon::~mjCTendon() { // delete objects allocated here - for (unsigned int i=0; ispec.compiler; - for (int i=0; imodel = _model; } } @@ -5433,7 +5439,7 @@ const mjCWrap* mjCTendon::GetWrap(int i) const { void mjCTendon::ResolveReferences(const mjCModel* m) { int nfailure = 0; int npulley = 0; - for (int i=0; iname; std::string psidesite = path[i]->sidesite; if (path[i]->type == mjWRAP_PULLEY) { @@ -5452,7 +5458,7 @@ void mjCTendon::ResolveReferences(const mjCModel* m) { nfailure++; } } - if (nfailure==path.size()-npulley) { + if (nfailure == path.size()-npulley) { throw mjCError(this, "tendon '%s' (id = %d): no attached reference found", name.c_str(), id); } prefix.clear(); @@ -5483,13 +5489,13 @@ void mjCTendon::Compile(void) { bool spatial = (path[0]->type != mjWRAP_JOINT); // require at least two objects in spatial path - if (spatial && sz<2) { + if (spatial && sz < 2) { throw mjCError(this, "tendon '%s' (id = %d): spatial path must contain at least two objects", name.c_str(), id); } // require positive width - if (spatial && width<=0) { + if (spatial && width <= 0) { throw mjCError(this, "tendon '%s' (id = %d) must have positive width", name.c_str(), id); } @@ -5497,7 +5503,7 @@ void mjCTendon::Compile(void) { ResolveReferences(model); // check path - for (int i=0; itype) { - case mjWRAP_PULLEY: - // pulley should not follow other pulley - if (i>0 && path[i-1]->type==mjWRAP_PULLEY) { - throw mjCError(this, "tendon '%s' (id = %d): consecutive pulleys (pos %d)", - name.c_str(), id, i); - } + case mjWRAP_PULLEY: + // pulley should not follow other pulley + if (i > 0 && path[i-1]->type == mjWRAP_PULLEY) { + throw mjCError(this, "tendon '%s' (id = %d): consecutive pulleys (pos %d)", + name.c_str(), id, i); + } - // pulley should not be last - if (i==sz-1) { - throw mjCError(this, "tendon '%s' (id = %d): path ends with pulley", name.c_str(), id); - } - break; + // pulley should not be last + if (i == sz-1) { + throw mjCError(this, "tendon '%s' (id = %d): path ends with pulley", name.c_str(), id); + } + break; - case mjWRAP_SITE: - // site needs a neighbor that is not a pulley - if ((i==0 || path[i-1]->type==mjWRAP_PULLEY) && - (i==sz-1 || path[i+1]->type==mjWRAP_PULLEY)) { + case mjWRAP_SITE: + // site needs a neighbor that is not a pulley + if ((i == 0 || path[i-1]->type == mjWRAP_PULLEY) && + (i == sz-1 || path[i+1]->type == mjWRAP_PULLEY)) { + throw mjCError(this, + "tendon '%s' (id = %d): site %d needs a neighbor that is not a pulley", + name.c_str(), id, i); + } + + // site cannot be repeated + if (i < sz-1 && path[i+1]->type == mjWRAP_SITE && path[i]->obj->id == path[i+1]->obj->id) { + throw mjCError(this, + "tendon '%s' (id = %d): site %d is repeated", + name.c_str(), id, i); + } + + break; + + case mjWRAP_SPHERE: + case mjWRAP_CYLINDER: + // geom must be bracketed by sites + if (i == 0 || i == sz-1 || path[i-1]->type != mjWRAP_SITE || path[i+1]->type != mjWRAP_SITE) { + throw mjCError(this, + "tendon '%s' (id = %d): geom at pos %d not bracketed by sites", + name.c_str(), id, i); + } + + // mark geoms as non visual + model->Geoms()[path[i]->obj->id]->SetNotVisual(); + break; + + case mjWRAP_JOINT: throw mjCError(this, - "tendon '%s' (id = %d): site %d needs a neighbor that is not a pulley", + "tendon '%s (id = %d)': joint wrap found in spatial path at pos %d", name.c_str(), id, i); - } - // site cannot be repeated - if (itype==mjWRAP_SITE && path[i]->obj->id==path[i+1]->obj->id) { + default: throw mjCError(this, - "tendon '%s' (id = %d): site %d is repeated", + "tendon '%s (id = %d)': invalid wrap object at pos %d", name.c_str(), id, i); - } - - break; - - case mjWRAP_SPHERE: - case mjWRAP_CYLINDER: - // geom must be bracketed by sites - if (i==0 || i==sz-1 || path[i-1]->type != mjWRAP_SITE || path[i+1]->type != mjWRAP_SITE) { - throw mjCError(this, - "tendon '%s' (id = %d): geom at pos %d not bracketed by sites", - name.c_str(), id, i); - } - - // mark geoms as non visual - model->Geoms()[path[i]->obj->id]->SetNotVisual(); - break; - - case mjWRAP_JOINT: - throw mjCError(this, - "tendon '%s (id = %d)': joint wrap found in spatial path at pos %d", - name.c_str(), id, i); - - default: - throw mjCError(this, - "tendon '%s (id = %d)': invalid wrap object at pos %d", - name.c_str(), id, i); } } } // if limited is auto, set to 1 if range is specified, otherwise unlimited if (limited == mjLIMITED_AUTO) { - bool hasrange = !(range[0]==0 && range[1]==0); + bool hasrange = !(range[0] == 0 && range[1] == 0); checklimited(this, compiler->autolimits, "tendon", "", limited, hasrange); } // check limits - if (range[0]>=range[1] && is_limited()) { + if (range[0] >= range[1] && is_limited()) { throw mjCError(this, "invalid limits in tendon"); } @@ -5650,70 +5656,70 @@ void mjCWrap::ResolveReferences(const mjCModel* m) { // handle wrap object types switch (type) { - case mjWRAP_JOINT: // joint - // find joint by name - obj = m->FindObject(mjOBJ_JOINT, name); - if (!obj) { - throw mjCError(this, - "joint '%s' not found in tendon %d, wrap %d", - name.c_str(), tendon->id, id); - } - - break; - - case mjWRAP_SPHERE: // geom (cylinder type set here) - // find geom by name - obj = m->FindObject(mjOBJ_GEOM, name); - if (!obj) { - throw mjCError(this, - "geom '%s' not found in tendon %d, wrap %d", - name.c_str(), tendon->id, id); - } - - // set/check geom type - if (((mjCGeom*)obj)->type == mjGEOM_CYLINDER) { - type = mjWRAP_CYLINDER; - } else if (((mjCGeom*)obj)->type != mjGEOM_SPHERE) { - throw mjCError(this, - "geom '%s' in tendon %d, wrap %d is not sphere or cylinder", - name.c_str(), tendon->id, id); - } - - // process side site - if (!sidesite.empty()) { - // find site by name - pside = m->FindObject(mjOBJ_SITE, sidesite); - if (!pside) { + case mjWRAP_JOINT: // joint + // find joint by name + obj = m->FindObject(mjOBJ_JOINT, name); + if (!obj) { throw mjCError(this, - "side site '%s' not found in tendon %d, wrap %d", - sidesite.c_str(), tendon->id, id); + "joint '%s' not found in tendon %d, wrap %d", + name.c_str(), tendon->id, id); } - // save side site id - sideid = pside->id; - } - break; + break; - case mjWRAP_PULLEY: // pulley - // make sure divisor is non-negative - if (prm<0) { - throw mjCError(this, - "pulley has negative divisor in tendon %d, wrap %d", - 0, tendon->id, id); - } + case mjWRAP_SPHERE: // geom (cylinder type set here) + // find geom by name + obj = m->FindObject(mjOBJ_GEOM, name); + if (!obj) { + throw mjCError(this, + "geom '%s' not found in tendon %d, wrap %d", + name.c_str(), tendon->id, id); + } - break; + // set/check geom type + if (((mjCGeom*)obj)->type == mjGEOM_CYLINDER) { + type = mjWRAP_CYLINDER; + } else if (((mjCGeom*)obj)->type != mjGEOM_SPHERE) { + throw mjCError(this, + "geom '%s' in tendon %d, wrap %d is not sphere or cylinder", + name.c_str(), tendon->id, id); + } - case mjWRAP_SITE: // site - // find site by name - obj = m->FindObject(mjOBJ_SITE, name); - if (!obj) { - throw mjCError(this, "site '%s' not found in wrap %d", name.c_str(), id); - } - break; + // process side site + if (!sidesite.empty()) { + // find site by name + pside = m->FindObject(mjOBJ_SITE, sidesite); + if (!pside) { + throw mjCError(this, + "side site '%s' not found in tendon %d, wrap %d", + sidesite.c_str(), tendon->id, id); + } - default: // SHOULD NOT OCCUR - throw mjCError(this, "unknown wrap type in tendon %d, wrap %d", 0, tendon->id, id); + // save side site id + sideid = pside->id; + } + break; + + case mjWRAP_PULLEY: // pulley + // make sure divisor is non-negative + if (prm < 0) { + throw mjCError(this, + "pulley has negative divisor in tendon %d, wrap %d", + 0, tendon->id, id); + } + + break; + + case mjWRAP_SITE: // site + // find site by name + obj = m->FindObject(mjOBJ_SITE, name); + if (!obj) { + throw mjCError(this, "site '%s' not found in wrap %d", name.c_str(), id); + } + break; + + default: // SHOULD NOT OCCUR + throw mjCError(this, "unknown wrap type in tendon %d, wrap %d", 0, tendon->id, id); } } @@ -5783,9 +5789,15 @@ void mjCActuator::ForgetKeyframes() { -bool mjCActuator::is_ctrllimited() const { return islimited(ctrllimited, ctrlrange); } -bool mjCActuator::is_forcelimited() const { return islimited(forcelimited, forcerange); } -bool mjCActuator::is_actlimited() const { return islimited(actlimited, actrange); } +bool mjCActuator::is_ctrllimited() const { + return islimited(ctrllimited, ctrlrange); +} +bool mjCActuator::is_forcelimited() const { + return islimited(forcelimited, forcerange); +} +bool mjCActuator::is_actlimited() const { + return islimited(actlimited, actrange); +} @@ -5865,63 +5877,63 @@ void mjCActuator::CopyPlugin() { void mjCActuator::ResolveReferences(const mjCModel* m) { switch (trntype) { - case mjTRN_JOINT: - case mjTRN_JOINTINPARENT: - // get joint - ptarget = m->FindObject(mjOBJ_JOINT, target_); - if (!ptarget) { - throw mjCError(this, - "unknown transmission target '%s' for actuator id = %d", target_.c_str(), id); - } - break; - - case mjTRN_SLIDERCRANK: - // get slidersite, copy in trnid[1] - if (slidersite_.empty()) { - throw mjCError(this, "missing base site for slider-crank '%s' (id = %d)", name.c_str(), id); - } - ptarget = m->FindObject(mjOBJ_SITE, slidersite_); - if (!ptarget) { - throw mjCError(this, "base site '%s' not found for actuator %d", slidersite_.c_str(), id); - } - trnid[1] = ptarget->id; - - // check cranklength - if (cranklength<=0) { - throw mjCError(this, - "crank length must be positive in actuator '%s' (id = %d)", name.c_str(), id); - } - - // proceed with regular target - ptarget = m->FindObject(mjOBJ_SITE, target_); - break; - - case mjTRN_TENDON: - // get tendon - ptarget = m->FindObject(mjOBJ_TENDON, target_); - break; - - case mjTRN_SITE: - // get refsite, copy into trnid[1] - if (!refsite_.empty()) { - ptarget = m->FindObject(mjOBJ_SITE, refsite_); + case mjTRN_JOINT: + case mjTRN_JOINTINPARENT: + // get joint + ptarget = m->FindObject(mjOBJ_JOINT, target_); if (!ptarget) { - throw mjCError(this, "reference site '%s' not found for actuator %d", refsite_.c_str(), id); + throw mjCError(this, + "unknown transmission target '%s' for actuator id = %d", target_.c_str(), id); + } + break; + + case mjTRN_SLIDERCRANK: + // get slidersite, copy in trnid[1] + if (slidersite_.empty()) { + throw mjCError(this, "missing base site for slider-crank '%s' (id = %d)", name.c_str(), id); + } + ptarget = m->FindObject(mjOBJ_SITE, slidersite_); + if (!ptarget) { + throw mjCError(this, "base site '%s' not found for actuator %d", slidersite_.c_str(), id); } trnid[1] = ptarget->id; - } - // proceed with regular site target - ptarget = m->FindObject(mjOBJ_SITE, target_); - break; + // check cranklength + if (cranklength <= 0) { + throw mjCError(this, + "crank length must be positive in actuator '%s' (id = %d)", name.c_str(), id); + } - case mjTRN_BODY: - // get body - ptarget = m->FindObject(mjOBJ_BODY, target_); - break; + // proceed with regular target + ptarget = m->FindObject(mjOBJ_SITE, target_); + break; - default: - throw mjCError(this, "invalid transmission type in actuator"); + case mjTRN_TENDON: + // get tendon + ptarget = m->FindObject(mjOBJ_TENDON, target_); + break; + + case mjTRN_SITE: + // get refsite, copy into trnid[1] + if (!refsite_.empty()) { + ptarget = m->FindObject(mjOBJ_SITE, refsite_); + if (!ptarget) { + throw mjCError(this, "reference site '%s' not found for actuator %d", refsite_.c_str(), id); + } + trnid[1] = ptarget->id; + } + + // proceed with regular site target + ptarget = m->FindObject(mjOBJ_SITE, target_); + break; + + case mjTRN_BODY: + // get body + ptarget = m->FindObject(mjOBJ_BODY, target_); + break; + + default: + throw mjCError(this, "invalid transmission type in actuator"); } // assign and check @@ -6000,26 +6012,26 @@ void mjCActuator::Compile(void) { // if limited is auto, check for inconsistency wrt to autolimits if (forcelimited == mjLIMITED_AUTO) { - bool hasrange = !(forcerange[0]==0 && forcerange[1]==0); + bool hasrange = !(forcerange[0] == 0 && forcerange[1] == 0); checklimited(this, compiler->autolimits, "actuator", "force", forcelimited, hasrange); } if (ctrllimited == mjLIMITED_AUTO) { - bool hasrange = !(ctrlrange[0]==0 && ctrlrange[1]==0); + bool hasrange = !(ctrlrange[0] == 0 && ctrlrange[1] == 0); checklimited(this, compiler->autolimits, "actuator", "ctrl", ctrllimited, hasrange); } if (actlimited == mjLIMITED_AUTO) { - bool hasrange = !(actrange[0]==0 && actrange[1]==0); + bool hasrange = !(actrange[0] == 0 && actrange[1] == 0); checklimited(this, compiler->autolimits, "actuator", "act", actlimited, hasrange); } // check limits - if (forcerange[0]>=forcerange[1] && is_forcelimited()) { + if (forcerange[0] >= forcerange[1] && is_forcelimited()) { throw mjCError(this, "invalid force range for actuator"); } - if (ctrlrange[0]>=ctrlrange[1] && is_ctrllimited()) { + if (ctrlrange[0] >= ctrlrange[1] && is_ctrllimited()) { throw mjCError(this, "invalid control range for actuator"); } - if (actrange[0]>=actrange[1] && is_actlimited()) { + if (actrange[0] >= actrange[1] && is_actlimited()) { throw mjCError(this, "invalid actrange for actuator"); } if (is_actlimited() && dyntype == mjDYN_NONE) { @@ -6045,12 +6057,12 @@ void mjCActuator::Compile(void) { } // check muscle parameters - for (int i=0; i<2; i++) { + for (int i=0; i < 2; i++) { // select gain or bias double* prm = NULL; - if (i==0 && gaintype==mjGAIN_MUSCLE) { + if (i == 0 && gaintype == mjGAIN_MUSCLE) { prm = gainprm; - } else if (i==1 && biastype==mjBIAS_MUSCLE) { + } else if (i == 1 && biastype == mjBIAS_MUSCLE) { prm = biasprm; } @@ -6060,17 +6072,17 @@ void mjCActuator::Compile(void) { } // range - if (prm[0]>=prm[1]) { + if (prm[0] >= prm[1]) { throw mjCError(this, "range[0]=1 || prm[5]<=1) { + if (prm[4] >= 1 || prm[5] <= 1) { throw mjCError(this, "lmin<10 - if (prm[3]<=0 || prm[6]<=0 || prm[7]<=0 || prm[8]<=0) { + if (prm[3] <= 0 || prm[6] <= 0 || prm[7] <= 0 || prm[8] <= 0) { throw mjCError(this, "positive scale, vmax, fpmax, fvmax required in muscle '%s' (id = %d)", name.c_str(), id); @@ -6081,8 +6093,8 @@ void mjCActuator::Compile(void) { if (plugin.active) { if (plugin_name.empty() && plugin_instance_name.empty()) { throw mjCError( - this, "neither 'plugin' nor 'instance' is specified for actuator '%s', (id = %d)", - name.c_str(), id); + this, "neither 'plugin' nor 'instance' is specified for actuator '%s', (id = %d)", + name.c_str(), id); } mjCPlugin* plugin_instance = static_cast(plugin.element); @@ -6282,12 +6294,12 @@ void mjCSensor::Compile(void) { userdata_.resize(model->nuser_sensor); // require non-negative noise - if (noise<0) { + if (noise < 0) { throw mjCError(this, "negative noise in sensor"); } // require non-negative cutoff - if (cutoff<0) { + if (cutoff < 0) { throw mjCError(this, "negative cutoff in sensor"); } @@ -6296,327 +6308,329 @@ void mjCSensor::Compile(void) { // process according to sensor type switch (type) { - case mjSENS_TOUCH: - case mjSENS_ACCELEROMETER: - case mjSENS_VELOCIMETER: - case mjSENS_GYRO: - case mjSENS_FORCE: - case mjSENS_TORQUE: - case mjSENS_MAGNETOMETER: - case mjSENS_RANGEFINDER: - case mjSENS_CAMPROJECTION: - // must be attached to site - if (objtype!=mjOBJ_SITE) { - throw mjCError(this, "sensor must be attached to site"); - } - - // set dim and datatype - if (type==mjSENS_TOUCH || type==mjSENS_RANGEFINDER) { - dim = 1; - datatype = mjDATATYPE_POSITIVE; - } else if (type==mjSENS_CAMPROJECTION) { - dim = 2; - datatype = mjDATATYPE_REAL; - } else { - dim = 3; - datatype = mjDATATYPE_REAL; - } - - // set stage - if (type==mjSENS_MAGNETOMETER || type==mjSENS_RANGEFINDER || type==mjSENS_CAMPROJECTION) { - needstage = mjSTAGE_POS; - } else if (type==mjSENS_GYRO || type==mjSENS_VELOCIMETER) { - needstage = mjSTAGE_VEL; - } else { - needstage = mjSTAGE_ACC; - } - - // check for camera resolution for camera projection sensor - if (type==mjSENS_CAMPROJECTION) { - mjCCamera* camref = (mjCCamera*)ref; - if (!camref->resolution[0] || !camref->resolution[1]) { - throw mjCError(this, "camera projection sensor requires camera resolution"); + case mjSENS_TOUCH: + case mjSENS_ACCELEROMETER: + case mjSENS_VELOCIMETER: + case mjSENS_GYRO: + case mjSENS_FORCE: + case mjSENS_TORQUE: + case mjSENS_MAGNETOMETER: + case mjSENS_RANGEFINDER: + case mjSENS_CAMPROJECTION: + // must be attached to site + if (objtype != mjOBJ_SITE) { + throw mjCError(this, "sensor must be attached to site"); } - } - break; - case mjSENS_JOINTPOS: - case mjSENS_JOINTVEL: - case mjSENS_JOINTACTFRC: - // must be attached to joint - if (objtype!=mjOBJ_JOINT) { - throw mjCError(this, "sensor must be attached to joint"); - } - - // make sure joint is slide or hinge - if (((mjCJoint*)obj)->type!=mjJNT_SLIDE && ((mjCJoint*)obj)->type!=mjJNT_HINGE) { - throw mjCError(this, "joint must be slide or hinge in sensor"); - } - - // set - dim = 1; - datatype = mjDATATYPE_REAL; - if (type==mjSENS_JOINTPOS) { - needstage = mjSTAGE_POS; - } else if (type==mjSENS_JOINTVEL) { - needstage = mjSTAGE_VEL; - } else if (type==mjSENS_JOINTACTFRC) { - needstage = mjSTAGE_ACC; - } - break; - - case mjSENS_TENDONPOS: - case mjSENS_TENDONVEL: - // must be attached to tendon - if (objtype!=mjOBJ_TENDON) { - throw mjCError(this, "sensor must be attached to tendon"); - } - - // set - dim = 1; - datatype = mjDATATYPE_REAL; - if (type==mjSENS_TENDONPOS) { - needstage = mjSTAGE_POS; - } else { - needstage = mjSTAGE_VEL; - } - break; - - case mjSENS_ACTUATORPOS: - case mjSENS_ACTUATORVEL: - case mjSENS_ACTUATORFRC: - // must be attached to actuator - if (objtype!=mjOBJ_ACTUATOR) { - throw mjCError(this, "sensor must be attached to actuator"); - } - - // set - dim = 1; - datatype = mjDATATYPE_REAL; - if (type==mjSENS_ACTUATORPOS) { - needstage = mjSTAGE_POS; - } else if (type==mjSENS_ACTUATORVEL) { - needstage = mjSTAGE_VEL; - } else { - needstage = mjSTAGE_ACC; - } - break; - - case mjSENS_BALLQUAT: - case mjSENS_BALLANGVEL: - // must be attached to joint - if (objtype!=mjOBJ_JOINT) { - throw mjCError(this, "sensor must be attached to joint"); - } - - // make sure joint is ball - if (((mjCJoint*)obj)->type!=mjJNT_BALL) { - throw mjCError(this, "joint must be ball in sensor"); - } - - // set - if (type==mjSENS_BALLQUAT) { - dim = 4; - datatype = mjDATATYPE_QUATERNION; - needstage = mjSTAGE_POS; - } else { - dim = 3; - datatype = mjDATATYPE_REAL; - needstage = mjSTAGE_VEL; - } - break; - - case mjSENS_JOINTLIMITPOS: - case mjSENS_JOINTLIMITVEL: - case mjSENS_JOINTLIMITFRC: - // must be attached to joint - if (objtype!=mjOBJ_JOINT) { - throw mjCError(this, "sensor must be attached to joint"); - } - - // make sure joint has limit - if (!((mjCJoint*)obj)->is_limited()) { - throw mjCError(this, "joint must be limited in sensor"); - } - - // set - dim = 1; - datatype = mjDATATYPE_REAL; - if (type==mjSENS_JOINTLIMITPOS) { - needstage = mjSTAGE_POS; - } else if (type==mjSENS_JOINTLIMITVEL) { - needstage = mjSTAGE_VEL; - } else { - needstage = mjSTAGE_ACC; - } - break; - - case mjSENS_TENDONLIMITPOS: - case mjSENS_TENDONLIMITVEL: - case mjSENS_TENDONLIMITFRC: - // must be attached to tendon - if (objtype!=mjOBJ_TENDON) { - throw mjCError(this, "sensor must be attached to tendon"); - } - - // make sure tendon has limit - if (!((mjCTendon*)obj)->is_limited()) { - throw mjCError(this, "tendon must be limited in sensor"); - } - - // set - dim = 1; - datatype = mjDATATYPE_REAL; - if (type==mjSENS_TENDONLIMITPOS) { - needstage = mjSTAGE_POS; - } else if (type==mjSENS_TENDONLIMITVEL) { - needstage = mjSTAGE_VEL; - } else { - needstage = mjSTAGE_ACC; - } - break; - - case mjSENS_FRAMEPOS: - case mjSENS_FRAMEQUAT: - case mjSENS_FRAMEXAXIS: - case mjSENS_FRAMEYAXIS: - case mjSENS_FRAMEZAXIS: - case mjSENS_FRAMELINVEL: - case mjSENS_FRAMEANGVEL: - case mjSENS_FRAMELINACC: - case mjSENS_FRAMEANGACC: - // must be attached to object with spatial frame - if (objtype!=mjOBJ_BODY && objtype!=mjOBJ_XBODY && - objtype!=mjOBJ_GEOM && objtype!=mjOBJ_SITE && objtype!=mjOBJ_CAMERA) { - throw mjCError(this, "sensor must be attached to (x)body, geom, site or camera"); - } - - // set dim - if (type==mjSENS_FRAMEQUAT) { - dim = 4; - } else { - dim = 3; - } - - // set datatype - if (type==mjSENS_FRAMEQUAT) { - datatype = mjDATATYPE_QUATERNION; - } else if (type==mjSENS_FRAMEXAXIS || type==mjSENS_FRAMEYAXIS || type==mjSENS_FRAMEZAXIS) { - datatype = mjDATATYPE_AXIS; - } else { - datatype = mjDATATYPE_REAL; - } - - // set needstage - if (type==mjSENS_FRAMELINACC || type==mjSENS_FRAMEANGACC) { - needstage = mjSTAGE_ACC; - } else if (type==mjSENS_FRAMELINVEL || type==mjSENS_FRAMEANGVEL) { - needstage = mjSTAGE_VEL; - } else { - needstage = mjSTAGE_POS; - } - break; - - case mjSENS_SUBTREECOM: - case mjSENS_SUBTREELINVEL: - case mjSENS_SUBTREEANGMOM: - // must be attached to body - if (objtype!=mjOBJ_BODY) { - throw mjCError(this, "sensor must be attached to body"); - } - - // set - dim = 3; - datatype = mjDATATYPE_REAL; - if (type==mjSENS_SUBTREECOM) { - needstage = mjSTAGE_POS; - } else { - needstage = mjSTAGE_VEL; - } - break; - - case mjSENS_GEOMDIST: - case mjSENS_GEOMNORMAL: - case mjSENS_GEOMFROMTO: - // must be attached to body or geom - if ((objtype!=mjOBJ_BODY && objtype!=mjOBJ_GEOM) || - (reftype!=mjOBJ_BODY && reftype!=mjOBJ_GEOM)) { - throw mjCError(this, "sensor must be attached to body or geom"); - } - - // objects must be different - if (objtype == reftype && obj == ref) { - throw mjCError(this, "1st body/geom must be different from 2nd body/geom"); - } - - // height fields are not necessarily convex and are not yet supported - if ((objtype == mjOBJ_GEOM && static_cast(obj)->Type() == mjGEOM_HFIELD) || - (reftype == mjOBJ_GEOM && static_cast(ref)->Type() == mjGEOM_HFIELD)) { - throw mjCError(this, "height fields are not supported in geom distance sensors"); - } - - // set - needstage = mjSTAGE_POS; - if (type==mjSENS_GEOMDIST) { - dim = 1; - datatype = mjDATATYPE_POSITIVE; - } else if (type==mjSENS_GEOMNORMAL) { - dim = 3; - datatype = mjDATATYPE_AXIS; - } else { - dim = 6; - datatype = mjDATATYPE_REAL; - } - break; - - case mjSENS_E_POTENTIAL: - case mjSENS_E_KINETIC: - case mjSENS_CLOCK: - dim = 1; - needstage = mjSTAGE_POS; - datatype = mjDATATYPE_REAL; - break; - - case mjSENS_USER: - // check for negative dim - if (dim<0) { - throw mjCError(this, "sensor dim must be positive in sensor"); - } - - // make sure dim is consistent with datatype - if (datatype==mjDATATYPE_AXIS && dim != 3) { - throw mjCError(this, - "datatype AXIS requires dim=3 in sensor"); - } - if (datatype==mjDATATYPE_QUATERNION && dim != 4) { - throw mjCError(this, "datatype QUATERNION requires dim=4 in sensor"); - } - break; - - case mjSENS_PLUGIN: - dim = 0; // to be filled in by the plugin later - datatype = mjDATATYPE_REAL; // no noise added to plugin sensors, this attribute is unused - - if (plugin_name.empty() && plugin_instance_name.empty()) { - throw mjCError(this, "neither 'plugin' nor 'instance' is specified for sensor"); - } - - // resolve plugin instance, or create one if using the "plugin" attribute shortcut - { - mjCPlugin* plugin_instance = static_cast(plugin.element); - model->ResolvePlugin(this, plugin_name, plugin_instance_name, &plugin_instance); - plugin.element = plugin_instance; - const mjpPlugin* pplugin = mjp_getPluginAtSlot(plugin_instance->plugin_slot); - if (!(pplugin->capabilityflags & mjPLUGIN_SENSOR)) { - throw mjCError(this, "plugin '%s' does not support sensors", pplugin->name); + // set dim and datatype + if (type == mjSENS_TOUCH || type == mjSENS_RANGEFINDER) { + dim = 1; + datatype = mjDATATYPE_POSITIVE; + } else if (type == mjSENS_CAMPROJECTION) { + dim = 2; + datatype = mjDATATYPE_REAL; + } else { + dim = 3; + datatype = mjDATATYPE_REAL; } - needstage = static_cast(pplugin->needstage); - } - break; + // set stage + if (type == mjSENS_MAGNETOMETER || type == mjSENS_RANGEFINDER || type == mjSENS_CAMPROJECTION) { + needstage = mjSTAGE_POS; + } else if (type == mjSENS_GYRO || type == mjSENS_VELOCIMETER) { + needstage = mjSTAGE_VEL; + } else { + needstage = mjSTAGE_ACC; + } - default: - throw mjCError(this, "invalid type in sensor '%s' (id = %d)", name.c_str(), id); + // check for camera resolution for camera projection sensor + if (type == mjSENS_CAMPROJECTION) { + mjCCamera* camref = (mjCCamera*)ref; + if (!camref->resolution[0] || !camref->resolution[1]) { + throw mjCError(this, "camera projection sensor requires camera resolution"); + } + } + break; + + case mjSENS_JOINTPOS: + case mjSENS_JOINTVEL: + case mjSENS_JOINTACTFRC: + // must be attached to joint + if (objtype != mjOBJ_JOINT) { + throw mjCError(this, "sensor must be attached to joint"); + } + + // make sure joint is slide or hinge + if (((mjCJoint*)obj)->type != mjJNT_SLIDE && ((mjCJoint*)obj)->type != mjJNT_HINGE) { + throw mjCError(this, "joint must be slide or hinge in sensor"); + } + + // set + dim = 1; + datatype = mjDATATYPE_REAL; + if (type == mjSENS_JOINTPOS) { + needstage = mjSTAGE_POS; + } else if (type == mjSENS_JOINTVEL) { + needstage = mjSTAGE_VEL; + } else if (type == mjSENS_JOINTACTFRC) { + needstage = mjSTAGE_ACC; + } + break; + + case mjSENS_TENDONPOS: + case mjSENS_TENDONVEL: + // must be attached to tendon + if (objtype != mjOBJ_TENDON) { + throw mjCError(this, "sensor must be attached to tendon"); + } + + // set + dim = 1; + datatype = mjDATATYPE_REAL; + if (type == mjSENS_TENDONPOS) { + needstage = mjSTAGE_POS; + } else { + needstage = mjSTAGE_VEL; + } + break; + + case mjSENS_ACTUATORPOS: + case mjSENS_ACTUATORVEL: + case mjSENS_ACTUATORFRC: + // must be attached to actuator + if (objtype != mjOBJ_ACTUATOR) { + throw mjCError(this, "sensor must be attached to actuator"); + } + + // set + dim = 1; + datatype = mjDATATYPE_REAL; + if (type == mjSENS_ACTUATORPOS) { + needstage = mjSTAGE_POS; + } else if (type == mjSENS_ACTUATORVEL) { + needstage = mjSTAGE_VEL; + } else { + needstage = mjSTAGE_ACC; + } + break; + + case mjSENS_BALLQUAT: + case mjSENS_BALLANGVEL: + // must be attached to joint + if (objtype != mjOBJ_JOINT) { + throw mjCError(this, "sensor must be attached to joint"); + } + + // make sure joint is ball + if (((mjCJoint*)obj)->type != mjJNT_BALL) { + throw mjCError(this, "joint must be ball in sensor"); + } + + // set + if (type == mjSENS_BALLQUAT) { + dim = 4; + datatype = mjDATATYPE_QUATERNION; + needstage = mjSTAGE_POS; + } else { + dim = 3; + datatype = mjDATATYPE_REAL; + needstage = mjSTAGE_VEL; + } + break; + + case mjSENS_JOINTLIMITPOS: + case mjSENS_JOINTLIMITVEL: + case mjSENS_JOINTLIMITFRC: + // must be attached to joint + if (objtype != mjOBJ_JOINT) { + throw mjCError(this, "sensor must be attached to joint"); + } + + // make sure joint has limit + if (!((mjCJoint*)obj)->is_limited()) { + throw mjCError(this, "joint must be limited in sensor"); + } + + // set + dim = 1; + datatype = mjDATATYPE_REAL; + if (type == mjSENS_JOINTLIMITPOS) { + needstage = mjSTAGE_POS; + } else if (type == mjSENS_JOINTLIMITVEL) { + needstage = mjSTAGE_VEL; + } else { + needstage = mjSTAGE_ACC; + } + break; + + case mjSENS_TENDONLIMITPOS: + case mjSENS_TENDONLIMITVEL: + case mjSENS_TENDONLIMITFRC: + // must be attached to tendon + if (objtype != mjOBJ_TENDON) { + throw mjCError(this, "sensor must be attached to tendon"); + } + + // make sure tendon has limit + if (!((mjCTendon*)obj)->is_limited()) { + throw mjCError(this, "tendon must be limited in sensor"); + } + + // set + dim = 1; + datatype = mjDATATYPE_REAL; + if (type == mjSENS_TENDONLIMITPOS) { + needstage = mjSTAGE_POS; + } else if (type == mjSENS_TENDONLIMITVEL) { + needstage = mjSTAGE_VEL; + } else { + needstage = mjSTAGE_ACC; + } + break; + + case mjSENS_FRAMEPOS: + case mjSENS_FRAMEQUAT: + case mjSENS_FRAMEXAXIS: + case mjSENS_FRAMEYAXIS: + case mjSENS_FRAMEZAXIS: + case mjSENS_FRAMELINVEL: + case mjSENS_FRAMEANGVEL: + case mjSENS_FRAMELINACC: + case mjSENS_FRAMEANGACC: + // must be attached to object with spatial frame + if (objtype != mjOBJ_BODY && objtype != mjOBJ_XBODY && + objtype != mjOBJ_GEOM && objtype != mjOBJ_SITE && objtype != mjOBJ_CAMERA) { + throw mjCError(this, "sensor must be attached to (x)body, geom, site or camera"); + } + + // set dim + if (type == mjSENS_FRAMEQUAT) { + dim = 4; + } else { + dim = 3; + } + + // set datatype + if (type == mjSENS_FRAMEQUAT) { + datatype = mjDATATYPE_QUATERNION; + } else if (type == mjSENS_FRAMEXAXIS || + type == mjSENS_FRAMEYAXIS || + type == mjSENS_FRAMEZAXIS) { + datatype = mjDATATYPE_AXIS; + } else { + datatype = mjDATATYPE_REAL; + } + + // set needstage + if (type == mjSENS_FRAMELINACC || type == mjSENS_FRAMEANGACC) { + needstage = mjSTAGE_ACC; + } else if (type == mjSENS_FRAMELINVEL || type == mjSENS_FRAMEANGVEL) { + needstage = mjSTAGE_VEL; + } else { + needstage = mjSTAGE_POS; + } + break; + + case mjSENS_SUBTREECOM: + case mjSENS_SUBTREELINVEL: + case mjSENS_SUBTREEANGMOM: + // must be attached to body + if (objtype != mjOBJ_BODY) { + throw mjCError(this, "sensor must be attached to body"); + } + + // set + dim = 3; + datatype = mjDATATYPE_REAL; + if (type == mjSENS_SUBTREECOM) { + needstage = mjSTAGE_POS; + } else { + needstage = mjSTAGE_VEL; + } + break; + + case mjSENS_GEOMDIST: + case mjSENS_GEOMNORMAL: + case mjSENS_GEOMFROMTO: + // must be attached to body or geom + if ((objtype != mjOBJ_BODY && objtype != mjOBJ_GEOM) || + (reftype != mjOBJ_BODY && reftype != mjOBJ_GEOM)) { + throw mjCError(this, "sensor must be attached to body or geom"); + } + + // objects must be different + if (objtype == reftype && obj == ref) { + throw mjCError(this, "1st body/geom must be different from 2nd body/geom"); + } + + // height fields are not necessarily convex and are not yet supported + if ((objtype == mjOBJ_GEOM && static_cast(obj)->Type() == mjGEOM_HFIELD) || + (reftype == mjOBJ_GEOM && static_cast(ref)->Type() == mjGEOM_HFIELD)) { + throw mjCError(this, "height fields are not supported in geom distance sensors"); + } + + // set + needstage = mjSTAGE_POS; + if (type == mjSENS_GEOMDIST) { + dim = 1; + datatype = mjDATATYPE_POSITIVE; + } else if (type == mjSENS_GEOMNORMAL) { + dim = 3; + datatype = mjDATATYPE_AXIS; + } else { + dim = 6; + datatype = mjDATATYPE_REAL; + } + break; + + case mjSENS_E_POTENTIAL: + case mjSENS_E_KINETIC: + case mjSENS_CLOCK: + dim = 1; + needstage = mjSTAGE_POS; + datatype = mjDATATYPE_REAL; + break; + + case mjSENS_USER: + // check for negative dim + if (dim < 0) { + throw mjCError(this, "sensor dim must be positive in sensor"); + } + + // make sure dim is consistent with datatype + if (datatype == mjDATATYPE_AXIS && dim != 3) { + throw mjCError(this, + "datatype AXIS requires dim=3 in sensor"); + } + if (datatype == mjDATATYPE_QUATERNION && dim != 4) { + throw mjCError(this, "datatype QUATERNION requires dim=4 in sensor"); + } + break; + + case mjSENS_PLUGIN: + dim = 0; // to be filled in by the plugin later + datatype = mjDATATYPE_REAL; // no noise added to plugin sensors, this attribute is unused + + if (plugin_name.empty() && plugin_instance_name.empty()) { + throw mjCError(this, "neither 'plugin' nor 'instance' is specified for sensor"); + } + + // resolve plugin instance, or create one if using the "plugin" attribute shortcut + { + mjCPlugin* plugin_instance = static_cast(plugin.element); + model->ResolvePlugin(this, plugin_name, plugin_instance_name, &plugin_instance); + plugin.element = plugin_instance; + const mjpPlugin* pplugin = mjp_getPluginAtSlot(plugin_instance->plugin_slot); + if (!(pplugin->capabilityflags & mjPLUGIN_SENSOR)) { + throw mjCError(this, "plugin '%s' does not support sensors", pplugin->name); + } + needstage = static_cast(pplugin->needstage); + } + + break; + + default: + throw mjCError(this, "invalid type in sensor '%s' (id = %d)", name.c_str(), id); } // check cutoff for incompatible data types @@ -6699,7 +6713,7 @@ void mjCNumeric::Compile(void) { CopyFromSpec(); // check for size conflict - if (size && !data_.empty() && size<(int)data_.size()) { + if (size && !data_.empty() && size < (int)data_.size()) { throw mjCError(this, "numeric '%s' (id = %d): specified size smaller than initialization array", name.c_str(), id); @@ -6857,7 +6871,7 @@ void mjCTuple::NameSpace(const mjCModel* m) { if (!name.empty()) { name = m->prefix + name + m->suffix; } - for (int i=0; iprefix + spec_objname_[i] + m->suffix; } } @@ -6903,7 +6917,7 @@ void mjCTuple::ResolveReferences(const mjCModel* m) { obj.resize(objtype_.size()); // find objects, fill in ids - for (int i=0; iFindObject(objtype_[i], objname_[i]); if (!res) { @@ -7033,7 +7047,7 @@ void mjCKey::Compile(const mjModel* m) { // qpos: allocate or check size if (qpos_.empty()) { qpos_.resize(m->nq); - for (int i=0; inq; i++) { + for (int i=0; i < m->nq; i++) { qpos_[i] = (double)m->qpos0[i]; } } else if (qpos_.size() != m->nq) { @@ -7043,7 +7057,7 @@ void mjCKey::Compile(const mjModel* m) { // qvel: allocate or check size if (qvel_.empty()) { qvel_.resize(m->nv); - for (int i=0; inv; i++) { + for (int i=0; i < m->nv; i++) { qvel_[i] = 0; } } else if (qvel_.size() != m->nv) { @@ -7053,7 +7067,7 @@ void mjCKey::Compile(const mjModel* m) { // act: allocate or check size if (act_.empty()) { act_.resize(m->na); - for (int i=0; ina; i++) { + for (int i=0; i < m->na; i++) { act_[i] = 0; } } else if (act_.size() != m->na) { @@ -7064,8 +7078,8 @@ void mjCKey::Compile(const mjModel* m) { if (mpos_.empty()) { mpos_.resize(3*m->nmocap); if (m->nmocap) { - for (int i=0; inbody; i++) { - if (m->body_mocapid[i]>=0) { + for (int i=0; i < m->nbody; i++) { + if (m->body_mocapid[i] >= 0) { int mocapid = m->body_mocapid[i]; mpos_[3*mocapid] = m->body_pos[3*i]; mpos_[3*mocapid+1] = m->body_pos[3*i+1]; @@ -7081,8 +7095,8 @@ void mjCKey::Compile(const mjModel* m) { if (mquat_.empty()) { mquat_.resize(4*m->nmocap); if (m->nmocap) { - for (int i=0; inbody; i++) { - if (m->body_mocapid[i]>=0) { + for (int i=0; i < m->nbody; i++) { + if (m->body_mocapid[i] >= 0) { int mocapid = m->body_mocapid[i]; mquat_[4*mocapid] = m->body_quat[4*i]; mquat_[4*mocapid+1] = m->body_quat[4*i+1]; @@ -7098,7 +7112,7 @@ void mjCKey::Compile(const mjModel* m) { // ctrl: allocate or check size if (ctrl_.empty()) { ctrl_.resize(m->nu); - for (int i=0; inu; i++) { + for (int i=0; i < m->nu; i++) { ctrl_[i] = 0; } } else if (ctrl_.size() != m->nu) { @@ -7157,7 +7171,7 @@ void mjCPlugin::Compile(void) { // clear precompiled flattened_attributes.clear(); - std::map> config_attribs_copy = config_attribs; + std::map > config_attribs_copy = config_attribs; // concatenate all of the plugin's attribute values (as null-terminated strings) into // flattened_attributes, in the order declared in the mjpPlugin @@ -7184,8 +7198,8 @@ void mjCPlugin::Compile(void) { // anything left in xml_attributes at this stage is not a valid attribute if (!config_attribs_copy.empty()) { std::string error = - "unrecognized attribute 'plugin:" + config_attribs_copy.begin()->first + - "' for plugin " + std::string(plugin->name) + "'"; + "unrecognized attribute 'plugin:" + config_attribs_copy.begin()->first + + "' for plugin " + std::string(plugin->name) + "'"; throw mjCError(parent, "%s", error.c_str()); } } diff --git a/src/user/user_resource.cc b/src/user/user_resource.cc index d0928dae..c66bfa87 100644 --- a/src/user/user_resource.cc +++ b/src/user/user_resource.cc @@ -204,7 +204,7 @@ void mju_closeResource(mjResource* resource) { if (provider) { if (provider->close) provider->close(resource); } else { - FileClose(resource); // clear OS filesystem if present + FileClose(resource); // clear OS filesystem if present } // free resource diff --git a/src/user/user_util.cc b/src/user/user_util.cc index 843f0e40..87ad738d 100644 --- a/src/user/user_util.cc +++ b/src/user/user_util.cc @@ -52,11 +52,11 @@ bool mjuu_defined(double num) { // compute address of M[g1][g2] where M is triangular n-by-n int mjuu_matadr(int g1, int g2, int n) { - if (g1<0 || g2<0 || g1>=n || g2>=n) { + if (g1 < 0 || g2 < 0 || g1 >= n || g2 >= n) { return -1; } - if (g1>g2) { + if (g1 > g2) { int tmp = g1; g1 = g2; g2 = tmp; @@ -103,21 +103,21 @@ void mjuu_setvec(double* dest, double x, double y) { // add to double array void mjuu_addtovec(double* dest, const double* src, int n) { - for (int i=0; i mjEPS) { - for (int i=0; i mjEPS) { - for (int i=0; i0) { + if (mat[0][0]+mat[1][1]+mat[2][2] > 0) { quat[0] = 0.5 * sqrt(1 + mat[0][0] + mat[1][1] + mat[2][2]); quat[1] = 0.25 * (mat[1][2] - mat[2][1]) / quat[0]; quat[2] = 0.25 * (mat[2][0] - mat[0][2]) / quat[0]; @@ -415,7 +415,7 @@ void mjuu_frame2quat(double* quat, const double* x, const double* y, const doubl } // q1 largest - else if (mat[0][0]>mat[1][1] && mat[0][0]>mat[2][2]) { + else if (mat[0][0] > mat[1][1] && mat[0][0] > mat[2][2]) { quat[1] = 0.5 * sqrt(1 + mat[0][0] - mat[1][1] - mat[2][2]); quat[0] = 0.25 * (mat[1][2] - mat[2][1]) / quat[1]; quat[2] = 0.25 * (mat[1][0] + mat[0][1]) / quat[1]; @@ -423,7 +423,7 @@ void mjuu_frame2quat(double* quat, const double* x, const double* y, const doubl } // q2 largest - else if (mat[1][1]>mat[2][2]) { + else if (mat[1][1] > mat[2][2]) { quat[2] = 0.5 * sqrt(1 - mat[0][0] + mat[1][1] - mat[2][2]); quat[0] = 0.25 * (mat[2][0] - mat[0][2]) / quat[2]; quat[1] = 0.25 * (mat[1][0] + mat[0][1]) / quat[2]; @@ -612,7 +612,7 @@ void mjuu_rotVecQuat(double res[3], const double vec[3], const double quat[4]) { // quat - frame orientation // normal - unit normal vector double mjuu_updateFrame(double quat[4], double normal[3], const double edge[3], - const double tprv[3], const double tnxt[3], int first) { + const double tprv[3], const double tnxt[3], int first) { double tangent[3], binormal[3]; // normalize tangent @@ -764,7 +764,7 @@ std::string mjuu_strippath(std::string filename) { size_t start = filename.find_last_of("/\\"); // no path found: return original - if (start==std::string::npos) { + if (start == std::string::npos) { return filename; } @@ -791,7 +791,7 @@ const char* mjuu_fullInertia(double quat[4], double inertia[3], const double ful mjuu_eig3(eigval, eigvec, quattmp, full); // check mimimal eigenvalue - if (eigval[2]