Replace double- or single-precision-denominated function calls in the engine with the equivalent mjtNum-denominated calls. This avoids unnecessary casting or precision loss when MuJoCo is compiled for 32-bit float precision. Accordingly, remove #include <math.h> from all engine source files.
build / windows-2022 (push) Has been cancelled
build / macos-12 (push) Has been cancelled
build / ubuntu-20.04-clang-10 (push) Has been cancelled
build / ubuntu-20.04-clang-11 (push) Has been cancelled
build / ubuntu-20.04-clang-12 (push) Has been cancelled
build / ubuntu-22.04-clang-13 (push) Has been cancelled
build / ubuntu-22.04-clang-14 (push) Has been cancelled
build / ubuntu-22.04-gcc-10 (push) Has been cancelled
build / ubuntu-22.04-gcc-11 (push) Has been cancelled
build / ubuntu-22.04-gcc-12 (push) Has been cancelled
build / ubuntu-22.04-gcc-9 (push) Has been cancelled

Also, minor type-related fixes in `user_mesh.cc`.

PiperOrigin-RevId: 675082329
Change-Id: Icc5eb9f9eb4fbf09b08d7d4b9b2929f414e4a75c
This commit is contained in:
Yuval Tassa
2024-09-16 03:39:11 -07:00
committed by Copybara-Service
parent dde25d039b
commit fd17b2144a
12 changed files with 52 additions and 64 deletions
+22 -24
View File
@@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <math.h>
#include <string.h>
#include <mujoco/mjmacro.h>
#include <mujoco/mujoco.h>
#include "engine/engine_collision_primitive.h"
#include "engine/engine_util_blas.h"
@@ -65,9 +63,9 @@ int mjraw_SphereBox(mjContact* con, mjtNum margin,
closest = (size2[0] + size2[1] + size2[2]) * 2;
for (i = 0; i < 6; i++)
if (closest > fabs((i % 2 ? 1 : -1)*size2[i / 2] - center[i / 2]))
if (closest > mju_abs((i % 2 ? 1 : -1)*size2[i / 2] - center[i / 2]))
{
closest = fabs((i % 2 ? 1 : -1) * size2[i / 2] - center[i / 2]);
closest = mju_abs((i % 2 ? 1 : -1) * size2[i / 2] - center[i / 2]);
k = i;
}
@@ -240,7 +238,7 @@ int mjraw_CapsuleBox(mjContact* con, mjtNum margin,
v = mju_dot3(halfaxis, dif);
det = ma * mc - mb * mb;
if (fabs(det) < mjMINVAL)
if (mju_abs(det) < mjMINVAL)
continue;
idet = 1 / det;
@@ -374,7 +372,7 @@ int mjraw_CapsuleBox(mjContact* con, mjtNum margin,
// c.x = s.x * ((c1 / 2) ? -1 : 1);
// c.y = s.y * ((c1 % 2) ? -1 : 1);
ee1 = fabs(w) / l;
ee1 = mju_abs(w) / l;
// e2 = best / l;
// printf("%g %g %g %g %g %g\n",c.x,c.y,d.x,d.y,e1,e2);
@@ -453,7 +451,7 @@ int mjraw_CapsuleBox(mjContact* con, mjtNum margin,
if (axis[ax]*axis[ax] > 0.5) { // second point along the edge of the box
secondpos = de; // initial position from the
e1 = 2 * size2[ax] / fabs(halfaxis[ax]);
e1 = 2 * size2[ax] / mju_abs(halfaxis[ax]);
if (e1 < secondpos) {
secondpos = e1; // we overshoot, move back to the other corner of the edge
@@ -464,11 +462,11 @@ int mjraw_CapsuleBox(mjContact* con, mjtNum margin,
// check for overshoot again
e1 = 2 * size2[ax1] / fabs(halfaxis[ax1]);
e1 = 2 * size2[ax1] / mju_abs(halfaxis[ax1]);
if (e1 < secondpos)
secondpos = e1;
e1 = 2 * size2[ax2] / fabs(halfaxis[ax2]);
e1 = 2 * size2[ax2] / mju_abs(halfaxis[ax2]);
if (e1 < secondpos)
secondpos = e1;
@@ -502,7 +500,7 @@ int mjraw_CapsuleBox(mjContact* con, mjtNum margin,
// Then it finds with which face the capsule has a lower angle and switches the axis names
if (fabs(axis[ax1]) > fabs(axis[ax2]))
if (mju_abs(axis[ax1]) > mju_abs(axis[ax2]))
ax1 = ax2;
ax2 = 3 - ax - ax1;
@@ -522,7 +520,7 @@ int mjraw_CapsuleBox(mjContact* con, mjtNum margin,
// now we have to find out whether we point towards the opposite side or towards one of the
// sides and also find the farthest point along the capsule that is above the box
e1 = 2 * size2[ax2] / fabs(halfaxis[ax2]);
e1 = 2 * size2[ax2] / mju_abs(halfaxis[ax2]);
if (e1 < secondpos)
secondpos = e1;
@@ -531,7 +529,7 @@ int mjraw_CapsuleBox(mjContact* con, mjtNum margin,
else
e2 = 1 + bestboxpos;
e1 = size2[ax] * e2 / fabs(halfaxis[ax]);
e1 = size2[ax] * e2 / mju_abs(halfaxis[ax]);
if (e1 < secondpos)
secondpos = e1;
@@ -642,9 +640,9 @@ int mjc_BoxBox(const mjModel* M, const mjData* D, mjContact* con, int g1, int g2
mju_transpose(rott, rot, 3, 3);
for (i = 0; i < 9; i++)
rotabs[i] = fabs(rot[i]);
rotabs[i] = mju_abs(rot[i]);
for (i = 0; i < 9; i++)
rottabs[i] = fabs(rott[i]);
rottabs[i] = mju_abs(rott[i]);
mju_mulMatVec3(plen2, rotabs, size2);
mju_mulMatTVec3(plen1, rotabs, size1);
@@ -653,8 +651,8 @@ int mjc_BoxBox(const mjModel* M, const mjData* D, mjContact* con, int g1, int g2
penetration += size1[i] * 3 + size2[i] * 3;
for (i = 0; i < 3; i++) {
c1 = -fabs(pos21[i]) + size1[i] + plen2[i];
c2 = -fabs(pos12[i]) + size2[i] + plen1[i];
c1 = -mju_abs(pos21[i]) + size1[i] + plen2[i];
c2 = -mju_abs(pos12[i]) + size2[i] + plen1[i];
if (c1 < -margin || c2 < -margin)
return 0;
@@ -697,12 +695,12 @@ int mjc_BoxBox(const mjModel* M, const mjData* D, mjContact* con, int g1, int g2
for (k = 0; k < 3; k++)
if (k != i)
c3 += size1[k] * fabs(tmp2[k]);
c3 += size1[k] * mju_abs(tmp2[k]);
for (k = 0; k < 3; k++)
if (k != j)
c3 += size2[k] * rotabs[3 * i + 3 - k - j] / c1;
c3 -= fabs(c2);
c3 -= mju_abs(c2);
if (c3 < -margin)
return 0;
@@ -851,7 +849,7 @@ int mjc_BoxBox(const mjModel* M, const mjData* D, mjContact* con, int g1, int g2
mju_copy3(pts[m++], lp);
for (i = 0; i < 3; i++)
if (fabs(r[6 + i]) < 0.5)
if (mju_abs(r[6 + i]) < 0.5)
mju_scl3(pts[m++], rt + 3 * i, s[i] * ((clcorner & (1 << i)) ? -2 : 2));
mju_add3(pts[3], pts[0], pts[1]);
@@ -880,14 +878,14 @@ int mjc_BoxBox(const mjModel* M, const mjData* D, mjContact* con, int g1, int g2
c = lines[i][1 - q];
d = lines[i][4 - q];
if (fabs(b) > mjMINVAL) {
if (mju_abs(b) > mjMINVAL) {
for (j = -1; j <= 1; j += 2) {
l = ss[q] * j;
c1 = (l - a) * (1 / b);
if (c1 < 0 || c1 > 1)
continue;
c2 = c + d * c1;
if (fabs(c2) > ss[1 - q])
if (mju_abs(c2) > ss[1 - q])
continue;
mju_copy3(points[n], lines[i]);
@@ -1123,7 +1121,7 @@ edgeedge:
mju_sub3(axi[2], points[2], points[0]);
if (fabs(rnorm[2]) < mjMINVAL)
if (mju_abs(rnorm[2]) < mjMINVAL)
return 0; // shouldn't happen
innorm = (1 / rnorm[2]) * (in ? -1 : 1);
@@ -1183,7 +1181,7 @@ edgeedge:
c = lines[i][1 - q];
d = lines[i][4 - q];
if (fabs(b) > mjMINVAL) {
if (mju_abs(b) > mjMINVAL) {
for (j = -1; j <= 1; j += 2) {
if (n < mjMAXCONPAIR) {
l = s[q] * j;
@@ -1191,7 +1189,7 @@ edgeedge:
if (c1 < 0 || c1 > 1)
continue;
c2 = c + d * c1;
if (fabs(c2) > s[1 - q])
if (mju_abs(c2) > s[1 - q])
continue;
if ((linesu[i][2] + linesu[i][5]*c1)*innorm > margin)