/usr/include/SurgSim/Math/Valid-inl.h is in libopensurgsim-dev 0.7.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | // This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// 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.
/// \file
/// Implementation of isValid(), isSubnormal() and setSubnormalToZero().
#ifndef SURGSIM_MATH_VALID_INL_H
#define SURGSIM_MATH_VALID_INL_H
#include <boost/math/special_functions/fpclassify.hpp>
namespace SurgSim
{
namespace Math
{
namespace internal
{
template <typename T, class V>
class PredicateAlwaysTrueVisitor
{
public:
typedef typename T::Index Index;
typedef typename T::Scalar Scalar;
PredicateAlwaysTrueVisitor() : m_result(true)
{
}
inline bool getResult() const
{
return m_result;
}
inline void init(const Scalar& value, Index i, Index j)
{
if (! V::evaluate(value))
{
m_result = false;
}
}
inline void operator()(const Scalar& value, Index i, Index j)
{
if (! V::evaluate(value))
{
m_result = false;
}
}
private:
bool m_result;
};
template <typename T>
class ValidVisitor : public PredicateAlwaysTrueVisitor<T, ValidVisitor<T>>
{
public:
typedef typename PredicateAlwaysTrueVisitor<T, ValidVisitor<T>>::Scalar Scalar;
static bool evaluate(const Scalar& value)
{
// The extra parentheses protect from pain if isfinite() is also defined as a macro.
return (boost::math::isfinite)(value);
}
};
template <typename T>
class NonSubnormalVisitor : public PredicateAlwaysTrueVisitor<T, NonSubnormalVisitor<T>>
{
public:
typedef typename PredicateAlwaysTrueVisitor<T, ValidVisitor<T>>::Scalar Scalar;
static bool evaluate(const Scalar& value)
{
// The extra parentheses protect from pain if fpclassify() is also defined as a macro.
return ((boost::math::fpclassify)(value) != FP_SUBNORMAL);
}
};
}; // namespace internal
inline bool isValid(float value)
{
// The extra parentheses protect from pain if isfinite() is also defined as a macro.
return (boost::math::isfinite)(value);
}
inline bool isValid(double value)
{
// The extra parentheses protect from pain if isfinite() is also defined as a macro.
return (boost::math::isfinite)(value);
}
template <typename T>
inline bool isValid(const Eigen::DenseBase<T>& value)
{
internal::ValidVisitor<T> visitor;
value.visit(visitor);
return visitor.getResult();
}
template <typename T>
inline bool isValid(const Eigen::QuaternionBase<T>& value)
{
return isValid(value.coeffs());
}
template <typename T>
inline bool isValid(const Eigen::AngleAxis<T>& value)
{
return isValid(value.angle()) && isValid(value.axis());
}
template <typename T>
inline bool isValid(const Eigen::Rotation2D<T>& value)
{
return isValid(value.angle());
}
template <typename T, int D, int M, int O>
inline bool isValid(const Eigen::Transform<T, D, M, O>& value)
{
return isValid(value.matrix());
}
inline bool isSubnormal(float value)
{
// The extra parentheses protect from pain if fpclassify() is also defined as a macro.
return ((boost::math::fpclassify)(value) == FP_SUBNORMAL);
}
inline bool isSubnormal(double value)
{
// The extra parentheses protect from pain if fpclassify() is also defined as a macro.
return ((boost::math::fpclassify)(value) == FP_SUBNORMAL);
}
template <typename T>
inline bool isSubnormal(const Eigen::DenseBase<T>& value)
{
internal::NonSubnormalVisitor<T> visitor;
value.visit(visitor);
// The visitor checks whether *all* entries are "non-subnormal", i.e. false means some are subnormal.
// This is a consequence of deriving from PredicateAlwaysTrueVisitor.
return ! visitor.getResult();
}
template <typename T>
inline bool isSubnormal(const Eigen::QuaternionBase<T>& value)
{
return isSubnormal(value.coeffs());
}
template <typename T>
inline bool isSubnormal(const Eigen::AngleAxis<T>& value)
{
return isSubnormal(value.angle()) || isSubnormal(value.axis());
}
template <typename T>
inline bool isSubnormal(const Eigen::Rotation2D<T>& value)
{
return isSubnormal(value.angle());
}
template <typename T, int D, int M, int O>
inline bool isSubnormal(const Eigen::Transform<T, D, M, O>& value)
{
return isSubnormal(value.matrix());
}
inline bool setSubnormalToZero(float* value)
{
// The extra parentheses protect from pain if fpclassify() is also defined as a macro.
if ((boost::math::fpclassify)(*value) != FP_SUBNORMAL)
{
return false;
}
else
{
*value = 0.0f;
return true;
}
}
inline bool setSubnormalToZero(double* value)
{
// The extra parentheses protect from pain if fpclassify() is also defined as a macro.
if ((boost::math::fpclassify)(*value) != FP_SUBNORMAL)
{
return false;
}
else
{
*value = 0.0;
return true;
}
}
template <typename T>
inline bool setSubnormalToZero(Eigen::DenseBase<T>* value)
{
if (! isSubnormal(*value)) // optimize for the common case where nothing is subnormal
{
return false;
}
// TODO(bert): This is a simple implementation; it could be much more optimized by e.g. unrolling loops along
// the lines of the implementation of Eigen::DenseBase<T>::visit(). Unfortunately, we can't just *use* Eigen's
// visitors here, because the visitor API doesn't allow modifying the values.
typedef typename Eigen::DenseBase<T>::Index Index;
const Index numColumns = value->cols();
const Index numRows = value->rows();
for (Index j = 0; j < numColumns; ++j)
{
for (Index i = 0; i < numRows; ++i)
{
// The extra parentheses protect from pain if fpclassify() is also defined as a macro.
if ((boost::math::fpclassify)(value->coeffRef(i, j)) == FP_SUBNORMAL)
{
value->coeffRef(i, j) = 0;
}
}
}
return true;
}
template <typename T>
inline bool setSubnormalToZero(Eigen::QuaternionBase<T>* value)
{
return setSubnormalToZero(&(value->coeffs()));
}
template <typename T>
inline bool setSubnormalToZero(Eigen::AngleAxis<T>* value)
{
bool angleChanged = setSubnormalToZero(&(value->angle()));
bool axisChanged = setSubnormalToZero(&(value->axis()));
return angleChanged || axisChanged; // careful about short-circuiting!
}
template <typename T>
inline bool setSubnormalToZero(Eigen::Rotation2D<T>* value)
{
return setSubnormalToZero(&(value->angle()));
}
template <typename T, int D, int M, int O>
inline bool setSubnormalToZero(Eigen::Transform<T, D, M, O>* value)
{
return setSubnormalToZero(&(value->matrix()));
}
}; // namespace Math
}; // namespace SurgSim
#endif // SURGSIM_MATH_VALID_INL_H
|