Replace mjMIN and mjMAX with mju_min and mju_max, where possible.

PiperOrigin-RevId: 566602924
Change-Id: I2d24183d0d9df3e647bb2d7d7544473582f38128
This commit is contained in:
Yuval Tassa
2023-09-19 05:39:55 -07:00
committed by Copybara-Service
parent 9cf1f6eba4
commit dfd48dd821
9 changed files with 45 additions and 50 deletions
+11 -12
View File
@@ -15,7 +15,6 @@
#include "engine/engine_derivative.h"
#include <mujoco/mjdata.h>
#include <mujoco/mjmacro.h>
#include <mujoco/mjmodel.h>
#include "engine/engine_core_constraint.h"
#include "engine/engine_crossplatform.h"
@@ -829,7 +828,7 @@ static mjtNum mjd_muscleGain_vel(mjtNum len, mjtNum vel, const mjtNum lengthrang
// scale force if negative
if (force < 0) {
force = scale / mjMAX(mjMINVAL, acc0);
force = scale / mju_max(mjMINVAL, acc0);
}
// mid-ranges
@@ -838,25 +837,25 @@ static mjtNum mjd_muscleGain_vel(mjtNum len, mjtNum vel, const mjtNum lengthrang
mjtNum x;
// optimum length
mjtNum L0 = (lengthrange[1]-lengthrange[0]) / mjMAX(mjMINVAL, range[1]-range[0]);
mjtNum L0 = (lengthrange[1]-lengthrange[0]) / mju_max(mjMINVAL, range[1]-range[0]);
// normalized length and velocity
mjtNum L = range[0] + (len-lengthrange[0]) / mjMAX(mjMINVAL, L0);
mjtNum V = vel / mjMAX(mjMINVAL, L0*vmax);
mjtNum L = range[0] + (len-lengthrange[0]) / mju_max(mjMINVAL, L0);
mjtNum V = vel / mju_max(mjMINVAL, L0*vmax);
// length curve
mjtNum FL = 0;
if (L >= lmin && L <= a) {
x = (L-lmin) / mjMAX(mjMINVAL, a-lmin);
x = (L-lmin) / mju_max(mjMINVAL, a-lmin);
FL = 0.5*x*x;
} else if (L <= 1) {
x = (1-L) / mjMAX(mjMINVAL, 1-a);
x = (1-L) / mju_max(mjMINVAL, 1-a);
FL = 1 - 0.5*x*x;
} else if (L <= b) {
x = (L-1) / mjMAX(mjMINVAL, b-1);
x = (L-1) / mju_max(mjMINVAL, b-1);
FL = 1 - 0.5*x*x;
} else if (L <= lmax) {
x = (lmax-L) / mjMAX(mjMINVAL, lmax-b);
x = (lmax-L) / mju_max(mjMINVAL, lmax-b);
FL = 0.5*x*x;
}
@@ -870,15 +869,15 @@ static mjtNum mjd_muscleGain_vel(mjtNum len, mjtNum vel, const mjtNum lengthrang
// FV = (V+1)*(V+1)
dFV = 2*V + 2;
} else if (V <= y) {
// FV = fvmax - (y-V)*(y-V) / mjMAX(mjMINVAL, y)
dFV = (-2*V + 2*y) / mjMAX(mjMINVAL, y);
// FV = fvmax - (y-V)*(y-V) / mju_max(mjMINVAL, y)
dFV = (-2*V + 2*y) / mju_max(mjMINVAL, y);
} else {
// FV = fvmax
dFV = 0;
}
// compute FVL and scale, make it negative
return -force*FL*dFV/mjMAX(mjMINVAL, L0*vmax);
return -force*FL*dFV/mju_max(mjMINVAL, L0*vmax);
}
+1 -2
View File
@@ -17,7 +17,6 @@
#include <stddef.h>
#include <mujoco/mjdata.h>
#include <mujoco/mjmacro.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mjplugin.h>
#include "engine/engine_callback.h"
@@ -59,7 +58,7 @@ static void add_noise(const mjModel* m, mjData* d, mjtStage stage) {
if (m->sensor_datatype[i] == mjDATATYPE_POSITIVE) {
// add noise only if positive, keep it positive
if (d->sensordata[adr+j] > 0) {
d->sensordata[adr+j] = mjMAX(0, d->sensordata[adr+j]+rnd[0]*noise);
d->sensordata[adr+j] = mju_max(0, d->sensordata[adr+j]+rnd[0]*noise);
}
}
+1 -2
View File
@@ -19,7 +19,6 @@
#include <string.h>
#include <mujoco/mjdata.h>
#include <mujoco/mjmacro.h>
#include <mujoco/mjmodel.h>
#include "engine/engine_core_constraint.h"
#include "engine/engine_crossplatform.h"
@@ -1543,7 +1542,7 @@ mjtNum mj_getTotalmass(const mjModel* m) {
// scale all body masses and inertias to achieve specified total mass
void mj_setTotalmass(mjModel* m, mjtNum newmass) {
// compute scale factor, avoid zeros
mjtNum scale = mjMAX(mjMINVAL, newmass / mjMAX(mjMINVAL, mj_getTotalmass(m)));
mjtNum scale = mju_max(mjMINVAL, newmass / mju_max(mjMINVAL, mj_getTotalmass(m)));
// scale all masses and inertias
for (int i=1; i < m->nbody; i++) {
-1
View File
@@ -22,7 +22,6 @@
#include <mujoco/mjmacro.h>
#include <mujoco/mjvisualize.h>
#include <mujoco/mujoco.h>
#include "engine/engine_array_safety.h"
#include "engine/engine_crossplatform.h"
#include "engine/engine_vis_init.h"
#include "render/render_context.h"
+1 -1
View File
@@ -1260,7 +1260,7 @@ void mjCComposite::BoxProject(double* pos) {
// cylinder
else if (type==mjCOMPTYPE_CYLINDER) {
double L0 = mjMAX(mju_abs(pos[0]), mju_abs(pos[1]));
double L0 = mju_max(mju_abs(pos[0]), mju_abs(pos[1]));
mjuu_normvec(pos, 2);
pos[0] *= size[0]*L0;
pos[1] *= size[1]*L0;
+8 -8
View File
@@ -13,7 +13,6 @@
// limitations under the License.
#include <algorithm>
#include <array>
#include <cmath>
#include <csetjmp>
#include <cstddef>
@@ -53,6 +52,7 @@
#include "engine/engine_resource.h"
#include "engine/engine_util_blas.h"
#include "engine/engine_util_errmem.h"
#include "engine/engine_util_misc.h"
#include "engine/engine_util_solve.h"
#include "engine/engine_util_spatial.h"
#include "user/user_model.h"
@@ -709,7 +709,7 @@ void mjCMesh::FitGeom(mjCGeom* geom, double* meshpos) {
case mjGEOM_CAPSULE:
geom->size[0] = (boxsz[0] + boxsz[1])/2;
geom->size[1] = mjMAX(0, boxsz[2] - geom->size[0]/2);
geom->size[1] = mju_max(0, boxsz[2] - geom->size[0]/2);
break;
case mjGEOM_CYLINDER:
@@ -747,7 +747,7 @@ void mjCMesh::FitGeom(mjCGeom* geom, double* meshpos) {
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] = mjMAX(geom->size[0], dst);
geom->size[0] = mju_max(geom->size[0], dst);
}
break;
@@ -760,11 +760,11 @@ void mjCMesh::FitGeom(mjCGeom* geom, double* meshpos) {
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] = mjMAX(geom->size[0], dst);
geom->size[0] = mju_max(geom->size[0], dst);
// proceed with z: valid for cylinder
double dst2 = fabs(v[2]-cen[2]);
geom->size[1] = mjMAX(geom->size[1], dst2);
geom->size[1] = mju_max(geom->size[1], dst2);
}
// special handling of capsule: consider curved cap
@@ -779,7 +779,7 @@ void mjCMesh::FitGeom(mjCGeom* geom, double* meshpos) {
// get spherical elevation at horizontal distance dst
double h = geom->size[0] * sin(acos(dst/geom->size[0]));
geom->size[1] = mjMAX(geom->size[1], dst2-h);
geom->size[1] = mju_max(geom->size[1], dst2-h);
}
}
break;
@@ -1399,8 +1399,8 @@ void mjCMesh::Process() {
vert_[3*i+j] = (float) res[j];
// axis-aligned bounding box
aabb_[j+0] = mjMIN(aabb_[j+0], res[j]);
aabb_[j+3] = mjMAX(aabb_[j+3], res[j]);
aabb_[j+0] = mju_min(aabb_[j+0], res[j]);
aabb_[j+3] = mju_max(aabb_[j+3], res[j]);
}
}
for (int i=0; i<nnormal_; i++) {
+14 -14
View File
@@ -946,10 +946,10 @@ void mjCBody::Compile(void) {
// check and correct mass and inertia
if (id>0) {
// fix minimum
mass = mjMAX(mass, model->boundmass);
inertia[0] = mjMAX(inertia[0], model->boundinertia);
inertia[1] = mjMAX(inertia[1], model->boundinertia);
inertia[2] = mjMAX(inertia[2], model->boundinertia);
mass = mju_max(mass, model->boundmass);
inertia[0] = mju_max(inertia[0], model->boundinertia);
inertia[1] = mju_max(inertia[1], model->boundinertia);
inertia[2] = mju_max(inertia[2], model->boundinertia);
// check for negative values
if (mass<0 || inertia[0]<0 || inertia[1]<0 ||inertia[2]<0) {
@@ -1438,7 +1438,7 @@ double mjCGeom::GetRBound(void) {
return sqrt(size[0]*size[0]+size[1]*size[1]);
case mjGEOM_ELLIPSOID:
return mjMAX(mjMAX(size[0], size[1]), size[2]);
return mju_max(mju_max(size[0], size[1]), size[2]);
case mjGEOM_BOX:
return sqrt(size[0]*size[0]+size[1]*size[1]+size[2]*size[2]);
@@ -1446,9 +1446,9 @@ double mjCGeom::GetRBound(void) {
case mjGEOM_MESH:
case mjGEOM_SDF:
aabb = model->meshes[meshid]->aabb();
haabb[0] = mjMAX(fabs(aabb[0]), fabs(aabb[3]));
haabb[1] = mjMAX(fabs(aabb[1]), fabs(aabb[4]));
haabb[2] = mjMAX(fabs(aabb[2]), fabs(aabb[5]));
haabb[0] = mju_max(fabs(aabb[0]), fabs(aabb[3]));
haabb[1] = mju_max(fabs(aabb[1]), fabs(aabb[4]));
haabb[2] = mju_max(fabs(aabb[2]), fabs(aabb[5]));
return sqrt(haabb[0]*haabb[0] + haabb[1]*haabb[1] + haabb[2]*haabb[2]);
default:
@@ -1761,9 +1761,9 @@ void mjCGeom::Compile(void) {
size[2] = 0.5*(model->hfields[hfieldid]->size[2]+model->hfields[hfieldid]->size[3]);
} else if (type==mjGEOM_MESH || type==mjGEOM_SDF) {
const double* aabb = model->meshes[meshid]->aabb();
size[0] = mjMAX(fabs(aabb[0]), fabs(aabb[3]));
size[1] = mjMAX(fabs(aabb[1]), fabs(aabb[4]));
size[2] = mjMAX(fabs(aabb[2]), fabs(aabb[5]));
size[0] = mju_max(fabs(aabb[0]), fabs(aabb[3]));
size[1] = mju_max(fabs(aabb[1]), fabs(aabb[4]));
size[2] = mju_max(fabs(aabb[2]), fabs(aabb[5]));
}
for (double s : size) {
@@ -3132,9 +3132,9 @@ void mjCPair::Compile(void) {
// friction: max
if (!mjuu_defined(friction[0])) {
friction[0] = friction[1] = mjMAX(pg1->friction[0], pg2->friction[0]);
friction[2] = mjMAX(pg1->friction[1], pg2->friction[1]);
friction[3] = friction[4] = mjMAX(pg1->friction[2], pg2->friction[2]);
friction[0] = friction[1] = mju_max(pg1->friction[0], pg2->friction[0]);
friction[2] = mju_max(pg1->friction[1], pg2->friction[1]);
friction[3] = friction[4] = mju_max(pg1->friction[2], pg2->friction[2]);
}
// solver mix factor
+4 -4
View File
@@ -22,10 +22,10 @@
#include <string>
#include <string_view>
#include <mujoco/mjmacro.h>
#include <mujoco/mjtnum.h>
#include <mujoco/mujoco.h>
#include "engine/engine_crossplatform.h"
#include "engine/engine_util_misc.h"
#include "engine/engine_util_spatial.h"
using std::isnan;
@@ -443,9 +443,9 @@ void mjuu_offcenter(double* res, const double mass, const double* vec) {
void mjuu_visccoef(double* visccoef, double mass, const double* inertia, double scl) {
// compute equivalent box
double equivbox[3];
equivbox[0] = sqrt(mjMAX(mjMINVAL, (inertia[1] + inertia[2] - inertia[0])) / mass * 6.0);
equivbox[1] = sqrt(mjMAX(mjMINVAL, (inertia[0] + inertia[2] - inertia[1])) / mass * 6.0);
equivbox[2] = sqrt(mjMAX(mjMINVAL, (inertia[0] + inertia[1] - inertia[2])) / mass * 6.0);
equivbox[0] = sqrt(mju_max(mjMINVAL, (inertia[1] + inertia[2] - inertia[0])) / mass * 6.0);
equivbox[1] = sqrt(mju_max(mjMINVAL, (inertia[0] + inertia[2] - inertia[1])) / mass * 6.0);
equivbox[2] = sqrt(mju_max(mjMINVAL, (inertia[0] + inertia[1] - inertia[2])) / mass * 6.0);
// apply formula for box (or rather cross) viscosity
+5 -6
View File
@@ -14,13 +14,10 @@
#include "xml/xml_native_reader.h"
#include <cfloat>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <optional>
@@ -2314,10 +2311,12 @@ void mjXReader::Custom(XMLElement* section) {
// read attributes
ReadAttrTxt(elem, "name", pnum->name, true);
if (ReadAttrInt(elem, "size", &pnum->size))
for (int i=0; i<mjMIN(pnum->size, 500); i++) {
if (ReadAttrInt(elem, "size", &pnum->size)) {
int sz = pnum->size < 500 ? pnum->size : 500;
for (int i=0; i<sz; i++) {
data[i] = 0;
} else {
}
} else {
pnum->size = 501;
}
int len = ReadAttr(elem, "data", pnum->size, data, text, false, false);