Skip to content

Commit 600f4ac

Browse files
kbayescopybara-github
authored andcommitted
Change GJK and EPA return values.
PiperOrigin-RevId: 717549079 Change-Id: I42df5660bcc8336e2e9c8fce71385cbb06104848
1 parent 5c4c79c commit 600f4ac

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/engine/engine_collision_gjk.c

+25-23
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ static mjtNum attachFace(Polytope* pt, int v1, int v2, int v3, int adj1, int adj
8181
// status must have initial tetrahedrons
8282
static int gjkIntersect(mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2);
8383

84-
// return the penetration depth of two convex objects; witness points are in status->{x1, x2}
85-
static mjtNum epa(mjCCDStatus* status, Polytope* pt, mjCCDObj* obj1, mjCCDObj* obj2);
84+
// return a face of the expanded polytope that best approximates the pentration depth
85+
// witness points are in status->{x1, x2}
86+
static Face* epa(mjCCDStatus* status, Polytope* pt, mjCCDObj* obj1, mjCCDObj* obj2);
8687

8788
// -------------------------------- inlined 3D vector utils --------------------------------------
8889

@@ -145,7 +146,7 @@ static int discreteGeoms(mjCCDObj* obj1, mjCCDObj* obj2) {
145146

146147

147148
// GJK algorithm
148-
static mjtNum gjk(mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2) {
149+
static void gjk(mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2) {
149150
int get_dist = status->dist_cutoff > 0; // need to recover geom distances if not in contact
150151
int backup_gjk = !get_dist; // use gjkIntersect if no geom distances needed
151152
mjtNum *simplex1 = status->simplex1; // simplex for obj1
@@ -192,7 +193,7 @@ static mjtNum gjk(mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2) {
192193
status->nsimplex = 0;
193194
status->nx = 0;
194195
status->dist = mjMAXVAL;
195-
return status->dist;
196+
return;
196197
}
197198
} else if (status->dist_cutoff < mjMAXVAL) {
198199
mjtNum vs = mju_dot3(x_k, s_k), vv = mju_dot3(x_k, x_k);
@@ -201,7 +202,7 @@ static mjtNum gjk(mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2) {
201202
status->nsimplex = 0;
202203
status->nx = 0;
203204
status->dist = mjMAXVAL;
204-
return status->dist;
205+
return;
205206
}
206207
}
207208

@@ -213,7 +214,7 @@ static mjtNum gjk(mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2) {
213214
if (ret != -1) {
214215
status->nx = 0;
215216
status->dist = ret > 0 ? 0 : mjMAXVAL;
216-
return status->dist;
217+
return;
217218
}
218219
k = status->gjk_iterations;
219220
backup_gjk = 0;
@@ -259,7 +260,6 @@ static mjtNum gjk(mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2) {
259260
status->gjk_iterations = k;
260261
status->nsimplex = n;
261262
status->dist = mju_norm3(x_k);
262-
return status->dist;
263263
}
264264

265265

@@ -1294,8 +1294,9 @@ static void epaWitness(const Polytope* pt, const Face* face, mjtNum x1[3], mjtNu
12941294

12951295

12961296

1297-
// return the penetration depth of two convex objects; witness points are in status->{x1, x2}
1298-
static mjtNum epa(mjCCDStatus* status, Polytope* pt, mjCCDObj* obj1, mjCCDObj* obj2) {
1297+
// return a face of the expanded polytope that best approximates the pentration depth
1298+
// witness points are in status->{x1, x2}
1299+
static Face* epa(mjCCDStatus* status, Polytope* pt, mjCCDObj* obj1, mjCCDObj* obj2) {
12991300
mjtNum tolerance = status->tolerance, lower, upper = FLT_MAX;
13001301
int k, kmax = status->max_iterations;
13011302
mjData* d = (mjData*) obj1->data;
@@ -1368,7 +1369,8 @@ static mjtNum epa(mjCCDStatus* status, Polytope* pt, mjCCDObj* obj1, mjCCDObj* o
13681369
mj_freeStack(d);
13691370
status->epa_iterations = k;
13701371
status->nx = 0;
1371-
return 0;
1372+
status->dist = 0;
1373+
return NULL;
13721374
}
13731375

13741376
// store face in map
@@ -1395,7 +1397,8 @@ static mjtNum epa(mjCCDStatus* status, Polytope* pt, mjCCDObj* obj1, mjCCDObj* o
13951397
mj_freeStack(d);
13961398
status->epa_iterations = k;
13971399
status->nx = 0;
1398-
return 0;
1400+
status->dist = 0;
1401+
return NULL;
13991402
}
14001403

14011404
// store face in map
@@ -1417,7 +1420,8 @@ static mjtNum epa(mjCCDStatus* status, Polytope* pt, mjCCDObj* obj1, mjCCDObj* o
14171420
epaWitness(pt, face, status->x1, status->x2);
14181421
status->epa_iterations = k;
14191422
status->nx = 1;
1420-
return face->dist;
1423+
status->dist = -face->dist;
1424+
return face;
14211425
}
14221426

14231427

@@ -1444,8 +1448,7 @@ static inline void inflate(mjCCDStatus* status, mjtNum margin1, mjtNum margin2)
14441448

14451449
// general convex collision detection
14461450
mjtNum mjc_ccd(const mjCCDConfig* config, mjCCDStatus* status, mjCCDObj* obj1, mjCCDObj* obj2) {
1447-
// set up
1448-
mjtNum dist;
1451+
// setup
14491452
obj1->center(status->x1, obj1);
14501453
obj2->center(status->x2, obj2);
14511454
status->gjk_iterations = 0;
@@ -1488,11 +1491,11 @@ mjtNum mjc_ccd(const mjCCDConfig* config, mjCCDStatus* status, mjCCDObj* obj1, m
14881491
}
14891492

14901493
status->dist_cutoff += margin1 + margin2;
1491-
dist = gjk(status, obj1, obj2);
1494+
gjk(status, obj1, obj2);
14921495
status->dist_cutoff = config->dist_cutoff;
14931496

14941497
// shallow penetration, inflate contact
1495-
if (dist > 0) {
1498+
if (status->dist > 0) {
14961499
inflate(status, margin1, margin2);
14971500
if (status->dist > status->dist_cutoff) {
14981501
status->dist = mjMAXVAL;
@@ -1515,14 +1518,15 @@ mjtNum mjc_ccd(const mjCCDConfig* config, mjCCDStatus* status, mjCCDObj* obj1, m
15151518
obj2->center(status->x2, obj2);
15161519
}
15171520

1518-
dist = gjk(status, obj1, obj2);
1521+
gjk(status, obj1, obj2);
15191522

15201523
// penetration recovery for contacts not needed
15211524
if (!config->max_contacts) {
1522-
return dist;
1525+
return status->dist;
15231526
}
15241527

1525-
if (dist <= config->tolerance && status->nsimplex > 1) {
1528+
if (status->dist <= config->tolerance && status->nsimplex > 1) {
1529+
status->dist = 0; // assume touching
15261530
int N = status->max_iterations;
15271531
mjData* d = (mjData*) obj1->data;
15281532
mj_markStack((mjData*) obj1->data);
@@ -1561,11 +1565,9 @@ mjtNum mjc_ccd(const mjCCDConfig* config, mjCCDStatus* status, mjCCDObj* obj1, m
15611565

15621566
// simplex not on boundary (objects are penetrating)
15631567
if (!ret) {
1564-
dist = -epa(status, &pt, obj1, obj2);
1565-
} else {
1566-
dist = 0;
1568+
epa(status, &pt, obj1, obj2);
15671569
}
15681570
mj_freeStack(d);
15691571
}
1570-
return dist;
1572+
return status->dist;
15711573
}

0 commit comments

Comments
 (0)