Skip to content

Commit

Permalink
Merge pull request #167 from dartsim/isnan
Browse files Browse the repository at this point in the history
Add portable isNan() and isInf()
  • Loading branch information
karenliu committed Mar 27, 2014
2 parents 3c1e109 + eed78fc commit c1f96f7
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 34 deletions.
10 changes: 10 additions & 0 deletions Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

### Additions

1. **dart/math/Helpers.h**
+ bool isNan(double _v)
+ bool isNan(const Eigen::MatrixXd& _m)
+ bool isInf(double _v)
+ bool isInf(const Eigen::MatrixXd& _m)

1. **dart/dynamics/BodyNode.h**
+ enum InverseKinematicsPolicy
+ class TransformObjFunc
Expand Down Expand Up @@ -31,6 +37,10 @@
1. **dart/dynamics/Skeleton.h**
+ Eigen::VectorXd getConfig() const

1. **dart/math/Geometry.h**
+ bool isNan(const Eigen::MatrixXd& _m)
+ Note: moved to dart/math/Helpers.h

### Modifications

1. **dart/dynamics/Skeleton.h**
Expand Down
1 change: 1 addition & 0 deletions apps/balance/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <iostream>
#include <vector>

#include "dart/utils/Paths.h"
Expand Down
2 changes: 2 additions & 0 deletions apps/harnessTest/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include "apps/harnessTest/MyWindow.h"

#include <iostream>

#include "dart/utils/Paths.h"
#include "dart/dynamics/Skeleton.h"
#include "dart/simulation/World.h"
Expand Down
1 change: 1 addition & 0 deletions dart/collision/CollisionDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "dart/collision/CollisionDetector.h"

#include <algorithm>
#include <iostream>
#include <vector>

#include "dart/dynamics/BodyNode.h"
Expand Down
2 changes: 2 additions & 0 deletions dart/collision/bullet/BulletCollisionNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include "dart/collision/bullet/BulletCollisionNode.h"

#include <iostream>

#include <assimp/scene.h>

#include "dart/collision/bullet/BulletTypes.h"
Expand Down
17 changes: 9 additions & 8 deletions dart/dynamics/GenCoord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <cassert>

#include "dart/math/MathTypes.h"
#include "dart/math/Helpers.h"
#include "dart/dynamics/GenCoord.h"

namespace dart {
Expand Down Expand Up @@ -146,7 +147,7 @@ double GenCoord::getConfigMax() const
//==============================================================================
void GenCoord::setConfigDeriv(double _configDeriv)
{
assert(_configDeriv == _configDeriv);
assert(!math::isNan(_configDeriv));
mConfigDeriv = _configDeriv;
}

Expand All @@ -165,7 +166,7 @@ double GenCoord::getVelMax() const
//==============================================================================
void GenCoord::setVelDeriv(double _velDeriv)
{
assert(_velDeriv == _velDeriv);
assert(!math::isNan(_velDeriv));
mVelDeriv = _velDeriv;
}

Expand All @@ -184,7 +185,7 @@ double GenCoord::getAccMax() const
//==============================================================================
void GenCoord::setAccDeriv(double _accDeriv)
{
assert(_accDeriv == _accDeriv);
assert(!math::isNan(_accDeriv));
mAccDeriv = _accDeriv;
}

Expand All @@ -203,7 +204,7 @@ double GenCoord::getForceMax() const
//==============================================================================
void GenCoord::setForceDeriv(double _forceDeriv)
{
assert(_forceDeriv == _forceDeriv);
assert(!math::isNan(_forceDeriv));
mForceDeriv = _forceDeriv;
}

Expand All @@ -216,28 +217,28 @@ double GenCoord::getForceDeriv() const
//==============================================================================
void GenCoord::setConfig(double _config)
{
assert(_config == _config);
assert(!math::isNan(_config));
mConfig = _config;
}

//==============================================================================
void GenCoord::setVel(double _vel)
{
assert(_vel == _vel);
assert(!math::isNan(_vel));
mVel = _vel;
}

//==============================================================================
void GenCoord::setAcc(double _acc)
{
assert(_acc == _acc);
assert(!math::isNan(_acc));
mAcc = _acc;
}

//==============================================================================
void GenCoord::setForce(double _force)
{
assert(_force == _force);
assert(!math::isNan(_force));
mForce = _force;
}

Expand Down
1 change: 1 addition & 0 deletions dart/dynamics/PlanarJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "dart/common/Console.h"
#include "dart/math/Geometry.h"
#include "dart/math/Helpers.h"

namespace dart {
namespace dynamics {
Expand Down
6 changes: 3 additions & 3 deletions dart/dynamics/PointMass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,16 @@ void PointMass::updateArticulatedInertia(double _dt)
= 1.0 / (mMass
+ _dt * mParentSoftBodyNode->getDampingCoefficient()
+ _dt * _dt * mParentSoftBodyNode->getVertexSpringStiffness());
assert(!std::isnan(mImplicitPsi));
assert(!math::isNan(mImplicitPsi));

// Cache data: AI_S_Psi
// - Do nothing

// Cache data: Pi
mPi = mMass - mMass * mMass * mPsi;
mImplicitPi = mMass - mMass * mMass * mImplicitPsi;
assert(!std::isnan(mPi));
assert(!std::isnan(mImplicitPi));
assert(!math::isNan(mPi));
assert(!math::isNan(mImplicitPi));
}

void PointMass::updateGeneralizedForce(bool _withDampingForces)
Expand Down
1 change: 1 addition & 0 deletions dart/dynamics/PrismaticJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <string>

#include "dart/math/Geometry.h"
#include "dart/math/Helpers.h"
#include "dart/dynamics/BodyNode.h"

namespace dart {
Expand Down
1 change: 1 addition & 0 deletions dart/dynamics/RevoluteJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "dart/common/Console.h"
#include "dart/math/Geometry.h"
#include "dart/math/Helpers.h"
#include "dart/dynamics/BodyNode.h"

namespace dart {
Expand Down
1 change: 1 addition & 0 deletions dart/dynamics/ScrewJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <string>

#include "dart/math/Geometry.h"
#include "dart/math/Helpers.h"
#include "dart/dynamics/BodyNode.h"

namespace dart {
Expand Down
1 change: 1 addition & 0 deletions dart/dynamics/SoftBodyNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <string>
#include <vector>

#include "dart/math/Helpers.h"
#include <dart/common/Console.h>
#include <dart/dynamics/Joint.h>
#include <dart/dynamics/Shape.h>
Expand Down
1 change: 1 addition & 0 deletions dart/dynamics/TranslationalJoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <string>

#include "dart/math/Geometry.h"
#include "dart/math/Helpers.h"

namespace dart {
namespace dynamics {
Expand Down
16 changes: 6 additions & 10 deletions dart/math/Geometry.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2013, Georgia Tech Research Corporation
* Copyright (c) 2011-2014, Georgia Tech Research Corporation
* All rights reserved.
*
* Author(s): Sehoon Ha <sehoon.ha@gmail.com>,
Expand Down Expand Up @@ -36,9 +36,14 @@
*/

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>

#include "dart/math/Geometry.h"
#include "dart/math/Helpers.h"

namespace dart {
namespace math {
Expand Down Expand Up @@ -1426,15 +1431,6 @@ bool verifyTransform(const Eigen::Isometry3d& _T) {
&& fabs(_T.linear().determinant() - 1.0) <= DART_EPSILON;
}

bool isNan(const Eigen::MatrixXd& _m) {
for (int i = 0; i < _m.rows(); ++i)
for (int j = 0; j < _m.cols(); ++j)
if (_m(i, j) != _m(i, j))
return true;

return false;
}

Eigen::Vector3d fromSkewSymmetric(const Eigen::Matrix3d& _m) {
#ifndef NDEBUG
if (fabs(_m(0, 0)) > DART_EPSILON
Expand Down
11 changes: 1 addition & 10 deletions dart/math/Geometry.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2013, Georgia Tech Research Corporation
* Copyright (c) 2011-2014, Georgia Tech Research Corporation
* All rights reserved.
*
* Author(s): Sehoon Ha <sehoon.ha@gmail.com>,
Expand Down Expand Up @@ -38,12 +38,6 @@
#ifndef DART_MATH_GEOMETRY_H_
#define DART_MATH_GEOMETRY_H_

#include <vector>
#include <cassert>
#include <iostream>
#include <cmath>
#include <cfloat>

#include <Eigen/Dense>

#include "dart/math/MathTypes.h"
Expand Down Expand Up @@ -287,9 +281,6 @@ bool verifyRotation(const Eigen::Matrix3d& _R);
/// all the elements are not NaN values.
bool verifyTransform(const Eigen::Isometry3d& _T);

/// \brief
bool isNan(const Eigen::MatrixXd& _m);

} // namespace math
} // namespace dart

Expand Down
43 changes: 42 additions & 1 deletion dart/math/Helpers.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2013, Georgia Tech Research Corporation
* Copyright (c) 2011-2014, Georgia Tech Research Corporation
* All rights reserved.
*
* Author(s): Sehoon Ha <sehoon.ha@gmail.com>
Expand Down Expand Up @@ -39,6 +39,7 @@

// Standard Libraries
#include <vector>
#include <cfloat>
#include <cmath>
#include <ctime>
#include <climits>
Expand Down Expand Up @@ -132,6 +133,46 @@ inline bool isInt(double _x) {
return false;
}

/// \brief Returns whether _v is a NaN (Not-A-Number) value
inline bool isNan(double _v) {
#ifdef WIN32
return _isnan(_v);
#else
return std::isnan(_v);
#endif
}

/// \brief Returns whether _m is a NaN (Not-A-Number) matrix
inline bool isNan(const Eigen::MatrixXd& _m) {
for (int i = 0; i < _m.rows(); ++i)
for (int j = 0; j < _m.cols(); ++j)
if (isNan(_m(i, j)))
return true;

return false;
}

/// \brief Returns whether _v is an infinity value (either positive infinity or
/// negative infinity).
inline bool isInf(double _v) {
#ifdef WIN32
return !_finite(_v);
#else
return std::isinf(_v);
#endif
}

/// \brief Returns whether _m is an infinity matrix (either positive infinity or
/// negative infinity).
inline bool isInf(const Eigen::MatrixXd& _m) {
for (int i = 0; i < _m.rows(); ++i)
for (int j = 0; j < _m.cols(); ++j)
if (isInf(_m(i, j)))
return true;

return false;
}

inline unsigned seedRand() {
time_t now = time(0);
unsigned char* p = reinterpret_cast<unsigned char*>(&now);
Expand Down
5 changes: 3 additions & 2 deletions unittests/testDynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,11 @@ TEST_F(DynamicsTest, compareEquationsOfMotion)
for (int i = 0; i < getList().size(); ++i)
{
////////////////////////////////////////////////////////////////////////////
// TODO(JS): Following five skel files, which contain euler joints couldn't
// TODO(JS): Following skel files, which contain euler joints couldn't
// pass EQUATIONS_OF_MOTION, are disabled.
std::string skelFileName = getList()[i];
if (skelFileName == DART_DATA_PATH"skel/test/chainwhipa.skel"
if (skelFileName == DART_DATA_PATH"skel/test/double_pendulum_euler_joint.skel"
|| skelFileName == DART_DATA_PATH"skel/test/chainwhipa.skel"
|| skelFileName == DART_DATA_PATH"skel/test/serial_chain_eulerxyz_joint.skel"
|| skelFileName == DART_DATA_PATH"skel/test/simple_tree_structure_euler_joint.skel"
|| skelFileName == DART_DATA_PATH"skel/test/tree_structure_euler_joint.skel"
Expand Down

0 comments on commit c1f96f7

Please sign in to comment.