1 /*************************************************************************
3 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of EITHER: *
8 * (1) The GNU Lesser General Public License as published by the Free *
9 * Software Foundation; either version 2.1 of the License, or (at *
10 * your option) any later version. The text of the GNU Lesser *
11 * General Public License is included with this library in the *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
16 * This library is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19 * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
21 *************************************************************************/
23 #include <ode/collision.h>
27 #if dTRIMESH_ENABLED && dTRIMESH_GIMPACT
29 #include "gimpact_contact_export_helper.h"
34 dReal
dxGImpactContactsExportHelper::FindContactsMarginalDepth(dReal
*pdepths
, unsigned contactcount
, unsigned maxcontacts
, dReal mindepth
, dReal maxdepth
)
40 dReal firstdepth
= REAL(0.5) * (mindepth
+ maxdepth
);
41 dReal lowdepth
= maxdepth
, highdepth
= mindepth
;
43 unsigned marginindex
= 0;
44 unsigned highindex
= marginindex
;
45 dIASSERT(contactcount
!= 0);
47 for (unsigned i
= 0; i
< contactcount
; i
++)
49 dReal depth
= pdepths
[i
];
51 if (depth
< firstdepth
)
53 dReal temp
= pdepths
[marginindex
]; pdepths
[highindex
++] = temp
; pdepths
[marginindex
++] = depth
;
54 if (highdepth
< depth
) { highdepth
= depth
; }
56 else if (depth
> firstdepth
)
58 pdepths
[highindex
++] = depth
;
59 if (depth
< lowdepth
) { lowdepth
= depth
; }
63 unsigned countabove
= highindex
- marginindex
;
64 if (maxcontacts
< countabove
)
66 contactcount
= countabove
;
67 pdepths
+= marginindex
;
70 else if (maxcontacts
== countabove
)
72 result
= dNextAfter(firstdepth
, dInfinity
);
77 unsigned countbelow
= marginindex
;
78 if (maxcontacts
<= contactcount
- countbelow
)
84 maxcontacts
-= contactcount
- countbelow
;
85 contactcount
= countbelow
;
94 #endif // #if dTRIMESH_ENABLED && dTRIMESH_GIMPACT