Skip to content

Commit bdfd995

Browse files
Make constants::eps and variants constexpr
In the constants, pi() and phi() were declared constexpr but eps and its variants were not. This was an oversight; there is no reason they couldn't be declared constexpr. Furthermore, call sites have been updated to clearly state that the values are constexpr so future developers can easily recognize that no runtime cost is paid to acquire those values of epsilon.
1 parent 0d98b83 commit bdfd995

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

include/fcl/math/constants.h

+10-15
Original file line numberDiff line numberDiff line change
@@ -142,36 +142,31 @@ static constexpr S phi() { return S(1.618033988749894848204586834365638117720309
142142
/// for floats (2e-14 vs 9e-7, respectively). The choice of ε^(7/8) as the
143143
/// default GJK tolerance reflects a tolerance that is a *slightly* tighter
144144
/// bound than the historical value of 1e-6 used for 32-bit floats.
145-
static Real gjk_default_tolerance() {
146-
static const Real value = eps_78();
147-
return value;
145+
static constexpr Real gjk_default_tolerance() {
146+
return eps_78();
148147
}
149148

150149
/// Returns ε for the precision of the underlying scalar type.
151-
static Real eps() {
150+
static constexpr Real eps() {
152151
static_assert(std::is_floating_point<Real>::value,
153152
"Constants can only be evaluated for scalars with floating "
154153
"point implementations");
155-
static const Real value = std::numeric_limits<Real>::epsilon();
156-
return value;
154+
return std::numeric_limits<Real>::epsilon();
157155
}
158156

159157
/// Returns ε^(7/8) for the precision of the underlying scalar type.
160-
static Real eps_78() {
161-
static const Real value = std::pow(eps(), 7./8.);
162-
return value;
158+
static constexpr Real eps_78() {
159+
return std::pow(eps(), 7./8.);
163160
}
164161

165162
/// Returns ε^(3/4) for the precision of the underlying scalar type.
166-
static Real eps_34() {
167-
static const Real value = std::pow(eps(), 3./4.);
168-
return value;
163+
static constexpr Real eps_34() {
164+
return std::pow(eps(), 3./4.);
169165
}
170166

171167
/// Returns ε^(1/2) for the precision of the underlying scalar type.
172-
static Real eps_12() {
173-
static const Real value = std::pow(eps(), 1./2.);
174-
return value;
168+
static constexpr Real eps_12() {
169+
return std::pow(eps(), 1./2.);
175170
}
176171

177172
};

include/fcl/narrowphase/detail/convexity_based_algorithm/gjk_libccd-inl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ _ccd_inline void tripleCross(const ccd_vec3_t *a, const ccd_vec3_t *b,
313313
static int doSimplex2(ccd_simplex_t *simplex, ccd_vec3_t *dir) {
314314
// Used to define numerical thresholds near zero; typically scaled to the size
315315
// of the quantities being tested.
316-
const ccd_real_t eps = constants<ccd_real_t>::eps();
316+
constexpr ccd_real_t eps = constants<ccd_real_t>::eps();
317317

318318
const Vector3<ccd_real_t> p_OA(simplex->ps[simplex->last].v.v);
319319
const Vector3<ccd_real_t> p_OB(simplex->ps[0].v.v);
@@ -922,7 +922,7 @@ static bool are_coincident(const ccd_vec3_t& p, const ccd_vec3_t& q) {
922922
using std::abs;
923923
using std::max;
924924

925-
const ccd_real_t eps = constants<ccd_real_t>::eps();
925+
constexpr ccd_real_t eps = constants<ccd_real_t>::eps();
926926
// NOTE: Wrapping "1.0" with ccd_real_t accounts for mac problems where ccd
927927
// is actually float based.
928928
for (int i = 0; i < 3; ++i) {
@@ -960,7 +960,7 @@ static bool triangle_area_is_zero(const ccd_vec3_t& a, const ccd_vec3_t& b,
960960
ccdVec3Normalize(&AB);
961961
ccdVec3Normalize(&AC);
962962
ccdVec3Cross(&n, &AB, &AC);
963-
const ccd_real_t eps = constants<ccd_real_t>::eps();
963+
constexpr ccd_real_t eps = constants<ccd_real_t>::eps();
964964
// Second co-linearity condition.
965965
if (ccdVec3Len2(&n) < eps * eps) return true;
966966
return false;

include/fcl/narrowphase/detail/primitive_shape_algorithm/capsule_capsule-inl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ S closestPtSegmentSegment(const Vector3<S>& p_FP1, const Vector3<S>& p_FQ1,
8181
S* s, S* t, Vector3<S>* p_FC1, Vector3<S>* p_FC2) {
8282
// TODO(SeanCurtis-TRI): Document the match underlying this function -- the
8383
// variables: a, b, c, e, and f are otherwise overly inscrutable.
84-
const auto kEps = constants<S>::eps_78();
85-
const auto kEpsSquared = kEps * kEps;
84+
constexpr auto kEps = constants<S>::eps_78();
85+
constexpr auto kEpsSquared = kEps * kEps;
8686

8787
Vector3<S> p_P1Q1 = p_FQ1 - p_FP1; // Segment 1's displacement vector: D1.
8888
Vector3<S> p_P2Q2 = p_FQ2 - p_FP2; // Segment 2's displacement vector: D2.
@@ -207,7 +207,7 @@ bool capsuleCapsuleDistance(const Capsule<S>& s1, const Transform3<S>& X_FC1,
207207
const S segment_dist = sqrt(squared_dist);
208208
*dist = segment_dist - s1.radius - s2.radius;
209209
Vector3<S> vhat_C1C2_F;
210-
const auto eps = constants<S>::eps_78();
210+
constexpr auto eps = constants<S>::eps_78();
211211
// We can only use the vector between the center-line nearest points to find
212212
// the witness points if they are sufficiently far apart.
213213
if (segment_dist > eps) {

include/fcl/narrowphase/detail/primitive_shape_algorithm/sphere_box-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ FCL_EXPORT bool sphereBoxIntersect(const Sphere<S>& sphere,
135135
// Furthermore, in finding the *near* face, a better candidate must be more
136136
// than this epsilon closer to the sphere center (see the test in the
137137
// else branch).
138-
auto eps = 16 * constants<S>::eps();
138+
constexpr auto eps = 16 * constants<S>::eps();
139139
if (N_is_not_C && squared_distance > eps * eps) {
140140
// The center is on the outside. Normal direction is from C to N (computed
141141
// above) and penetration depth is r - |p_BN - p_BC|. The contact position

include/fcl/narrowphase/detail/primitive_shape_algorithm/sphere_cylinder-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ FCL_EXPORT bool sphereCylinderIntersect(
155155
// Furthermore, in finding the *near* face, a better candidate must be more
156156
// than this epsilon closer to the sphere center (see the test in the
157157
// else branch).
158-
const auto eps = 16 * constants<S>::eps();
158+
constexpr auto eps = 16 * constants<S>::eps();
159159
if (S_is_outside && p_SN_squared_dist > eps * eps) {
160160
// The sphere center is *measurably outside* the cylinder. There are three
161161
// possibilities: nearest point lies on the cap face, cap edge, or barrel.

0 commit comments

Comments
 (0)