Files
Mujoco_WASM/test/engine/engine_collision_box_test.cc
T
Yuval Tassa 7942fe957e Circumvent bug in mjc_BoxBox.
The box-box collider sometimes outputs bad contact points which are far outside one of the two boxes. This happens at configurations which are very rare numerically, but rare configurations (e.g. perfect box alignment) are often induced by the designer and the physics, making them actually not-so-rare.

Since the box collision functions are very difficult to understand, we circumvent the issue by extending the post processing code to detect and remove such contacts.

PiperOrigin-RevId: 589178197
Change-Id: I82b8a888ef04f9196111c960757e3314d0ab6d8e
2023-12-08 10:39:27 -08:00

227 lines
6.8 KiB
C++

// Copyright 2023 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Tests for engine/engine_collision_box.c.
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <mujoco/mjmodel.h>
#include <mujoco/mujoco.h>
#include "test/fixture.h"
#include "src/engine/engine_collision_primitive.h"
#include "src/engine/engine_util_misc.h"
namespace mujoco {
namespace {
using MjCollisionBoxTest = MujocoTest;
using ::testing::NotNull;
static const char* const kBad0FilePath =
"engine/testdata/collision_box/boxbox_bad0.xml";
static const char* const kBad1FilePath =
"engine/testdata/collision_box/boxbox_bad1.xml";
TEST_F(MjCollisionBoxTest, BadContacts) {
for (const char* local_path : {kBad0FilePath, kBad1FilePath}) {
const std::string xml_path = GetTestDataFilePath(local_path);
mjModel* model = mj_loadXML(xml_path.c_str(), nullptr, 0, 0);
ASSERT_THAT(model, NotNull());
mjData* data = mj_makeData(model);
mj_forward(model, data);
// allocate contact array and matching arrays
mj_markStack(data);
mjContact* con_raw = (mjContact*) mj_stackAllocByte(
data, mjMAXCONPAIR * sizeof(mjContact), alignof(mjContact));
int* match_raw = mj_stackAllocInt(data, mjMAXCONPAIR);
int* match = mj_stackAllocInt(data, data->ncon);
int g1 = -1;
int g2 = -1;
for (int c = 0; c < data->ncon; c++) {
mjContact* con = data->contact + c;
int g1new = con->geom[0];
int g2new = con->geom[1];
// not box-box: skip
if (model->geom_type[g1new] != mjGEOM_BOX ||
model->geom_type[g2new] != mjGEOM_BOX) {
continue;
}
// same geom pair: skip
if (g1 == g1new && g2 == g2new) {
continue;
}
g1 = g1new;
g2 = g2new;
// call low-level box-box collider
int num = mjc_BoxBox(model, data, con_raw, g1, g2, con->includemargin);
// allocate and clear arrays marking already matched contacts
mju_zeroInt(match_raw, num);
mju_zeroInt(match, data->ncon);
// loop over raw contacts, match with contact array using pos
int nmatched = 0;
for (int i = 0; i < num; i++) {
for (int j = 0; j < data->ncon; j++) {
if (!match[j] &&
con_raw[i].pos[0] == data->contact[j].pos[0] &&
con_raw[i].pos[1] == data->contact[j].pos[1] &&
con_raw[i].pos[2] == data->contact[j].pos[2]) {
match_raw[i] = match[j] = 1;
nmatched++;
}
}
}
// expect some contacts to have been removed
EXPECT_LT(nmatched, num);
// get box info
const mjtNum* pos1 = data->geom_xpos + 3 * g1;
const mjtNum* mat1 = data->geom_xmat + 9 * g1;
const mjtNum* size1 = model->geom_size + 3 * g1;
const mjtNum* pos2 = data->geom_xpos + 3 * g2;
const mjtNum* mat2 = data->geom_xmat + 9 * g2;
const mjtNum* size2 = model->geom_size + 3 * g2;
// loop over raw contacts, find removed
for (int i = 0; i < num; i++) {
if (!match_raw[i]) {
// === check if outside
// get margin and adjusted sizes
const mjtNum kBoxRemoveMargin = 1.01;
mjtNum sz1[3], sz2[3];
mju_scl3(sz1, size1, kBoxRemoveMargin);
mju_scl3(sz2, size2, kBoxRemoveMargin);
// is contact outside one of the boxes
bool outside = mju_outsideBox(con_raw[i].pos, pos1, mat1, sz1) ||
mju_outsideBox(con_raw[i].pos, pos2, mat2, sz2);
// expect that removed contact was either outside
EXPECT_TRUE(outside);
}
}
}
mj_freeStack(data);
mj_deleteData(data);
mj_deleteModel(model);
}
}
static const char* const kDuplicateFilePath =
"engine/testdata/collision_box/boxbox_duplicate.xml";
TEST_F(MjCollisionBoxTest, DuplicateContacts) {
const std::string xml_path = GetTestDataFilePath(kDuplicateFilePath);
mjModel* model = mj_loadXML(xml_path.c_str(), nullptr, 0, 0);
ASSERT_THAT(model, NotNull());
mjData* data = mj_makeData(model);
mj_forward(model, data);
// allocate contact array and matching arrays
mj_markStack(data);
mjContact* con_raw = (mjContact*) mj_stackAllocByte(
data, mjMAXCONPAIR * sizeof(mjContact), alignof(mjContact));
int* match_raw = mj_stackAllocInt(data, mjMAXCONPAIR);
int* match = mj_stackAllocInt(data, data->ncon);
int g1 = -1;
int g2 = -1;
for (int c = 0; c < data->ncon; c++) {
mjContact* con = data->contact + c;
int g1new = con->geom[0];
int g2new = con->geom[1];
// not box-box: skip
if (model->geom_type[g1new] != mjGEOM_BOX ||
model->geom_type[g2new] != mjGEOM_BOX) {
continue;
}
// same geom pair: skip
if (g1 == g1new && g2 == g2new) {
continue;
}
g1 = g1new;
g2 = g2new;
// call low-level box-box collider
int num = mjc_BoxBox(model, data, con_raw, g1, g2, con->includemargin);
// allocate and clear arrays marking already matched contacts
mju_zeroInt(match_raw, num);
mju_zeroInt(match, data->ncon);
// loop over raw contacts, match with contact array using pos
int nmatched = 0;
for (int i = 0; i < num; i++) {
for (int j = 0; j < data->ncon; j++) {
if (!match[j] &&
con_raw[i].pos[0] == data->contact[j].pos[0] &&
con_raw[i].pos[1] == data->contact[j].pos[1] &&
con_raw[i].pos[2] == data->contact[j].pos[2]) {
match_raw[i] = match[j] = 1;
nmatched++;
}
}
}
// expect some contacts to have been removed
EXPECT_LT(nmatched, num);
// loop over raw contacts, find removed
for (int i = 0; i < num; i++) {
if (!match_raw[i]) {
// === check if duplicate
bool duplicate = false;
for (int j = 0; j < num; j++) {
if (duplicate || i == j) {
continue;
}
if (con_raw[i].pos[0] == con_raw[j].pos[0] &&
con_raw[i].pos[1] == con_raw[j].pos[1] &&
con_raw[i].pos[2] == con_raw[j].pos[2]) {
duplicate = true;
}
}
// expect that removed contact was duplicated
EXPECT_TRUE(duplicate);
}
}
}
mj_freeStack(data);
mj_deleteData(data);
mj_deleteModel(model);
}
} // namespace
} // namespace mujoco